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.

ReferenceCountedObject.h 14KB

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