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

7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 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. jassert (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. jassert (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. /** Destructor. */
  86. virtual ~ReferenceCountedObject()
  87. {
  88. // it's dangerous to delete an object that's still referenced by something else!
  89. jassert (getReferenceCount() == 0);
  90. }
  91. /** Resets the reference count to zero without deleting the object.
  92. You should probably never need to use this!
  93. */
  94. void resetReferenceCount() noexcept
  95. {
  96. refCount = 0;
  97. }
  98. private:
  99. //==============================================================================
  100. Atomic <int> refCount;
  101. CARLA_DECLARE_NON_COPY_CLASS (ReferenceCountedObject)
  102. };
  103. //==============================================================================
  104. /**
  105. Adds reference-counting to an object.
  106. This is effectively a version of the ReferenceCountedObject class, but which
  107. uses a non-atomic counter, and so is not thread-safe (but which will be more
  108. efficient).
  109. For more details on how to use it, see the ReferenceCountedObject class notes.
  110. @see ReferenceCountedObject, ReferenceCountedObjectPtr, ReferenceCountedArray
  111. */
  112. class SingleThreadedReferenceCountedObject
  113. {
  114. public:
  115. //==============================================================================
  116. /** Increments the object's reference count.
  117. This is done automatically by the smart pointer, but is public just
  118. in case it's needed for nefarious purposes.
  119. */
  120. void incReferenceCount() noexcept
  121. {
  122. ++refCount;
  123. }
  124. /** Decreases the object's reference count.
  125. If the count gets to zero, the object will be deleted.
  126. */
  127. void decReferenceCount() noexcept
  128. {
  129. jassert (getReferenceCount() > 0);
  130. if (--refCount == 0)
  131. delete this;
  132. }
  133. /** Decreases the object's reference count.
  134. If the count gets to zero, the object will not be deleted, but this method
  135. will return true, allowing the caller to take care of deletion.
  136. */
  137. bool decReferenceCountWithoutDeleting() noexcept
  138. {
  139. jassert (getReferenceCount() > 0);
  140. return --refCount == 0;
  141. }
  142. /** Returns the object's current reference count. */
  143. int getReferenceCount() const noexcept { return refCount; }
  144. protected:
  145. //==============================================================================
  146. /** Creates the reference-counted object (with an initial ref count of zero). */
  147. SingleThreadedReferenceCountedObject() : refCount (0) {}
  148. /** Destructor. */
  149. virtual ~SingleThreadedReferenceCountedObject()
  150. {
  151. // it's dangerous to delete an object that's still referenced by something else!
  152. jassert (getReferenceCount() == 0);
  153. }
  154. private:
  155. //==============================================================================
  156. int refCount;
  157. CARLA_DECLARE_NON_COPY_CLASS (SingleThreadedReferenceCountedObject)
  158. };
  159. //==============================================================================
  160. /**
  161. A smart-pointer class which points to a reference-counted object.
  162. The template parameter specifies the class of the object you want to point to - the easiest
  163. way to make a class reference-countable is to simply make it inherit from ReferenceCountedObject
  164. or SingleThreadedReferenceCountedObject, but if you need to, you can roll your own reference-countable
  165. class by implementing a set of methods called incReferenceCount(), decReferenceCount(), and
  166. decReferenceCountWithoutDeleting(). See ReferenceCountedObject for examples of how these methods
  167. should behave.
  168. When using this class, you'll probably want to create a typedef to abbreviate the full
  169. templated name - e.g.
  170. @code
  171. struct MyClass : public ReferenceCountedObject
  172. {
  173. typedef ReferenceCountedObjectPtr<MyClass> Ptr;
  174. ...
  175. @endcode
  176. @see ReferenceCountedObject, ReferenceCountedObjectArray
  177. */
  178. template <class ReferenceCountedObjectClass>
  179. class ReferenceCountedObjectPtr
  180. {
  181. public:
  182. /** The class being referenced by this pointer. */
  183. typedef ReferenceCountedObjectClass ReferencedType;
  184. //==============================================================================
  185. /** Creates a pointer to a null object. */
  186. ReferenceCountedObjectPtr() noexcept
  187. : referencedObject (nullptr)
  188. {
  189. }
  190. /** Creates a pointer to an object.
  191. This will increment the object's reference-count.
  192. */
  193. ReferenceCountedObjectPtr (ReferencedType* refCountedObject) noexcept
  194. : referencedObject (refCountedObject)
  195. {
  196. incIfNotNull (refCountedObject);
  197. }
  198. /** Copies another pointer.
  199. This will increment the object's reference-count.
  200. */
  201. ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr& other) noexcept
  202. : referencedObject (other.referencedObject)
  203. {
  204. incIfNotNull (referencedObject);
  205. }
  206. /** Copies another pointer.
  207. This will increment the object's reference-count (if it is non-null).
  208. */
  209. template <typename Convertible>
  210. ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr<Convertible>& other) noexcept
  211. : referencedObject (static_cast<ReferencedType*> (other.get()))
  212. {
  213. incIfNotNull (referencedObject);
  214. }
  215. /** Changes this pointer to point at a different object.
  216. The reference count of the old object is decremented, and it might be
  217. deleted if it hits zero. The new object's count is incremented.
  218. */
  219. ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr& other)
  220. {
  221. return operator= (other.referencedObject);
  222. }
  223. /** Changes this pointer to point at a different object.
  224. The reference count of the old object is decremented, and it might be
  225. deleted if it hits zero. The new object's count is incremented.
  226. */
  227. template <typename Convertible>
  228. ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr<Convertible>& other)
  229. {
  230. return operator= (static_cast<ReferencedType*> (other.get()));
  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. ReferenceCountedObjectPtr& operator= (ReferencedType* const newObject)
  237. {
  238. if (referencedObject != newObject)
  239. {
  240. incIfNotNull (newObject);
  241. ReferencedType* const oldObject = referencedObject;
  242. referencedObject = newObject;
  243. decIfNotNull (oldObject);
  244. }
  245. return *this;
  246. }
  247. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  248. /** Takes-over the object from another pointer. */
  249. ReferenceCountedObjectPtr (ReferenceCountedObjectPtr&& other) noexcept
  250. : referencedObject (other.referencedObject)
  251. {
  252. other.referencedObject = nullptr;
  253. }
  254. /** Takes-over the object from another pointer. */
  255. ReferenceCountedObjectPtr& operator= (ReferenceCountedObjectPtr&& other)
  256. {
  257. std::swap (referencedObject, other.referencedObject);
  258. return *this;
  259. }
  260. #endif
  261. /** Destructor.
  262. This will decrement the object's reference-count, which will cause the
  263. object to be deleted when the ref-count hits zero.
  264. */
  265. ~ReferenceCountedObjectPtr()
  266. {
  267. decIfNotNull (referencedObject);
  268. }
  269. //==============================================================================
  270. /** Returns the object that this pointer references.
  271. The pointer returned may be null, of course.
  272. */
  273. operator ReferencedType*() const noexcept { return referencedObject; }
  274. /** Returns the object that this pointer references.
  275. The pointer returned may be null, of course.
  276. */
  277. ReferencedType* get() const noexcept { return referencedObject; }
  278. /** Returns the object that this pointer references.
  279. The pointer returned may be null, of course.
  280. */
  281. ReferencedType* getObject() const noexcept { return referencedObject; }
  282. // the -> operator is called on the referenced object
  283. ReferencedType* operator->() const noexcept
  284. {
  285. jassert (referencedObject != nullptr); // null pointer method call!
  286. return referencedObject;
  287. }
  288. private:
  289. //==============================================================================
  290. ReferencedType* referencedObject;
  291. static void incIfNotNull (ReferencedType* o) noexcept
  292. {
  293. if (o != nullptr)
  294. o->incReferenceCount();
  295. }
  296. static void decIfNotNull (ReferencedType* o) noexcept
  297. {
  298. if (o != nullptr && o->decReferenceCountWithoutDeleting())
  299. delete o;
  300. }
  301. };
  302. //==============================================================================
  303. /** Compares two ReferenceCountedObjectPtrs. */
  304. template <typename ReferenceCountedObjectClass>
  305. bool operator== (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, ReferenceCountedObjectClass* const object2) noexcept
  306. {
  307. return object1.get() == object2;
  308. }
  309. /** Compares two ReferenceCountedObjectPtrs. */
  310. template <typename ReferenceCountedObjectClass>
  311. bool operator== (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  312. {
  313. return object1.get() == object2.get();
  314. }
  315. /** Compares two ReferenceCountedObjectPtrs. */
  316. template <typename ReferenceCountedObjectClass>
  317. bool operator== (ReferenceCountedObjectClass* object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  318. {
  319. return object1 == object2.get();
  320. }
  321. /** Compares two ReferenceCountedObjectPtrs. */
  322. template <typename ReferenceCountedObjectClass>
  323. bool operator!= (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectClass* object2) noexcept
  324. {
  325. return object1.get() != object2;
  326. }
  327. /** Compares two ReferenceCountedObjectPtrs. */
  328. template <typename ReferenceCountedObjectClass>
  329. bool operator!= (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  330. {
  331. return object1.get() != object2.get();
  332. }
  333. /** Compares two ReferenceCountedObjectPtrs. */
  334. template <typename ReferenceCountedObjectClass>
  335. bool operator!= (ReferenceCountedObjectClass* object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  336. {
  337. return object1 != object2.get();
  338. }
  339. }
  340. #endif // WATER_REFERENCECOUNTEDOBJECT_H_INCLUDED