Kicking it Olde Sküül! Burgerlib on Github Follow Olde Sküül on Twitter Burgerbecky on LinkedIn Burgerbecky on LinkedIn
Loading...
Searching...
No Matches
Burger::StaticRTTI Struct Reference

Structure to handle simple Run Time Type Identification. More...

Collaboration diagram for Burger::StaticRTTI:

Public Member Functions

const char * get_class_name (void) const noexcept
 Return the class name.
 
uint_t is_in_list (const StaticRTTI *pInput) const noexcept
 Determine if a class is of a specific type.
 

Public Attributes

const char * m_pClassName
 Pointer to the name of the class.
 
const StaticRTTIm_pParent
 Pointer to the parent in a derived class.
 

Detailed Description

Structure to handle simple Run Time Type Identification.


This implements a method for handling run time type identification without any runtime overhead and a tiny amount of memory space in the form of a single extra pointer in the VTable and a single instance of piece of code returning a pointer.

The class itself does not contain any additional data to support this feature!!!

By using macros for calls, data tables and class hooks, a simple way to check if a base class is really a specific class is created.

// **** In the header ****
// Class definitions using a virtual table (A VTable must exist in the base
// for this system to work
// Base class
class foo {
public:
foo() {}
virtual ~foo() {}
BURGER_RTTI_IN_BASE_CLASS(); // Insert magic code
};
// Derived classes
class foo2 : public foo {
public:
foo2() {}
virtual ~foo2() {}
BURGER_RTTI_IN_CLASS(); // Insert magic code
};
class foo3 : public foo2 {
public:
foo3() {}
virtual ~foo3() {}
BURGER_RTTI_IN_CLASS(); // Insert magic code
};
class foo4 : public foo2 {
public:
foo4() {}
virtual ~foo4() {}
BURGER_RTTI_IN_CLASS(); // Insert magic code
};
class foo5 : public foo {
public:
foo5() {}
virtual ~foo5() {}
BURGER_RTTI_IN_CLASS(); // Insert magic code
};
// ***** Declared in a single CPP file
// Declare the base class
// Declare the derived classes and their immediate parents
// Example code for use
foo bar;
foo2 bar2;
foo3 bar3;
foo4 bar4;
foo5 bar5;
foo *pBar = &bar;
foo *pBar2 = &bar2;
foo *pBar3 = &bar3;
foo *pBar4 = &bar4;
foo *pBar5 = &bar5;
// Return a string with the true class name
Debug::Message(pBar->get_class_name()); // "foo"
Debug::Message(pBar2->get_class_name()); // "foo2"
Debug::Message(pBar3->get_class_name()); // "foo3"
Debug::Message(pBar4->get_class_name()); // "foo4"
Debug::Message(pBar5->get_class_name()); // "foo5"
// Demonstrate a truth table for class interactions
// TRUE
Debug::Message("foo pBar %u\n",BURGER_STATICRTTI_ISTYPE(foo,pBar));
// FALSE
Debug::Message("foo2 pBar %u\n",BURGER_STATICRTTI_ISTYPE(foo2,pBar));
// FALSE
Debug::Message("foo3 pBar %u\n",BURGER_STATICRTTI_ISTYPE(foo3,pBar));
// FALSE
Debug::Message("foo4 pBar %u\n",BURGER_STATICRTTI_ISTYPE(foo4,pBar));
// FALSE
Debug::Message("foo5 pBar %u\n\n",BURGER_STATICRTTI_ISTYPE(foo5,pBar));
// TRUE
Debug::Message("foo pBar2 %u\n",BURGER_STATICRTTI_ISTYPE(foo,pBar2));
// TRUE
Debug::Message("foo2 pBar2 %u\n",BURGER_STATICRTTI_ISTYPE(foo2,pBar2));
// FALSE
Debug::Message("foo3 pBar2 %u\n",BURGER_STATICRTTI_ISTYPE(foo3,pBar2));
// FALSE
Debug::Message("foo4 pBar2 %u\n",BURGER_STATICRTTI_ISTYPE(foo4,pBar2));
// FALSE
Debug::Message("foo5 pBar2 %u\n\n",BURGER_STATICRTTI_ISTYPE(foo5,pBar2));
// TRUE
Debug::Message("foo pBar3 %u\n",BURGER_STATICRTTI_ISTYPE(foo,pBar3));
// TRUE
Debug::Message("foo2 pBar3 %u\n",BURGER_STATICRTTI_ISTYPE(foo2,pBar3));
// TRUE
Debug::Message("foo3 pBar3 %u\n",BURGER_STATICRTTI_ISTYPE(foo3,pBar3));
// FALSE
Debug::Message("foo4 pBar3 %u\n",BURGER_STATICRTTI_ISTYPE(foo4,pBar3));
// FALSE
Debug::Message("foo5 pBar3 %u\n\n",BURGER_STATICRTTI_ISTYPE(foo5,pBar3));
// TRUE
Debug::Message("foo pBar4 %u\n",BURGER_STATICRTTI_ISTYPE(foo,pBar4));
// TRUE
Debug::Message("foo2 pBar4 %u\n",BURGER_STATICRTTI_ISTYPE(foo2,pBar4));
// FALSE
Debug::Message("foo3 pBar4 %u\n",BURGER_STATICRTTI_ISTYPE(foo3,pBar4));
// TRUE
Debug::Message("foo4 pBar4 %u\n",BURGER_STATICRTTI_ISTYPE(foo4,pBar4));
// FALSE
Debug::Message("foo5 pBar4 %u\n\n",BURGER_STATICRTTI_ISTYPE(foo5,pBar4));
// TRUE
Debug::Message("foo pBar5 %u\n",BURGER_STATICRTTI_ISTYPE(foo,pBar5));
// FALSE
Debug::Message("foo2 pBar5 %u\n",BURGER_STATICRTTI_ISTYPE(foo2,pBar5));
// FALSE
Debug::Message("foo3 pBar5 %u\n",BURGER_STATICRTTI_ISTYPE(foo3,pBar5));
// FALSE
Debug::Message("foo4 pBar5 %u\n",BURGER_STATICRTTI_ISTYPE(foo4,pBar5));
// TRUE
Debug::Message("foo5 pBar5 %u\n\n",BURGER_STATICRTTI_ISTYPE(foo5,pBar5));
// Will properly cast a foo to a foo4
foo5 *pTemp = BURGER_RTTICAST(foo5,pBar5);
const foo5 *pCTemp = BURGER_RTTICONSTCAST(foo5,pBar5);
// Will return nullptr since these classes are not a foo5
pTemp = BURGER_RTTICAST(foo5,pBar4);
pCTemp = BURGER_RTTICONSTCAST(foo5,pBar3);
#define BURGER_CREATE_STATICRTTI_PARENT(__ClassName, __ParentClass)
Define to create a StaticRTTI for a derived class.
Definition burger.h:5222
#define BURGER_RTTICONSTCAST(__ClassName, __Pointer)
Cast up a const base class with verification.
Definition burger.h:5241
#define BURGER_RTTICAST(__ClassName, __Pointer)
Cast up a base class with verification.
Definition burger.h:5234
#define BURGER_STATICRTTI_ISTYPE(__ClassName, __Pointer)
Return TRUE if the base class is also the requested class.
Definition burger.h:5231
#define BURGER_CREATE_STATICRTTI_BASE(__ClassName)
Define to create a StaticRTTI for a base class.
Definition burger.h:5214
#define BURGER_RTTI_IN_BASE_CLASS()
Macro for StaticRTTI support in a base class.
Definition burger.h:5205
#define BURGER_RTTI_IN_CLASS()
Macro for StaticRTTI support in a class.
Definition burger.h:5199
void Message(const char *pMessage,...) noexcept
Print a message to the debugger output stream.
Definition brdebug.cpp:193
Note
This system was made for classes that use single inheritance. It will only backtrack one class tree and cannot track multiple inheritances.
See also
BURGER_RTTICAST, BURGER_RTTICONSTCAST, BURGER_STATICRTTI_ISTYPE, BURGER_RTTI_IN_CLASS, BURGER_CREATE_STATICRTTI_BASE or BURGER_CREATE_STATICRTTI_PARENT

Member Function Documentation

◆ get_class_name()

const char * Burger::StaticRTTI::get_class_name ( void ) const
inlinenoexcept

Return the class name.


Return the name of the class, not the base class.

Returns
Pointer to a "C" string with the name of the true class

◆ is_in_list()

uint_t BURGER_API Burger::StaticRTTI::is_in_list ( const StaticRTTI * pInput) const
noexcept

Determine if a class is of a specific type.


Walk the StaticRTTI linked list from the most derived type and up to the base class, all the while checking for a match.

Parameters
pInputPointer to a StaticRTTI record to compare to this one
Returns
TRUE if this record is in the linked list chain

Member Data Documentation

◆ m_pClassName

const char* Burger::StaticRTTI::m_pClassName

Pointer to the name of the class.

◆ m_pParent

const StaticRTTI* Burger::StaticRTTI::m_pParent

Pointer to the parent in a derived class.