Main MRPT website > C++ reference for MRPT 1.9.9
CObject.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
13 #include <mrpt/typemeta/static_string.h> // literal()
14 #include <vector>
15 #include <memory>
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  const char *altName;
39 
40  // Operations
42  bool derivedFrom(const TRuntimeClassId* pBaseClass) const;
43  bool derivedFrom(const char* pBaseClass_name) const;
44 };
45 
46 /** Register a class into the MRPT internal list of "CObject" descendents.
47  * Used internally in the macros DEFINE_SERIALIZABLE, etc...
48  * \sa getAllRegisteredClasses
49  */
50 void registerClass(const mrpt::rtti::TRuntimeClassId* pNewClass);
51 
52 /** Mostly for internal use within mrpt sources, to handle exceptional cases
53  * with multiple serialization names for backward compatibility
54  * (CMultiMetricMaps, CImage,...)
55  */
57  const char* customName, const TRuntimeClassId* pNewClass);
58 
59 /** Returns a list with all the classes registered in the system through
60  * mrpt::rtti::registerClass.
61  * \sa registerClass, findRegisteredClass
62  */
63 std::vector<const mrpt::rtti::TRuntimeClassId*> getAllRegisteredClasses();
64 
65 /** Like getAllRegisteredClasses(), but filters the list to only include
66  * children clases of a given base one.
67  * \sa getAllRegisteredClasses(), getAllRegisteredClassesChildrenOf() */
68 std::vector<const TRuntimeClassId*> getAllRegisteredClassesChildrenOf(
69  const TRuntimeClassId* parent_id);
70 
71 /** Return info about a given class by its name, or nullptr if the class is not
72  * registered
73  * \sa registerClass, getAllRegisteredClasses
74  */
75 const TRuntimeClassId* findRegisteredClass(const std::string& className);
76 
77 template <typename T>
79 {
80  return &T::GetRuntimeClassIdStatic();
81 }
82 
83 /** Access to runtime class ID for a defined class name.
84  */
85 #define CLASS_ID(T) mrpt::rtti::CLASS_ID_impl<T>()
86 // Convert these
87 #define CLASS_ID_TEMPLATE(class_name, T) mrpt::rtti::CLASS_ID_impl<T>()
88 #define CLASS_ID_NAMESPACE(class_name, namespaceName) \
89  mrpt::rtti::CLASS_ID_impl<namespaceName::class_name>()
90 
91 template <typename T>
93 {
94  template <typename PTR>
95  static bool check(const PTR& p)
96  {
97  return p->GetRuntimeClass() == CLASS_ID_impl<T>();
98  }
99 };
100 
101 /** Evaluates to true if the given pointer to an object (derived from
102  * mrpt::rtti::CObject) is of the given class. */
103 #define IS_CLASS(ptrObj, class_name) \
104  mrpt::rtti::IS_CLASS_impl<class_name>::check(ptrObj)
105 
106 /** Evaluates to true if a pointer to an object (derived from
107  * mrpt::rtti::CObject) is an instance of the given class OR any of its
108  * derived classes. */
109 #define IS_DERIVED(ptrObj, class_name) \
110  ((ptrObj)->GetRuntimeClass()->derivedFrom(CLASS_ID(class_name)))
111 
112 /** Auxiliary structure used for CObject-based RTTI. \ingroup mrpt_rtti_grp */
113 struct CLASSINIT
114 {
116  {
117  registerClass(pNewClass);
118  }
119 };
120 
121 /** The virtual base class of all MRPT classes with a unified RTTI system.
122  * For each class named <code>CMyClass</code>, a new type
123  * <code>CMyClass::Ptr</code> is defined as a smart pointer (`std::shared_ptr`)
124  * suitable for keeping reference-counting smart pointers to objects of the
125  * class.
126  *
127  * It is recommended to use MRPT-defined `mrpt::make_aligned_shared<>` instead
128  * of `std::make_shared<>` to create objects, to avoid memory alignment
129  * problems caused by classes containing Eigen vectors or matrices. Example:
130  * \code
131  * CFoo::Ptr o = mrpt::make_aligned_shared<CFoo>();
132  * \endcode
133  * Or using the shorter auxiliary static method `::Create()` to keep
134  * compatibility
135  * with MRPT 1.5.* code bases:
136  * \code
137  * CFoo::Ptr o = CFoo::Create();
138  * \endcode
139  * \sa mrpt::rtti::CObject
140  * \ingroup mrpt_rtti_grp
141  */
142 class CObject
143 {
144  protected:
147 
148  public:
152  /** Returns information about the class of an object in runtime. */
154  {
155  return CLASS_ID(CObject);
156  }
157 
158  /** Returns a copy of the object, indepently of its class, as a smart
159  * pointer (the newly created object will exist as long as any copy of this
160  * smart pointer). */
162 
163  /** Returns a deep copy (clone) of the object, indepently of its class. */
164  virtual CObject* clone() const = 0;
165 
166  virtual ~CObject() {}
167 }; // End of class def.
168 
170 {
171  return mrpt::rtti::CObject::Ptr(this->clone());
172 }
173 
174 /** This declaration must be inserted in all CObject classes definition, within
175  * the class declaration. */
176 #define DEFINE_MRPT_OBJECT(class_name) \
177  /*! @name RTTI stuff */ \
178  /*! @{ */ \
179  protected: \
180  static const mrpt::rtti::TRuntimeClassId* _GetBaseClass(); \
181  static mrpt::rtti::CLASSINIT _init_##class_name; \
182  static const mrpt::rtti::TRuntimeClassId runtimeClassId; \
183  \
184  public: \
185  /*! A type for the associated smart pointer */ \
186  using Ptr = std::shared_ptr<class_name>; \
187  using ConstPtr = std::shared_ptr<const class_name>; \
188  using UniquePtr = std::unique_ptr<class_name>; \
189  using ConstUniquePtr = std::unique_ptr<const class_name>; \
190  static constexpr const char* className = #class_name; \
191  static constexpr auto getClassName() \
192  { \
193  return mrpt::typemeta::literal(#class_name); \
194  } \
195  static const mrpt::rtti::TRuntimeClassId& GetRuntimeClassIdStatic(); \
196  virtual const mrpt::rtti::TRuntimeClassId* GetRuntimeClass() \
197  const override; \
198  static mrpt::rtti::CObject* CreateObject(); \
199  virtual mrpt::rtti::CObject* clone() const override; \
200  template <typename... Args> \
201  static Ptr Create(Args&&... args) \
202  { \
203  return mrpt::make_aligned_shared<class_name>( \
204  std::forward<Args>(args)...); \
205  } \
206  template <typename... Args> \
207  static UniquePtr CreateUnique(Args&&... args) \
208  { \
209  return std::make_unique<class_name>(std::forward<Args>(args)...); \
210  } \
211  /*! @} */ \
212  public: \
213  MRPT_MAKE_ALIGNED_OPERATOR_NEW
214 
215 /** This must be inserted in all CObject classes implementation files
216  */
217 #define IMPLEMENTS_MRPT_OBJECT_ALT_NAME(class_name, base, NameSpace, alt_name) \
218  mrpt::rtti::CObject* NameSpace::class_name::CreateObject() \
219  { \
220  return static_cast<mrpt::rtti::CObject*>(new NameSpace::class_name); \
221  } \
222  const mrpt::rtti::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \
223  { \
224  return CLASS_ID(base); \
225  } \
226  const mrpt::rtti::TRuntimeClassId& \
227  NameSpace::class_name::GetRuntimeClassIdStatic() \
228  { \
229  return NameSpace::class_name::runtimeClassId; \
230  } \
231  const mrpt::rtti::TRuntimeClassId NameSpace::class_name::runtimeClassId = \
232  {#class_name, NameSpace::class_name::CreateObject, \
233  &class_name::_GetBaseClass, alt_name}; \
234  const mrpt::rtti::TRuntimeClassId* \
235  NameSpace::class_name::GetRuntimeClass() const \
236  { \
237  return CLASS_ID_NAMESPACE(class_name, NameSpace); \
238  } \
239  mrpt::rtti::CLASSINIT NameSpace::class_name::_init_##class_name( \
240  CLASS_ID(base)); \
241  mrpt::rtti::CObject* NameSpace::class_name::clone() const \
242  { \
243  return static_cast<mrpt::rtti::CObject*>( \
244  new NameSpace::class_name(*this)); \
245  }
246 
247 #define IMPLEMENTS_MRPT_OBJECT(class_name, base, NameSpace) IMPLEMENTS_MRPT_OBJECT_ALT_NAME(class_name, base, NameSpace, nullptr)
248 
249 /** This declaration must be inserted in virtual CObject classes
250  * definition:
251  */
252 #define DEFINE_VIRTUAL_MRPT_OBJECT(class_name) \
253  /*! @name RTTI stuff */ \
254  /*! @{ */ \
255  protected: \
256  static const mrpt::rtti::TRuntimeClassId* _GetBaseClass(); \
257  static const mrpt::rtti::TRuntimeClassId runtimeClassId; \
258  \
259  public: \
260  using Ptr = std::shared_ptr<class_name>; \
261  using ConstPtr = std::shared_ptr<const class_name>; \
262  virtual const mrpt::rtti::TRuntimeClassId* GetRuntimeClass() \
263  const override; \
264  static const mrpt::rtti::TRuntimeClassId& GetRuntimeClassIdStatic(); \
265 /*! @} */
266 
267 /** This must be inserted as implementation of some required members for
268  * virtual CObject classes:
269  */
270 #define IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name, NameSpace) \
271  const mrpt::rtti::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \
272  { \
273  return CLASS_ID(base_class_name); \
274  } \
275  const mrpt::rtti::TRuntimeClassId NameSpace::class_name::runtimeClassId = \
276  {#class_name, nullptr, &NameSpace::class_name::_GetBaseClass}; \
277  const mrpt::rtti::TRuntimeClassId* \
278  NameSpace::class_name::GetRuntimeClass() const \
279  { \
280  return CLASS_ID(class_name); \
281  } \
282  const mrpt::rtti::TRuntimeClassId& \
283  NameSpace::class_name::GetRuntimeClassIdStatic() \
284  { \
285  return NameSpace::class_name::runtimeClassId; \
286  }
287 
288 /** Register all pending classes - to be called just before de-serializing an
289  * object, for example.
290  * After calling this method, pending_class_registers_modified is set to false
291  * until pending_class_registers() is invoked.
292  */
294 
295 /** Creates an object given by its registered name.
296  * \sa findRegisteredClass(), registerClass(), classFactoryPtr() */
297 mrpt::rtti::CObject* classFactory(const std::string& className);
298 
299 /** Like classFactory() but returns a smart pointer */
301 
302 /** @} */ // end of RTTI
303 
304 } // namespace rtti
305 
306 /** Converts a polymorphic smart pointer Base::Ptr to Derived::Ptr, in a way
307  * compatible with MRPT >=1.5.4 and MRPT 2.x series.
308  * \ingroup mrpt_rtti_grp
309  */
310 template <typename CAST_TO>
311 struct ptr_cast
312 {
313  template <typename CAST_FROM_PTR>
314  static typename CAST_TO::Ptr from(const CAST_FROM_PTR& ptr)
315  {
316  return std::dynamic_pointer_cast<CAST_TO>(ptr);
317  }
318 };
319 
320 } // namespace mrpt
void registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
Auxiliary structure used for CObject-based RTTI.
Definition: CObject.h:113
virtual ~CObject()
Definition: CObject.h:166
constexpr const mrpt::rtti::TRuntimeClassId * CLASS_ID_impl()
Definition: CObject.h:78
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:311
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:142
CLASSINIT(const mrpt::rtti::TRuntimeClassId *pNewClass)
Definition: CObject.h:115
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:85
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:95
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:153
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:4101
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:314
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:146
mrpt::rtti::CObject * createObject() const
Definition: CObject.cpp:79
GLfloat GLfloat p
Definition: glext.h:6305
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:149
mrpt::rtti::CObject::Ptr duplicateGetSmartPtr() const
Returns a copy of the object, indepently of its class, as a smart pointer (the newly created object w...
Definition: CObject.h:169
const char * altName
Definition: CObject.h:38



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019