Add remaining files
[juce-lv2.git] / juce / source / src / memory / juce_ReferenceCountedObject.h
blobe4f2f03e9f167a923327742c142b28ec3940218d
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_REFERENCECOUNTEDOBJECT_JUCEHEADER__
27 #define __JUCE_REFERENCECOUNTEDOBJECT_JUCEHEADER__
29 #include "juce_Atomic.h"
32 //==============================================================================
33 /**
34 Adds reference-counting to an object.
36 To add reference-counting to a class, derive it from this class, and
37 use the ReferenceCountedObjectPtr class to point to it.
39 e.g. @code
40 class MyClass : public ReferenceCountedObject
42 void foo();
44 // This is a neat way of declaring a typedef for a pointer class,
45 // rather than typing out the full templated name each time..
46 typedef ReferenceCountedObjectPtr<MyClass> Ptr;
49 MyClass::Ptr p = new MyClass();
50 MyClass::Ptr p2 = p;
51 p = nullptr;
52 p2->foo();
53 @endcode
55 Once a new ReferenceCountedObject has been assigned to a pointer, be
56 careful not to delete the object manually.
58 This class uses an Atomic<int> value to hold the reference count, so that it
59 the pointers can be passed between threads safely. For a faster but non-thread-safe
60 version, use SingleThreadedReferenceCountedObject instead.
62 @see ReferenceCountedObjectPtr, ReferenceCountedArray, SingleThreadedReferenceCountedObject
64 class JUCE_API ReferenceCountedObject
66 public:
67 //==============================================================================
68 /** Increments the object's reference count.
70 This is done automatically by the smart pointer, but is public just
71 in case it's needed for nefarious purposes.
73 inline void incReferenceCount() noexcept
75 ++refCount;
78 /** Decreases the object's reference count.
80 If the count gets to zero, the object will be deleted.
82 inline void decReferenceCount() noexcept
84 jassert (getReferenceCount() > 0);
86 if (--refCount == 0)
87 delete this;
90 /** Returns the object's current reference count. */
91 inline int getReferenceCount() const noexcept { return refCount.get(); }
94 protected:
95 //==============================================================================
96 /** Creates the reference-counted object (with an initial ref count of zero). */
97 ReferenceCountedObject()
101 /** Destructor. */
102 virtual ~ReferenceCountedObject()
104 // it's dangerous to delete an object that's still referenced by something else!
105 jassert (getReferenceCount() == 0);
108 private:
109 //==============================================================================
110 Atomic <int> refCount;
114 //==============================================================================
116 Adds reference-counting to an object.
118 This is efectively a version of the ReferenceCountedObject class, but which
119 uses a non-atomic counter, and so is not thread-safe (but which will be more
120 efficient).
121 For more details on how to use it, see the ReferenceCountedObject class notes.
123 @see ReferenceCountedObject, ReferenceCountedObjectPtr, ReferenceCountedArray
125 class JUCE_API SingleThreadedReferenceCountedObject
127 public:
128 //==============================================================================
129 /** Increments the object's reference count.
131 This is done automatically by the smart pointer, but is public just
132 in case it's needed for nefarious purposes.
134 inline void incReferenceCount() noexcept
136 ++refCount;
139 /** Decreases the object's reference count.
141 If the count gets to zero, the object will be deleted.
143 inline void decReferenceCount() noexcept
145 jassert (getReferenceCount() > 0);
147 if (--refCount == 0)
148 delete this;
151 /** Returns the object's current reference count. */
152 inline int getReferenceCount() const noexcept { return refCount; }
155 protected:
156 //==============================================================================
157 /** Creates the reference-counted object (with an initial ref count of zero). */
158 SingleThreadedReferenceCountedObject() : refCount (0) {}
160 /** Destructor. */
161 virtual ~SingleThreadedReferenceCountedObject()
163 // it's dangerous to delete an object that's still referenced by something else!
164 jassert (getReferenceCount() == 0);
167 private:
168 //==============================================================================
169 int refCount;
173 //==============================================================================
175 A smart-pointer class which points to a reference-counted object.
177 The template parameter specifies the class of the object you want to point to - the easiest
178 way to make a class reference-countable is to simply make it inherit from ReferenceCountedObject,
179 but if you need to, you could roll your own reference-countable class by implementing a pair of
180 mathods called incReferenceCount() and decReferenceCount().
182 When using this class, you'll probably want to create a typedef to abbreviate the full
183 templated name - e.g.
184 @code typedef ReferenceCountedObjectPtr<MyClass> MyClassPtr;@endcode
186 @see ReferenceCountedObject, ReferenceCountedObjectArray
188 template <class ReferenceCountedObjectClass>
189 class ReferenceCountedObjectPtr
191 public:
192 /** The class being referenced by this pointer. */
193 typedef ReferenceCountedObjectClass ReferencedType;
195 //==============================================================================
196 /** Creates a pointer to a null object. */
197 inline ReferenceCountedObjectPtr() noexcept
198 : referencedObject (nullptr)
202 /** Creates a pointer to an object.
204 This will increment the object's reference-count if it is non-null.
206 inline ReferenceCountedObjectPtr (ReferenceCountedObjectClass* const refCountedObject) noexcept
207 : referencedObject (refCountedObject)
209 if (refCountedObject != nullptr)
210 refCountedObject->incReferenceCount();
213 /** Copies another pointer.
215 This will increment the object's reference-count (if it is non-null).
217 inline ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& other) noexcept
218 : referencedObject (other.referencedObject)
220 if (referencedObject != nullptr)
221 referencedObject->incReferenceCount();
224 /** 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.
229 ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& operator= (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& other)
231 ReferenceCountedObjectClass* const newObject = other.referencedObject;
233 if (newObject != referencedObject)
235 if (newObject != nullptr)
236 newObject->incReferenceCount();
238 ReferenceCountedObjectClass* const oldObject = referencedObject;
239 referencedObject = newObject;
241 if (oldObject != nullptr)
242 oldObject->decReferenceCount();
245 return *this;
248 /** Changes this pointer to point at a different object.
250 The reference count of the old object is decremented, and it might be
251 deleted if it hits zero. The new object's count is incremented.
253 ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& operator= (ReferenceCountedObjectClass* const newObject)
255 if (referencedObject != newObject)
257 if (newObject != nullptr)
258 newObject->incReferenceCount();
260 ReferenceCountedObjectClass* const oldObject = referencedObject;
261 referencedObject = newObject;
263 if (oldObject != nullptr)
264 oldObject->decReferenceCount();
267 return *this;
270 /** Destructor.
272 This will decrement the object's reference-count, and may delete it if it
273 gets to zero.
275 inline ~ReferenceCountedObjectPtr()
277 if (referencedObject != nullptr)
278 referencedObject->decReferenceCount();
281 /** Returns the object that this pointer references.
282 The pointer returned may be zero, of course.
284 inline operator ReferenceCountedObjectClass*() const noexcept
286 return referencedObject;
289 // the -> operator is called on the referenced object
290 inline ReferenceCountedObjectClass* operator->() const noexcept
292 return referencedObject;
295 /** Returns the object that this pointer references.
296 The pointer returned may be zero, of course.
298 inline ReferenceCountedObjectClass* getObject() const noexcept
300 return referencedObject;
303 private:
304 //==============================================================================
305 ReferenceCountedObjectClass* referencedObject;
309 /** Compares two ReferenceCountedObjectPointers. */
310 template <class ReferenceCountedObjectClass>
311 bool operator== (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, ReferenceCountedObjectClass* const object2) noexcept
313 return object1.getObject() == object2;
316 /** Compares two ReferenceCountedObjectPointers. */
317 template <class ReferenceCountedObjectClass>
318 bool operator== (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
320 return object1.getObject() == object2.getObject();
323 /** Compares two ReferenceCountedObjectPointers. */
324 template <class ReferenceCountedObjectClass>
325 bool operator== (ReferenceCountedObjectClass* object1, ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
327 return object1 == object2.getObject();
330 /** Compares two ReferenceCountedObjectPointers. */
331 template <class ReferenceCountedObjectClass>
332 bool operator!= (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, const ReferenceCountedObjectClass* object2) noexcept
334 return object1.getObject() != object2;
337 /** Compares two ReferenceCountedObjectPointers. */
338 template <class ReferenceCountedObjectClass>
339 bool operator!= (const ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object1, ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
341 return object1.getObject() != object2.getObject();
344 /** Compares two ReferenceCountedObjectPointers. */
345 template <class ReferenceCountedObjectClass>
346 bool operator!= (ReferenceCountedObjectClass* object1, ReferenceCountedObjectPtr<ReferenceCountedObjectClass>& object2) noexcept
348 return object1 != object2.getObject();
352 #endif // __JUCE_REFERENCECOUNTEDOBJECT_JUCEHEADER__