JUCE
Public Member Functions | Protected Member Functions | List of all members
ReferenceCountedObject Class Reference

A base class which provides methods for reference-counting. More...

Inheritance diagram for ReferenceCountedObject:

Public Member Functions

void incReferenceCount () noexcept
 Increments the object's reference count. More...
 
void decReferenceCount () noexcept
 Decreases the object's reference count. More...
 
bool decReferenceCountWithoutDeleting () noexcept
 Decreases the object's reference count. More...
 
int getReferenceCount () const noexcept
 Returns the object's current reference count. More...
 

Protected Member Functions

 ReferenceCountedObject ()=default
 Creates the reference-counted object (with an initial ref count of zero). More...
 
 ReferenceCountedObject (const ReferenceCountedObject &) noexcept
 Copying from another object does not affect this one's reference-count. More...
 
 ReferenceCountedObject (ReferenceCountedObject &&) noexcept
 Copying from another object does not affect this one's reference-count. More...
 
ReferenceCountedObjectoperator= (const ReferenceCountedObject &) noexcept
 Copying from another object does not affect this one's reference-count. More...
 
ReferenceCountedObjectoperator= (ReferenceCountedObject &&) noexcept
 Copying from another object does not affect this one's reference-count. More...
 
virtual ~ReferenceCountedObject ()
 Destructor. More...
 
void resetReferenceCount () noexcept
 Resets the reference count to zero without deleting the object. More...
 

Detailed Description

A base class which provides methods for reference-counting.

To add reference-counting to a class, derive it from this class, and use the ReferenceCountedObjectPtr class to point to it.

e.g.

class MyClass : public ReferenceCountedObject
{
void foo();
// This is a neat way of declaring a typedef for a pointer class,
// rather than typing out the full templated name each time..
};
MyClass::Ptr p = new MyClass();
MyClass::Ptr p2 = p;
p = nullptr;
p2->foo();

Once a new ReferenceCountedObject has been assigned to a pointer, be careful not to delete the object manually.

This class uses an Atomic<int> value to hold the reference count, so the reference count can be updated on multiple threads. Note that whilst it's thread-safe to create and delete a ReferenceCountedObjectPtr to a ReferenceCountedObject shared between threads, it's not thread-safe to modify or swap the ReferenceCountedObject.

For a faster but non-thread-safe version, use SingleThreadedReferenceCountedObject instead.

See also
ReferenceCountedObjectPtr, ReferenceCountedArray, SingleThreadedReferenceCountedObject

Constructor & Destructor Documentation

◆ ReferenceCountedObject() [1/3]

ReferenceCountedObject::ReferenceCountedObject ( )
protecteddefault

Creates the reference-counted object (with an initial ref count of zero).

◆ ReferenceCountedObject() [2/3]

ReferenceCountedObject::ReferenceCountedObject ( const ReferenceCountedObject )
protectednoexcept

Copying from another object does not affect this one's reference-count.

◆ ReferenceCountedObject() [3/3]

ReferenceCountedObject::ReferenceCountedObject ( ReferenceCountedObject &&  )
protectednoexcept

Copying from another object does not affect this one's reference-count.

◆ ~ReferenceCountedObject()

virtual ReferenceCountedObject::~ReferenceCountedObject ( )
protectedvirtual

Destructor.

References jassert.

Member Function Documentation

◆ incReferenceCount()

void ReferenceCountedObject::incReferenceCount ( )
noexcept

Increments the object's reference count.

This is done automatically by the smart pointer, but is public just in case it's needed for nefarious purposes.

◆ decReferenceCount()

void ReferenceCountedObject::decReferenceCount ( )
noexcept

Decreases the object's reference count.

If the count gets to zero, the object will be deleted.

References jassert.

◆ decReferenceCountWithoutDeleting()

bool ReferenceCountedObject::decReferenceCountWithoutDeleting ( )
noexcept

Decreases the object's reference count.

If the count gets to zero, the object will not be deleted, but this method will return true, allowing the caller to take care of deletion.

References jassert.

◆ getReferenceCount()

int ReferenceCountedObject::getReferenceCount ( ) const
noexcept

Returns the object's current reference count.

◆ operator=() [1/2]

ReferenceCountedObject& ReferenceCountedObject::operator= ( const ReferenceCountedObject )
protectednoexcept

Copying from another object does not affect this one's reference-count.

◆ operator=() [2/2]

ReferenceCountedObject& ReferenceCountedObject::operator= ( ReferenceCountedObject &&  )
protectednoexcept

Copying from another object does not affect this one's reference-count.

◆ resetReferenceCount()

void ReferenceCountedObject::resetReferenceCount ( )
protectednoexcept

Resets the reference count to zero without deleting the object.

You should probably never need to use this!


The documentation for this class was generated from the following file: