Runtime Type Information (RTTI) library, providing compiler-independent class registry, class factory, and inheritance information.
Back to list of all libraries | See all modules
Library <tt>mrpt-rtti</tt>
[New in MRPT 2.0.0]
This library is part of MRPT and can be installed in Debian-based systems with:
sudo apt install libmrpt-rtti-dev
Any class with RTTI support has to be derived from mrpt::rtti::CObject, either directly or via a hierarchy of inheriting classes. Class factory by name enables deserialization of polymorphic classes in the library [mrpt-serialization].
All classes defined in each MRPT module are automatically registered when loading the module (if dynamically linked).
Example #1: defining new user classes
See: rtti_example1/test.cpp
#include <iostream>
#include <memory>
{
{
public:
void printName() { std::cout <<
"printName: Foo" << std::endl; }
};
{
public:
virtual void printName() { std::cout <<
"printName: BarBase" << std::endl; }
};
class Bar : public BarBase
{
public:
{
std::cout << "class: Bar" << std::endl;
}
{
std::cout << "specificBarMethod: reached." << std::endl;
}
};
}
IMPLEMENTS_MRPT_OBJECT(CMultiObjMotionOpt_Scalarization, CMultiObjectiveMotionOptimizerBase, mrpt::nav) CMultiObjMotionOpt_Scalarization
IMPLEMENTS_VIRTUAL_MRPT_OBJECT(CMultiObjectiveMotionOptimizerBase, CObject, mrpt::nav) CMultiObjectiveMotionOptimizerBase
#define DEFINE_VIRTUAL_MRPT_OBJECT(class_name)
This declaration must be inserted in virtual CObject classes definition:
#define DEFINE_MRPT_OBJECT(class_name)
This declaration must be inserted in all CObject classes definition, within the class declaration.
virtual void printName() override
The virtual base class of all MRPT classes with a unified RTTI system.
{
std::cout << "RTTI Foo (static): " << id_foo->className << std::endl;
Bar::Ptr pBar = std::make_shared<Bar>();
pBar->printName();
pBase->printName();
std::cout <<
"Is Foo? => " << (
IS_DERIVED(pObj,
Foo) ?
"Yes" :
"No")
<< std::endl;
std::cout <<
"Is BarBase? => " << (
IS_DERIVED(pObj, BarBase) ?
"Yes" :
"No")
<< std::endl;
std::cout <<
"Is Bar? => " << (
IS_DERIVED(pObj, Bar) ?
"Yes" :
"No")
<< std::endl;
{
pBar->specificBarMethod();
}
}
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::rtti::CObject) is of the give...
#define IS_DERIVED(ptrObj, class_name)
Evaluates to true if a pointer to an object (derived from mrpt::rtti::CObject) is an instance of the ...
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
std::shared_ptr< BarBase > Ptr
std::shared_ptr< Bar > Ptr
std::shared_ptr< CObject > Ptr
static CAST_TO::Ptr from(const CAST_FROM_PTR &ptr)
void Test_UserTypes()
[example-define-class]
Output:
Example #2: using the class factory
See: rtti_example1/test.cpp
#include <iostream>
#include <memory>
{
{
public:
void printName() { std::cout <<
"printName: Foo" << std::endl; }
};
{
public:
virtual void printName() { std::cout <<
"printName: BarBase" << std::endl; }
};
class Bar : public BarBase
{
public:
{
std::cout << "class: Bar" << std::endl;
}
{
std::cout << "specificBarMethod: reached." << std::endl;
}
};
}
{
{
}
}
{
{
for (const auto& cl : allClasses)
{
std::cout << "Known class: " << cl->className << ", children of "
<< (cl->getBaseClass ? cl->getBaseClass()->className
: "(none)")
<< std::endl;
}
}
{
{
pBar->specificBarMethod();
}
}
}
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
void registerClassCustomName(const char *customName, const TRuntimeClassId *pNewClass)
Mostly for internal use within mrpt sources, to handle exceptional cases with multiple serialization ...
void registerClass(const mrpt::rtti::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CObject" descendents.
std::vector< const mrpt::rtti::TRuntimeClassId * > getAllRegisteredClasses()
Returns a list with all the classes registered in the system through mrpt::rtti::registerClass.
mrpt::rtti::CObject::Ptr classFactoryPtr(const std::string &className)
Like classFactory() but returns a smart pointer.
void Test_UserTypesFactory()
Output: