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.

ScopedPointer.hpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 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. /**
  24. Copyright (C) 2013 Raw Material Software Ltd.
  25. Permission is granted to use this software under the terms of the ISC license
  26. http://www.isc.org/downloads/software-support-policy/isc-license/
  27. Permission to use, copy, modify, and/or distribute this software for any
  28. purpose with or without fee is hereby granted, provided that the above
  29. copyright notice and this permission notice appear in all copies.
  30. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  31. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  32. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  33. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  34. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  35. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  36. OF THIS SOFTWARE.
  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. delete 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. delete 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. delete 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& getObject() const noexcept { return *object; }
  137. /** Returns the object that this ScopedPointer refers to. */
  138. ObjectType& operator*() const noexcept { return *object; }
  139. /** Lets you access methods and properties of the object that this ScopedPointer refers to. */
  140. ObjectType* operator->() const noexcept { return object; }
  141. //==============================================================================
  142. /** Removes the current object from this ScopedPointer without deleting it.
  143. This will return the current object, and set the ScopedPointer to a null pointer.
  144. */
  145. ObjectType* release() noexcept { ObjectType* const o = object; object = nullptr; return o; }
  146. //==============================================================================
  147. /** Swaps this object with that of another ScopedPointer.
  148. The two objects simply exchange their pointers.
  149. */
  150. void swapWith(ScopedPointer<ObjectType>& other) noexcept
  151. {
  152. // Two ScopedPointers should never be able to refer to the same object - if
  153. // this happens, you must have done something dodgy!
  154. DISTRHO_SAFE_ASSERT_RETURN(object != other.object || this == other.getAddress() || object == nullptr,);
  155. std::swap(object, other.object);
  156. }
  157. private:
  158. //==============================================================================
  159. ObjectType* object;
  160. // (Required as an alternative to the overloaded & operator).
  161. const ScopedPointer* getAddress() const noexcept { return this; }
  162. #ifndef _MSC_VER // (MSVC can't deal with multiple copy constructors)
  163. /* The copy constructors are private to stop people accidentally copying a const ScopedPointer
  164. (the compiler would let you do so by implicitly casting the source to its raw object pointer).
  165. A side effect of this is that in a compiler that doesn't support C++11, you may hit an
  166. error when you write something like this:
  167. ScopedPointer<MyClass> m = new MyClass(); // Compile error: copy constructor is private.
  168. Even though the compiler would normally ignore the assignment here, it can't do so when the
  169. copy constructor is private. It's very easy to fix though - just write it like this:
  170. ScopedPointer<MyClass> m (new MyClass()); // Compiles OK
  171. It's probably best to use the latter form when writing your object declarations anyway, as
  172. this is a better representation of the code that you actually want the compiler to produce.
  173. */
  174. # ifdef DISTRHO_PROPER_CPP11_SUPPORT
  175. ScopedPointer(const ScopedPointer&) = delete;
  176. ScopedPointer& operator=(const ScopedPointer&) = delete;
  177. # else
  178. ScopedPointer(const ScopedPointer&);
  179. ScopedPointer& operator=(const ScopedPointer&);
  180. # endif
  181. #endif
  182. };
  183. //==============================================================================
  184. /** Compares a ScopedPointer with another pointer.
  185. This can be handy for checking whether this is a null pointer.
  186. */
  187. template<class ObjectType>
  188. bool operator==(const ScopedPointer<ObjectType>& pointer1, ObjectType* const pointer2) noexcept
  189. {
  190. return static_cast<ObjectType*>(pointer1) == pointer2;
  191. }
  192. /** Compares a ScopedPointer with another pointer.
  193. This can be handy for checking whether this is a null pointer.
  194. */
  195. template<class ObjectType>
  196. bool operator!=(const ScopedPointer<ObjectType>& pointer1, ObjectType* const pointer2) noexcept
  197. {
  198. return static_cast<ObjectType*>(pointer1) != pointer2;
  199. }
  200. // -----------------------------------------------------------------------
  201. END_NAMESPACE_DISTRHO
  202. #endif // DISTRHO_SCOPED_POINTER_HPP_INCLUDED