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.

428 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_REFERENCECOUNTEDOBJECT_H_INCLUDED
  24. #define JUCE_REFERENCECOUNTEDOBJECT_H_INCLUDED
  25. #include "Atomic.h"
  26. namespace water {
  27. //==============================================================================
  28. /**
  29. A base class which provides methods for reference-counting.
  30. To add reference-counting to a class, derive it from this class, and
  31. use the ReferenceCountedObjectPtr class to point to it.
  32. e.g. @code
  33. class MyClass : public ReferenceCountedObject
  34. {
  35. void foo();
  36. // This is a neat way of declaring a typedef for a pointer class,
  37. // rather than typing out the full templated name each time..
  38. typedef ReferenceCountedObjectPtr<MyClass> Ptr;
  39. };
  40. MyClass::Ptr p = new MyClass();
  41. MyClass::Ptr p2 = p;
  42. p = nullptr;
  43. p2->foo();
  44. @endcode
  45. Once a new ReferenceCountedObject has been assigned to a pointer, be
  46. careful not to delete the object manually.
  47. This class uses an Atomic<int> value to hold the reference count, so that it
  48. the pointers can be passed between threads safely. For a faster but non-thread-safe
  49. version, use SingleThreadedReferenceCountedObject instead.
  50. @see ReferenceCountedObjectPtr, ReferenceCountedArray, SingleThreadedReferenceCountedObject
  51. */
  52. class ReferenceCountedObject
  53. {
  54. public:
  55. //==============================================================================
  56. /** Increments the object's reference count.
  57. This is done automatically by the smart pointer, but is public just
  58. in case it's needed for nefarious purposes.
  59. */
  60. void incReferenceCount() noexcept
  61. {
  62. ++refCount;
  63. }
  64. /** Decreases the object's reference count.
  65. If the count gets to zero, the object will be deleted.
  66. */
  67. void decReferenceCount() noexcept
  68. {
  69. jassert (getReferenceCount() > 0);
  70. if (--refCount == 0)
  71. delete this;
  72. }
  73. /** Decreases the object's reference count.
  74. If the count gets to zero, the object will not be deleted, but this method
  75. will return true, allowing the caller to take care of deletion.
  76. */
  77. bool decReferenceCountWithoutDeleting() noexcept
  78. {
  79. jassert (getReferenceCount() > 0);
  80. return --refCount == 0;
  81. }
  82. /** Returns the object's current reference count. */
  83. int getReferenceCount() const noexcept { return refCount.get(); }
  84. protected:
  85. //==============================================================================
  86. /** Creates the reference-counted object (with an initial ref count of zero). */
  87. ReferenceCountedObject() {}
  88. /** Destructor. */
  89. virtual ~ReferenceCountedObject()
  90. {
  91. // it's dangerous to delete an object that's still referenced by something else!
  92. jassert (getReferenceCount() == 0);
  93. }
  94. /** Resets the reference count to zero without deleting the object.
  95. You should probably never need to use this!
  96. */
  97. void resetReferenceCount() noexcept
  98. {
  99. refCount = 0;
  100. }
  101. private:
  102. //==============================================================================
  103. Atomic <int> refCount;
  104. JUCE_DECLARE_NON_COPYABLE (ReferenceCountedObject)
  105. };
  106. //==============================================================================
  107. /**
  108. Adds reference-counting to an object.
  109. This is effectively a version of the ReferenceCountedObject class, but which
  110. uses a non-atomic counter, and so is not thread-safe (but which will be more
  111. efficient).
  112. For more details on how to use it, see the ReferenceCountedObject class notes.
  113. @see ReferenceCountedObject, ReferenceCountedObjectPtr, ReferenceCountedArray
  114. */
  115. class SingleThreadedReferenceCountedObject
  116. {
  117. public:
  118. //==============================================================================
  119. /** Increments the object's reference count.
  120. This is done automatically by the smart pointer, but is public just
  121. in case it's needed for nefarious purposes.
  122. */
  123. void incReferenceCount() noexcept
  124. {
  125. ++refCount;
  126. }
  127. /** Decreases the object's reference count.
  128. If the count gets to zero, the object will be deleted.
  129. */
  130. void decReferenceCount() noexcept
  131. {
  132. jassert (getReferenceCount() > 0);
  133. if (--refCount == 0)
  134. delete this;
  135. }
  136. /** Decreases the object's reference count.
  137. If the count gets to zero, the object will not be deleted, but this method
  138. will return true, allowing the caller to take care of deletion.
  139. */
  140. bool decReferenceCountWithoutDeleting() noexcept
  141. {
  142. jassert (getReferenceCount() > 0);
  143. return --refCount == 0;
  144. }
  145. /** Returns the object's current reference count. */
  146. int getReferenceCount() const noexcept { return refCount; }
  147. protected:
  148. //==============================================================================
  149. /** Creates the reference-counted object (with an initial ref count of zero). */
  150. SingleThreadedReferenceCountedObject() : refCount (0) {}
  151. /** Destructor. */
  152. virtual ~SingleThreadedReferenceCountedObject()
  153. {
  154. // it's dangerous to delete an object that's still referenced by something else!
  155. jassert (getReferenceCount() == 0);
  156. }
  157. private:
  158. //==============================================================================
  159. int refCount;
  160. JUCE_DECLARE_NON_COPYABLE (SingleThreadedReferenceCountedObject)
  161. };
  162. //==============================================================================
  163. /**
  164. A smart-pointer class which points to a reference-counted object.
  165. The template parameter specifies the class of the object you want to point to - the easiest
  166. way to make a class reference-countable is to simply make it inherit from ReferenceCountedObject
  167. or SingleThreadedReferenceCountedObject, but if you need to, you can roll your own reference-countable
  168. class by implementing a set of methods called incReferenceCount(), decReferenceCount(), and
  169. decReferenceCountWithoutDeleting(). See ReferenceCountedObject for examples of how these methods
  170. should behave.
  171. When using this class, you'll probably want to create a typedef to abbreviate the full
  172. templated name - e.g.
  173. @code
  174. struct MyClass : public ReferenceCountedObject
  175. {
  176. typedef ReferenceCountedObjectPtr<MyClass> Ptr;
  177. ...
  178. @endcode
  179. @see ReferenceCountedObject, ReferenceCountedObjectArray
  180. */
  181. template <class ReferenceCountedObjectClass>
  182. class ReferenceCountedObjectPtr
  183. {
  184. public:
  185. /** The class being referenced by this pointer. */
  186. typedef ReferenceCountedObjectClass ReferencedType;
  187. //==============================================================================
  188. /** Creates a pointer to a null object. */
  189. ReferenceCountedObjectPtr() noexcept
  190. : referencedObject (nullptr)
  191. {
  192. }
  193. /** Creates a pointer to an object.
  194. This will increment the object's reference-count.
  195. */
  196. ReferenceCountedObjectPtr (ReferencedType* refCountedObject) noexcept
  197. : referencedObject (refCountedObject)
  198. {
  199. incIfNotNull (refCountedObject);
  200. }
  201. #if JUCE_COMPILER_SUPPORTS_NULLPTR
  202. /** Creates a pointer to a null object. */
  203. ReferenceCountedObjectPtr (decltype (nullptr)) noexcept
  204. : referencedObject (nullptr)
  205. {
  206. }
  207. #endif
  208. /** Copies another pointer.
  209. This will increment the object's reference-count.
  210. */
  211. ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr& other) noexcept
  212. : referencedObject (other.referencedObject)
  213. {
  214. incIfNotNull (referencedObject);
  215. }
  216. /** Copies another pointer.
  217. This will increment the object's reference-count (if it is non-null).
  218. */
  219. template <typename Convertible>
  220. ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr<Convertible>& other) noexcept
  221. : referencedObject (static_cast<ReferencedType*> (other.get()))
  222. {
  223. incIfNotNull (referencedObject);
  224. }
  225. /** Changes this pointer to point at a different object.
  226. The reference count of the old object is decremented, and it might be
  227. deleted if it hits zero. The new object's count is incremented.
  228. */
  229. ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr& other)
  230. {
  231. return operator= (other.referencedObject);
  232. }
  233. /** Changes this pointer to point at a different object.
  234. The reference count of the old object is decremented, and it might be
  235. deleted if it hits zero. The new object's count is incremented.
  236. */
  237. template <typename Convertible>
  238. ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr<Convertible>& other)
  239. {
  240. return operator= (static_cast<ReferencedType*> (other.get()));
  241. }
  242. /** Changes this pointer to point at a different object.
  243. The reference count of the old object is decremented, and it might be
  244. deleted if it hits zero. The new object's count is incremented.
  245. */
  246. ReferenceCountedObjectPtr& operator= (ReferencedType* const newObject)
  247. {
  248. if (referencedObject != newObject)
  249. {
  250. incIfNotNull (newObject);
  251. ReferencedType* const oldObject = referencedObject;
  252. referencedObject = newObject;
  253. decIfNotNull (oldObject);
  254. }
  255. return *this;
  256. }
  257. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  258. /** Takes-over the object from another pointer. */
  259. ReferenceCountedObjectPtr (ReferenceCountedObjectPtr&& other) noexcept
  260. : referencedObject (other.referencedObject)
  261. {
  262. other.referencedObject = nullptr;
  263. }
  264. /** Takes-over the object from another pointer. */
  265. ReferenceCountedObjectPtr& operator= (ReferenceCountedObjectPtr&& other)
  266. {
  267. std::swap (referencedObject, other.referencedObject);
  268. return *this;
  269. }
  270. #endif
  271. /** Destructor.
  272. This will decrement the object's reference-count, which will cause the
  273. object to be deleted when the ref-count hits zero.
  274. */
  275. ~ReferenceCountedObjectPtr()
  276. {
  277. decIfNotNull (referencedObject);
  278. }
  279. //==============================================================================
  280. /** Returns the object that this pointer references.
  281. The pointer returned may be null, of course.
  282. */
  283. operator ReferencedType*() const noexcept { return referencedObject; }
  284. /** Returns the object that this pointer references.
  285. The pointer returned may be null, of course.
  286. */
  287. ReferencedType* get() const noexcept { return referencedObject; }
  288. /** Returns the object that this pointer references.
  289. The pointer returned may be null, of course.
  290. */
  291. ReferencedType* getObject() const noexcept { return referencedObject; }
  292. // the -> operator is called on the referenced object
  293. ReferencedType* operator->() const noexcept
  294. {
  295. jassert (referencedObject != nullptr); // null pointer method call!
  296. return referencedObject;
  297. }
  298. private:
  299. //==============================================================================
  300. ReferencedType* referencedObject;
  301. static void incIfNotNull (ReferencedType* o) noexcept
  302. {
  303. if (o != nullptr)
  304. o->incReferenceCount();
  305. }
  306. static void decIfNotNull (ReferencedType* o) noexcept
  307. {
  308. if (o != nullptr && o->decReferenceCountWithoutDeleting())
  309. delete o;
  310. }
  311. };
  312. //==============================================================================
  313. /** Compares two ReferenceCountedObjectPtrs. */
  314. template <typename ReferenceCountedObjectClass>
  315. bool operator== (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, ReferenceCountedObjectClass* const object2) noexcept
  316. {
  317. return object1.get() == object2;
  318. }
  319. /** Compares two ReferenceCountedObjectPtrs. */
  320. template <typename ReferenceCountedObjectClass>
  321. bool operator== (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  322. {
  323. return object1.get() == object2.get();
  324. }
  325. /** Compares two ReferenceCountedObjectPtrs. */
  326. template <typename ReferenceCountedObjectClass>
  327. bool operator== (ReferenceCountedObjectClass* object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  328. {
  329. return object1 == object2.get();
  330. }
  331. /** Compares two ReferenceCountedObjectPtrs. */
  332. template <typename ReferenceCountedObjectClass>
  333. bool operator!= (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectClass* object2) noexcept
  334. {
  335. return object1.get() != object2;
  336. }
  337. /** Compares two ReferenceCountedObjectPtrs. */
  338. template <typename ReferenceCountedObjectClass>
  339. bool operator!= (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  340. {
  341. return object1.get() != object2.get();
  342. }
  343. /** Compares two ReferenceCountedObjectPtrs. */
  344. template <typename ReferenceCountedObjectClass>
  345. bool operator!= (ReferenceCountedObjectClass* object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  346. {
  347. return object1 != object2.get();
  348. }
  349. }
  350. #endif // JUCE_REFERENCECOUNTEDOBJECT_H_INCLUDED