Main MRPT website > C++ reference
MRPT logo

CObject.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  MRPT_COBJECT_H
00029 #define  MRPT_COBJECT_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/safe_pointers.h>
00033 
00034 namespace mrpt
00035 {
00036         namespace utils
00037         {
00038                 /** @name RTTI classes and functions
00039                     @{ */
00040 
00041                 class BASE_IMPEXP CObject;
00042 
00043                 /** A smart pointer to a CObject object
00044                   * \note Declared as a class instead of a typedef to avoid multiple defined symbols when linking dynamic libs.
00045                   */
00046                 class BASE_IMPEXP CObjectPtr : public stlplus::smart_ptr_clone<CObject>
00047                 {
00048                         typedef stlplus::smart_ptr_clone<CObject> BASE;
00049                 public:
00050                         inline CObjectPtr() : BASE() {}
00051                         explicit inline CObjectPtr(const CObject& data) :  BASE(data) {}
00052                         explicit inline CObjectPtr(CObject* data) :  BASE(data) { }
00053                         inline CObjectPtr& operator=(const CObject& data) { BASE::operator=(data); return *this; }
00054                         inline CObjectPtr& operator=(const CObjectPtr& r) { BASE::operator=(r); return *this; }
00055                 };
00056 
00057                 /** A structure that holds runtime class type information. Use CLASS_ID(<class_name>) to get a reference to the class_name's TRuntimeClassId descriptor.
00058                   */
00059                 struct BASE_IMPEXP TRuntimeClassId
00060                 {
00061                         const char* className;
00062                         /** Create an object of the related class, or NULL if it is virtual. */
00063                         mrpt::utils::CObject*                   (*ptrCreateObject)();
00064                         /** Gets the base class runtime id. */
00065                         const TRuntimeClassId*          (*getBaseClass)();
00066 
00067                         // Operations
00068                         mrpt::utils::CObject*           createObject() const;
00069                         bool    derivedFrom(const TRuntimeClassId* pBaseClass) const;
00070                         bool    derivedFrom(const char* pBaseClass_name) const;
00071 
00072                 };
00073 
00074                 /** A wrapper class for a "TRuntimeClassId *", well-defined with respect to copy operators and constructors.
00075                   */
00076                 typedef safe_ptr<TRuntimeClassId> TRuntimeClassIdPtr;
00077 
00078                 /** Register a class into the MRPT internal list of "CSerializable" descendents.
00079                   *  Used internally in the macros DEFINE_SERIALIZABLE, etc...
00080                   * \sa getAllRegisteredClasses, CStartUpClassesRegister
00081                   */
00082                 void BASE_IMPEXP registerClass(const mrpt::utils::TRuntimeClassId* pNewClass);
00083 
00084                 /** Mostly for internal use within mrpt sources, to handle exceptional cases with multiple serialization names for backward compatibility (CMultiMetricMaps, CImage,...)
00085                   */
00086                 void  BASE_IMPEXP registerClassCustomName(const char*customName, const TRuntimeClassId* pNewClass);
00087 
00088                 /** Returns a list with all the classes registered in the system through mrpt::utils::registerClass.
00089                   * \sa registerClass, findRegisteredClass
00090                   */
00091                 std::vector<const mrpt::utils::TRuntimeClassId*> BASE_IMPEXP getAllRegisteredClasses();
00092 
00093                 /** Return info about a given class by its name, or NULL if the class is not registered
00094                   * \sa registerClass, getAllRegisteredClasses
00095                   */
00096                 const TRuntimeClassId BASE_IMPEXP * findRegisteredClass(const std::string &className);
00097 
00098 
00099                 /** Access to runtime class ID for a defined class name.
00100                   */
00101                 #define CLASS_ID(class_name) static_cast<const mrpt::utils::TRuntimeClassId*>(&class_name::class##class_name)
00102 
00103                 /** Access to runtime class ID for a defined class name.
00104                   */
00105                 #define CLASS_ID_NAMESPACE(class_name,namespaceName) static_cast<const mrpt::utils::TRuntimeClassId*>(&namespaceName::class_name::class##class_name)
00106 
00107                 /** Access to runtime class ID for a defined template class name.
00108                   */
00109                 #define CLASS_ID_TEMPLATE(class_name,T) static_cast<const mrpt::utils::TRuntimeClassId*>(& template <class T> class_name<T>::class##class_name)
00110 
00111                 /** Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of the given class. */
00112                 #define IS_CLASS( ptrObj, class_name )  ((ptrObj)->GetRuntimeClass()==CLASS_ID(class_name))
00113 
00114                 /** Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is an instance of the given class or any of its derived classes. */
00115                 #define IS_DERIVED( ptrObj, class_name )  ((ptrObj)->GetRuntimeClass()->derivedFrom(CLASS_ID(class_name)))
00116 
00117                 /** Auxiliary structure used for CObject-based RTTI. */
00118                 struct BASE_IMPEXP CLASSINIT
00119                 {
00120                         CLASSINIT(const mrpt::utils::TRuntimeClassId* pNewClass)
00121                         {
00122                                 registerClass(pNewClass);
00123                         }
00124                 };
00125 
00126 
00127                 /** The virtual base class of all MRPT classes with a unified RTTI system.
00128                  *   For each class named <code>CMyClass</code>, a new type named <code>CMyClassPtr</code> will be created as a smart pointer suitable for
00129                  *    keeping referencing count smart pointers to objects of that class. By default the base class of all these smart pointers is CObjectPtr.
00130                  * \sa  mrpt::utils::CSerializable
00131                  */
00132                 class BASE_IMPEXP CObject
00133                 {
00134                 protected:
00135                         static  mrpt::utils::TRuntimeClassId* _GetBaseClass();
00136                 public:
00137                         static const mrpt::utils::TRuntimeClassId classCObject;
00138 
00139                         /** Returns information about the class of an object in runtime. */
00140                         virtual const mrpt::utils::TRuntimeClassId* GetRuntimeClass() const
00141                         {
00142                                 return CLASS_ID(CObject);
00143                         }
00144 
00145                         /** Returns a copy of the object, indepently of its class. */
00146                         virtual CObject *duplicate() const = 0;
00147 
00148                         /** Returns a copy of the object, indepently of its class, as a smart pointer (the newly created object will exist as long as any copy of this smart pointer). */
00149                         inline mrpt::utils::CObjectPtr duplicateGetSmartPtr() const { return mrpt::utils::CObjectPtr( this->duplicate() ); }
00150 
00151                         /** Cloning interface for smart pointers */
00152                         inline CObject *clone() const { return duplicate(); }
00153 
00154                         virtual ~CObject() {  }
00155 
00156                 }; // End of class def.
00157 
00158                 /** This declaration must be inserted in all CObject classes definition, within the class declaration.
00159                   */
00160                 #define DEFINE_MRPT_OBJECT(class_name) \
00161                         /*! @name RTTI stuff  */ \
00162                         /*! @{  */ \
00163                 protected: \
00164                         static  const mrpt::utils::TRuntimeClassId* _GetBaseClass(); \
00165                         static mrpt::utils::CLASSINIT _init_##class_name;\
00166                 public: \
00167                         /*! A typedef for the associated smart pointer */ \
00168                         typedef class_name##Ptr SmartPtr; \
00169                         static  mrpt::utils::TRuntimeClassId  class##class_name; \
00170                         static  const mrpt::utils::TRuntimeClassId *classinfo; \
00171                         virtual const mrpt::utils::TRuntimeClassId* GetRuntimeClass() const; \
00172                         static  mrpt::utils::CObject* CreateObject(); \
00173                         static class_name##Ptr Create(); \
00174                         virtual mrpt::utils::CObject *duplicate() const; \
00175                         /*! @} */ \
00176                 public: \
00177                         EIGEN_MAKE_ALIGNED_OPERATOR_NEW \
00178 
00179 
00180                 // This macro is a workaround to avoid possibly empty arguments to MACROS (when _LINKAGE_ evals to nothing...)
00181                 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_ ) \
00182                         DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE2(class_name, base_name, _LINKAGE_ class_name)
00183 
00184                 // Use this one when there is NO import/export macro:
00185                 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_NO_LINKAGE(class_name, base_name) \
00186                         DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE2(class_name, base_name, class_name)
00187 
00188                 /**  This declaration must be inserted in all CObject classes definition, before the class declaration.
00189                   */
00190                 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE2(class_name, base_name, class_name_LINKAGE_ ) \
00191                         class class_name_LINKAGE_; \
00192                         /*! The smart pointer type for the associated class */ \
00193                         struct class_name_LINKAGE_##Ptr : public base_name##Ptr \
00194                         { \
00195                                 inline class_name##Ptr() : base_name##Ptr(static_cast<base_name*>(NULL)) { } \
00196                                 inline explicit class_name##Ptr(class_name* p) : base_name##Ptr( reinterpret_cast<base_name*>(p) ) { } \
00197                                 inline explicit class_name##Ptr(const base_name##Ptr & p) : base_name##Ptr(p) { ASSERTMSG_( p->GetRuntimeClass()->derivedFrom(#class_name),::mrpt::format("Wrong typecasting of smart pointers: %s -> %s",p->GetRuntimeClass()->className, #class_name) )  } \
00198                                 inline explicit class_name##Ptr(const mrpt::utils::CObjectPtr & p) : base_name##Ptr(p) { ASSERTMSG_( p->GetRuntimeClass()->derivedFrom(#class_name),::mrpt::format("Wrong typecasting of smart pointers: %s -> %s",p->GetRuntimeClass()->className, #class_name) )  } \
00199                                 inline void setFromPointerDoNotFreeAtDtor(const class_name* p) { this->set(const_cast<mrpt::utils::CObject*>(reinterpret_cast<const mrpt::utils::CObject*>(p))); m_holder->increment(); } \
00200                                 /*! Return the internal plain C++ pointer */ \
00201                                 inline class_name * pointer() { return reinterpret_cast<class_name*>(base_name##Ptr::pointer()); } \
00202                                 /*! Return the internal plain C++ pointer (const) */ \
00203                                 inline const class_name * pointer() const { return reinterpret_cast<const class_name*>(base_name##Ptr::pointer()); } \
00204                                 inline class_name* operator ->(void) { return reinterpret_cast<class_name*>( base_name##Ptr::operator ->() ); } \
00205                                 inline const class_name* operator ->(void) const { return reinterpret_cast<const class_name*>( base_name##Ptr::operator ->() ); } \
00206                                 inline class_name& operator *(void) { return *reinterpret_cast<class_name*>( base_name##Ptr::operator ->() ); } \
00207                                 inline const class_name& operator *(void) const { return *reinterpret_cast<const class_name*>( base_name##Ptr::operator ->() ); } \
00208                         };
00209 
00210 
00211                 // This macro is a workaround to avoid possibly empty arguments to MACROS (when _LINKAGE_ evals to nothing...)
00212                 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE(class_name,_LINKAGE_) \
00213                         DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE2(class_name, _LINKAGE_ class_name)
00214 
00215                 // Use this macro when there is NO export/import macro:
00216                 #define DEFINE_MRPT_OBJECT_PRE_NO_LINKAGE(class_name) \
00217                         DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE2(class_name, class_name)
00218 
00219                 // This one is almost identical to the one above, but without a member:
00220                 /**  This declaration must be inserted in all CObject classes definition, before the class declaration. */
00221                 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE2(class_name,class_name_LINKAGE_) \
00222                         class class_name_LINKAGE_; \
00223                         /*! The smart pointer type for the associated class */ \
00224                         struct class_name_LINKAGE_##Ptr : public mrpt::utils::CObjectPtr \
00225                         { \
00226                                 inline class_name##Ptr() : mrpt::utils::CObjectPtr(static_cast<mrpt::utils::CObject*>(NULL)) { } \
00227                                 inline explicit class_name##Ptr(class_name* p) : mrpt::utils::CObjectPtr( reinterpret_cast<mrpt::utils::CObject*>(p) ) { } \
00228                                 inline explicit class_name##Ptr(const mrpt::utils::CObjectPtr & p) : mrpt::utils::CObjectPtr(p) { ASSERTMSG_( p->GetRuntimeClass()->derivedFrom(#class_name),::mrpt::format("Wrong typecasting of smart pointers: %s -> %s",p->GetRuntimeClass()->className, #class_name) )  } \
00229                                 inline void setFromPointerDoNotFreeAtDtor(const class_name* p) { this->set(const_cast<mrpt::utils::CObject*>(reinterpret_cast<const mrpt::utils::CObject*>(p))); m_holder->increment(); } \
00230                                 /*! Return the internal plain C++ pointer */ \
00231                                 inline class_name * pointer() { return reinterpret_cast<class_name*>(mrpt::utils::CObjectPtr::pointer()); } \
00232                                 /*! Return the internal plain C++ pointer (const) */ \
00233                                 inline const class_name * pointer() const { return reinterpret_cast<const class_name*>(mrpt::utils::CObjectPtr::pointer()); } \
00234                                 inline class_name* operator ->(void) { return reinterpret_cast<class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \
00235                                 inline const class_name* operator ->(void) const { return reinterpret_cast<const class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \
00236                                 inline class_name& operator *(void) { return *reinterpret_cast<class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \
00237                                 inline const class_name& operator *(void) const { return *reinterpret_cast<const class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \
00238                         };
00239 
00240                 /**  This declaration must be inserted in all CObject classes definition, before the class declaration.
00241                   */
00242                 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE(class_name, base_name) \
00243                         DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, BASE_IMPEXP )
00244 
00245                 /**  This declaration must be inserted in all CObject classes definition, before the class declaration.
00246                   */
00247                 #define DEFINE_MRPT_OBJECT_PRE(class_name) \
00248                         DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE(class_name, BASE_IMPEXP )  // This macro is valid for classes within mrpt-core only.
00249 
00250                 /** This must be inserted in all CObject classes implementation files
00251                   */
00252                 #define IMPLEMENTS_MRPT_OBJECT(class_name, base,NameSpace) \
00253                         mrpt::utils::CObject* NameSpace::class_name::CreateObject() \
00254                                 { return static_cast<mrpt::utils::CObject*>( new NameSpace::class_name ); } \
00255                         NameSpace::class_name##Ptr NameSpace::class_name::Create() \
00256                                 { return NameSpace::class_name##Ptr( new NameSpace::class_name ); } \
00257                         const mrpt::utils::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \
00258                                 { return CLASS_ID(base); } \
00259                         mrpt::utils::TRuntimeClassId NameSpace::class_name::class##class_name = { \
00260                                 #class_name, NameSpace::class_name::CreateObject, &class_name::_GetBaseClass }; \
00261                         const mrpt::utils::TRuntimeClassId *NameSpace::class_name::classinfo = & NameSpace::class_name::class##class_name; \
00262                         const mrpt::utils::TRuntimeClassId* NameSpace::class_name::GetRuntimeClass() const \
00263                         { return CLASS_ID_NAMESPACE(class_name,NameSpace); } \
00264                         mrpt::utils::CLASSINIT NameSpace::class_name::_init_##class_name(CLASS_ID(base)); \
00265                         mrpt::utils::CObject * NameSpace::class_name::duplicate() const \
00266                         { return static_cast<mrpt::utils::CObject*>( new NameSpace::class_name(*this) ); }
00267 
00268 
00269                 /** This declaration must be inserted in virtual CSerializable classes definition:
00270                   */
00271                 #define DEFINE_VIRTUAL_MRPT_OBJECT(class_name) \
00272                 /*! @name RTTI stuff  */ \
00273                 /*! @{  */ \
00274                 protected: \
00275                         static const mrpt::utils::TRuntimeClassId* _GetBaseClass(); \
00276                 public: \
00277                         static const mrpt::utils::TRuntimeClassId class##class_name; \
00278                         virtual const mrpt::utils::TRuntimeClassId* GetRuntimeClass() const; \
00279                         friend class mrpt::utils::CStream; \
00280                 /*! @}  */ \
00281 
00282                 /** This must be inserted as implementation of some required members for
00283                   *  virtual CSerializable classes:
00284                   */
00285                 #define IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name,NameSpace) \
00286                         const mrpt::utils::TRuntimeClassId* class_name::_GetBaseClass() \
00287                                 { return CLASS_ID(base_class_name); } \
00288                         const mrpt::utils::TRuntimeClassId class_name::class##class_name = { \
00289                                 #class_name, NULL, &class_name::_GetBaseClass }; \
00290                         const mrpt::utils::TRuntimeClassId* class_name::GetRuntimeClass() const \
00291                                 { return CLASS_ID(class_name); }
00292 
00293                 /** @}  */   // end of RTTI
00294 
00295         } // End of namespace
00296 } // End of namespace
00297 
00298 // JL: I want these operators to reside in std so STL algorithms can always find them.
00299 namespace std
00300 {
00301         /**  This operator enables comparing two smart pointers with "==" to test whether they point to the same object.
00302           */
00303         template <typename T,typename C, typename COUNTER>
00304         inline bool operator == ( const stlplus::smart_ptr_base<T,C,COUNTER>&a,const stlplus::smart_ptr_base<T,C,COUNTER>&b) {
00305                 return a.aliases(b);
00306         }
00307         /**  This operator enables comparing two smart pointers with "!=" to test whether they don't point to the same object.
00308           */
00309         template <typename T,typename C, typename COUNTER>
00310         inline bool operator != ( const stlplus::smart_ptr_base<T,C,COUNTER>&a,const stlplus::smart_ptr_base<T,C,COUNTER>&b) {
00311                 return !a.aliases(b);
00312         }
00313 }
00314 
00315 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011