MRPT  1.9.9
Classes
[mrpt-rtti]

Detailed Description

Runtime Type Information (RTTI) library, providing compiler-independent class registry, class factory, and inheritance information.

Library `mrpt-rtti`

[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

See: Using MRPT from your CMake project

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() {}
void printName() override { std::cout << "class: Bar" << std::endl; }
{
std::cout << "specificBarMethod: reached." << std::endl;
}
};
} // namespace MyNS
{
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\n" : "No\n");
std::cout << "Is BarBase? => "
<< (IS_DERIVED(*pObj, BarBase) ? "Yes\n" : "No\n");
std::cout << "Is Bar? => " << (IS_DERIVED(*pObj, Bar) ? "Yes\n" : "No\n");
if (IS_CLASS(*pObj, Bar))
{
auto pBar2 = mrpt::ptr_cast<Bar>::from(pObj);
pBar2->specificBarMethod();
}
}

Output:

RTTI Foo (static): Foo
class: Bar
class: Bar
Is Foo? => No
Is BarBase? => Yes
Is Bar? => Yes
specificBarMethod: reached.

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() {}
void printName() override { std::cout << "class: Bar" << std::endl; }
{
std::cout << "specificBarMethod: reached." << std::endl;
}
};
} // namespace MyNS
{
// 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:

Known class: Bar, children of BarBase
Known class: BarBase, children of CObject
Known class: CObject, children of (none)
Known class: Foo, children of CObject
Known class: Bar, children of BarBase
specificBarMethod: reached.

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...
 
class  mrpt::rtti::CObject
 Virtual base to provide a compiler-independent 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...
 



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: d655b8ca0 Wed Dec 4 23:23:44 2019 +0100 at miƩ dic 4 23:30:15 CET 2019