MRPT  2.0.4
CObject.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "rtti-precomp.h" // Precompiled headers
11 
12 #include <mrpt/rtti/CObject.h>
13 #include <cstdio>
15 
16 using namespace mrpt;
17 using namespace mrpt::rtti;
18 
20 {
22 }
23 
24 bool TRuntimeClassId::derivedFrom(const TRuntimeClassId* pBaseClass) const
25 {
26  ASSERT_(pBaseClass != nullptr);
27 
28  // The same class??
29  if (pBaseClass == this) return true;
30 
31  // Automatically register all pending classes, just in case:
33 
34  // Check heritage:
35  const TRuntimeClassId* pClassThis = this;
36  while (pClassThis != nullptr)
37  {
38  if (pClassThis == pBaseClass) return true;
39 
40  if (pClassThis->getBaseClass)
41  pClassThis = (*pClassThis->getBaseClass)();
42  else
43  pClassThis = nullptr; // The root class
44  }
45 
46  // Parent class not found
47  return false;
48 }
49 
50 bool TRuntimeClassId::derivedFrom(const char* pBaseClass_name) const
51 {
52  // Automatically register all pending classes, just in case:
54 
55  const TRuntimeClassId* pBaseClass = findRegisteredClass(pBaseClass_name);
56  ASSERTMSG_(
57  pBaseClass != nullptr,
58  format("Class %s not registered??", pBaseClass_name));
59 
60  // The same class??
61  if (pBaseClass == this) return true;
62 
63  // Check heritage:
64  const TRuntimeClassId* pClassThis = this;
65  while (pClassThis != nullptr)
66  {
67  if (pClassThis == pBaseClass) return true;
68 
69  if (pClassThis->getBaseClass)
70  pClassThis = (*pClassThis->getBaseClass)();
71  else
72  pClassThis = nullptr; // The root class
73  }
74 
75  // Parent class not found
76  return false;
77 }
78 
80 {
81  if (!ptrCreateObject)
82  {
83  fprintf(
84  stderr,
85  "[TRuntimeClassId::createObject] Trying to create an object "
86  "without dynamic constructor. classname=`%s`\n",
87  className != nullptr ? className : "nullptr");
88  return CObject::Ptr();
89  }
90 
91  try
92  {
93  return this->ptrCreateObject();
94  }
95  catch (std::bad_alloc&)
96  {
97  throw;
98  }
99 }
100 
101 // For class CObject, special methods must be defined
102 // since it has no base class. These methods are defined
103 // automatically for derived classes.
105 const struct TRuntimeClassId CObject::runtimeClassId = {"CObject", nullptr,
106  nullptr};
107 
109 {
110  auto pR = findRegisteredClass(className);
111  if (!pR) return nullptr;
112  return pR->createObject();
113 }
std::shared_ptr< CObject > createObject() const
Definition: CObject.cpp:79
void registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
A structure that holds runtime class type information.
Definition: CObject.h:31
const TRuntimeClassId *(* getBaseClass)()
Gets the base class runtime id.
Definition: CObject.h:38
Virtual base to provide a compiler-independent RTTI system.
Definition: CObject.h:177
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
const char * className
Definition: CObject.h:34
std::function< std::shared_ptr< CObject >void)> ptrCreateObject
Create an object of the related class, or nullptr if it is virtual.
Definition: CObject.h:36
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.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:419
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
static const mrpt::rtti::TRuntimeClassId & GetRuntimeClassIdStatic()
Definition: CObject.cpp:19
static mrpt::rtti::TRuntimeClassId * _GetBaseClass()
Definition: CObject.cpp:104
mrpt::rtti::CObject::Ptr classFactory(const std::string &className)
Creates an object given by its registered name.
Definition: CObject.cpp:108
bool derivedFrom(const TRuntimeClassId *pBaseClass) const
Definition: CObject.cpp:24
static const mrpt::rtti::TRuntimeClassId runtimeClassId
Definition: CObject.h:181
std::shared_ptr< CObject > Ptr
Definition: CObject.h:184



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020