Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
RefTarget< T > Class Template Reference

#include <Reference.h>

Public Member Functions

 RefTarget ()=default
 Constructor.
 
 RefTarget (const RefTarget &)
 
 ~RefTarget ()
 assert no one is referencing us
 
void SetEmbedded () const
 
RefTargetoperator= (const RefTarget &)
 Assignment operator.
 
uint32 GetRefCount () const
 Get current refcount of this object.
 
void AddRef () const
 Add or release a reference to this object.
 
void Release () const
 

Static Public Member Functions

static int sInternalGetRefCountOffset ()
 INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
 

Protected Attributes

atomic< uint32mRefCount = 0
 Current reference count.
 

Static Protected Attributes

static constexpr uint32 cEmbedded = 0x0ebedded
 A large value that gets added to the refcount to mark the object as embedded.
 

Detailed Description

template<class T>
class RefTarget< T >

Simple class to facilitate reference counting / releasing Derive your class from RefTarget and you can reference it by using Ref<classname> or RefConst<classname>

Reference counting classes keep an integer which indicates how many references to the object are active. Reference counting objects are derived from RefTarget and staT & their life with a reference count of zero. They can then be assigned to equivalents of pointers (Ref) which will increase the reference count immediately. If the destructor of Ref is called or another object is assigned to the reference counting pointer it will decrease the reference count of the object again. If this reference count becomes zero, the object is destroyed.

This provides a very powerful mechanism to prevent memory leaks, but also gives some responsibility to the programmer. The most notable point is that you cannot have one object reference another and have the other reference the first one back, because this way the reference count of both objects will never become lower than 1, resulting in a memory leak. By carefully designing your classes (and particularly identifying who owns who in the class hierarchy) you can avoid these problems.

Constructor & Destructor Documentation

◆ RefTarget() [1/2]

template<class T >
RefTarget< T >::RefTarget ( )
inlinedefault

Constructor.

◆ RefTarget() [2/2]

template<class T >
RefTarget< T >::RefTarget ( const RefTarget< T > &  )
inline

◆ ~RefTarget()

template<class T >
RefTarget< T >::~RefTarget ( )
inline

assert no one is referencing us

Member Function Documentation

◆ AddRef()

template<class T >
void RefTarget< T >::AddRef ( ) const
inline

Add or release a reference to this object.

◆ GetRefCount()

template<class T >
uint32 RefTarget< T >::GetRefCount ( ) const
inline

Get current refcount of this object.

◆ operator=()

template<class T >
RefTarget & RefTarget< T >::operator= ( const RefTarget< T > &  )
inline

Assignment operator.

◆ Release()

template<class T >
void RefTarget< T >::Release ( ) const
inline

◆ SetEmbedded()

template<class T >
void RefTarget< T >::SetEmbedded ( ) const
inline

Mark this class as embedded, this means the type can be used in a compound or constructed on the stack. The Release function will never destruct the object, it is assumed the destructor will be called by whoever allocated the object and at that point in time it is checked that no references are left to the structure.

◆ sInternalGetRefCountOffset()

template<class T >
static int RefTarget< T >::sInternalGetRefCountOffset ( )
inlinestatic

INTERNAL HELPER FUNCTION USED BY SERIALIZATION.

Member Data Documentation

◆ cEmbedded

template<class T >
constexpr uint32 RefTarget< T >::cEmbedded = 0x0ebedded
staticconstexprprotected

A large value that gets added to the refcount to mark the object as embedded.

◆ mRefCount

template<class T >
atomic<uint32> RefTarget< T >::mRefCount = 0
mutableprotected

Current reference count.


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