Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

d_scopedpointer.hpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_SCOPED_POINTER_HPP_INCLUDED
  17. #define DISTRHO_SCOPED_POINTER_HPP_INCLUDED
  18. #include "../DistrhoUtils.hpp"
  19. #include <algorithm>
  20. START_NAMESPACE_DISTRHO
  21. // -----------------------------------------------------------------------
  22. // The following code was based from juce-core ScopedPointer class
  23. // Copyright (C) 2013 Raw Material Software Ltd.
  24. /**
  25. Used by container classes as an indirect way to delete an object of a
  26. particular type.
  27. The generic implementation of this class simply calls 'delete', but you can
  28. create a specialised version of it for a particular class if you need to
  29. delete that type of object in a more appropriate way.
  30. */
  31. template<typename ObjectType>
  32. struct ContainerDeletePolicy
  33. {
  34. static void destroy(ObjectType* const object)
  35. {
  36. delete object;
  37. }
  38. };
  39. //==============================================================================
  40. /**
  41. This class holds a pointer which is automatically deleted when this object goes
  42. out of scope.
  43. Once a pointer has been passed to a ScopedPointer, it will make sure that the pointer
  44. gets deleted when the ScopedPointer is deleted. Using the ScopedPointer on the stack or
  45. as member variables is a good way to use RAII to avoid accidentally leaking dynamically
  46. created objects.
  47. A ScopedPointer can be used in pretty much the same way that you'd use a normal pointer
  48. to an object. If you use the assignment operator to assign a different object to a
  49. ScopedPointer, the old one will be automatically deleted.
  50. A const ScopedPointer is guaranteed not to lose ownership of its object or change the
  51. object to which it points during its lifetime. This means that making a copy of a const
  52. ScopedPointer is impossible, as that would involve the new copy taking ownership from the
  53. old one.
  54. If you need to get a pointer out of a ScopedPointer without it being deleted, you
  55. can use the release() method.
  56. Something to note is the main difference between this class and the std::auto_ptr class,
  57. which is that ScopedPointer provides a cast-to-object operator, wheras std::auto_ptr
  58. requires that you always call get() to retrieve the pointer. The advantages of providing
  59. the cast is that you don't need to call get(), so can use the ScopedPointer in pretty much
  60. exactly the same way as a raw pointer. The disadvantage is that the compiler is free to
  61. use the cast in unexpected and sometimes dangerous ways - in particular, it becomes difficult
  62. to return a ScopedPointer as the result of a function. To avoid this causing errors,
  63. ScopedPointer contains an overloaded constructor that should cause a syntax error in these
  64. circumstances, but it does mean that instead of returning a ScopedPointer from a function,
  65. you'd need to return a raw pointer (or use a std::auto_ptr instead).
  66. */
  67. template<class ObjectType>
  68. class ScopedPointer
  69. {
  70. public:
  71. //==============================================================================
  72. /** Creates a ScopedPointer containing a null pointer. */
  73. ScopedPointer() noexcept
  74. : object(nullptr) {}
  75. /** Creates a ScopedPointer that owns the specified object. */
  76. ScopedPointer(ObjectType* const objectToTakePossessionOf) noexcept
  77. : object(objectToTakePossessionOf) {}
  78. /** Creates a ScopedPointer that takes its pointer from another ScopedPointer.
  79. Because a pointer can only belong to one ScopedPointer, this transfers
  80. the pointer from the other object to this one, and the other object is reset to
  81. be a null pointer.
  82. */
  83. ScopedPointer(ScopedPointer& objectToTransferFrom) noexcept
  84. : object(objectToTransferFrom.object)
  85. {
  86. objectToTransferFrom.object = nullptr;
  87. }
  88. /** Destructor.
  89. This will delete the object that this ScopedPointer currently refers to.
  90. */
  91. ~ScopedPointer()
  92. {
  93. ContainerDeletePolicy<ObjectType>::destroy(object);
  94. }
  95. /** Changes this ScopedPointer to point to a new object.
  96. Because a pointer can only belong to one ScopedPointer, this transfers
  97. the pointer from the other object to this one, and the other object is reset to
  98. be a null pointer.
  99. If this ScopedPointer already points to an object, that object
  100. will first be deleted.
  101. */
  102. ScopedPointer& operator=(ScopedPointer& objectToTransferFrom)
  103. {
  104. if (this != objectToTransferFrom.getAddress())
  105. {
  106. // Two ScopedPointers should never be able to refer to the same object - if
  107. // this happens, you must have done something dodgy!
  108. DISTRHO_SAFE_ASSERT_RETURN(object == nullptr || object != objectToTransferFrom.object, *this);
  109. ObjectType* const oldObject = object;
  110. object = objectToTransferFrom.object;
  111. objectToTransferFrom.object = nullptr;
  112. ContainerDeletePolicy<ObjectType>::destroy(oldObject);
  113. }
  114. return *this;
  115. }
  116. /** Changes this ScopedPointer to point to a new object.
  117. If this ScopedPointer already points to an object, that object
  118. will first be deleted.
  119. The pointer that you pass in may be a nullptr.
  120. */
  121. ScopedPointer& operator=(ObjectType* const newObjectToTakePossessionOf)
  122. {
  123. if (object != newObjectToTakePossessionOf)
  124. {
  125. ObjectType* const oldObject = object;
  126. object = newObjectToTakePossessionOf;
  127. ContainerDeletePolicy<ObjectType>::destroy(oldObject);
  128. }
  129. return *this;
  130. }
  131. //==============================================================================
  132. /** Returns the object that this ScopedPointer refers to. */
  133. operator ObjectType*() const noexcept { return object; }
  134. /** Returns the object that this ScopedPointer refers to. */
  135. ObjectType* get() const noexcept { return object; }
  136. /** Returns the object that this ScopedPointer refers to. */
  137. ObjectType& operator*() const noexcept { return *object; }
  138. /** Lets you access methods and properties of the object that this ScopedPointer refers to. */
  139. ObjectType* operator->() const noexcept { return object; }
  140. //==============================================================================
  141. /** Removes the current object from this ScopedPointer without deleting it.
  142. This will return the current object, and set the ScopedPointer to a null pointer.
  143. */
  144. ObjectType* release() noexcept { ObjectType* const o = object; object = nullptr; return o; }
  145. //==============================================================================
  146. /** Swaps this object with that of another ScopedPointer.
  147. The two objects simply exchange their pointers.
  148. */
  149. void swapWith(ScopedPointer<ObjectType>& other) noexcept
  150. {
  151. // Two ScopedPointers should never be able to refer to the same object - if
  152. // this happens, you must have done something dodgy!
  153. DISTRHO_SAFE_ASSERT_RETURN(object != other.object || this == other.getAddress() || object == nullptr,);
  154. std::swap(object, other.object);
  155. }
  156. private:
  157. //==============================================================================
  158. ObjectType* object;
  159. // (Required as an alternative to the overloaded & operator).
  160. const ScopedPointer* getAddress() const noexcept { return this; }
  161. #ifndef _MSC_VER // (MSVC can't deal with multiple copy constructors)
  162. /* The copy constructors are private to stop people accidentally copying a const ScopedPointer
  163. (the compiler would let you do so by implicitly casting the source to its raw object pointer).
  164. A side effect of this is that in a compiler that doesn't support C++11, you may hit an
  165. error when you write something like this:
  166. ScopedPointer<MyClass> m = new MyClass(); // Compile error: copy constructor is private.
  167. Even though the compiler would normally ignore the assignment here, it can't do so when the
  168. copy constructor is private. It's very easy to fix though - just write it like this:
  169. ScopedPointer<MyClass> m (new MyClass()); // Compiles OK
  170. It's probably best to use the latter form when writing your object declarations anyway, as
  171. this is a better representation of the code that you actually want the compiler to produce.
  172. */
  173. # ifdef DISTRHO_PROPER_CPP11_SUPPORT
  174. ScopedPointer(const ScopedPointer&) = delete;
  175. ScopedPointer& operator=(const ScopedPointer&) = delete;
  176. # else
  177. ScopedPointer(const ScopedPointer&);
  178. ScopedPointer& operator=(const ScopedPointer&);
  179. # endif
  180. #endif
  181. };
  182. //==============================================================================
  183. /** Compares a ScopedPointer with another pointer.
  184. This can be handy for checking whether this is a null pointer.
  185. */
  186. template<class ObjectType>
  187. bool operator==(const ScopedPointer<ObjectType>& pointer1, ObjectType* const pointer2) noexcept
  188. {
  189. return static_cast<ObjectType*>(pointer1) == pointer2;
  190. }
  191. /** Compares a ScopedPointer with another pointer.
  192. This can be handy for checking whether this is a null pointer.
  193. */
  194. template<class ObjectType>
  195. bool operator!=(const ScopedPointer<ObjectType>& pointer1, ObjectType* const pointer2) noexcept
  196. {
  197. return static_cast<ObjectType*>(pointer1) != pointer2;
  198. }
  199. // -----------------------------------------------------------------------
  200. END_NAMESPACE_DISTRHO
  201. #endif // DISTRHO_SCOPED_POINTER_HPP_INCLUDED