Base Framework
Public Types | Public Member Functions | List of all members
ReferenceCounter< TYPE > Class Template Reference

Automation pointer that counts the number of references to an object. More...

#include <base/mem/ReferenceCounter.h>

Public Types

typedef PreferredAtomicCounter Counter
 

Public Member Functions

 ReferenceCounter () noexcept
 
 ReferenceCounter (TYPE *_value) noexcept
 
 ReferenceCounter (TYPE *_value, Counter *_references) noexcept
 
 ReferenceCounter (const ReferenceCounter &copy) noexcept
 
 ReferenceCounter (ReferenceCounter &&move) noexcept
 
template<class POLY >
 ReferenceCounter (const ReferenceCounter< POLY > &copy) noexcept
 
template<class POLY >
 ReferenceCounter (ReferenceCounter< POLY > &&move) noexcept
 
template<class POLY >
bool isType () const noexcept
 
template<class POLY >
ReferenceCounter< POLY > castChecked () const
 
template<class POLY >
ReferenceCounter< POLY > cast () const noexcept
 
ReferenceCounteroperator= (const ReferenceCounter &assign)
 
template<class POLY >
ReferenceCounteroperator= (const ReferenceCounter< POLY > &assign)
 
ReferenceCounteroperator= (TYPE *assign)
 
bool operator== (const ReferenceCounter &compare) const noexcept
 
bool operator!= (const ReferenceCounter &compare) const noexcept
 
TYPE * getValue () const noexcept
 
CountergetReferences () const noexcept
 
bool isMultiReferenced () const noexcept
 
MemorySize getNumberOfReferences () const noexcept
 
void copyOnWrite ()
 
void invalidate ()
 
bool isValid () const noexcept
 
TYPE & operator* ()
 
const TYPE & operator* () const
 
TYPE * operator-> () noexcept
 
const TYPE * operator-> () const noexcept
 
 operator bool () const noexcept
 
 ~ReferenceCounter ()
 

Detailed Description

template<class TYPE>
class ReferenceCounter< TYPE >

Automation pointer that counts the number of references to an object.

Automation pointer for reference counting objects. This class is responsible for counting the total number of references to an object. The pointer automatically deletes a reference counted object when the number of references reaches zero. If possible, you should use the CombinedReferenceCounter in preference to this class. ReferenceCounter is a bit slower than CombinedReferenceCounter but works for any class being reference counted. The ReferenceCounter class is relocateable.

ReferenceCounter<MyClass> myObject = new MyClass();
ReferenceCounter<MyClass> myOtherObject = myObject;
See also
CombinedReferenceCounter
Version
1.0

Member Typedef Documentation

◆ Counter

template<class TYPE >
typedef PreferredAtomicCounter ReferenceCounter< TYPE >::Counter

The type of the reference counter.

Constructor & Destructor Documentation

◆ ReferenceCounter() [1/7]

template<class TYPE >
ReferenceCounter< TYPE >::ReferenceCounter ( )
inlinenoexcept

Initializes an automation pointer as invalid (i.e. nullptr).

◆ ReferenceCounter() [2/7]

template<class TYPE >
ReferenceCounter< TYPE >::ReferenceCounter ( TYPE *  _value)
inlinenoexcept

Initializes an automation pointer with the specified pointer value. The automation pointer may be implicitly initialized.

ReferenceCounter<MyClass> object = new MyClass();
Parameters
valueThe desired pointer value.

◆ ReferenceCounter() [3/7]

template<class TYPE >
ReferenceCounter< TYPE >::ReferenceCounter ( TYPE *  _value,
Counter _references 
)
inlinenoexcept

Initializes an automation pointer with the specified pointer value.

Parameters
valueThe desired pointer value.
referencesThe reference counter.

◆ ReferenceCounter() [4/7]

template<class TYPE >
ReferenceCounter< TYPE >::ReferenceCounter ( const ReferenceCounter< TYPE > &  copy)
inlinenoexcept

Initialization of automation pointer from other automation pointer.

◆ ReferenceCounter() [5/7]

template<class TYPE >
ReferenceCounter< TYPE >::ReferenceCounter ( ReferenceCounter< TYPE > &&  move)
inlinenoexcept

Move initialization of automation pointer from other automation pointer.

◆ ReferenceCounter() [6/7]

template<class TYPE >
template<class POLY >
ReferenceCounter< TYPE >::ReferenceCounter ( const ReferenceCounter< POLY > &  copy)
inlinenoexcept

Initialization of automation pointer from other automation pointer using compile time polymorphism.

◆ ReferenceCounter() [7/7]

template<class TYPE >
template<class POLY >
ReferenceCounter< TYPE >::ReferenceCounter ( ReferenceCounter< POLY > &&  move)
inlinenoexcept

Move initialization of automation pointer from other automation pointer using compile time polymorphism.

◆ ~ReferenceCounter()

template<class TYPE >
ReferenceCounter< TYPE >::~ReferenceCounter ( )
inline

Destroys the automation pointer.

Member Function Documentation

◆ cast()

template<class TYPE >
template<class POLY >
ReferenceCounter<POLY> ReferenceCounter< TYPE >::cast ( ) const
inlinenoexcept

Dynamic cast to the specified type.

◆ castChecked()

template<class TYPE >
template<class POLY >
ReferenceCounter<POLY> ReferenceCounter< TYPE >::castChecked ( ) const
inline

Dynamic cast to the specified type. Raises CastException if the reference is invalid or the reference cannot be cast to the specified type.

◆ copyOnWrite()

template<class TYPE >
void ReferenceCounter< TYPE >::copyOnWrite ( )
inline

Makes a new copy of the reference counted object if referenced by more than one automation pointer. This member function is invocated by some classes before a object is modified. The reference counted object must implement the default copy constructor for this to work.

◆ getNumberOfReferences()

template<class TYPE >
MemorySize ReferenceCounter< TYPE >::getNumberOfReferences ( ) const
inlinenoexcept

Returns the number of references. Returns 0 if pointer is nullptr.

◆ getReferences()

template<class TYPE >
Counter* ReferenceCounter< TYPE >::getReferences ( ) const
inlinenoexcept

Returns the reference counter. Do NOT modify.

◆ getValue()

template<class TYPE >
TYPE* ReferenceCounter< TYPE >::getValue ( ) const
inlinenoexcept

Returns the pointer value of this automation pointer. Be careful when utilizing this member function. Its completely up to you to ensure that the reference counting rules aren't violated. Invocation of this method should be avoided.

◆ invalidate()

template<class TYPE >
void ReferenceCounter< TYPE >::invalidate ( )
inline

Invalidates the reference.

◆ isMultiReferenced()

template<class TYPE >
bool ReferenceCounter< TYPE >::isMultiReferenced ( ) const
inlinenoexcept

Returns true if the reference counted object is referenced by more than one automation pointer.

Returns
False if the pointer is invalid (i.e. not pointing to an object).

◆ isType()

template<class TYPE >
template<class POLY >
bool ReferenceCounter< TYPE >::isType ( ) const
inlinenoexcept

Returns true if the object may be cast to the specified type.

◆ isValid()

template<class TYPE >
bool ReferenceCounter< TYPE >::isValid ( ) const
inlinenoexcept

Returns true if the automation pointer is valid (i.e. it is pointing to an object).

◆ operator bool()

template<class TYPE >
ReferenceCounter< TYPE >::operator bool ( ) const
inlinenoexcept

Returns true if the automation pointer is valid (i.e. it is pointing to an object).

◆ operator!=()

template<class TYPE >
bool ReferenceCounter< TYPE >::operator!= ( const ReferenceCounter< TYPE > &  compare) const
inlinenoexcept

Returns true if the references are non-equal.

◆ operator*() [1/2]

template<class TYPE >
TYPE& ReferenceCounter< TYPE >::operator* ( )
inline

Returns the reference counted object.

◆ operator*() [2/2]

template<class TYPE >
const TYPE& ReferenceCounter< TYPE >::operator* ( ) const
inline

Returns the reference counted object.

◆ operator->() [1/2]

template<class TYPE >
const TYPE* ReferenceCounter< TYPE >::operator-> ( ) const
inlinenoexcept

Returns the reference counted object.

◆ operator->() [2/2]

template<class TYPE >
TYPE* ReferenceCounter< TYPE >::operator-> ( )
inlinenoexcept

Returns the reference counted object.

◆ operator=() [1/3]

template<class TYPE >
ReferenceCounter& ReferenceCounter< TYPE >::operator= ( const ReferenceCounter< TYPE > &  assign)
inline

Assignment of automation pointer to this automation pointer.

◆ operator=() [2/3]

template<class TYPE >
template<class POLY >
ReferenceCounter& ReferenceCounter< TYPE >::operator= ( const ReferenceCounter< POLY > &  assign)
inline

Assignment of automation pointer to this automation pointer using compile time polymorphism.

◆ operator=() [3/3]

template<class TYPE >
ReferenceCounter& ReferenceCounter< TYPE >::operator= ( TYPE *  assign)
inline

Assignment of normal pointer to this automation pointer.

◆ operator==()

template<class TYPE >
bool ReferenceCounter< TYPE >::operator== ( const ReferenceCounter< TYPE > &  compare) const
inlinenoexcept

Returns true if the references are equal.

ReferenceCounter
Automation pointer that counts the number of references to an object.
Definition: ReferenceCounter.h:45