Loading...
Searching...
No Matches
juce_ReferenceCountedObject.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class JUCE_API ReferenceCountedObject
{
public:
//==============================================================================
void incReferenceCount() noexcept
{
++refCount;
}
void decReferenceCount() noexcept
{
jassert (getReferenceCount() > 0);
if (--refCount == 0)
delete this;
}
bool decReferenceCountWithoutDeleting() noexcept
{
jassert (getReferenceCount() > 0);
return --refCount == 0;
}
int getReferenceCount() const noexcept { return refCount.get(); }
protected:
//==============================================================================
ReferenceCountedObject() = default;
ReferenceCountedObject (const ReferenceCountedObject&) noexcept {}
ReferenceCountedObject (ReferenceCountedObject&&) noexcept {}
ReferenceCountedObject& operator= (const ReferenceCountedObject&) noexcept { return *this; }
ReferenceCountedObject& operator= (ReferenceCountedObject&&) noexcept { return *this; }
virtual ~ReferenceCountedObject()
{
// it's dangerous to delete an object that's still referenced by something else!
jassert (getReferenceCount() == 0);
}
void resetReferenceCount() noexcept
{
refCount = 0;
}
private:
//==============================================================================
Atomic<int> refCount { 0 };
friend struct ContainerDeletePolicy<ReferenceCountedObject>;
};
//==============================================================================
class JUCE_API SingleThreadedReferenceCountedObject
{
public:
//==============================================================================
void incReferenceCount() noexcept
{
++refCount;
}
void decReferenceCount() noexcept
{
jassert (getReferenceCount() > 0);
if (--refCount == 0)
delete this;
}
bool decReferenceCountWithoutDeleting() noexcept
{
jassert (getReferenceCount() > 0);
return --refCount == 0;
}
int getReferenceCount() const noexcept { return refCount; }
protected:
//==============================================================================
SingleThreadedReferenceCountedObject() = default;
SingleThreadedReferenceCountedObject (const SingleThreadedReferenceCountedObject&) {}
SingleThreadedReferenceCountedObject (SingleThreadedReferenceCountedObject&&) {}
SingleThreadedReferenceCountedObject& operator= (const SingleThreadedReferenceCountedObject&) { return *this; }
SingleThreadedReferenceCountedObject& operator= (SingleThreadedReferenceCountedObject&&) { return *this; }
virtual ~SingleThreadedReferenceCountedObject()
{
// it's dangerous to delete an object that's still referenced by something else!
jassert (getReferenceCount() == 0);
}
private:
//==============================================================================
int refCount = 0;
friend struct ContainerDeletePolicy<ReferenceCountedObject>;
};
//==============================================================================
template <class ObjectType>
class ReferenceCountedObjectPtr
{
public:
using ReferencedType = ObjectType;
//==============================================================================
ReferenceCountedObjectPtr() = default;
ReferenceCountedObjectPtr (decltype (nullptr)) noexcept {}
ReferenceCountedObjectPtr (ReferencedType* refCountedObject) noexcept
: referencedObject (refCountedObject)
{
incIfNotNull (refCountedObject);
}
ReferenceCountedObjectPtr (ReferencedType& refCountedObject) noexcept
: referencedObject (&refCountedObject)
{
refCountedObject.incReferenceCount();
}
ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr& other) noexcept
: referencedObject (other.referencedObject)
{
incIfNotNull (referencedObject);
}
ReferenceCountedObjectPtr (ReferenceCountedObjectPtr&& other) noexcept
: referencedObject (other.referencedObject)
{
other.referencedObject = nullptr;
}
template <typename Convertible>
ReferenceCountedObjectPtr (const ReferenceCountedObjectPtr<Convertible>& other) noexcept
: referencedObject (other.get())
{
incIfNotNull (referencedObject);
}
ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr& other)
{
return operator= (other.referencedObject);
}
template <typename Convertible>
ReferenceCountedObjectPtr& operator= (const ReferenceCountedObjectPtr<Convertible>& other)
{
return operator= (other.get());
}
ReferenceCountedObjectPtr& operator= (ReferencedType* newObject)
{
if (newObject != nullptr)
return operator= (*newObject);
reset();
return *this;
}
ReferenceCountedObjectPtr& operator= (ReferencedType& newObject)
{
if (referencedObject != &newObject)
{
newObject.incReferenceCount();
auto* oldObject = referencedObject;
referencedObject = &newObject;
decIfNotNull (oldObject);
}
return *this;
}
ReferenceCountedObjectPtr& operator= (decltype (nullptr))
{
reset();
return *this;
}
ReferenceCountedObjectPtr& operator= (ReferenceCountedObjectPtr&& other) noexcept
{
std::swap (referencedObject, other.referencedObject);
return *this;
}
~ReferenceCountedObjectPtr()
{
decIfNotNull (referencedObject);
}
//==============================================================================
ReferencedType* get() const noexcept { return referencedObject; }
void reset() noexcept
{
auto oldObject = referencedObject; // need to null the pointer before deleting the object
referencedObject = nullptr; // in case this ptr is itself deleted as a side-effect
decIfNotNull (oldObject); // of the destructor
}
// the -> operator is called on the referenced object
ReferencedType* operator->() const noexcept
{
jassert (referencedObject != nullptr); // null pointer method call!
return referencedObject;
}
ReferencedType& operator*() const noexcept { jassert (referencedObject != nullptr); return *referencedObject; }
bool operator== (decltype (nullptr)) const noexcept { return referencedObject == nullptr; }
bool operator!= (decltype (nullptr)) const noexcept { return referencedObject != nullptr; }
bool operator== (const ObjectType* other) const noexcept { return referencedObject == other; }
bool operator== (const ReferenceCountedObjectPtr& other) const noexcept { return referencedObject == other.get(); }
bool operator!= (const ObjectType* other) const noexcept { return referencedObject != other; }
bool operator!= (const ReferenceCountedObjectPtr& other) const noexcept { return referencedObject != other.get(); }
#if JUCE_STRICT_REFCOUNTEDPOINTER
explicit operator bool() const noexcept { return referencedObject != nullptr; }
#else
operator ReferencedType*() const noexcept { return referencedObject; }
#endif
#ifndef DOXYGEN
[[deprecated ("Use the get method instead.")]]
ReferencedType* getObject() const { return get(); }
#endif
private:
//==============================================================================
ReferencedType* referencedObject = nullptr;
static void incIfNotNull (ReferencedType* o) noexcept
{
if (o != nullptr)
o->incReferenceCount();
}
static void decIfNotNull (ReferencedType* o) noexcept
{
if (o != nullptr && o->decReferenceCountWithoutDeleting())
ContainerDeletePolicy<ReferencedType>::destroy (o);
}
};
//==============================================================================
template <typename Type>
bool operator== (const Type* object1, const ReferenceCountedObjectPtr<Type>& object2) noexcept
{
return object1 == object2.get();
}
template <typename Type>
bool operator!= (const Type* object1, const ReferenceCountedObjectPtr<Type>& object2) noexcept
{
return object1 != object2.get();
}
} // 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< SynthesiserSound >::ReferencedType
SynthesiserSound 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::~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