OpenShot Audio Library | OpenShotAudio: juce_ReferenceCountedObject.h Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_ReferenceCountedObject.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
65 class JUCE_API ReferenceCountedObject
66{
67public:
68 //==============================================================================
74 void incReferenceCount() noexcept
75 {
76 ++refCount;
77 }
78
82 void decReferenceCount() noexcept
83 {
84 jassert (getReferenceCount() > 0);
85
86 if (--refCount == 0)
87 delete this;
88 }
89
94 bool decReferenceCountWithoutDeleting() noexcept
95 {
96 jassert (getReferenceCount() > 0);
97 return --refCount == 0;
98 }
99
101 int getReferenceCount() const noexcept { return refCount.get(); }
102
103
104protected:
105 //==============================================================================
107 ReferenceCountedObject() = default;
108
110 ReferenceCountedObject (const ReferenceCountedObject&) noexcept {}
112 ReferenceCountedObject (ReferenceCountedObject&&) noexcept {}
114 ReferenceCountedObject& operator= (const ReferenceCountedObject&) noexcept { return *this; }
116 ReferenceCountedObject& operator= (ReferenceCountedObject&&) noexcept { return *this; }
117
119 virtual ~ReferenceCountedObject()
120 {
121 // it's dangerous to delete an object that's still referenced by something else!
122 jassert (getReferenceCount() == 0);
123 }
124
128 void resetReferenceCount() noexcept
129 {
130 refCount = 0;
131 }
132
133private:
134 //==============================================================================
135 Atomic<int> refCount { 0 };
136 friend struct ContainerDeletePolicy<ReferenceCountedObject>;
137};
138
139
140//==============================================================================
153 class JUCE_API SingleThreadedReferenceCountedObject
154{
155public:
156 //==============================================================================
162 void incReferenceCount() noexcept
163 {
164 ++refCount;
165 }
166
170 void decReferenceCount() noexcept
171 {
172 jassert (getReferenceCount() > 0);
173
174 if (--refCount == 0)
175 delete this;
176 }
177
182 bool decReferenceCountWithoutDeleting() noexcept
183 {
184 jassert (getReferenceCount() > 0);
185 return --refCount == 0;
186 }
187
189 int getReferenceCount() const noexcept { return refCount; }
190
191
192protected:
193 //==============================================================================
195 SingleThreadedReferenceCountedObject() = default;
196
198 SingleThreadedReferenceCountedObject (const SingleThreadedReferenceCountedObject&) {}
200 SingleThreadedReferenceCountedObject (SingleThreadedReferenceCountedObject&&) {}
202 SingleThreadedReferenceCountedObject& operator= (const SingleThreadedReferenceCountedObject&) { return *this; }
204 SingleThreadedReferenceCountedObject& operator= (SingleThreadedReferenceCountedObject&&) { return *this; }
205
207 virtual ~SingleThreadedReferenceCountedObject()
208 {
209 // it's dangerous to delete an object that's still referenced by something else!
210 jassert (getReferenceCount() == 0);
211 }
212
213private:
214 //==============================================================================
215 int refCount = 0;
216 friend struct ContainerDeletePolicy<ReferenceCountedObject>;
217};
218
219
220//==============================================================================
245template <class ObjectType>
246 class ReferenceCountedObjectPtr
247{
248public:
250 using ReferencedType = ObjectType;
251
252 //==============================================================================
254 ReferenceCountedObjectPtr() = default;
255
257 ReferenceCountedObjectPtr (decltype (nullptr)) noexcept {}
258
262 ReferenceCountedObjectPtr (ReferencedType* refCountedObject) noexcept
263 : referencedObject (refCountedObject)
264 {
265 incIfNotNull (refCountedObject);
266 }
267
271 ReferenceCountedObjectPtr (ReferencedType& refCountedObject) noexcept
272 : referencedObject (&refCountedObject)
273 {
274 refCountedObject.incReferenceCount();
275 }
276
280 ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr& other) noexcept
281 : referencedObject (other.referencedObject)
282 {
283 incIfNotNull (referencedObject);
284 }
285
287 ReferenceCountedObjectPtr (ReferenceCountedObjectPtr&& other) noexcept
288 : referencedObject (other.referencedObject)
289 {
290 other.referencedObject = nullptr;
291 }
292
296 template <typename Convertible>
297 ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr<Convertible>& other) noexcept
298 : referencedObject (other.get())
299 {
300 incIfNotNull (referencedObject);
301 }
302
307 ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr& other)
308 {
309 return operator= (other.referencedObject);
310 }
311
316 template <typename Convertible>
317 ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr<Convertible>& other)
318 {
319 return operator= (other.get());
320 }
321
327 ReferenceCountedObjectPtr& operator= (ReferencedType* newObject)
328 {
329 if (newObject != nullptr)
330 return operator= (*newObject);
331
332 reset();
333 return *this;
334 }
335
341 ReferenceCountedObjectPtr& operator= (ReferencedType& newObject)
342 {
343 if (referencedObject != &newObject)
344 {
345 newObject.incReferenceCount();
346 auto* oldObject = referencedObject;
347 referencedObject = &newObject;
348 decIfNotNull (oldObject);
349 }
350
351 return *this;
352 }
353
355 ReferenceCountedObjectPtr& operator= (decltype (nullptr))
356 {
357 reset();
358 return *this;
359 }
360
362 ReferenceCountedObjectPtr& operator= (ReferenceCountedObjectPtr&& other) noexcept
363 {
364 std::swap (referencedObject, other.referencedObject);
365 return *this;
366 }
367
372 ~ReferenceCountedObjectPtr()
373 {
374 decIfNotNull (referencedObject);
375 }
376
377 //==============================================================================
381 ReferencedType* get() const noexcept { return referencedObject; }
382
384 void reset() noexcept
385 {
386 auto oldObject = referencedObject; // need to null the pointer before deleting the object
387 referencedObject = nullptr; // in case this ptr is itself deleted as a side-effect
388 decIfNotNull (oldObject); // of the destructor
389 }
390
391 // the -> operator is called on the referenced object
392 ReferencedType* operator->() const noexcept
393 {
394 jassert (referencedObject != nullptr); // null pointer method call!
395 return referencedObject;
396 }
397
401 ReferencedType& operator*() const noexcept { jassert (referencedObject != nullptr); return *referencedObject; }
402
404 bool operator== (decltype (nullptr)) const noexcept { return referencedObject == nullptr; }
406 bool operator!= (decltype (nullptr)) const noexcept { return referencedObject != nullptr; }
407
409 bool operator== (const ObjectType* other) const noexcept { return referencedObject == other; }
411 bool operator== (const ReferenceCountedObjectPtr& other) const noexcept { return referencedObject == other.get(); }
413 bool operator!= (const ObjectType* other) const noexcept { return referencedObject != other; }
415 bool operator!= (const ReferenceCountedObjectPtr& other) const noexcept { return referencedObject != other.get(); }
416
417 #if JUCE_STRICT_REFCOUNTEDPOINTER
419 explicit operator bool() const noexcept { return referencedObject != nullptr; }
420
421 #else
428 operator ReferencedType*() const noexcept { return referencedObject; }
429 #endif
430
431 #ifndef DOXYGEN
432 [[deprecated ("Use the get method instead.")]]
433 ReferencedType* getObject() const { return get(); }
434 #endif
435
436private:
437 //==============================================================================
438 ReferencedType* referencedObject = nullptr;
439
440 static void incIfNotNull (ReferencedType* o) noexcept
441 {
442 if (o != nullptr)
443 o->incReferenceCount();
444 }
445
446 static void decIfNotNull (ReferencedType* o) noexcept
447 {
448 if (o != nullptr && o->decReferenceCountWithoutDeleting())
449 ContainerDeletePolicy<ReferencedType>::destroy (o);
450 }
451};
452
453
454//==============================================================================
456template <typename Type>
457bool operator== (const Type* object1, const ReferenceCountedObjectPtr<Type>& object2) noexcept
458{
459 return object1 == object2.get();
460}
461
463template <typename Type>
464bool operator!= (const Type* object1, const ReferenceCountedObjectPtr<Type>& object2) noexcept
465{
466 return object1 != object2.get();
467}
468
469} // namespace juce
juce::ReferenceCountedObjectPtr
Definition juce_ReferenceCountedObject.h:247
juce::ReferenceCountedObjectPtr::~ReferenceCountedObjectPtr
~ReferenceCountedObjectPtr()
Definition juce_ReferenceCountedObject.h:372
juce::ReferenceCountedObjectPtr::operator=
ReferenceCountedObjectPtr & operator=(const ReferenceCountedObjectPtr &other)
Definition juce_ReferenceCountedObject.h:307
juce::ReferenceCountedObjectPtr::ReferencedType
ObjectType ReferencedType
Definition juce_ReferenceCountedObject.h:250
juce::ReferenceCountedObjectPtr::operator!=
bool operator!=(decltype(nullptr)) const noexcept
Definition juce_ReferenceCountedObject.h:406
juce::ReferenceCountedObjectPtr::ReferenceCountedObjectPtr
ReferenceCountedObjectPtr(ReferenceCountedObjectPtr &&other) noexcept
Definition juce_ReferenceCountedObject.h:287
juce::ReferenceCountedObjectPtr::operator==
bool operator==(decltype(nullptr)) const noexcept
Definition juce_ReferenceCountedObject.h:404
juce::ReferenceCountedObjectPtr::ReferenceCountedObjectPtr
ReferenceCountedObjectPtr(ReferencedType *refCountedObject) noexcept
Definition juce_ReferenceCountedObject.h:262
juce::ReferenceCountedObjectPtr::ReferenceCountedObjectPtr
ReferenceCountedObjectPtr(ReferencedType &refCountedObject) noexcept
Definition juce_ReferenceCountedObject.h:271
juce::ReferenceCountedObjectPtr::ReferenceCountedObjectPtr
ReferenceCountedObjectPtr()=default
juce::ReferenceCountedObjectPtr::ReferenceCountedObjectPtr
ReferenceCountedObjectPtr(const ReferenceCountedObjectPtr< Convertible > &other) noexcept
Definition juce_ReferenceCountedObject.h:297
juce::ReferenceCountedObjectPtr::reset
void reset() noexcept
Definition juce_ReferenceCountedObject.h:384
juce::ReferenceCountedObjectPtr::ReferenceCountedObjectPtr
ReferenceCountedObjectPtr(decltype(nullptr)) noexcept
Definition juce_ReferenceCountedObject.h:257
juce::ReferenceCountedObjectPtr::operator*
ReferencedType & operator*() const noexcept
Definition juce_ReferenceCountedObject.h:401
juce::ReferenceCountedObjectPtr::ReferenceCountedObjectPtr
ReferenceCountedObjectPtr(const ReferenceCountedObjectPtr &other) noexcept
Definition juce_ReferenceCountedObject.h:280
juce::ReferenceCountedObjectPtr::get
ReferencedType * get() const noexcept
Definition juce_ReferenceCountedObject.h:381
juce::ReferenceCountedObject
Definition juce_ReferenceCountedObject.h:66
juce::ReferenceCountedObject::ReferenceCountedObject
ReferenceCountedObject(const ReferenceCountedObject &) noexcept
Definition juce_ReferenceCountedObject.h:110
juce::ReferenceCountedObject::ReferenceCountedObject
ReferenceCountedObject(ReferenceCountedObject &&) noexcept
Definition juce_ReferenceCountedObject.h:112
juce::ReferenceCountedObject::resetReferenceCount
void resetReferenceCount() noexcept
Definition juce_ReferenceCountedObject.h:128
juce::ReferenceCountedObject::decReferenceCountWithoutDeleting
bool decReferenceCountWithoutDeleting() noexcept
Definition juce_ReferenceCountedObject.h:94
juce::ReferenceCountedObject::ReferenceCountedObject
ReferenceCountedObject()=default
juce::ReferenceCountedObject::incReferenceCount
void incReferenceCount() noexcept
Definition juce_ReferenceCountedObject.h:74
juce::ReferenceCountedObject::getReferenceCount
int getReferenceCount() const noexcept
Definition juce_ReferenceCountedObject.h:101
juce::ReferenceCountedObject::decReferenceCount
void decReferenceCount() noexcept
Definition juce_ReferenceCountedObject.h:82
juce::ReferenceCountedObject::~ReferenceCountedObject
virtual ~ReferenceCountedObject()
Definition juce_ReferenceCountedObject.h:119
juce::SingleThreadedReferenceCountedObject
Definition juce_ReferenceCountedObject.h:154
juce::SingleThreadedReferenceCountedObject::~SingleThreadedReferenceCountedObject
virtual ~SingleThreadedReferenceCountedObject()
Definition juce_ReferenceCountedObject.h:207
juce::SingleThreadedReferenceCountedObject::SingleThreadedReferenceCountedObject
SingleThreadedReferenceCountedObject(SingleThreadedReferenceCountedObject &&)
Definition juce_ReferenceCountedObject.h:200
juce::SingleThreadedReferenceCountedObject::SingleThreadedReferenceCountedObject
SingleThreadedReferenceCountedObject(const SingleThreadedReferenceCountedObject &)
Definition juce_ReferenceCountedObject.h:198
juce::SingleThreadedReferenceCountedObject::SingleThreadedReferenceCountedObject
SingleThreadedReferenceCountedObject()=default
juce::SingleThreadedReferenceCountedObject::decReferenceCount
void decReferenceCount() noexcept
Definition juce_ReferenceCountedObject.h:170
juce::SingleThreadedReferenceCountedObject::incReferenceCount
void incReferenceCount() noexcept
Definition juce_ReferenceCountedObject.h:162
juce::SingleThreadedReferenceCountedObject::getReferenceCount
int getReferenceCount() const noexcept
Definition juce_ReferenceCountedObject.h:189
juce::SingleThreadedReferenceCountedObject::decReferenceCountWithoutDeleting
bool decReferenceCountWithoutDeleting() noexcept
Definition juce_ReferenceCountedObject.h:182
juce::Atomic
Definition juce_Atomic.h:42
juce::ContainerDeletePolicy
Definition juce_ContainerDeletePolicy.h:41