AGX Dynamics 2.38.0.0
Loading...
Searching...
No Matches
agx::Referenced Class Reference

Base class providing referencing counted objects. More...

#include <Referenced.h>

+ Inheritance diagram for agx::Referenced:

Public Member Functions

 Referenced ()
 Default constructor.
 
 Referenced (const Referenced &)
 
template<typename T >
T * as ()
 Subclass casting.
 
template<typename T >
const T * as () const
 
template<typename T >
T * asSafe ()
 Safe subclass casting, return nullptr if template type does not match.
 
template<typename T >
const T * asSafe () const
 
int getReferenceCount () const
 
template<typename T >
bool is () const
 Subclass test.
 
Referencedoperator= (const Referenced &)
 Assignment operator. Will increment the number of references to the referenced object.
 
void reference (void *ptr=nullptr) const
 Explicitly increment the reference count by one, indicating that this object has another pointer which is referencing it.
 
void unreference (void *ptr=nullptr) const
 Decrement the reference count by one, indicating that a pointer to this object is referencing it.
 
void unreference_nodelete () const
 Decrement the reference count by one, indicating that a pointer to this object is referencing it.
 

Static Public Member Functions

template<typename T >
static bool ValidateCast (const Referenced *object)
 

Protected Member Functions

virtual ~Referenced ()
 Destructor.
 
void allocateObserverVector () const
 
void deleteUsingDeleteHandler () const
 

Static Protected Member Functions

static DeleteHandlergetDeleteHandler ()
 
static void setDeleteHandler (DeleteHandler *handler)
 Internal: Set a DeleteHandler to which deletion of all referenced counted objects will be delegated to.
 

Protected Attributes

Mutex m_mutex
 
ObserverContainer m_observers
 
AtomicValue m_refCount
 

Detailed Description

Base class providing referencing counted objects.

Derive from this class when you need automatic reference counting for objects. Always keep the destructor of your derived class protected. One must never create a reference counted object on the stack. This will lead to corrupted memory if any reference pointers try to delete it.

// when ptr goes out of scope, the reference to the instance of YourClass will be
// decremented. If reference count is 0, it will be deleted.
ref_ptr<YourClass> ptr = new YourClass();
// Keep an observer pointer to the allocated object. When the instance of YourClass is deleted,
// the oPtr.valid() returns true.
Smart pointer for observed objects, that automatically set pointers to them to null when they deleted...
Definition: observer_ptr.h:61
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
T * get() const
Definition: ref_ptr.h:256

Definition at line 119 of file Referenced.h.

Constructor & Destructor Documentation

◆ Referenced() [1/2]

agx::Referenced::Referenced ( )

Default constructor.

◆ Referenced() [2/2]

agx::Referenced::Referenced ( const Referenced )

◆ ~Referenced()

virtual agx::Referenced::~Referenced ( )
protectedvirtual

Destructor.

Member Function Documentation

◆ allocateObserverVector()

void agx::Referenced::allocateObserverVector ( ) const
protected

◆ as() [1/2]

template<typename T >
T * agx::Referenced::as ( )

Subclass casting.

Template type must match actual type, dynamic_cast assertion will fail otherwise.

Referenced by agx::InternalData::get(), agxVehicle::Track::getNode(), and agx::InternalData::getOrCreate().

◆ as() [2/2]

template<typename T >
const T * agx::Referenced::as ( ) const

◆ asSafe() [1/2]

template<typename T >
T * agx::Referenced::asSafe ( )

Safe subclass casting, return nullptr if template type does not match.

Referenced by agxUtil::CollectBodiesAndWiresVisitor::visit().

◆ asSafe() [2/2]

template<typename T >
const T * agx::Referenced::asSafe ( ) const

◆ deleteUsingDeleteHandler()

void agx::Referenced::deleteUsingDeleteHandler ( ) const
protected

◆ getDeleteHandler()

static DeleteHandler * agx::Referenced::getDeleteHandler ( )
staticprotected
Returns
a DeleteHandler.

◆ getReferenceCount()

int agx::Referenced::getReferenceCount ( ) const
Returns
the number pointers currently referencing this object.

◆ is()

template<typename T >
bool agx::Referenced::is ( ) const

◆ operator=()

Referenced & agx::Referenced::operator= ( const Referenced )

Assignment operator. Will increment the number of references to the referenced object.

◆ reference()

void agx::Referenced::reference ( void *  ptr = nullptr) const

Explicitly increment the reference count by one, indicating that this object has another pointer which is referencing it.

This method should be used with care, always balanced with the same number of calls to unreference.

Referenced by agxData::Val< T >::Val().

◆ setDeleteHandler()

static void agx::Referenced::setDeleteHandler ( DeleteHandler handler)
staticprotected

Internal: Set a DeleteHandler to which deletion of all referenced counted objects will be delegated to.

◆ unreference()

void agx::Referenced::unreference ( void *  ptr = nullptr) const

Decrement the reference count by one, indicating that a pointer to this object is referencing it.

If the reference count goes to zero, it is assumed that this object is no longer referenced and is automatically deleted.

This method should be used with care, always balanced with the same number of calls to reference.

◆ unreference_nodelete()

void agx::Referenced::unreference_nodelete ( ) const

Decrement the reference count by one, indicating that a pointer to this object is referencing it.

However, do not delete it, even if ref count goes to 0. Warning, unref_nodelete() should only be called if the user knows exactly who will be responsible for, one should prefer unref() over unref_nodelete() as the later can lead to memory leaks.

◆ ValidateCast()

template<typename T >
static bool agx::Referenced::ValidateCast ( const Referenced object)
static

Member Data Documentation

◆ m_mutex

Mutex agx::Referenced::m_mutex
mutableprotected

Definition at line 246 of file Referenced.h.

◆ m_observers

ObserverContainer agx::Referenced::m_observers
mutableprotected

Definition at line 245 of file Referenced.h.

◆ m_refCount

AtomicValue agx::Referenced::m_refCount
mutableprotected

Definition at line 247 of file Referenced.h.


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