43 bool derivedFrom(
const char* pBaseClass_name)
const;
88 const std::string& className,
const bool allow_ignore_namespace =
true);
95 return &T::GetRuntimeClassIdStatic();
102 #define CLASS_ID(T) mrpt::rtti::CLASS_ID_impl<T>::get() 104 #define CLASS_ID_TEMPLATE(class_name, T) mrpt::rtti::CLASS_ID_impl<T>::get() 105 #define CLASS_ID_NAMESPACE(class_name, namespaceName) \ 106 mrpt::rtti::CLASS_ID_impl<namespaceName::class_name>::get() 108 template <
typename T>
111 template <
typename REF>
120 template <
bool is_copy_ctrtible>
125 template <
typename T>
134 template <
typename T>
137 throw std::runtime_error(
138 "clone(): Attempt to call copy ctor of non copy-constructible " 146 #define IS_CLASS(obj, class_name) \ 147 mrpt::rtti::IS_CLASS_impl<class_name>::check(obj) 151 #define IS_DERIVED(obj, class_name) \ 152 ((obj).GetRuntimeClass()->derivedFrom(CLASS_ID(class_name))) 211 #define DEFINE_MRPT_OBJECT(class_name, NameSpace) \ 215 static const mrpt::rtti::TRuntimeClassId* _GetBaseClass(); \ 216 static const mrpt::rtti::TRuntimeClassId runtimeClassId; \ 220 using Ptr = std::shared_ptr<NameSpace::class_name>; \ 221 using ConstPtr = std::shared_ptr<const NameSpace::class_name>; \ 222 using UniquePtr = std::unique_ptr<NameSpace::class_name>; \ 223 using ConstUniquePtr = std::unique_ptr<const NameSpace::class_name>; \ 224 static constexpr const char* className = #NameSpace "::" #class_name; \ 225 static constexpr auto getClassName() \ 227 return mrpt::typemeta::literal(#NameSpace "::" #class_name); \ 229 static const mrpt::rtti::TRuntimeClassId& GetRuntimeClassIdStatic(); \ 230 virtual const mrpt::rtti::TRuntimeClassId* GetRuntimeClass() \ 232 virtual mrpt::rtti::CObject* clone() const override; \ 233 static std::shared_ptr<CObject> CreateObject(); \ 234 template <typename... Args> \ 235 static Ptr Create(Args&&... args) \ 237 return std::make_shared<class_name>(std::forward<Args>(args)...); \ 239 template <typename Alloc, typename... Args> \ 240 static Ptr CreateAlloc(const Alloc& alloc, Args&&... args) \ 242 return std::allocate_shared<class_name>( \ 243 alloc, std::forward<Args>(args)...); \ 245 template <typename... Args> \ 246 static UniquePtr CreateUnique(Args&&... args) \ 248 return std::make_unique<class_name>(std::forward<Args>(args)...); \ 253 #define INTERNAL_IMPLEMENTS_MRPT_OBJECT( \ 254 class_name, base, NameSpace, class_registry_name) \ 255 mrpt::rtti::CObject::Ptr NameSpace::class_name::CreateObject() \ 257 return std::static_pointer_cast<CObject>( \ 258 std::make_shared<NameSpace::class_name>()); \ 260 const mrpt::rtti::TRuntimeClassId* NameSpace::class_name::_GetBaseClass() \ 262 return CLASS_ID(base); \ 264 const mrpt::rtti::TRuntimeClassId& \ 265 NameSpace::class_name::GetRuntimeClassIdStatic() \ 267 return NameSpace::class_name::runtimeClassId; \ 269 const mrpt::rtti::TRuntimeClassId NameSpace::class_name::runtimeClassId = \ 270 {class_registry_name, NameSpace::class_name::CreateObject, \ 271 &class_name::_GetBaseClass}; \ 272 const mrpt::rtti::TRuntimeClassId* \ 273 NameSpace::class_name::GetRuntimeClass() const \ 275 return CLASS_ID_NAMESPACE(class_name, NameSpace); \ 277 mrpt::rtti::CObject* NameSpace::class_name::clone() const \ 279 return mrpt::rtti::internal::CopyCtor<std::is_copy_constructible< \ 280 NameSpace::class_name>::value>::clone(*this); \ 286 #define IMPLEMENTS_MRPT_OBJECT(class_name, base, NameSpace) \ 287 INTERNAL_IMPLEMENTS_MRPT_OBJECT( \ 288 class_name, base, NameSpace, #NameSpace "::" #class_name) 293 #define DEFINE_VIRTUAL_MRPT_OBJECT(class_name) \ 297 static const mrpt::rtti::TRuntimeClassId* _GetBaseClass(); \ 298 static const mrpt::rtti::TRuntimeClassId runtimeClassId; \ 301 using Ptr = std::shared_ptr<class_name>; \ 302 using ConstPtr = std::shared_ptr<const class_name>; \ 303 virtual const mrpt::rtti::TRuntimeClassId* GetRuntimeClass() \ 305 static const mrpt::rtti::TRuntimeClassId& GetRuntimeClassIdStatic(); \ 311 #define INTERNAL_IMPLEMENTS_VIRTUAL_MRPT_OBJECT( \ 312 class_name, base_name, NS, registered_name) \ 313 const mrpt::rtti::TRuntimeClassId* NS::class_name::_GetBaseClass() \ 315 return CLASS_ID(base_name); \ 317 const mrpt::rtti::TRuntimeClassId NS::class_name::runtimeClassId = { \ 318 registered_name, nullptr, &NS::class_name::_GetBaseClass}; \ 319 const mrpt::rtti::TRuntimeClassId* NS::class_name::GetRuntimeClass() const \ 321 return CLASS_ID(class_name); \ 323 const mrpt::rtti::TRuntimeClassId& \ 324 NS::class_name::GetRuntimeClassIdStatic() \ 326 return NS::class_name::runtimeClassId; \ 329 #define IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base, NS) \ 330 INTERNAL_IMPLEMENTS_VIRTUAL_MRPT_OBJECT( \ 331 class_name, base, NS, #NS "::" #class_name) 352 template <
typename CAST_TO>
355 template <
typename CAST_FROM_PTR>
356 static typename CAST_TO::Ptr
from(
const CAST_FROM_PTR& ptr)
358 return std::dynamic_pointer_cast<CAST_TO>(ptr);
virtual ~CObject()=default
std::shared_ptr< CObject > createObject() const
void registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
std::unique_ptr< CObject > UniquePtr
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.
Converts a polymorphic smart pointer Base::Ptr to Derived::Ptr, in a way compatible with MRPT >=1...
Virtual base to provide a compiler-independent RTTI system.
std::unique_ptr< const CObject > ConstUniquePtr
static constexpr const mrpt::rtti::TRuntimeClassId * get()
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
virtual CObject * clone() const =0
Returns a deep copy (clone) of the object, indepently of its class.
static bool check(const REF &p)
static T * clone(const T &o)
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const
Returns information about the class of an object in runtime.
std::function< std::shared_ptr< CObject >void)> ptrCreateObject
Create an object of the related class, or nullptr if it is virtual.
const TRuntimeClassId * findRegisteredClass(const std::string &className, const bool allow_ignore_namespace=true)
Return info about a given class by its name, or nullptr if the class is not registered.
A wrapper class for pointers that can be safely copied with "=" operator without problems.
void registerClassCustomName(const char *customName, const TRuntimeClassId *pNewClass)
Mostly for internal use within mrpt sources, to handle exceptional cases with multiple serialization ...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
static const mrpt::rtti::TRuntimeClassId & GetRuntimeClassIdStatic()
static mrpt::rtti::TRuntimeClassId * _GetBaseClass()
static T * clone(const T &o)
mrpt::rtti::CObject::Ptr classFactory(const std::string &className)
Creates an object given by its registered name.
bool derivedFrom(const TRuntimeClassId *pBaseClass) const
static CAST_TO::Ptr from(const CAST_FROM_PTR &ptr)
void registerClass(const mrpt::rtti::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CObject" descendents.
static const mrpt::rtti::TRuntimeClassId runtimeClassId
std::vector< const mrpt::rtti::TRuntimeClassId * > getAllRegisteredClasses()
Returns a list with all the classes registered in the system through mrpt::rtti::registerClass.
std::shared_ptr< CObject > Ptr
mrpt::rtti::CObject::Ptr duplicateGetSmartPtr() const
Makes a deep copy of the object and returns a smart pointer to it.