Main MRPT website > C++ reference for MRPT 1.5.7
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 
24 /*---------------------------------------------------------------
25  TRuntimeClassId::derivedFrom
26  ---------------------------------------------------------------*/
27 bool TRuntimeClassId::derivedFrom(const TRuntimeClassId* pBaseClass) const
28 {
29  ASSERT_(this != NULL)
30  ASSERT_(pBaseClass != NULL)
31 
32  // The same class??
33  if (pBaseClass==this)
34  return true;
35 
36  // Automatically register all pending classes, just in case:
38 
39  // Check heritage:
40  const TRuntimeClassId* pClassThis = this;
41  while (pClassThis != NULL)
42  {
43  if (pClassThis == pBaseClass) return true;
44 
45  if (pClassThis->getBaseClass) pClassThis = (*pClassThis->getBaseClass)();
46  else pClassThis = NULL; // The root class
47  }
48 
49  // Parent class not found
50  return false;
51 }
52 
53 /*---------------------------------------------------------------
54  TRuntimeClassId::derivedFrom
55  ---------------------------------------------------------------*/
56 bool TRuntimeClassId::derivedFrom(const char* pBaseClass_name) const
57 {
58  ASSERT_(this != NULL)
59 
60  // Automatically register all pending classes, just in case:
62 
63  const TRuntimeClassId* pBaseClass = findRegisteredClass(pBaseClass_name);
64  ASSERTMSG_(pBaseClass != NULL, format("Class %s not registered??",pBaseClass_name) )
65 
66  // The same class??
67  if (pBaseClass==this)
68  return true;
69 
70  // Check heritage:
71  const TRuntimeClassId* pClassThis = this;
72  while (pClassThis != NULL)
73  {
74  if (pClassThis == pBaseClass) return true;
75 
76  if (pClassThis->getBaseClass) pClassThis = (*pClassThis->getBaseClass)();
77  else pClassThis = NULL; // The root class
78  }
79 
80  // Parent class not found
81  return false;
82 }
83 
84 
85 /*---------------------------------------------------------------
86  TRuntimeClassId::createObject
87  ---------------------------------------------------------------*/
89 {
90  if (! ptrCreateObject )
91  {
92  perror("[TRuntimeClassId::createObject] Trying to create an object with not dynamic constructor\n");
93  return NULL;
94  }
95 
96  try
97  {
98  CObject* ret = (*ptrCreateObject)();
99  return ret;
100  }
101  catch (std::bad_alloc &)
102  {
103  throw;
104  }
105 }
106 
107 
108 /* -----------------------------------------------------------------------
109  For class CObject, special methods must be defined
110  since it has no base class. These methods are defined
111  automatically for derived classes.
112  ----------------------------------------------------------------------- */
114 {
115  return NULL;
116 }
117 
118 const struct TRuntimeClassId CObject::classCObject =
119 {
120  "CObject",
121  NULL,
122  NULL
123 };
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
void BASE_IMPEXP registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
bool derivedFrom(const TRuntimeClassId *pBaseClass) const
Definition: CObject.cpp:27
const TRuntimeClassId *(* getBaseClass)()
Gets the base class runtime id.
Definition: CObject.h:42
mrpt::utils::CObject * createObject() const
Definition: CObject.cpp:88
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.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
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:36
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:113
#define ASSERTMSG_(f, __ERROR_MSG)
static mrpt::utils::TRuntimeClassId * _GetBaseClass()
Definition: CObject.cpp:113



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019