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

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

#include <base/mem/Reference.h>

Public Member Functions

 Reference () noexcept
 
 Reference (TYPE *_value) noexcept
 
 Reference (const Reference &copy) noexcept
 
 Reference (Reference &&move) noexcept
 
template<class POLY >
 Reference (const Reference< POLY > &copy) noexcept
 
template<class POLY >
bool isType () const noexcept
 
template<class POLY >
Reference< POLY > castChecked () const
 
template<class POLY >
Reference< POLY > cast () const noexcept
 
Referenceoperator= (const Reference &copy)
 
Referenceoperator= (Reference &&move)
 
template<class POLY >
Referenceoperator= (const Reference< POLY > &assign)
 
Referenceoperator= (TYPE *assign)
 
bool operator== (NullPtr) const noexcept
 
bool operator== (const Reference &compare) const noexcept
 
bool operator!= (const Reference &compare) const noexcept
 
TYPE * getValue () const noexcept
 
void setValue (TYPE *_value)
 
bool isMultiReferenced () const noexcept
 
MemorySize getNumberOfReferences () const noexcept
 
void copyOnWrite ()
 
void invalidate ()
 
bool isValid () const noexcept
 
TYPE & operator* ()
 
const TYPE & operator* () const
 
TYPE * operator-> ()
 
const TYPE * operator-> () const
 
 operator bool () const noexcept
 
void garbageCollect ()
 
 ~Reference ()
 

Static Public Member Functions

static Reference make ()
 
static void swapper (Reference &a, Reference &b) noexcept
 

Detailed Description

template<class TYPE>
class Reference< 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. This pointer can only be used to reference count objects of type ReferenceCountedObject but is faster than ReferenceCounter. The Reference class is relocateable.

Note that is it safe to convert this pointer back to a Reference pointer.

This is the recommended automation pointer. Only use other automation classes when needed.

void MyClass::doit() { Reference<MyClass> myObject = this; useIt(myObject); }

See also
ReferenceCountedObject ReferenceCounter
Version
1.0

Constructor & Destructor Documentation

◆ Reference() [1/5]

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

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

◆ Reference() [2/5]

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

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

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

◆ Reference() [3/5]

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

Initialization of automation pointer from other automation pointer.

◆ Reference() [4/5]

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

Move initialization of automation pointer from other automation pointer.

◆ Reference() [5/5]

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

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

◆ ~Reference()

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

Destroys the automation pointer.

Member Function Documentation

◆ cast()

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

Dynamic cast to the specified type.

◆ castChecked()

template<class TYPE >
template<class POLY >
Reference<POLY> Reference< 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 Reference< 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.

◆ garbageCollect()

template<class TYPE >
void Reference< TYPE >::garbageCollect ( )
inline

Garbage collects object. Object will be set to nullptr.

◆ getNumberOfReferences()

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

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

◆ getValue()

template<class TYPE >
TYPE* Reference< 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.

Examples
testsuite/HashTable.cpp, and testsuite/Map.cpp.

◆ invalidate()

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

Invalidates the reference.

◆ isMultiReferenced()

template<class TYPE >
bool Reference< 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 Reference< TYPE >::isType ( ) const
inlinenoexcept

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

◆ isValid()

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

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

◆ make()

template<class TYPE >
static Reference Reference< TYPE >::make ( )
inlinestatic

Returns a new object by using default construction.

◆ operator bool()

template<class TYPE >
Reference< 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 Reference< TYPE >::operator!= ( const Reference< TYPE > &  compare) const
inlinenoexcept

Returns true if the references are non-equal.

◆ operator*() [1/2]

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

Returns the reference counted object.

◆ operator*() [2/2]

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

Returns the reference counted object.

◆ operator->() [1/2]

template<class TYPE >
TYPE* Reference< TYPE >::operator-> ( )
inline

Returns the reference counted object.

◆ operator->() [2/2]

template<class TYPE >
const TYPE* Reference< TYPE >::operator-> ( ) const
inline

Returns the reference counted object.

◆ operator=() [1/4]

template<class TYPE >
Reference& Reference< TYPE >::operator= ( const Reference< TYPE > &  copy)
inline

Assignment of automation pointer to this automation pointer.

◆ operator=() [2/4]

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

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

◆ operator=() [3/4]

template<class TYPE >
Reference& Reference< TYPE >::operator= ( Reference< TYPE > &&  move)
inline

Assignment of automation pointer to this automation pointer.

◆ operator=() [4/4]

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

Assignment of normal pointer to this automation pointer.

◆ operator==() [1/2]

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

Returns true if the references are equal.

◆ operator==() [2/2]

template<class TYPE >
bool Reference< TYPE >::operator== ( NullPtr  ) const
inlinenoexcept

Returns true if the reference is nullptr.

◆ setValue()

template<class TYPE >
void Reference< TYPE >::setValue ( TYPE *  _value)
inline

Sets the pointer value of this automation pointer.

Reference
Automation pointer that counts the number of references to an object.
Definition: Reference.h:25