MRPT  1.9.9
rtti_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
12 #include <mrpt/rtti/CObject.h>
13 
14 namespace MyNS
15 {
17 {
18  public:
19  int m_value{0};
20 
21  MyDerived1() = default;
22  MyDerived1(int v) : m_value(v) {}
24 };
25 
27 {
28  public:
29  MyDerived2() = default;
31 };
32 
33 } // namespace MyNS
34 
35 // Register "MyDerived1"
37 
38 // Register "MyNS::MyDerived2"
40 
42 {
45 }
46 
47 TEST(rtti, CObject_CLASSID)
48 {
49  EXPECT_TRUE(
52 }
53 
54 TEST(rtti, MyDerived1_CLASSID)
55 {
56  using namespace std;
57  const auto cid_myd1 = CLASS_ID(MyNS::MyDerived1);
58  EXPECT_TRUE(std::string(cid_myd1->className) == std::string("MyDerived1"));
59 
60  const auto cid_cobj = CLASS_ID(mrpt::rtti::CObject);
61  EXPECT_TRUE(cid_myd1->getBaseClass() == cid_cobj);
62 
63  // RTTI IS_DERIVED(*)
64  {
66  EXPECT_TRUE(IS_DERIVED(*p, MyNS::MyDerived1));
67  EXPECT_TRUE(IS_DERIVED(*p, mrpt::rtti::CObject));
68  }
69 }
70 
71 TEST(rtti, Factory)
72 {
73  do_register();
74  {
76  EXPECT_TRUE(p);
77  }
78  {
79  auto p = mrpt::rtti::classFactory("MyNS::MyDerived2");
80  EXPECT_TRUE(p);
81  }
82 }
83 
84 TEST(rtti, CreateSmartPointerTypes)
85 {
86  using T = MyNS::MyDerived1;
87  {
88  auto p = T::Create();
89  EXPECT_TRUE(p);
90  EXPECT_EQ(p->m_value, 0);
91  }
92  {
93  auto p = T::Create(123);
94  EXPECT_TRUE(p);
95  EXPECT_EQ(p->m_value, 123);
96  }
97  {
98  auto p = T::CreateUnique();
99  EXPECT_TRUE(p);
100  EXPECT_EQ(p->m_value, 0);
101  }
102  {
103  auto p = T::CreateUnique(123);
104  EXPECT_TRUE(p);
105  EXPECT_EQ(p->m_value, 123);
106  }
107  {
109  auto p = T::CreateAlloc(alloc, 123);
110  EXPECT_TRUE(p);
111  EXPECT_EQ(p->m_value, 123);
112  }
113 }
MyDerived2()=default
MyDerived1()=default
IMPLEMENTS_MRPT_OBJECT(CMultiObjMotionOpt_Scalarization, CMultiObjectiveMotionOptimizerBase, mrpt::nav) CMultiObjMotionOpt_Scalarization
STL namespace.
Virtual base to provide a compiler-independent RTTI system.
Definition: CObject.h:173
TEST(rtti, CObject_CLASSID)
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:89
#define DEFINE_MRPT_OBJECT(class_name)
This declaration must be inserted in all CObject classes definition, within the class declaration...
Definition: CObject.h:207
#define IS_DERIVED(obj, class_name)
True if the given reference to object (derived from mrpt::rtti::CObject) is an instance of the given ...
Definition: CObject.h:138
Aligned allocator that is compatible with C++11.
GLsizei const GLchar ** string
Definition: glext.h:4116
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
Definition: CObject.h:92
const GLdouble * v
Definition: glext.h:3684
static const mrpt::rtti::TRuntimeClassId & GetRuntimeClassIdStatic()
Definition: CObject.cpp:19
#define IMPLEMENTS_MRPT_OBJECT_NS_PREFIX(class_name, base, NameSpace)
This must be inserted in all CObject classes implementation files.
Definition: CObject.h:286
mrpt::rtti::CObject::Ptr classFactory(const std::string &className)
Creates an object given by its registered name.
Definition: CObject.cpp:108
void registerClass(const mrpt::rtti::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CObject" descendents.
void do_register()
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLfloat GLfloat p
Definition: glext.h:6398
[example-define-class]
std::shared_ptr< CObject > Ptr
Definition: CObject.h:180



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019