10 #define MRPT_COBJECT_H 17 #include <mrpt/otherlibs/stlplus/smart_ptr.hpp> 34 typedef stlplus::smart_ptr_clone<CObject>
BASE;
57 bool derivedFrom(
const char* pBaseClass_name)
const;
92 #define CLASS_ID(class_name) static_cast<const mrpt::utils::TRuntimeClassId*>(&class_name::class##class_name) 96 #define CLASS_ID_NAMESPACE(class_name,namespaceName) static_cast<const mrpt::utils::TRuntimeClassId*>(&namespaceName::class_name::class##class_name) 100 #define CLASS_ID_TEMPLATE(class_name,T) static_cast<const mrpt::utils::TRuntimeClassId*>(& template <class T> class_name<T>::class##class_name) 103 #define IS_CLASS( ptrObj, class_name ) ((ptrObj)->GetRuntimeClass()==CLASS_ID(class_name)) 106 #define IS_DERIVED( ptrObj, class_name ) ((ptrObj)->GetRuntimeClass()->derivedFrom(CLASS_ID(class_name))) 137 virtual CObject *duplicate()
const = 0;
151 #define DEFINE_MRPT_OBJECT_CUSTOM_LINKAGE(class_name, _STATIC_LINKAGE_, _VIRTUAL_LINKAGE_) \ 155 _STATIC_LINKAGE_ const mrpt::utils::TRuntimeClassId* _GetBaseClass(); \ 156 _STATIC_LINKAGE_ mrpt::utils::CLASSINIT _init_##class_name;\ 159 typedef class_name##Ptr Ptr; \ 160 typedef class_name##Ptr ConstPtr; \ 161 _STATIC_LINKAGE_ mrpt::utils::TRuntimeClassId class##class_name; \ 162 _STATIC_LINKAGE_ const mrpt::utils::TRuntimeClassId *classinfo; \ 163 _VIRTUAL_LINKAGE_ const mrpt::utils::TRuntimeClassId* GetRuntimeClass() const MRPT_OVERRIDE; \ 164 _STATIC_LINKAGE_ mrpt::utils::CObject* CreateObject(); \ 165 _STATIC_LINKAGE_ class_name##Ptr Create(); \ 166 _VIRTUAL_LINKAGE_ mrpt::utils::CObject *duplicate() const MRPT_OVERRIDE; \ 169 MRPT_MAKE_ALIGNED_OPERATOR_NEW \ 172 #define DEFINE_MRPT_OBJECT(class_name) \ 173 DEFINE_MRPT_OBJECT_CUSTOM_LINKAGE(class_name, static , virtual ) 176 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_ ) DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE2(class_name, base_name, _LINKAGE_ class_name) 177 #define DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_ ) DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE2(class_name, base_name, _LINKAGE_ class_name) 180 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_NO_LINKAGE(class_name, base_name) DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE2(class_name, base_name, class_name) 181 #define DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_NO_LINKAGE(class_name, base_name) DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE2(class_name, base_name, class_name) 184 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE2(class_name, base_name, class_name_LINKAGE_ ) \ 185 class class_name_LINKAGE_; \ 186 struct class_name_LINKAGE_##Ptr; 189 #define DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE2(class_name, base_name, class_name_LINKAGE_ ) \ 191 struct class_name_LINKAGE_##Ptr : public base_name##Ptr \ 193 typedef class_name value_type; \ 194 inline class_name##Ptr() : base_name##Ptr(static_cast<base_name*>(NULL)) { } \ 195 inline explicit class_name##Ptr(class_name* p) : base_name##Ptr( static_cast<base_name*>(p) ) { } \ 196 inline explicit class_name##Ptr(const base_name##Ptr & p) : base_name##Ptr(p) { if(!p.null()) ASSERTMSG_( p->GetRuntimeClass()->derivedFrom(#class_name),::mrpt::format("Wrong typecasting of smart pointers: %s -> %s",p->GetRuntimeClass()->className, #class_name) ) } \ 197 inline explicit class_name##Ptr(const mrpt::utils::CObjectPtr & p) : base_name##Ptr(p) { if(!p.null())ASSERTMSG_( p->GetRuntimeClass()->derivedFrom(#class_name),::mrpt::format("Wrong typecasting of smart pointers: %s -> %s",p->GetRuntimeClass()->className, #class_name) ) } \ 198 inline void setFromPointerDoNotFreeAtDtor(const class_name* p) { this->set(const_cast<mrpt::utils::CObject*>(static_cast<const mrpt::utils::CObject*>(p))); m_holder->increment(); } \ 200 inline class_name * pointer() { return dynamic_cast<class_name*>(base_name##Ptr::pointer()); } \ 201 inline class_name * get() { return this->pointer(); } \ 203 inline const class_name * pointer() const { return dynamic_cast<const class_name*>(base_name##Ptr::pointer()); } \ 204 inline const class_name * get() const { return this->pointer(); } \ 205 inline class_name* operator ->(void) { return dynamic_cast<class_name*>( base_name##Ptr::operator ->() ); } \ 206 inline const class_name* operator ->(void) const { return dynamic_cast<const class_name*>( base_name##Ptr::operator ->() ); } \ 207 inline class_name& operator *(void) { return *dynamic_cast<class_name*>( base_name##Ptr::operator ->() ); } \ 208 inline const class_name& operator *(void) const { return *dynamic_cast<const class_name*>( base_name##Ptr::operator ->() ); } \ 213 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE(class_name,_LINKAGE_) DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE2(class_name, _LINKAGE_ class_name) 214 #define DEFINE_MRPT_OBJECT_POST_CUSTOM_LINKAGE(class_name,_LINKAGE_) DEFINE_MRPT_OBJECT_POST_CUSTOM_LINKAGE2(class_name, _LINKAGE_ class_name) 217 #define DEFINE_MRPT_OBJECT_PRE_NO_LINKAGE(class_name) DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE2(class_name, class_name) 218 #define DEFINE_MRPT_OBJECT_POST_NO_LINKAGE(class_name) DEFINE_MRPT_OBJECT_POST_CUSTOM_LINKAGE2(class_name, class_name) 222 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE2(class_name,class_name_LINKAGE_) \ 223 struct class_name_LINKAGE_##Ptr; 226 #define DEFINE_MRPT_OBJECT_POST_CUSTOM_LINKAGE2(class_name,class_name_LINKAGE_) \ 228 struct class_name_LINKAGE_##Ptr : public mrpt::utils::CObjectPtr \ 230 inline class_name##Ptr() : mrpt::utils::CObjectPtr(static_cast<mrpt::utils::CObject*>(NULL)) { } \ 231 inline explicit class_name##Ptr(class_name* p) : mrpt::utils::CObjectPtr( static_cast<mrpt::utils::CObject*>(p) ) { } \ 232 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) ) } \ 233 inline void setFromPointerDoNotFreeAtDtor(const class_name* p) { this->set(const_cast<mrpt::utils::CObject*>(static_cast<const mrpt::utils::CObject*>(p))); m_holder->increment(); } \ 235 inline class_name * pointer() { return dynamic_cast<class_name*>(mrpt::utils::CObjectPtr::pointer()); } \ 237 inline const class_name * pointer() const { return dynamic_cast<const class_name*>(mrpt::utils::CObjectPtr::pointer()); } \ 238 inline class_name* operator ->(void) { return dynamic_cast<class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \ 239 inline const class_name* operator ->(void) const { return dynamic_cast<const class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \ 240 inline class_name& operator *(void) { return *dynamic_cast<class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \ 241 inline const class_name& operator *(void) const { return *dynamic_cast<const class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \ 246 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE(class_name, base_name) \ 247 DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, BASE_IMPEXP ) 251 #define DEFINE_MRPT_OBJECT_PRE(class_name) DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE(class_name, BASE_IMPEXP ) // This macro is valid for classes within mrpt-base only. 252 #define DEFINE_MRPT_OBJECT_POST(class_name) DEFINE_MRPT_OBJECT_POST_CUSTOM_LINKAGE(class_name, BASE_IMPEXP ) // This macro is valid for classes within mrpt-base only. 256 #define IMPLEMENTS_MRPT_OBJECT(class_name, base,NameSpace) \ 257 mrpt::utils::CObject* NameSpace::class_name::CreateObject() \ 258 { return static_cast<mrpt::utils::CObject*>( new NameSpace::class_name ); } \ 259 NameSpace::class_name##Ptr NameSpace::class_name::Create() \ 260 { return NameSpace::class_name##Ptr( new NameSpace::class_name ); } \ 261 const mrpt::utils::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \ 262 { return CLASS_ID(base); } \ 263 mrpt::utils::TRuntimeClassId NameSpace::class_name::class##class_name = { \ 264 #class_name, NameSpace::class_name::CreateObject, &class_name::_GetBaseClass }; \ 265 const mrpt::utils::TRuntimeClassId *NameSpace::class_name::classinfo = & NameSpace::class_name::class##class_name; \ 266 const mrpt::utils::TRuntimeClassId* NameSpace::class_name::GetRuntimeClass() const \ 267 { return CLASS_ID_NAMESPACE(class_name,NameSpace); } \ 268 mrpt::utils::CLASSINIT NameSpace::class_name::_init_##class_name(CLASS_ID(base)); \ 269 mrpt::utils::CObject * NameSpace::class_name::duplicate() const \ 270 { return static_cast<mrpt::utils::CObject*>( new NameSpace::class_name(*this) ); } 275 #define DEFINE_VIRTUAL_MRPT_OBJECT(class_name) \ 279 static const mrpt::utils::TRuntimeClassId* _GetBaseClass(); \ 281 static const mrpt::utils::TRuntimeClassId class##class_name; \ 282 virtual const mrpt::utils::TRuntimeClassId* GetRuntimeClass() const MRPT_OVERRIDE; \ 283 friend class mrpt::utils::CStream; \ 284 typedef class_name##Ptr Ptr; \ 285 typedef class_name##Ptr ConstPtr; \ 291 #define IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name,NameSpace) \ 292 const mrpt::utils::TRuntimeClassId* class_name::_GetBaseClass() \ 293 { return CLASS_ID(base_class_name); } \ 294 const mrpt::utils::TRuntimeClassId class_name::class##class_name = { \ 295 #class_name, NULL, &class_name::_GetBaseClass }; \ 296 const mrpt::utils::TRuntimeClassId* class_name::GetRuntimeClass() const \ 297 { return CLASS_ID(class_name); } 312 template <
typename CAST_TO>
315 template <
typename CAST_FROM_PTR>
316 static typename CAST_TO::Ptr
from(
const CAST_FROM_PTR &ptr)
318 return typename CAST_TO::Ptr(ptr);
329 template <
typename T,
typename C,
typename COUNTER>
330 inline bool operator == (
const stlplus::smart_ptr_base<T,C,COUNTER>&
a,
const stlplus::smart_ptr_base<T,C,COUNTER>&
b) {
335 template <
typename T,
typename C,
typename COUNTER>
336 inline bool operator != (
const stlplus::smart_ptr_base<T,C,COUNTER>&
a,
const stlplus::smart_ptr_base<T,C,COUNTER>&
b) {
337 return !
a.aliases(
b);
Auxiliary structure used for CObject-based RTTI.
void BASE_IMPEXP registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
safe_ptr< TRuntimeClassId > TRuntimeClassIdPtr
A wrapper class for a "TRuntimeClassId *", well-defined with respect to copy operators and constructo...
std::vector< const mrpt::utils::TRuntimeClassId * > BASE_IMPEXP getAllRegisteredClasses()
Returns a list with all the classes registered in the system through mrpt::utils::registerClass.
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const
Returns information about the class of an object in runtime.
Converts a smart pointer Base::Ptr to Derived::Ptr, in a way compatible with MRPT >=1...
void BASE_IMPEXP registerClassCustomName(const char *customName, const TRuntimeClassId *pNewClass)
Mostly for internal use within mrpt sources, to handle exceptional cases with multiple serialization ...
A smart pointer to a CObject object.
CObjectPtr(const CObject &data)
mrpt::utils::CObjectPtr duplicateGetSmartPtr() const
Returns a copy of the object, indepently of its class, as a smart pointer (the newly created object w...
const TRuntimeClassId BASE_IMPEXP * findRegisteredClass(const std::string &className)
Return info about a given class by its name, or NULL if the class is not registered.
GLsizei const GLchar ** string
static const mrpt::utils::TRuntimeClassId classCObject
#define CLASS_ID(class_name)
Access to runtime class ID for a defined class name.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
static CAST_TO::Ptr from(const CAST_FROM_PTR &ptr)
CLASSINIT(const mrpt::utils::TRuntimeClassId *pNewClass)
void BASE_IMPEXP registerClass(const mrpt::utils::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CSerializable" descendents.
A structure that holds runtime class type information.
The virtual base class of all MRPT classes with a unified RTTI system.
CObjectPtr(CObject *data)
CObjectPtr & operator=(const CObjectPtr &r)
CObject * clone() const
Cloning interface for smart pointers.
stlplus::smart_ptr_clone< CObject > BASE
GLsizei GLsizei GLenum GLenum const GLvoid * data
CObjectPtr & operator=(const CObject &data)
GLubyte GLubyte GLubyte a
std::vector< const TRuntimeClassId * > BASE_IMPEXP getAllRegisteredClassesChildrenOf(const TRuntimeClassId *parent_id)
Like getAllRegisteredClasses(), but filters the list to only include children clases of a given base ...
A wrapper class for pointers that can be safely copied with "=" operator without problems.