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.

424 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. //==============================================================================
  26. /**
  27. A base class which provides methods for reference-counting.
  28. To add reference-counting to a class, derive it from this class, and
  29. use the ReferenceCountedObjectPtr class to point to it.
  30. e.g. @code
  31. class MyClass : public ReferenceCountedObject
  32. {
  33. void foo();
  34. // This is a neat way of declaring a typedef for a pointer class,
  35. // rather than typing out the full templated name each time..
  36. typedef ReferenceCountedObjectPtr<MyClass> Ptr;
  37. };
  38. MyClass::Ptr p = new MyClass();
  39. MyClass::Ptr p2 = p;
  40. p = nullptr;
  41. p2->foo();
  42. @endcode
  43. Once a new ReferenceCountedObject has been assigned to a pointer, be
  44. careful not to delete the object manually.
  45. This class uses an Atomic<int> value to hold the reference count, so that it
  46. the pointers can be passed between threads safely. For a faster but non-thread-safe
  47. version, use SingleThreadedReferenceCountedObject instead.
  48. @see ReferenceCountedObjectPtr, ReferenceCountedArray, SingleThreadedReferenceCountedObject
  49. */
  50. class JUCE_API ReferenceCountedObject
  51. {
  52. public:
  53. //==============================================================================
  54. /** Increments the object's reference count.
  55. This is done automatically by the smart pointer, but is public just
  56. in case it's needed for nefarious purposes.
  57. */
  58. void incReferenceCount() noexcept
  59. {
  60. ++refCount;
  61. }
  62. /** Decreases the object's reference count.
  63. If the count gets to zero, the object will be deleted.
  64. */
  65. void decReferenceCount() noexcept
  66. {
  67. jassert (getReferenceCount() > 0);
  68. if (--refCount == 0)
  69. delete this;
  70. }
  71. /** Decreases the object's reference count.
  72. If the count gets to zero, the object will not be deleted, but this method
  73. will return true, allowing the caller to take care of deletion.
  74. */
  75. bool decReferenceCountWithoutDeleting() noexcept
  76. {
  77. jassert (getReferenceCount() > 0);
  78. return --refCount == 0;
  79. }
  80. /** Returns the object's current reference count. */
  81. int getReferenceCount() const noexcept { return refCount.get(); }
  82. protected:
  83. //==============================================================================
  84. /** Creates the reference-counted object (with an initial ref count of zero). */
  85. ReferenceCountedObject() {}
  86. /** Destructor. */
  87. virtual ~ReferenceCountedObject()
  88. {
  89. // it's dangerous to delete an object that's still referenced by something else!
  90. jassert (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. JUCE_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 JUCE_API 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. jassert (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. jassert (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. jassert (getReferenceCount() == 0);
  154. }
  155. private:
  156. //==============================================================================
  157. int refCount;
  158. JUCE_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. #if JUCE_COMPILER_SUPPORTS_NULLPTR
  200. /** Creates a pointer to a null object. */
  201. ReferenceCountedObjectPtr (decltype (nullptr)) noexcept
  202. : referencedObject (nullptr)
  203. {
  204. }
  205. #endif
  206. /** Copies another pointer.
  207. This will increment the object's reference-count.
  208. */
  209. ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr& other) noexcept
  210. : referencedObject (other.referencedObject)
  211. {
  212. incIfNotNull (referencedObject);
  213. }
  214. /** Copies another pointer.
  215. This will increment the object's reference-count (if it is non-null).
  216. */
  217. template <typename Convertible>
  218. ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr<Convertible>& other) noexcept
  219. : referencedObject (static_cast<ReferencedType*> (other.get()))
  220. {
  221. incIfNotNull (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. ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr& other)
  228. {
  229. return operator= (other.referencedObject);
  230. }
  231. /** Changes this pointer to point at a different object.
  232. The reference count of the old object is decremented, and it might be
  233. deleted if it hits zero. The new object's count is incremented.
  234. */
  235. template <typename Convertible>
  236. ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr<Convertible>& other)
  237. {
  238. return operator= (static_cast<ReferencedType*> (other.get()));
  239. }
  240. /** Changes this pointer to point at a different object.
  241. The reference count of the old object is decremented, and it might be
  242. deleted if it hits zero. The new object's count is incremented.
  243. */
  244. ReferenceCountedObjectPtr& operator= (ReferencedType* const newObject)
  245. {
  246. if (referencedObject != newObject)
  247. {
  248. incIfNotNull (newObject);
  249. ReferencedType* const oldObject = referencedObject;
  250. referencedObject = newObject;
  251. decIfNotNull (oldObject);
  252. }
  253. return *this;
  254. }
  255. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  256. /** Takes-over the object from another pointer. */
  257. ReferenceCountedObjectPtr (ReferenceCountedObjectPtr&& other) noexcept
  258. : referencedObject (other.referencedObject)
  259. {
  260. other.referencedObject = nullptr;
  261. }
  262. /** Takes-over the object from another pointer. */
  263. ReferenceCountedObjectPtr& operator= (ReferenceCountedObjectPtr&& other)
  264. {
  265. std::swap (referencedObject, other.referencedObject);
  266. return *this;
  267. }
  268. #endif
  269. /** Destructor.
  270. This will decrement the object's reference-count, which will cause the
  271. object to be deleted when the ref-count hits zero.
  272. */
  273. ~ReferenceCountedObjectPtr()
  274. {
  275. decIfNotNull (referencedObject);
  276. }
  277. //==============================================================================
  278. /** Returns the object that this pointer references.
  279. The pointer returned may be null, of course.
  280. */
  281. operator ReferencedType*() const noexcept { return referencedObject; }
  282. /** Returns the object that this pointer references.
  283. The pointer returned may be null, of course.
  284. */
  285. ReferencedType* get() const noexcept { return referencedObject; }
  286. /** Returns the object that this pointer references.
  287. The pointer returned may be null, of course.
  288. */
  289. ReferencedType* getObject() const noexcept { return referencedObject; }
  290. // the -> operator is called on the referenced object
  291. ReferencedType* operator->() const noexcept
  292. {
  293. jassert (referencedObject != nullptr); // null pointer method call!
  294. return referencedObject;
  295. }
  296. private:
  297. //==============================================================================
  298. ReferencedType* referencedObject;
  299. static void incIfNotNull (ReferencedType* o) noexcept
  300. {
  301. if (o != nullptr)
  302. o->incReferenceCount();
  303. }
  304. static void decIfNotNull (ReferencedType* o) noexcept
  305. {
  306. if (o != nullptr && o->decReferenceCountWithoutDeleting())
  307. delete o;
  308. }
  309. };
  310. //==============================================================================
  311. /** Compares two ReferenceCountedObjectPtrs. */
  312. template <typename ReferenceCountedObjectClass>
  313. bool operator== (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, ReferenceCountedObjectClass* const object2) noexcept
  314. {
  315. return object1.get() == object2;
  316. }
  317. /** Compares two ReferenceCountedObjectPtrs. */
  318. template <typename ReferenceCountedObjectClass>
  319. bool operator== (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  320. {
  321. return object1.get() == object2.get();
  322. }
  323. /** Compares two ReferenceCountedObjectPtrs. */
  324. template <typename ReferenceCountedObjectClass>
  325. bool operator== (ReferenceCountedObjectClass* object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  326. {
  327. return object1 == object2.get();
  328. }
  329. /** Compares two ReferenceCountedObjectPtrs. */
  330. template <typename ReferenceCountedObjectClass>
  331. bool operator!= (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectClass* object2) noexcept
  332. {
  333. return object1.get() != object2;
  334. }
  335. /** Compares two ReferenceCountedObjectPtrs. */
  336. template <typename ReferenceCountedObjectClass>
  337. bool operator!= (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  338. {
  339. return object1.get() != object2.get();
  340. }
  341. /** Compares two ReferenceCountedObjectPtrs. */
  342. template <typename ReferenceCountedObjectClass>
  343. bool operator!= (ReferenceCountedObjectClass* object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
  344. {
  345. return object1 != object2.get();
  346. }
  347. #endif // JUCE_REFERENCECOUNTEDOBJECT_H_INCLUDED