Main MRPT website > C++ reference for MRPT 1.9.9
CObject.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/utils/CObject.h>
14 #include <mrpt/system/os.h>
15 
17 
18 using namespace mrpt;
19 using namespace mrpt::utils;
20 using namespace mrpt::system;
21 
22 #include <cstdio>
23 
25 {
27 }
28 
29 /*---------------------------------------------------------------
30  TRuntimeClassId::derivedFrom
31  ---------------------------------------------------------------*/
32 bool TRuntimeClassId::derivedFrom(const TRuntimeClassId* pBaseClass) const
33 {
34  ASSERT_(pBaseClass != nullptr)
35 
36  // The same class??
37  if (pBaseClass == this) return true;
38 
39  // Automatically register all pending classes, just in case:
41 
42  // Check heritage:
43  const TRuntimeClassId* pClassThis = this;
44  while (pClassThis != nullptr)
45  {
46  if (pClassThis == pBaseClass) return true;
47 
48  if (pClassThis->getBaseClass)
49  pClassThis = (*pClassThis->getBaseClass)();
50  else
51  pClassThis = nullptr; // The root class
52  }
53 
54  // Parent class not found
55  return false;
56 }
57 
58 /*---------------------------------------------------------------
59  TRuntimeClassId::derivedFrom
60  ---------------------------------------------------------------*/
61 bool TRuntimeClassId::derivedFrom(const char* pBaseClass_name) const
62 {
63  // Automatically register all pending classes, just in case:
65 
66  const TRuntimeClassId* pBaseClass = findRegisteredClass(pBaseClass_name);
67  ASSERTMSG_(
68  pBaseClass != nullptr,
69  format("Class %s not registered??", pBaseClass_name))
70 
71  // The same class??
72  if (pBaseClass == this) return true;
73 
74  // Check heritage:
75  const TRuntimeClassId* pClassThis = this;
76  while (pClassThis != nullptr)
77  {
78  if (pClassThis == pBaseClass) return true;
79 
80  if (pClassThis->getBaseClass)
81  pClassThis = (*pClassThis->getBaseClass)();
82  else
83  pClassThis = nullptr; // The root class
84  }
85 
86  // Parent class not found
87  return false;
88 }
89 
90 /*---------------------------------------------------------------
91  TRuntimeClassId::createObject
92  ---------------------------------------------------------------*/
94 {
95  if (!ptrCreateObject)
96  {
97  perror(
98  "[TRuntimeClassId::createObject] Trying to create an object with "
99  "not dynamic constructor\n");
100  return nullptr;
101  }
102 
103  try
104  {
105  CObject* ret = (*ptrCreateObject)();
106  return ret;
107  }
108  catch (std::bad_alloc&)
109  {
110  throw;
111  }
112 }
113 
114 /* -----------------------------------------------------------------------
115  For class CObject, special methods must be defined
116  since it has no base class. These methods are defined
117  automatically for derived classes.
118  ----------------------------------------------------------------------- */
120 const struct TRuntimeClassId CObject::runtimeClassId = {"CObject", nullptr,
121  NULL};
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
void registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
static const mrpt::utils::TRuntimeClassId runtimeClassId
Definition: CObject.h:151
bool derivedFrom(const TRuntimeClassId *pBaseClass) const
Definition: CObject.cpp:32
const TRuntimeClassId *(* getBaseClass)()
Gets the base class runtime id.
Definition: CObject.h:38
mrpt::utils::CObject * createObject() const
Definition: CObject.cpp:93
static const mrpt::utils::TRuntimeClassId & GetRuntimeClassIdStatic()
Definition: CObject.cpp:24
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
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.
#define ASSERT_(f)
A structure that holds runtime class type information.
Definition: CObject.h:31
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:147
#define ASSERTMSG_(f, __ERROR_MSG)
static mrpt::utils::TRuntimeClassId * _GetBaseClass()
Definition: CObject.cpp:119



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019