10 #define MRPT_COBJECT_H 43 bool derivedFrom(
const char* pBaseClass_name)
const;
80 return &T::GetRuntimeClassIdStatic();
85 #define CLASS_ID(T) mrpt::utils::CLASS_ID_impl<T>() 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>() 94 template <
typename PTR>
97 return p->GetRuntimeClass() == CLASS_ID_impl<T>();
103 #define IS_CLASS(ptrObj, class_name) \ 104 mrpt::utils::IS_CLASS_impl<class_name>::check(ptrObj) 109 #define IS_DERIVED(ptrObj, class_name) \ 110 ((ptrObj)->GetRuntimeClass()->derivedFrom(CLASS_ID(class_name))) 154 using Ptr = std::shared_ptr<CObject>;
186 #define DEFINE_MRPT_OBJECT(class_name) \ 190 static const mrpt::utils::TRuntimeClassId* _GetBaseClass(); \ 191 static mrpt::utils::CLASSINIT _init_##class_name; \ 192 static const mrpt::utils::TRuntimeClassId runtimeClassId; \ 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() \ 202 static mrpt::utils::CObject* CreateObject(); \ 203 virtual mrpt::utils::CObject* clone() const override; \ 204 template <typename... Args> \ 205 static Ptr Create(Args&&... args) \ 207 return mrpt::make_aligned_shared<class_name>( \ 208 std::forward<Args>(args)...); \ 212 MRPT_MAKE_ALIGNED_OPERATOR_NEW 216 #define IMPLEMENTS_MRPT_OBJECT(class_name, base, NameSpace) \ 217 mrpt::utils::CObject* NameSpace::class_name::CreateObject() \ 219 return static_cast<mrpt::utils::CObject*>(new NameSpace::class_name); \ 221 const mrpt::utils::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \ 223 return CLASS_ID(base); \ 225 const mrpt::utils::TRuntimeClassId& \ 226 NameSpace::class_name::GetRuntimeClassIdStatic() \ 228 return NameSpace::class_name::runtimeClassId; \ 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 \ 236 return CLASS_ID_NAMESPACE(class_name, NameSpace); \ 238 mrpt::utils::CLASSINIT NameSpace::class_name::_init_##class_name( \ 240 mrpt::utils::CObject* NameSpace::class_name::clone() const \ 242 return static_cast<mrpt::utils::CObject*>( \ 243 new NameSpace::class_name(*this)); \ 249 #define DEFINE_VIRTUAL_MRPT_OBJECT(class_name) \ 253 static const mrpt::utils::TRuntimeClassId* _GetBaseClass(); \ 254 static const mrpt::utils::TRuntimeClassId runtimeClassId; \ 257 using Ptr = std::shared_ptr<class_name>; \ 258 using ConstPtr = std::shared_ptr<const class_name>; \ 259 virtual const mrpt::utils::TRuntimeClassId* GetRuntimeClass() \ 261 static const mrpt::utils::TRuntimeClassId& GetRuntimeClassIdStatic(); \ 262 friend class mrpt::utils::CStream; \ 268 #define IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name, NameSpace) \ 269 const mrpt::utils::TRuntimeClassId* class_name::_GetBaseClass() \ 271 return CLASS_ID(base_class_name); \ 273 const mrpt::utils::TRuntimeClassId class_name::runtimeClassId = { \ 274 #class_name, nullptr, &class_name::_GetBaseClass}; \ 275 const mrpt::utils::TRuntimeClassId* class_name::GetRuntimeClass() const \ 277 return CLASS_ID(class_name); \ 279 const mrpt::utils::TRuntimeClassId& \ 280 NameSpace::class_name::GetRuntimeClassIdStatic() \ 282 return NameSpace::class_name::runtimeClassId; \ 300 template <
typename CAST_TO>
303 template <
typename CAST_FROM_PTR>
304 static typename CAST_TO::Ptr
from(
const CAST_FROM_PTR& ptr)
306 return std::dynamic_pointer_cast<CAST_TO>(ptr);
Auxiliary structure used for CObject-based RTTI.
constexpr const mrpt::utils::TRuntimeClassId * CLASS_ID_impl()
void registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
static const mrpt::utils::TRuntimeClassId runtimeClassId
virtual CObject * clone() const =0
Returns a deep copy (clone) of the object, indepently of its class.
bool derivedFrom(const TRuntimeClassId *pBaseClass) const
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const
Returns information about the class of an object in runtime.
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...
std::shared_ptr< CObject > Ptr
mrpt::utils::CObject * createObject() const
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
static const mrpt::utils::TRuntimeClassId & GetRuntimeClassIdStatic()
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...
GLsizei const GLchar ** string
std::shared_ptr< const CObject > ConstPtr
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)
This is useful for checking ::Ptr types.
void registerClass(const mrpt::utils::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CSerializable" descendents.
CLASSINIT(const mrpt::utils::TRuntimeClassId *pNewClass)
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.
The virtual base class of all MRPT classes with a unified RTTI system.
static mrpt::utils::TRuntimeClassId * _GetBaseClass()
static bool check(const PTR p)
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.