Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
RTTI Class Reference

#include <RTTI.h>

Classes

struct  BaseClass
 Base class information. More...
 

Public Types

using pCreateObjectFunction = void *(*)()
 Function to create an object. More...
 
using pDestructObjectFunction = void(*)(void *inObject)
 Function to destroy an object. More...
 
using pCreateRTTIFunction = void(*)(RTTI &inRTTI)
 Function to initialize the runtime type info structure. More...
 

Public Member Functions

 RTTI (const char *inName, int inSize, pCreateObjectFunction inCreateObject, pDestructObjectFunction inDestructObject)
 Constructor. More...
 
 RTTI (const char *inName, int inSize, pCreateObjectFunction inCreateObject, pDestructObjectFunction inDestructObject, pCreateRTTIFunction inCreateRTTI)
 
const char * GetName () const
 
void SetName (const char *inName)
 
int GetSize () const
 
bool IsAbstract () const
 
int GetBaseClassCount () const
 
const RTTIGetBaseClass (int inIdx) const
 
uint32 GetHash () const
 
void * CreateObject () const
 Create an object of this type (returns nullptr if the object is abstract) More...
 
void DestructObject (void *inObject) const
 Destruct object of this type (does nothing if the object is abstract) More...
 
void AddBaseClass (const RTTI *inRTTI, int inOffset)
 Add base class. More...
 
bool operator== (const RTTI &inRHS) const
 Equality operators. More...
 
bool operator!= (const RTTI &inRHS) const
 
bool IsKindOf (const RTTI *inRTTI) const
 Test if this class is derived from class of type inRTTI. More...
 
const void * CastTo (const void *inObject, const RTTI *inRTTI) const
 Cast inObject of this type to object of type inRTTI, returns nullptr if the cast is unsuccessful. More...
 
void AddAttribute (const SerializableAttribute &inAttribute)
 Attribute access. More...
 
int GetAttributeCount () const
 
const SerializableAttributeGetAttribute (int inIdx) const
 

Protected Attributes

const char * mName
 Class name. More...
 
int mSize
 Class size. More...
 
StaticArray< BaseClass, 4 > mBaseClasses
 Names of base classes. More...
 
pCreateObjectFunction mCreate
 Pointer to a function that will create a new instance of this class. More...
 
pDestructObjectFunction mDestruct
 Pointer to a function that will destruct an object of this class. More...
 
StaticArray< SerializableAttribute, 32 > mAttributes
 All attributes of this class. More...
 

Detailed Description

Light weight runtime type information system. This way we don't need to turn on the default RTTI system of the compiler (introducing a possible overhead for every class)

Notes:

  • An extra virtual member function is added. This adds 8 bytes to the size of an instance of the class (unless you are already using virtual functions).

To use RTTI on a specific class use:

Header file:

class Foo
{
    JPH_DECLARE_RTTI_VIRTUAL_BASE(Foo)
}

class Bar : public Foo
{
    JPH_DECLARE_RTTI_VIRTUAL(Bar)
};

Implementation file:

JPH_IMPLEMENT_RTTI_VIRTUAL_BASE(Foo)
{
}

JPH_IMPLEMENT_RTTI_VIRTUAL(Bar)
{
    JPH_ADD_BASE_CLASS(Bar, Foo) // Multiple inheritance is allowed, just do JPH_ADD_BASE_CLASS for every base class
}

For abstract classes use:

Header file:

class Foo
{
    JPH_DECLARE_RTTI_ABSTRACT_BASE(Foo)

public:
    virtual void AbstractFunction() = 0;
}

class Bar : public Foo
{
    JPH_DECLARE_RTTI_VIRTUAL(Bar)

public:
    virtual void AbstractFunction() { } // Function is now implemented so this class is no longer abstract
};

Implementation file:

JPH_IMPLEMENT_RTTI_ABSTRACT_BASE(Foo)
{
}

JPH_IMPLEMENT_RTTI_VIRTUAL(Bar)
{
    JPH_ADD_BASE_CLASS(Bar, Foo)
}

Example of usage in a program:

Foo *foo_ptr = new Foo;
Foo *bar_ptr = new Bar;

IsType(foo_ptr, RTTI(Bar)) returns false
IsType(bar_ptr, RTTI(Bar)) returns true

IsKindOf(foo_ptr, RTTI(Bar)) returns false
IsKindOf(bar_ptr, RTTI(Foo)) returns true
IsKindOf(bar_ptr, RTTI(Bar)) returns true

StaticCast<Bar>(foo_ptr) asserts and returns foo_ptr casted to Bar *
StaticCast<Bar>(bar_ptr) returns bar_ptr casted to Bar *

DynamicCast<Bar>(foo_ptr) returns nullptr
DynamicCast<Bar>(bar_ptr) returns bar_ptr casted to Bar *

Other feature of DynamicCast:

class A { int data[5]; };
class B { int data[7]; };
class C : public A, public B { int data[9]; };

C *c = new C;
A *a = c;

Note that:

B *b = (B *)a;

generates an invalid pointer,

B *b = StaticCast<B>(a);

doesn't compile, and

B *b = DynamicCast<B>(a);

does the correct cast

Member Typedef Documentation

◆ pCreateObjectFunction

using RTTI::pCreateObjectFunction = void *(*)()

Function to create an object.

◆ pCreateRTTIFunction

using RTTI::pCreateRTTIFunction = void (*)(RTTI &inRTTI)

Function to initialize the runtime type info structure.

◆ pDestructObjectFunction

using RTTI::pDestructObjectFunction = void (*)(void *inObject)

Function to destroy an object.

Constructor & Destructor Documentation

◆ RTTI() [1/2]

JPH_NAMESPACE_BEGIN RTTI::RTTI ( const char *  inName,
int  inSize,
pCreateObjectFunction  inCreateObject,
pDestructObjectFunction  inDestructObject 
)

Constructor.

◆ RTTI() [2/2]

RTTI::RTTI ( const char *  inName,
int  inSize,
pCreateObjectFunction  inCreateObject,
pDestructObjectFunction  inDestructObject,
pCreateRTTIFunction  inCreateRTTI 
)

Member Function Documentation

◆ AddAttribute()

void RTTI::AddAttribute ( const SerializableAttribute inAttribute)

Attribute access.

◆ AddBaseClass()

void RTTI::AddBaseClass ( const RTTI inRTTI,
int  inOffset 
)

Add base class.

◆ CastTo()

const void * RTTI::CastTo ( const void *  inObject,
const RTTI inRTTI 
) const

Cast inObject of this type to object of type inRTTI, returns nullptr if the cast is unsuccessful.

◆ CreateObject()

void * RTTI::CreateObject ( ) const

Create an object of this type (returns nullptr if the object is abstract)

◆ DestructObject()

void RTTI::DestructObject ( void *  inObject) const

Destruct object of this type (does nothing if the object is abstract)

◆ GetAttribute()

const SerializableAttribute & RTTI::GetAttribute ( int  inIdx) const

◆ GetAttributeCount()

int RTTI::GetAttributeCount ( ) const

◆ GetBaseClass()

const RTTI * RTTI::GetBaseClass ( int  inIdx) const

◆ GetBaseClassCount()

int RTTI::GetBaseClassCount ( ) const

◆ GetHash()

uint32 RTTI::GetHash ( ) const

◆ GetName()

const char * RTTI::GetName ( ) const
inline

◆ GetSize()

int RTTI::GetSize ( ) const
inline

◆ IsAbstract()

bool RTTI::IsAbstract ( ) const
inline

◆ IsKindOf()

bool RTTI::IsKindOf ( const RTTI inRTTI) const

Test if this class is derived from class of type inRTTI.

◆ operator!=()

bool RTTI::operator!= ( const RTTI inRHS) const
inline

◆ operator==()

bool RTTI::operator== ( const RTTI inRHS) const

Equality operators.

◆ SetName()

void RTTI::SetName ( const char *  inName)
inline

Member Data Documentation

◆ mAttributes

StaticArray<SerializableAttribute, 32> RTTI::mAttributes
protected

All attributes of this class.

◆ mBaseClasses

StaticArray<BaseClass, 4> RTTI::mBaseClasses
protected

Names of base classes.

◆ mCreate

pCreateObjectFunction RTTI::mCreate
protected

Pointer to a function that will create a new instance of this class.

◆ mDestruct

pDestructObjectFunction RTTI::mDestruct
protected

Pointer to a function that will destruct an object of this class.

◆ mName

const char* RTTI::mName
protected

Class name.

◆ mSize

int RTTI::mSize
protected

Class size.


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