Public Member Functions | Protected Member Functions

ReferenceCountedObject Class Reference

Adds reference-counting to an object. More...

Inherited by AudioProcessorGraph::Node, DynamicObject, Image::SharedImage, Message, and SynthesiserSound.

List of all members.

Public Member Functions

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

Protected Member Functions

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

Detailed Description

Adds reference-counting to an object.

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..
        typedef ReferenceCountedObjectPtr<MyClass> Ptr;
    };

    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 that it the pointers can be passed between threads safely. For a faster but non-thread-safe version, use SingleThreadedReferenceCountedObject instead.

See also:
ReferenceCountedObjectPtr, ReferenceCountedArray, SingleThreadedReferenceCountedObject

Constructor & Destructor Documentation

ReferenceCountedObject::ReferenceCountedObject (  ) [protected]

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

virtual ReferenceCountedObject::~ReferenceCountedObject (  ) [protected, virtual]

Destructor.

References jassert.


Member Function Documentation

void ReferenceCountedObject::incReferenceCount (  )

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.

void ReferenceCountedObject::decReferenceCount (  )

Decreases the object's reference count.

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

References jassert.

int ReferenceCountedObject::getReferenceCount (  ) const

Returns the object's current reference count.


The documentation for this class was generated from the following file:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines