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.

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