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 
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 /** The virtual base class of all MRPT classes with a unified RTTI system.
121  * For each class named <code>CMyClass</code>, a new type
122  * <code>CMyClass::Ptr</code> is defined as a smart pointer (`std::shared_ptr`)
123  * suitable for keeping reference-counting smart pointers to objects of the
124  * class.
125  *
126  * It is recommended to use MRPT-defined `mrpt::make_aligned_shared<>` instead
127  * of `std::make_shared<>` to create objects, to avoid memory alignment
128  * problems caused by classes containing Eigen vectors or matrices. Example:
129  * \code
130  * CFoo::Ptr o = mrpt::make_aligned_shared<CFoo>();
131  * \endcode
132  * Or using the shorter auxiliary static method `::Create()` to keep
133  * compatibility
134  * with MRPT 1.5.* code bases:
135  * \code
136  * CFoo::Ptr o = CFoo::Create();
137  * \endcode
138  * \sa mrpt::rtti::CObject
139  * \ingroup mrpt_rtti_grp
140  */
141 class CObject
142 {
143  protected:
146 
147  public:
151  /** Returns information about the class of an object in runtime. */
153  {
154  return CLASS_ID(CObject);
155  }
156 
157  /** Returns a copy of the object, indepently of its class, as a smart
158  * pointer (the newly created object will exist as long as any copy of this
159  * smart pointer). */
161 
162  /** Returns a deep copy (clone) of the object, indepently of its class. */
163  virtual CObject* clone() const = 0;
164 
165  virtual ~CObject() {}
166 }; // End of class def.
167 
169 {
170  return mrpt::rtti::CObject::Ptr(this->clone());
171 }
172 
173 /** This declaration must be inserted in all CObject classes definition, within
174  * the class declaration. */
175 #define DEFINE_MRPT_OBJECT(class_name) \
176  /*! @name RTTI stuff */ \
177  /*! @{ */ \
178  protected: \
179  static const mrpt::rtti::TRuntimeClassId* _GetBaseClass(); \
180  static mrpt::rtti::CLASSINIT _init_##class_name; \
181  static const mrpt::rtti::TRuntimeClassId runtimeClassId; \
182  \
183  public: \
184  /*! A type for the associated smart pointer */ \
185  using Ptr = std::shared_ptr<class_name>; \
186  using ConstPtr = std::shared_ptr<const class_name>; \
187  using UniquePtr = std::unique_ptr<class_name>; \
188  using ConstUniquePtr = std::unique_ptr<const class_name>; \
189  static constexpr const char* className = #class_name; \
190  static constexpr auto getClassName() \
191  { \
192  return mrpt::typemeta::literal(#class_name); \
193  } \
194  static const mrpt::rtti::TRuntimeClassId& GetRuntimeClassIdStatic(); \
195  virtual const mrpt::rtti::TRuntimeClassId* GetRuntimeClass() \
196  const override; \
197  static mrpt::rtti::CObject* CreateObject(); \
198  virtual mrpt::rtti::CObject* clone() const override; \
199  template <typename... Args> \
200  static Ptr Create(Args&&... args) \
201  { \
202  return mrpt::make_aligned_shared<class_name>( \
203  std::forward<Args>(args)...); \
204  } \
205  template <typename... Args> \
206  static UniquePtr CreateUnique(Args&&... args) \
207  { \
208  return std::make_unique<class_name>(std::forward<Args>(args)...); \
209  } \
210  /*! @} */ \
211  public: \
212  MRPT_MAKE_ALIGNED_OPERATOR_NEW
213 
214 /** This must be inserted in all CObject classes implementation files
215  */
216 #define IMPLEMENTS_MRPT_OBJECT(class_name, base, NameSpace) \
217  mrpt::rtti::CObject* NameSpace::class_name::CreateObject() \
218  { \
219  return static_cast<mrpt::rtti::CObject*>(new NameSpace::class_name); \
220  } \
221  const mrpt::rtti::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \
222  { \
223  return CLASS_ID(base); \
224  } \
225  const mrpt::rtti::TRuntimeClassId& \
226  NameSpace::class_name::GetRuntimeClassIdStatic() \
227  { \
228  return NameSpace::class_name::runtimeClassId; \
229  } \
230  const mrpt::rtti::TRuntimeClassId NameSpace::class_name::runtimeClassId = \
231  {#class_name, NameSpace::class_name::CreateObject, \
232  &class_name::_GetBaseClass}; \
233  const mrpt::rtti::TRuntimeClassId* \
234  NameSpace::class_name::GetRuntimeClass() const \
235  { \
236  return CLASS_ID_NAMESPACE(class_name, NameSpace); \
237  } \
238  mrpt::rtti::CLASSINIT NameSpace::class_name::_init_##class_name( \
239  CLASS_ID(base)); \
240  mrpt::rtti::CObject* NameSpace::class_name::clone() const \
241  { \
242  return static_cast<mrpt::rtti::CObject*>( \
243  new NameSpace::class_name(*this)); \
244  }
245 
246 /** This declaration must be inserted in virtual CObject classes
247  * definition:
248  */
249 #define DEFINE_VIRTUAL_MRPT_OBJECT(class_name) \
250  /*! @name RTTI stuff */ \
251  /*! @{ */ \
252  protected: \
253  static const mrpt::rtti::TRuntimeClassId* _GetBaseClass(); \
254  static const mrpt::rtti::TRuntimeClassId runtimeClassId; \
255  \
256  public: \
257  using Ptr = std::shared_ptr<class_name>; \
258  using ConstPtr = std::shared_ptr<const class_name>; \
259  virtual const mrpt::rtti::TRuntimeClassId* GetRuntimeClass() \
260  const override; \
261  static const mrpt::rtti::TRuntimeClassId& GetRuntimeClassIdStatic(); \
262 /*! @} */
263 
264 /** This must be inserted as implementation of some required members for
265  * virtual CObject classes:
266  */
267 #define IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name, NameSpace) \
268  const mrpt::rtti::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \
269  { \
270  return CLASS_ID(base_class_name); \
271  } \
272  const mrpt::rtti::TRuntimeClassId NameSpace::class_name::runtimeClassId = \
273  {#class_name, nullptr, &NameSpace::class_name::_GetBaseClass}; \
274  const mrpt::rtti::TRuntimeClassId* \
275  NameSpace::class_name::GetRuntimeClass() const \
276  { \
277  return CLASS_ID(class_name); \
278  } \
279  const mrpt::rtti::TRuntimeClassId& \
280  NameSpace::class_name::GetRuntimeClassIdStatic() \
281  { \
282  return NameSpace::class_name::runtimeClassId; \
283  }
284 
285 /** Register all pending classes - to be called just before de-serializing an
286  * object, for example.
287  * After calling this method, pending_class_registers_modified is set to false
288  * until pending_class_registers() is invoked.
289  */
291 
292 /** Creates an object given by its registered name.
293  * \sa findRegisteredClass(), registerClass(), classFactoryPtr() */
294 mrpt::rtti::CObject* classFactory(const std::string& className);
295 
296 /** Like classFactory() but returns a smart pointer */
298 
299 /** @} */ // end of RTTI
300 
301 } // namespace rtti
302 
303 /** Converts a polymorphic smart pointer Base::Ptr to Derived::Ptr, in a way
304  * compatible with MRPT >=1.5.4 and MRPT 2.x series.
305  * \ingroup mrpt_rtti_grp
306  */
307 template <typename CAST_TO>
308 struct ptr_cast
309 {
310  template <typename CAST_FROM_PTR>
311  static typename CAST_TO::Ptr from(const CAST_FROM_PTR& ptr)
312  {
313  return std::dynamic_pointer_cast<CAST_TO>(ptr);
314  }
315 };
316 
317 } // 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:112
virtual ~CObject()
Definition: CObject.h:165
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:308
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:141
CLASSINIT(const mrpt::rtti::TRuntimeClassId *pNewClass)
Definition: CObject.h:114
#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:152
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:311
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:145
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:148
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:168



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020