MRPT  1.9.9
CObject.h
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 #pragma once
10 
13 #include <mrpt/typemeta/static_string.h> // literal()
14 #include <memory>
15 #include <vector>
16 
17 namespace mrpt
18 {
19 namespace rtti
20 {
21 /** @name RTTI classes and functions for polymorphic hierarchies
22  @{ */
23 class CObject;
24 
25 /** A structure that holds runtime class type information. Use
26  * CLASS_ID(<class_name>) to get a reference to the class_name's TRuntimeClassId
27  * descriptor.
28  * \ingroup mrpt_rtti_grp
29  */
31 {
33  const char* className;
34  /** Create an object of the related class, or nullptr if it is virtual. */
35  mrpt::rtti::CObject* (*ptrCreateObject)();
36  /** Gets the base class runtime id. */
37  const TRuntimeClassId* (*getBaseClass)();
38 
39  // Operations
41  bool derivedFrom(const TRuntimeClassId* pBaseClass) const;
42  bool derivedFrom(const char* pBaseClass_name) const;
43 };
44 
45 /** Register a class into the MRPT internal list of "CObject" descendents.
46  * Used internally in the macros DEFINE_SERIALIZABLE, etc...
47  * \sa getAllRegisteredClasses
48  */
49 void registerClass(const mrpt::rtti::TRuntimeClassId* pNewClass);
50 
51 /** Mostly for internal use within mrpt sources, to handle exceptional cases
52  * with multiple serialization names for backward compatibility
53  * (CMultiMetricMaps, CImage,...)
54  */
56  const char* customName, const TRuntimeClassId* pNewClass);
57 
58 /** Returns a list with all the classes registered in the system through
59  * mrpt::rtti::registerClass.
60  * \sa registerClass, findRegisteredClass
61  */
62 std::vector<const mrpt::rtti::TRuntimeClassId*> getAllRegisteredClasses();
63 
64 /** Like getAllRegisteredClasses(), but filters the list to only include
65  * children clases of a given base one.
66  * \sa getAllRegisteredClasses(), getAllRegisteredClassesChildrenOf() */
67 std::vector<const TRuntimeClassId*> getAllRegisteredClassesChildrenOf(
68  const TRuntimeClassId* parent_id);
69 
70 /** Return info about a given class by its name, or nullptr if the class is not
71  * registered
72  * \sa registerClass, getAllRegisteredClasses
73  */
74 const TRuntimeClassId* findRegisteredClass(const std::string& className);
75 
76 template <typename T>
78 {
79  return &T::GetRuntimeClassIdStatic();
80 }
81 
82 /** Access to runtime class ID for a defined class name.
83  */
84 #define CLASS_ID(T) mrpt::rtti::CLASS_ID_impl<T>()
85 // Convert these
86 #define CLASS_ID_TEMPLATE(class_name, T) mrpt::rtti::CLASS_ID_impl<T>()
87 #define CLASS_ID_NAMESPACE(class_name, namespaceName) \
88  mrpt::rtti::CLASS_ID_impl<namespaceName::class_name>()
89 
90 template <typename T>
92 {
93  template <typename PTR>
94  static bool check(const PTR& p)
95  {
96  return p->GetRuntimeClass() == CLASS_ID_impl<T>();
97  }
98 };
99 
100 /** Evaluates to true if the given pointer to an object (derived from
101  * mrpt::rtti::CObject) is of the given class. */
102 #define IS_CLASS(ptrObj, class_name) \
103  mrpt::rtti::IS_CLASS_impl<class_name>::check(ptrObj)
104 
105 /** Evaluates to true if a pointer to an object (derived from
106  * mrpt::rtti::CObject) is an instance of the given class OR any of its
107  * derived classes. */
108 #define IS_DERIVED(ptrObj, class_name) \
109  ((ptrObj)->GetRuntimeClass()->derivedFrom(CLASS_ID(class_name)))
110 
111 /** Auxiliary structure used for CObject-based RTTI. \ingroup mrpt_rtti_grp */
112 struct CLASSINIT
113 {
115  {
116  registerClass(pNewClass);
117  }
118 };
119 
120 /** Virtual base to provide a compiler-independent RTTI system.
121  *
122  * Each class named `Foo` will have associated smart pointer types:
123  * - `Foo::Ptr` => `std::shared_ptr<Foo>` (the most commonly-used one)
124  * - `Foo::ConstPtr` => `std::shared_ptr<const Foo>`
125  * - `Foo::UniquePtr` => `std::unique_ptr<Foo>`
126  * - `Foo::ConstUniquePtr` => `std::unique_ptr<const Foo>`
127  *
128  * It is recommended to use MRPT-defined `mrpt::make_aligned_shared<>` instead
129  * of `std::make_shared<>` to create objects, to avoid memory alignment
130  * problems caused by classes containing Eigen vectors or matrices. Example:
131  * \code
132  * Foo::Ptr o = mrpt::make_aligned_shared<Foo>();
133  * \endcode
134  * Or using the shorter auxiliary static method `::Create()` for conciseness or
135  * to keep compatibility with MRPT 1.5.* code bases:
136  * \code
137  * Foo::Ptr o = Foo::Create();
138  * \endcode
139  * If a special memory allocator is needed, use `Foo::CreateAlloc(alloc,...);`.
140  * \sa mrpt::rtti::CObject
141  * \ingroup mrpt_rtti_grp
142  */
143 class CObject
144 {
145  protected:
148 
149  public:
152  using UniquePtr = std::unique_ptr<CObject>;
153  using ConstUniquePtr = std::unique_ptr<const CObject>;
155  /** Returns information about the class of an object in runtime. */
157  {
158  return CLASS_ID(CObject);
159  }
160 
161  /** Makes a deep copy of the object and returns a smart pointer to it */
163 
164  /** Returns a deep copy (clone) of the object, indepently of its class. */
165  virtual CObject* clone() const = 0;
166 
167  virtual ~CObject() = default;
168 }; // End of class def.
169 
171 {
172  return mrpt::rtti::CObject::Ptr(this->clone());
173 }
174 
175 /** This declaration must be inserted in all CObject classes definition, within
176  * the class declaration. */
177 #define DEFINE_MRPT_OBJECT(class_name) \
178  /*! @name RTTI stuff */ \
179  /*! @{ */ \
180  protected: \
181  static const mrpt::rtti::TRuntimeClassId* _GetBaseClass(); \
182  static mrpt::rtti::CLASSINIT _init_##class_name; \
183  static const mrpt::rtti::TRuntimeClassId runtimeClassId; \
184  \
185  public: \
186  /*! A type for the associated smart pointer */ \
187  using Ptr = std::shared_ptr<class_name>; \
188  using ConstPtr = std::shared_ptr<const class_name>; \
189  using UniquePtr = std::unique_ptr<class_name>; \
190  using ConstUniquePtr = std::unique_ptr<const class_name>; \
191  static constexpr const char* className = #class_name; \
192  static constexpr auto getClassName() \
193  { \
194  return mrpt::typemeta::literal(#class_name); \
195  } \
196  static const mrpt::rtti::TRuntimeClassId& GetRuntimeClassIdStatic(); \
197  virtual const mrpt::rtti::TRuntimeClassId* GetRuntimeClass() \
198  const override; \
199  static mrpt::rtti::CObject* CreateObject(); \
200  virtual mrpt::rtti::CObject* clone() const override; \
201  template <typename... Args> \
202  static Ptr Create(Args&&... args) \
203  { \
204  return mrpt::make_aligned_shared<class_name>( \
205  std::forward<Args>(args)...); \
206  } \
207  template <typename Alloc, typename... Args> \
208  static Ptr CreateAlloc(const Alloc& alloc, Args&&... args) \
209  { \
210  return std::allocate_shared<class_name>( \
211  alloc, std::forward<Args>(args)...); \
212  } \
213  template <typename... Args> \
214  static UniquePtr CreateUnique(Args&&... args) \
215  { \
216  return std::make_unique<class_name>(std::forward<Args>(args)...); \
217  } \
218  /*! @} */ \
219  public: \
220  MRPT_MAKE_ALIGNED_OPERATOR_NEW
221 
222 /** This must be inserted in all CObject classes implementation files
223  */
224 #define IMPLEMENTS_MRPT_OBJECT(class_name, base, NameSpace) \
225  mrpt::rtti::CObject* NameSpace::class_name::CreateObject() \
226  { \
227  return static_cast<mrpt::rtti::CObject*>(new NameSpace::class_name); \
228  } \
229  const mrpt::rtti::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \
230  { \
231  return CLASS_ID(base); \
232  } \
233  const mrpt::rtti::TRuntimeClassId& \
234  NameSpace::class_name::GetRuntimeClassIdStatic() \
235  { \
236  return NameSpace::class_name::runtimeClassId; \
237  } \
238  const mrpt::rtti::TRuntimeClassId NameSpace::class_name::runtimeClassId = \
239  {#class_name, NameSpace::class_name::CreateObject, \
240  &class_name::_GetBaseClass}; \
241  const mrpt::rtti::TRuntimeClassId* \
242  NameSpace::class_name::GetRuntimeClass() const \
243  { \
244  return CLASS_ID_NAMESPACE(class_name, NameSpace); \
245  } \
246  mrpt::rtti::CLASSINIT NameSpace::class_name::_init_##class_name( \
247  CLASS_ID(base)); \
248  mrpt::rtti::CObject* NameSpace::class_name::clone() const \
249  { \
250  return static_cast<mrpt::rtti::CObject*>( \
251  new NameSpace::class_name(*this)); \
252  }
253 
254 /** This declaration must be inserted in virtual CObject classes
255  * definition:
256  */
257 #define DEFINE_VIRTUAL_MRPT_OBJECT(class_name) \
258  /*! @name RTTI stuff */ \
259  /*! @{ */ \
260  protected: \
261  static const mrpt::rtti::TRuntimeClassId* _GetBaseClass(); \
262  static const mrpt::rtti::TRuntimeClassId runtimeClassId; \
263  \
264  public: \
265  using Ptr = std::shared_ptr<class_name>; \
266  using ConstPtr = std::shared_ptr<const class_name>; \
267  virtual const mrpt::rtti::TRuntimeClassId* GetRuntimeClass() \
268  const override; \
269  static const mrpt::rtti::TRuntimeClassId& GetRuntimeClassIdStatic(); \
270  /*! @} */
271 
272 /** This must be inserted as implementation of some required members for
273  * virtual CObject classes:
274  */
275 #define IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name, NameSpace) \
276  const mrpt::rtti::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \
277  { \
278  return CLASS_ID(base_class_name); \
279  } \
280  const mrpt::rtti::TRuntimeClassId NameSpace::class_name::runtimeClassId = \
281  {#class_name, nullptr, &NameSpace::class_name::_GetBaseClass}; \
282  const mrpt::rtti::TRuntimeClassId* \
283  NameSpace::class_name::GetRuntimeClass() const \
284  { \
285  return CLASS_ID(class_name); \
286  } \
287  const mrpt::rtti::TRuntimeClassId& \
288  NameSpace::class_name::GetRuntimeClassIdStatic() \
289  { \
290  return NameSpace::class_name::runtimeClassId; \
291  }
292 
293 /** Register all pending classes - to be called just before
294  * de-serializing an object, for example. After calling this method,
295  * pending_class_registers_modified is set to false until
296  * pending_class_registers() is invoked.
297  */
299 
300 /** Creates an object given by its registered name.
301  * \sa findRegisteredClass(), registerClass(), classFactoryPtr() */
302 mrpt::rtti::CObject* classFactory(const std::string& className);
303 
304 /** Like classFactory() but returns a smart pointer */
306 
307 /** @} */ // end of RTTI
308 
309 } // namespace rtti
310 
311 /** Converts a polymorphic smart pointer Base::Ptr to Derived::Ptr, in a
312  * way compatible with MRPT >=1.5.4 and MRPT 2.x series. \ingroup
313  * mrpt_rtti_grp
314  */
315 template <typename CAST_TO>
316 struct ptr_cast
317 {
318  template <typename CAST_FROM_PTR>
319  static typename CAST_TO::Ptr from(const CAST_FROM_PTR& ptr)
320  {
321  return std::dynamic_pointer_cast<CAST_TO>(ptr);
322  }
323 };
324 
325 } // namespace mrpt
virtual ~CObject()=default
void registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
std::unique_ptr< CObject > UniquePtr
Definition: CObject.h:152
Auxiliary structure used for CObject-based RTTI.
Definition: CObject.h:112
constexpr const mrpt::rtti::TRuntimeClassId * CLASS_ID_impl()
Definition: CObject.h:77
std::vector< const TRuntimeClassId * > getAllRegisteredClassesChildrenOf(const TRuntimeClassId *parent_id)
Like getAllRegisteredClasses(), but filters the list to only include children clases of a given base ...
A structure that holds runtime class type information.
Definition: CObject.h:30
Converts a polymorphic smart pointer Base::Ptr to Derived::Ptr, in a way compatible with MRPT >=1...
Definition: CObject.h:316
Virtual base to provide a compiler-independent RTTI system.
Definition: CObject.h:143
CLASSINIT(const mrpt::rtti::TRuntimeClassId *pNewClass)
Definition: CObject.h:114
std::unique_ptr< const CObject > ConstUniquePtr
Definition: CObject.h:153
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:84
virtual CObject * clone() const =0
Returns a deep copy (clone) of the object, indepently of its class.
static bool check(const PTR &p)
Definition: CObject.h:94
const char * className
Definition: CObject.h:33
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const
Returns information about the class of an object in runtime.
Definition: CObject.h:156
A wrapper class for pointers that can be safely copied with "=" operator without problems.
Definition: safe_pointers.h:71
GLsizei const GLchar ** string
Definition: glext.h:4116
void registerClassCustomName(const char *customName, const TRuntimeClassId *pNewClass)
Mostly for internal use within mrpt sources, to handle exceptional cases with multiple serialization ...
const TRuntimeClassId * findRegisteredClass(const std::string &className)
Return info about a given class by its name, or nullptr if the class is not registered.
mrpt::rtti::CObject * classFactory(const std::string &className)
Creates an object given by its registered name.
Definition: CObject.cpp:109
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
static const mrpt::rtti::TRuntimeClassId & GetRuntimeClassIdStatic()
Definition: CObject.cpp:19
static mrpt::rtti::TRuntimeClassId * _GetBaseClass()
Definition: CObject.cpp:105
bool derivedFrom(const TRuntimeClassId *pBaseClass) const
Definition: CObject.cpp:24
static CAST_TO::Ptr from(const CAST_FROM_PTR &ptr)
Definition: CObject.h:319
void registerClass(const mrpt::rtti::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CObject" descendents.
static const mrpt::rtti::TRuntimeClassId runtimeClassId
Definition: CObject.h:147
mrpt::rtti::CObject * createObject() const
Definition: CObject.cpp:79
GLfloat GLfloat p
Definition: glext.h:6398
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
std::shared_ptr< CObject > Ptr
Definition: CObject.h:150
mrpt::rtti::CObject::Ptr duplicateGetSmartPtr() const
Makes a deep copy of the object and returns a smart pointer to it.
Definition: CObject.h:170



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 5887d2b31 Wed Apr 24 13:03:27 2019 +0200 at miƩ abr 24 13:10:13 CEST 2019