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 
13 #include <mrpt/utils/CObject.h>
15 #include <mrpt/system/os.h>
16 
18 
19 using namespace mrpt;
20 using namespace mrpt::utils;
21 using namespace mrpt::system;
22 
23 #include <cstdio>
24 
25 /*---------------------------------------------------------------
26  TRuntimeClassId::derivedFrom
27  ---------------------------------------------------------------*/
28 bool TRuntimeClassId::derivedFrom(const TRuntimeClassId* pBaseClass) const
29 {
30  ASSERT_(this != NULL)
31  ASSERT_(pBaseClass != NULL)
32 
33  // The same class??
34  if (pBaseClass==this)
35  return true;
36 
37  // Automatically register all pending classes, just in case:
39 
40  // Check heritage:
41  const TRuntimeClassId* pClassThis = this;
42  while (pClassThis != NULL)
43  {
44  if (pClassThis == pBaseClass) return true;
45 
46  if (pClassThis->getBaseClass) pClassThis = (*pClassThis->getBaseClass)();
47  else pClassThis = NULL; // The root class
48  }
49 
50  // Parent class not found
51  return false;
52 }
53 
54 /*---------------------------------------------------------------
55  TRuntimeClassId::derivedFrom
56  ---------------------------------------------------------------*/
57 bool TRuntimeClassId::derivedFrom(const char* pBaseClass_name) const
58 {
59  ASSERT_(this != NULL)
60 
61  // Automatically register all pending classes, just in case:
63 
64  const TRuntimeClassId* pBaseClass = findRegisteredClass(pBaseClass_name);
65  ASSERTMSG_(pBaseClass != NULL, format("Class %s not registered??",pBaseClass_name) )
66 
67  // The same class??
68  if (pBaseClass==this)
69  return true;
70 
71  // Check heritage:
72  const TRuntimeClassId* pClassThis = this;
73  while (pClassThis != NULL)
74  {
75  if (pClassThis == pBaseClass) return true;
76 
77  if (pClassThis->getBaseClass) pClassThis = (*pClassThis->getBaseClass)();
78  else pClassThis = NULL; // The root class
79  }
80 
81  // Parent class not found
82  return false;
83 }
84 
85 
86 /*---------------------------------------------------------------
87  TRuntimeClassId::createObject
88  ---------------------------------------------------------------*/
90 {
91  if (! ptrCreateObject )
92  {
93  perror("[TRuntimeClassId::createObject] Trying to create an object with not dynamic constructor\n");
94  return NULL;
95  }
96 
97  try
98  {
99  CObject* ret = (*ptrCreateObject)();
100  return ret;
101  }
102  catch (std::bad_alloc &)
103  {
104  throw;
105  }
106 }
107 
108 
109 /* -----------------------------------------------------------------------
110  For class CObject, special methods must be defined
111  since it has no base class. These methods are defined
112  automatically for derived classes.
113  ----------------------------------------------------------------------- */
115 {
116  return NULL;
117 }
118 
119 const struct TRuntimeClassId CObject::classCObject =
120 {
121  "CObject",
122  NULL,
123  NULL
124 };
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...
mrpt::utils::CObject * createObject() const
Definition: CObject.cpp:89
const TRuntimeClassId *(* getBaseClass)()
Gets the base class runtime id.
Definition: CObject.h:52
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
bool derivedFrom(const TRuntimeClassId *pBaseClass) const
Definition: CObject.cpp:28
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:46
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:123
#define ASSERTMSG_(f, __ERROR_MSG)
static mrpt::utils::TRuntimeClassId * _GetBaseClass()
Definition: CObject.cpp:114



Page generated by Doxygen 1.8.11 for MRPT 1.5.7 Git: 2190203 Tue May 15 02:01:15 2018 +0200 at miƩ may 16 12:40:16 CEST 2018