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.

231 lines
9.5KB

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