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.

244 lines
10KB

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