Jolt Physics
A multi core friendly Game Physics Engine
|
#include <Reference.h>
Public Member Functions | |
RefTarget ()=default | |
Constructor. | |
RefTarget (const RefTarget &) | |
~RefTarget () | |
assert no one is referencing us | |
void | SetEmbedded () const |
RefTarget & | operator= (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< uint32 > | mRefCount = 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. | |
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.
|
inlinedefault |
Constructor.
|
inline |
|
inline |
Add or release a reference to this object.
Get current refcount of this object.
Assignment operator.
|
inline |
|
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.
|
inlinestatic |
INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
A large value that gets added to the refcount to mark the object as embedded.
Current reference count.