10 #define MRPT_COBJECT_H 47 bool derivedFrom(
const char* pBaseClass_name)
const;
82 #define CLASS_ID(class_name) static_cast<const mrpt::utils::TRuntimeClassId*>(&class_name::class##class_name) 86 #define CLASS_ID_NAMESPACE(class_name,namespaceName) static_cast<const mrpt::utils::TRuntimeClassId*>(&namespaceName::class_name::class##class_name) 90 #define CLASS_ID_TEMPLATE(class_name,T) static_cast<const mrpt::utils::TRuntimeClassId*>(& template <class T> class_name<T>::class##class_name) 93 #define IS_CLASS( ptrObj, class_name ) ((ptrObj)->GetRuntimeClass()==CLASS_ID(class_name)) 96 #define IS_DERIVED( ptrObj, class_name ) ((ptrObj)->GetRuntimeClass()->derivedFrom(CLASS_ID(class_name))) 127 virtual CObject *duplicate()
const = 0;
145 virtual CObject *
get() {
return m_ptr.get(); }
147 virtual const CObject *
get()
const {
return m_ptr.get(); }
148 virtual CObject* operator ->(
void) {
return m_ptr.get(); }
149 virtual const CObject* operator ->(
void)
const {
return m_ptr.get(); }
154 operator bool()
const {
return m_ptr.operator bool(); }
155 void make_unique() {
if (!m_ptr)
return; m_ptr.reset(m_ptr.get()->clone()); }
156 bool present()
const {
return m_ptr.get()!=NULL; }
166 #define DEFINE_MRPT_OBJECT_CUSTOM_LINKAGE(class_name, _STATIC_LINKAGE_, _VIRTUAL_LINKAGE_) \ 170 _STATIC_LINKAGE_ const mrpt::utils::TRuntimeClassId* _GetBaseClass(); \ 171 _STATIC_LINKAGE_ mrpt::utils::CLASSINIT _init_##class_name;\ 174 typedef class_name##Ptr Ptr; \ 175 typedef class_name##Ptr ConstPtr; \ 176 _STATIC_LINKAGE_ mrpt::utils::TRuntimeClassId class##class_name; \ 177 _STATIC_LINKAGE_ const mrpt::utils::TRuntimeClassId *classinfo; \ 178 _VIRTUAL_LINKAGE_ const mrpt::utils::TRuntimeClassId* GetRuntimeClass() const MRPT_OVERRIDE; \ 179 _STATIC_LINKAGE_ mrpt::utils::CObject* CreateObject(); \ 180 _STATIC_LINKAGE_ class_name##Ptr Create(); \ 181 _VIRTUAL_LINKAGE_ mrpt::utils::CObject *duplicate() const MRPT_OVERRIDE; \ 184 MRPT_MAKE_ALIGNED_OPERATOR_NEW \ 187 #define DEFINE_MRPT_OBJECT(class_name) \ 188 DEFINE_MRPT_OBJECT_CUSTOM_LINKAGE(class_name, static , virtual ) 191 #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) 192 #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) 195 #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) 196 #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) 199 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE2(class_name, base_name, class_name_LINKAGE_ ) \ 200 class class_name_LINKAGE_; \ 201 struct class_name_LINKAGE_##Ptr; 204 #define DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE2(class_name, base_name, class_name_LINKAGE_ ) \ 206 struct class_name_LINKAGE_##Ptr : public base_name##Ptr \ 208 typedef class_name value_type; \ 209 class_name##Ptr() { } \ 210 explicit class_name##Ptr(class_name* p) : base_name##Ptr(p) { } \ 211 class_name##Ptr(const mrpt::utils::CObjectPtr &p) { m_ptr = p.m_ptr; } \ 213 class_name * pointer() { return dynamic_cast<class_name*>(m_ptr.get()); } \ 214 class_name * get() { return dynamic_cast<class_name*>(m_ptr.get()); } \ 216 const class_name * pointer() const { return dynamic_cast<const class_name*>(m_ptr.get()); } \ 217 const class_name * get() const { return dynamic_cast<const class_name*>(m_ptr.get()); } \ 218 class_name* operator ->(void) { return dynamic_cast<class_name*>(m_ptr.get()); } \ 219 const class_name* operator ->(void) const { return dynamic_cast<const class_name*>(m_ptr.get()); } \ 220 class_name& operator *(void) { ASSERT_(m_ptr); return *dynamic_cast<class_name*>(m_ptr.get()); } \ 221 const class_name& operator *(void) const { ASSERT_(m_ptr); return *dynamic_cast<const class_name*>(m_ptr.get()); } \ 226 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE(class_name,_LINKAGE_) DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE2(class_name, _LINKAGE_ class_name) 227 #define DEFINE_MRPT_OBJECT_POST_CUSTOM_LINKAGE(class_name,_LINKAGE_) DEFINE_MRPT_OBJECT_POST_CUSTOM_LINKAGE2(class_name, _LINKAGE_ class_name) 230 #define DEFINE_MRPT_OBJECT_PRE_NO_LINKAGE(class_name) DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE2(class_name, class_name) 231 #define DEFINE_MRPT_OBJECT_POST_NO_LINKAGE(class_name) DEFINE_MRPT_OBJECT_POST_CUSTOM_LINKAGE2(class_name, class_name) 235 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_LINKAGE2(class_name,class_name_LINKAGE_) \ 236 struct class_name_LINKAGE_##Ptr; 239 #define DEFINE_MRPT_OBJECT_POST_CUSTOM_LINKAGE2(class_name,class_name_LINKAGE_) \ 241 struct class_name_LINKAGE_##Ptr : public mrpt::utils::CObjectPtr \ 243 inline class_name##Ptr() : mrpt::utils::CObjectPtr(static_cast<mrpt::utils::CObject*>(NULL)) { } \ 244 inline explicit class_name##Ptr(class_name* p) : mrpt::utils::CObjectPtr( static_cast<mrpt::utils::CObject*>(p) ) { } \ 245 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) ) } \ 247 inline class_name * pointer() { return dynamic_cast<class_name*>(mrpt::utils::CObjectPtr::pointer()); } \ 249 inline const class_name * pointer() const { return dynamic_cast<const class_name*>(mrpt::utils::CObjectPtr::pointer()); } \ 250 inline class_name* operator ->(void) { return dynamic_cast<class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \ 251 inline const class_name* operator ->(void) const { return dynamic_cast<const class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \ 252 inline class_name& operator *(void) { return *dynamic_cast<class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \ 253 inline const class_name& operator *(void) const { return *dynamic_cast<const class_name*>( mrpt::utils::CObjectPtr::operator ->() ); } \ 258 #define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE(class_name, base_name) \ 259 DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, BASE_IMPEXP ) 263 #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. 264 #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. 268 #define IMPLEMENTS_MRPT_OBJECT(class_name, base,NameSpace) \ 269 mrpt::utils::CObject* NameSpace::class_name::CreateObject() \ 270 { return static_cast<mrpt::utils::CObject*>( new NameSpace::class_name ); } \ 271 NameSpace::class_name##Ptr NameSpace::class_name::Create() \ 272 { return NameSpace::class_name##Ptr( new NameSpace::class_name ); } \ 273 const mrpt::utils::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \ 274 { return CLASS_ID(base); } \ 275 mrpt::utils::TRuntimeClassId NameSpace::class_name::class##class_name = { \ 276 #class_name, NameSpace::class_name::CreateObject, &class_name::_GetBaseClass }; \ 277 const mrpt::utils::TRuntimeClassId *NameSpace::class_name::classinfo = & NameSpace::class_name::class##class_name; \ 278 const mrpt::utils::TRuntimeClassId* NameSpace::class_name::GetRuntimeClass() const \ 279 { return CLASS_ID_NAMESPACE(class_name,NameSpace); } \ 280 mrpt::utils::CLASSINIT NameSpace::class_name::_init_##class_name(CLASS_ID(base)); \ 281 mrpt::utils::CObject * NameSpace::class_name::duplicate() const \ 282 { return static_cast<mrpt::utils::CObject*>( new NameSpace::class_name(*this) ); } 287 #define DEFINE_VIRTUAL_MRPT_OBJECT(class_name) \ 291 static const mrpt::utils::TRuntimeClassId* _GetBaseClass(); \ 293 static const mrpt::utils::TRuntimeClassId class##class_name; \ 294 virtual const mrpt::utils::TRuntimeClassId* GetRuntimeClass() const MRPT_OVERRIDE; \ 295 friend class mrpt::utils::CStream; \ 296 typedef class_name##Ptr Ptr; \ 297 typedef class_name##Ptr ConstPtr; \ 303 #define IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name,NameSpace) \ 304 const mrpt::utils::TRuntimeClassId* class_name::_GetBaseClass() \ 305 { return CLASS_ID(base_class_name); } \ 306 const mrpt::utils::TRuntimeClassId class_name::class##class_name = { \ 307 #class_name, NULL, &class_name::_GetBaseClass }; \ 308 const mrpt::utils::TRuntimeClassId* class_name::GetRuntimeClass() const \ 309 { return CLASS_ID(class_name); } 324 template <
typename CAST_TO>
327 template <
typename CAST_FROM_PTR>
328 static typename CAST_TO::Ptr
from(
const CAST_FROM_PTR &ptr)
330 return typename CAST_TO::Ptr(ptr);
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 ...
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.
void reset(CObject *p=NULL)
GLsizei const GLchar ** string
mrpt::utils::CObjectPtr duplicateGetSmartPtr() const
Returns a copy of the object, indepently of its class, as a smart pointer (the newly created object w...
std::shared_ptr< CObject > m_ptr
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.
virtual const CObject * pointer() const
Eigen::MatrixBase< Derived >::PlainObject operator!(const Eigen::MatrixBase< Derived > &m)
Unary inversion operator.
virtual CObject * pointer()
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.
struct BASE_IMPEXP CObjectPtr
A smart pointer to a CObject object.
CObject * clone() const
Cloning interface for smart pointers.
GLsizei GLsizei GLenum GLenum const GLvoid * data
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 ...
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
A wrapper class for pointers that can be safely copied with "=" operator without problems.
CObjectPtr(CObject *data)