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-2020, 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 "MyNS::MyDerived1"
37 
38 // Register "MyNS::MyDerived2"
40 
42 {
45 }
46 
47 TEST(rtti, CObject_CLASSID)
48 {
52 }
53 
54 TEST(rtti, MyDerived1_CLASSID)
55 {
56  using namespace std;
57  const auto cid_myd1 = CLASS_ID(MyNS::MyDerived1);
59  std::string(cid_myd1->className) == std::string("MyNS::MyDerived1"));
60 
61  const auto cid_cobj = CLASS_ID(mrpt::rtti::CObject);
62  EXPECT_TRUE(cid_myd1->getBaseClass() == cid_cobj);
63 
64  // RTTI IS_DERIVED(*)
65  {
69  }
70 }
71 
72 TEST(rtti, Factory)
73 {
74  do_register();
75  {
77  mrpt::rtti::classFactory("MyNS::MyDerived1");
78  EXPECT_TRUE(p);
79  }
80  {
81  auto p = mrpt::rtti::classFactory("MyNS::MyDerived2");
82  EXPECT_TRUE(p);
83  }
84 }
85 
86 TEST(rtti, CreateSmartPointerTypes)
87 {
88  using T = MyNS::MyDerived1;
89  {
90  auto p = T::Create();
91  EXPECT_TRUE(p);
92  EXPECT_EQ(p->m_value, 0);
93  }
94  {
95  auto p = T::Create(123);
96  EXPECT_TRUE(p);
97  EXPECT_EQ(p->m_value, 123);
98  }
99  {
100  auto p = T::CreateUnique();
101  EXPECT_TRUE(p);
102  EXPECT_EQ(p->m_value, 0);
103  }
104  {
105  auto p = T::CreateUnique(123);
106  EXPECT_TRUE(p);
107  EXPECT_EQ(p->m_value, 123);
108  }
109  {
111  auto p = T::CreateAlloc(alloc, 123);
112  EXPECT_TRUE(p);
113  EXPECT_EQ(p->m_value, 123);
114  }
115 }
MyDerived2()=default
EXPECT_TRUE(mrpt::system::fileExists(ini_fil))
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:177
TEST(rtti, CObject_CLASSID)
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:102
#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:151
Aligned allocator that is compatible with C++11.
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
Definition: CObject.h:105
static const mrpt::rtti::TRuntimeClassId & GetRuntimeClassIdStatic()
Definition: CObject.cpp:19
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
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()
#define DEFINE_MRPT_OBJECT(class_name, NameSpace)
This declaration must be inserted in all CObject classes definition, within the class declaration...
Definition: CObject.h:211
[example-define-class]
std::shared_ptr< CObject > Ptr
Definition: CObject.h:184



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