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-2017, 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 #ifndef MRPT_COBJECT_H
10 #define MRPT_COBJECT_H
11 
12 #include <mrpt/system/memory.h>
15 #include <vector>
16 #include <memory>
17 
18 namespace mrpt
19 {
20 namespace utils
21 {
22 /** @name RTTI classes and functions
23  @{ */
24 class CObject;
25 
26 /** A structure that holds runtime class type information. Use
27  * CLASS_ID(<class_name>) to get a reference to the class_name's TRuntimeClassId
28  * descriptor.
29  * \ingroup mrpt_base_grp
30  */
32 {
34  const char* className;
35  /** Create an object of the related class, or nullptr if it is virtual. */
36  mrpt::utils::CObject* (*ptrCreateObject)();
37  /** Gets the base class runtime id. */
38  const TRuntimeClassId* (*getBaseClass)();
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 "CSerializable" descendents.
47  * Used internally in the macros DEFINE_SERIALIZABLE, etc...
48  * \sa getAllRegisteredClasses
49  */
50 void registerClass(const mrpt::utils::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::utils::registerClass.
61  * \sa registerClass, findRegisteredClass
62  */
63 std::vector<const mrpt::utils::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::utils::CLASS_ID_impl<T>()
86 // Convert these
87 #define CLASS_ID_TEMPLATE(class_name, T) mrpt::utils::CLASS_ID_impl<T>()
88 #define CLASS_ID_NAMESPACE(class_name, namespaceName) \
89  mrpt::utils::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::utils::CSerializable) is of the given class. */
103 #define IS_CLASS(ptrObj, class_name) \
104  mrpt::utils::IS_CLASS_impl<class_name>::check(ptrObj)
105 
106 /** Evaluates to true if the given pointer to an object (derived from
107  * mrpt::utils::CSerializable) 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_base_grp */
113 struct CLASSINIT
114 {
116  {
117  registerClass(pNewClass);
118  }
119 };
120 
121 /** This is useful for checking ::Ptr types.
122  * I'm surprised it's not defined in <memory>
123  */
124 template <class T>
125 struct is_shared_ptr : std::false_type
126 {
127 };
128 template <class T>
129 struct is_shared_ptr<std::shared_ptr<T>> : std::true_type
130 {
131 };
132 
133 /** The virtual base class of all MRPT classes with a unified RTTI system.
134  * For each class named <code>CMyClass</code>, a new type named
135  * <code>CMyClass::Ptr</code> will be created as a smart pointer suitable for
136  * keeping referencing count smart pointers to objects of that class. By
137  * default the base class of all these smart pointers is CObject::Ptr.
138  *
139  * It is recommended to use MRPT-defined `mrpt::make_aligned_shared<>` instead
140  * of `std::make_shared<>` to create objects, to avoid memory alignment
141  * problems caused by classes containing Eigen vectors or matrices. Example:
142  * \code
143  * CFoo::Ptr o = mrpt::make_aligned_shared<CFoo>();
144  * \endcode
145  * \sa mrpt::utils::CSerializable \ingroup mrpt_base_grp
146  */
147 class CObject
148 {
149  protected:
152 
153  public:
154  using Ptr = std::shared_ptr<CObject>;
155  using ConstPtr = std::shared_ptr<const CObject>;
157  /** Returns information about the class of an object in runtime. */
159  {
160  return CLASS_ID(CObject);
161  }
162 
163  /** Returns a copy of the object, indepently of its class, as a smart
164  * pointer (the newly created object will exist as long as any copy of this
165  * smart pointer). */
167 
168  /** Returns a deep copy (clone) of the object, indepently of its class. */
169  virtual CObject* clone() const = 0;
170 
171  virtual ~CObject() {}
172 }; // End of class def.
173 
174 /** A smart pointer to a CObject object
175  * \note Declared as a class instead of a typedef to avoid multiple defined
176  * symbols when linking dynamic libs.
177  * \ingroup mrpt_base_grp
178  */
180 {
181  return mrpt::utils::CObject::Ptr(this->clone());
182 }
183 
184 /** This declaration must be inserted in all CObject classes definition, within
185 * the class declaration. */
186 #define DEFINE_MRPT_OBJECT(class_name) \
187  /*! @name RTTI stuff */ \
188  /*! @{ */ \
189  protected: \
190  static const mrpt::utils::TRuntimeClassId* _GetBaseClass(); \
191  static mrpt::utils::CLASSINIT _init_##class_name; \
192  static const mrpt::utils::TRuntimeClassId runtimeClassId; \
193  \
194  public: \
195  /*! A typedef for the associated smart pointer */ \
196  using Ptr = std::shared_ptr<class_name>; \
197  using ConstPtr = std::shared_ptr<const class_name>; \
198  static constexpr const char* className = #class_name; \
199  static const mrpt::utils::TRuntimeClassId& GetRuntimeClassIdStatic(); \
200  virtual const mrpt::utils::TRuntimeClassId* GetRuntimeClass() \
201  const override; \
202  static mrpt::utils::CObject* CreateObject(); \
203  virtual mrpt::utils::CObject* clone() const override; \
204  template <typename... Args> \
205  static Ptr Create(Args&&... args) \
206  { \
207  return mrpt::make_aligned_shared<class_name>( \
208  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::utils::CObject* NameSpace::class_name::CreateObject() \
218  { \
219  return static_cast<mrpt::utils::CObject*>(new NameSpace::class_name); \
220  } \
221  const mrpt::utils::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \
222  { \
223  return CLASS_ID(base); \
224  } \
225  const mrpt::utils::TRuntimeClassId& \
226  NameSpace::class_name::GetRuntimeClassIdStatic() \
227  { \
228  return NameSpace::class_name::runtimeClassId; \
229  } \
230  const mrpt::utils::TRuntimeClassId NameSpace::class_name::runtimeClassId = \
231  {#class_name, NameSpace::class_name::CreateObject, \
232  &class_name::_GetBaseClass}; \
233  const mrpt::utils::TRuntimeClassId* \
234  NameSpace::class_name::GetRuntimeClass() const \
235  { \
236  return CLASS_ID_NAMESPACE(class_name, NameSpace); \
237  } \
238  mrpt::utils::CLASSINIT NameSpace::class_name::_init_##class_name( \
239  CLASS_ID(base)); \
240  mrpt::utils::CObject* NameSpace::class_name::clone() const \
241  { \
242  return static_cast<mrpt::utils::CObject*>( \
243  new NameSpace::class_name(*this)); \
244  }
245 
246 /** This declaration must be inserted in virtual CSerializable classes
247  * definition:
248  */
249 #define DEFINE_VIRTUAL_MRPT_OBJECT(class_name) \
250  /*! @name RTTI stuff */ \
251  /*! @{ */ \
252  protected: \
253  static const mrpt::utils::TRuntimeClassId* _GetBaseClass(); \
254  static const mrpt::utils::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::utils::TRuntimeClassId* GetRuntimeClass() \
260  const override; \
261  static const mrpt::utils::TRuntimeClassId& GetRuntimeClassIdStatic(); \
262  friend class mrpt::utils::CStream; \
263 /*! @} */
264 
265 /** This must be inserted as implementation of some required members for
266  * virtual CSerializable classes:
267  */
268 #define IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name, NameSpace) \
269  const mrpt::utils::TRuntimeClassId* class_name::_GetBaseClass() \
270  { \
271  return CLASS_ID(base_class_name); \
272  } \
273  const mrpt::utils::TRuntimeClassId class_name::runtimeClassId = { \
274  #class_name, nullptr, &class_name::_GetBaseClass}; \
275  const mrpt::utils::TRuntimeClassId* class_name::GetRuntimeClass() const \
276  { \
277  return CLASS_ID(class_name); \
278  } \
279  const mrpt::utils::TRuntimeClassId& \
280  NameSpace::class_name::GetRuntimeClassIdStatic() \
281  { \
282  return NameSpace::class_name::runtimeClassId; \
283  }
284 
285 /** @} */ // end of RTTI
286 
287 /** Register all pending classes - to be called just before de-serializing an
288  * object, for example.
289  * After calling this method, pending_class_registers_modified is set to false
290  * until pending_class_registers() is invoked.
291  */
293 
294 } // End of namespace
295 
296 /** Converts a smart pointer Base::Ptr to Derived::Ptr, in a way compatible
297 * with MRPT >=1.5.4 and MRPT 2.x series.
298 * \ingroup mrpt_base_grp
299 */
300 template <typename CAST_TO>
301 struct ptr_cast
302 {
303  template <typename CAST_FROM_PTR>
304  static typename CAST_TO::Ptr from(const CAST_FROM_PTR& ptr)
305  {
306  return std::dynamic_pointer_cast<CAST_TO>(ptr);
307  }
308 };
309 
310 } // End of namespace
311 
312 #endif
Auxiliary structure used for CObject-based RTTI.
Definition: CObject.h:113
constexpr const mrpt::utils::TRuntimeClassId * CLASS_ID_impl()
Definition: CObject.h:78
void registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
static const mrpt::utils::TRuntimeClassId runtimeClassId
Definition: CObject.h:151
virtual CObject * clone() const =0
Returns a deep copy (clone) of the object, indepently of its class.
bool derivedFrom(const TRuntimeClassId *pBaseClass) const
Definition: CObject.cpp:32
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const
Returns information about the class of an object in runtime.
Definition: CObject.h:158
std::vector< const mrpt::utils::TRuntimeClassId * > getAllRegisteredClasses()
Returns a list with all the classes registered in the system through mrpt::utils::registerClass.
Converts a smart pointer Base::Ptr to Derived::Ptr, in a way compatible with MRPT >=1...
Definition: CObject.h:301
STL namespace.
std::shared_ptr< CObject > Ptr
Definition: CObject.h:154
mrpt::utils::CObject * createObject() const
Definition: CObject.cpp:93
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:85
static const mrpt::utils::TRuntimeClassId & GetRuntimeClassIdStatic()
Definition: CObject.cpp:24
virtual ~CObject()
Definition: CObject.h:171
mrpt::utils::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:179
GLsizei const GLchar ** string
Definition: glext.h:4101
std::shared_ptr< const CObject > ConstPtr
Definition: CObject.h:155
const TRuntimeClassId * findRegisteredClass(const std::string &className)
Return info about a given class by its name, or nullptr if the class is not registered.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
static CAST_TO::Ptr from(const CAST_FROM_PTR &ptr)
Definition: CObject.h:304
This is useful for checking ::Ptr types.
Definition: CObject.h:125
void registerClass(const mrpt::utils::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CSerializable" descendents.
CLASSINIT(const mrpt::utils::TRuntimeClassId *pNewClass)
Definition: CObject.h:115
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:31
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:147
const char * className
Definition: CObject.h:34
GLfloat GLfloat p
Definition: glext.h:6305
static mrpt::utils::TRuntimeClassId * _GetBaseClass()
Definition: CObject.cpp:119
static bool check(const PTR p)
Definition: CObject.h:95
void registerClassCustomName(const char *customName, const TRuntimeClassId *pNewClass)
Mostly for internal use within mrpt sources, to handle exceptional cases with multiple serialization ...
A wrapper class for pointers that can be safely copied with "=" operator without problems.
Definition: safe_pointers.h:75



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019