Main MRPT website > C++ reference for MRPT 1.9.9
Classes
[mrpt-rtti]

Detailed Description

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>
namespace MyNS
{
class Foo : public mrpt::rtti::CObject
{
public:
Foo() {}
void printName() { std::cout << "printName: Foo" << std::endl; }
};
class BarBase : public mrpt::rtti::CObject
{
public:
BarBase() {}
virtual void printName() { std::cout << "printName: BarBase" << std::endl; }
};
class Bar : public BarBase
{
public:
Bar() {}
virtual void printName() override
{
std::cout << "class: Bar" << std::endl;
}
{
std::cout << "specificBarMethod: reached." << std::endl;
}
};
}
{
using namespace MyNS;
const auto id_foo = CLASS_ID(Foo);
std::cout << "RTTI Foo (static): " << id_foo->className << std::endl;
// Pointers:
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;
if (IS_CLASS(pObj, Bar))
{
auto pBar = mrpt::ptr_cast<Bar>::from(pObj);
pBar->specificBarMethod();
}
}

Output:

Example #2: using the class factory

See: rtti_example1/test.cpp

#include <iostream>
#include <memory>
namespace MyNS
{
class Foo : public mrpt::rtti::CObject
{
public:
Foo() {}
void printName() { std::cout << "printName: Foo" << std::endl; }
};
class BarBase : public mrpt::rtti::CObject
{
public:
BarBase() {}
virtual void printName() { std::cout << "printName: BarBase" << std::endl; }
};
class Bar : public BarBase
{
public:
Bar() {}
virtual void printName() override
{
std::cout << "class: Bar" << std::endl;
}
{
std::cout << "specificBarMethod: reached." << std::endl;
}
};
}
{
// Register with explicit namespace:
{
// Register without explicit namespace:
using namespace MyNS;
}
}
{
// Test register:
{
const auto& allClasses = mrpt::rtti::getAllRegisteredClasses();
for (const auto& cl : allClasses)
{
std::cout << "Known class: " << cl->className << ", children of "
<< (cl->getBaseClass ? cl->getBaseClass()->className
: "(none)")
<< std::endl;
}
}
// Test factory:
{
if (IS_CLASS(pObj, MyNS::Bar))
{
pBar->specificBarMethod();
}
}
}

Output:

Classes

class  mrpt::rtti::CListOfClasses
 A list (actually based on a std::set) of MRPT classes, capable of keeping any class registered by the mechanism of CObject classes. More...
 
struct  mrpt::rtti::TRuntimeClassId
 A structure that holds runtime class type information. More...
 
struct  mrpt::rtti::CLASSINIT
 Auxiliary structure used for CObject-based RTTI. More...
 
class  mrpt::rtti::CObject
 The virtual base class of all MRPT classes with a unified RTTI system. More...
 
struct  mrpt::ptr_cast< CAST_TO >
 Converts a polymorphic smart pointer Base::Ptr to Derived::Ptr, in a way compatible with MRPT >=1.5.4 and MRPT 2.x series. More...
 
CObject.h
do_register
void do_register()
Definition: rtti_unittest.cpp:25
mrpt::rtti::registerClass
void registerClass(const mrpt::rtti::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CObject" descendents.
Definition: internal_class_registry.cpp:155
MyNS::BarBase::printName
virtual void printName()
Definition: vision_stereo_rectify/test.cpp:33
mrpt::rtti::CObject::Ptr
std::shared_ptr< CObject > Ptr
Definition: CObject.h:149
DEFINE_VIRTUAL_MRPT_OBJECT
#define DEFINE_VIRTUAL_MRPT_OBJECT(class_name)
This declaration must be inserted in virtual CObject classes definition:
Definition: CObject.h:252
Foo
Definition: stl_serialize_unittest.cpp:64
IS_DERIVED
#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 ...
Definition: CObject.h:109
MyNS::Foo::Foo
Foo()
Definition: vision_stereo_rectify/test.cpp:21
MyNS
[example-define-class]
Definition: rtti_unittest.cpp:13
Test_UserTypesFactory
void Test_UserTypesFactory()
Definition: vision_stereo_rectify/test.cpp:103
MyNS::Bar::printName
virtual void printName() override
Definition: vision_stereo_rectify/test.cpp:42
mrpt::rtti::getAllRegisteredClasses
std::vector< const mrpt::rtti::TRuntimeClassId * > getAllRegisteredClasses()
Returns a list with all the classes registered in the system through mrpt::rtti::registerClass.
Definition: internal_class_registry.cpp:178
MyNS::Bar::Bar
Bar()
Definition: vision_stereo_rectify/test.cpp:39
IMPLEMENTS_VIRTUAL_MRPT_OBJECT
IMPLEMENTS_VIRTUAL_MRPT_OBJECT(CMultiObjectiveMotionOptimizerBase, CObject, mrpt::nav) CMultiObjectiveMotionOptimizerBase
Definition: CMultiObjectiveMotionOptimizerBase.cpp:18
MyNS::Foo
Definition: serialization/src/CSerializable_unittest.cpp:19
mrpt::ptr_cast::from
static CAST_TO::Ptr from(const CAST_FROM_PTR &ptr)
Definition: CObject.h:314
MyNS::BarBase::BarBase
BarBase()
Definition: vision_stereo_rectify/test.cpp:30
mrpt::rtti::CObject
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:142
MyNS::Bar
Definition: vision_stereo_rectify/test.cpp:36
IS_CLASS
#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...
Definition: CObject.h:103
MyNS::Bar::specificBarMethod
void specificBarMethod()
Definition: vision_stereo_rectify/test.cpp:46
CLASS_ID_NAMESPACE
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
Definition: CObject.h:88
MyNS::BarBase::Ptr
std::shared_ptr< BarBase > Ptr
Definition: vision_stereo_rectify/test.cpp:31
mrpt::rtti::registerClassCustomName
void registerClassCustomName(const char *customName, const TRuntimeClassId *pNewClass)
Mostly for internal use within mrpt sources, to handle exceptional cases with multiple serialization ...
Definition: internal_class_registry.cpp:168
MyNS::BarBase
Definition: vision_stereo_rectify/test.cpp:27
IMPLEMENTS_MRPT_OBJECT
IMPLEMENTS_MRPT_OBJECT(CMultiObjMotionOpt_Scalarization, CMultiObjectiveMotionOptimizerBase, mrpt::nav) CMultiObjMotionOpt_Scalarization
Definition: CMultiObjMotionOpt_Scalarization.cpp:16
CLASS_ID
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:85
Test_UserTypes
void Test_UserTypes()
[example-define-class]
Definition: vision_stereo_rectify/test.cpp:60
MyNS::Foo::printName
void printName()
Definition: vision_stereo_rectify/test.cpp:24
MyNS::Bar::Ptr
std::shared_ptr< Bar > Ptr
Definition: vision_stereo_rectify/test.cpp:40
mrpt::rtti::classFactoryPtr
mrpt::rtti::CObject::Ptr classFactoryPtr(const std::string &className)
Like classFactory() but returns a smart pointer.
Definition: CObject.cpp:116
DEFINE_MRPT_OBJECT
#define DEFINE_MRPT_OBJECT(class_name)
This declaration must be inserted in all CObject classes definition, within the class declaration.
Definition: CObject.h:176



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST