MRPT  1.9.9
rtti_example1/test.cpp
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| http://www.mrpt.org/ |
| |
| Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
| See: http://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See details in http://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
/** \example rtti_example1/test.cpp */
//! [example-define-class]
#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;
}
};
}
//! [example-define-class]
//! [example-define-class-test]
{
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();
}
}
//! [example-define-class-test]
//! [example-factory]
{
// 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();
}
}
}
//! [example-factory]
int main(int argc, char** argv)
{
try
{
return 0;
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
return -1;
}
}
IMPLEMENTS_MRPT_OBJECT(CMultiObjMotionOpt_Scalarization, CMultiObjectiveMotionOptimizerBase, mrpt::nav) CMultiObjMotionOpt_Scalarization
IMPLEMENTS_VIRTUAL_MRPT_OBJECT(CMultiObjectiveMotionOptimizerBase, CObject, mrpt::nav) CMultiObjectiveMotionOptimizerBase
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
Definition: CObject.h:87
#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:102
#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:108
#define DEFINE_VIRTUAL_MRPT_OBJECT(class_name)
This declaration must be inserted in virtual CObject classes definition:
Definition: CObject.h:249
#define DEFINE_MRPT_OBJECT(class_name)
This declaration must be inserted in all CObject classes definition, within the class declaration.
Definition: CObject.h:175
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:84
std::shared_ptr< BarBase > Ptr
std::shared_ptr< Bar > Ptr
virtual void printName() override
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:142
std::shared_ptr< CObject > Ptr
Definition: CObject.h:148
[example-define-class]
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.
Definition: CObject.cpp:116
void do_register()
static CAST_TO::Ptr from(const CAST_FROM_PTR &ptr)
Definition: CObject.h:311
void Test_UserTypesFactory()
void Test_UserTypes()
[example-define-class]



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 814d80880 Fri Aug 24 01:51:28 2018 +0200 at mar 26 may 2026 12:30:59 CEST