MRPT  1.9.9
rtti_example1/test.cpp
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://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() {}
void printName() override { std::cout << "class: Bar" << std::endl; }
{
std::cout << "specificBarMethod: reached." << std::endl;
}
};
} // namespace MyNS
//! [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\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();
}
}
//! [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 (const std::exception& e)
{
std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
return -1;
}
}



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020