Main MRPT website > C++ reference for MRPT 1.5.9
internal_class_registry.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>
14 
15 #include <map>
16 #include <cstdarg>
17 #include <mutex>
18 #include <atomic>
19 #include <iostream>
20 
22 
23 using namespace mrpt;
24 using namespace mrpt::utils;
25 using namespace mrpt::synch;
26 using namespace mrpt::system;
27 using namespace std;
28 
30 {
31  std::string ret = n;
32  const auto pos = ret.rfind("::");
33  if (pos != std::string::npos)
34  {
35  return ret.substr(pos + 2);
36  }
37  return ret;
38 }
39 
40 /*---------------------------------------------------------------
41  STATIC GLOBAL VARIABLES
42  ---------------------------------------------------------------*/
44 
45 // Creation on first call pattern:
47 {
48  static std::atomic<int> cnt(0);
49  return cnt;
50 }
51 
52 // Creation on first call pattern:
54 {
57  return lst;
58 }
59 
60 
61 namespace mrpt
62 {
63  namespace utils
64  {
65  typedef std::map<std::string,const TRuntimeClassId*> TClassnameToRuntimeId;
66 
67  /** A singleton with the central registry for CSerializable run-time classes: users do not use this class in any direct way.
68  * \note Class is thread-safe.
69  */
71  {
72  public:
73  /** The unique access point point to the singleton instance.
74  */
76  {
77  static CClassRegistry obj;
78  return obj;
79  }
80 
81  void Add( const std::string &className, const TRuntimeClassId &id )
82  {
83  m_being_modified=true;
84  {
85  std::unique_lock<std::mutex> lk(m_cs);
86 
87  // Sanity check: don't allow registering twice the same class name!
88  const auto it = registeredClasses.find(className);
89  if (it != registeredClasses.cend()) {
90  if (it->second != &id) {
91  std::cerr << mrpt::format("[MRPT class registry] Warning: overwriting already registered className=`%s` with different `TRuntimeClassId`!\n", className.c_str());
92  }
93  }
94  registeredClasses[className] = &id;
95  }
96  m_being_modified=false;
97  }
98 
99  const TRuntimeClassId *Get(const std::string &className)
100  {
101  // Make this fordward compatible with mrpt2 serialization:
102  const std::string name = stripNamespace(className);
103 
104  // Optimization to avoid the costly lock() in virtually all situations:
105  bool has_to_unlock = false;
106  if (m_being_modified)
107  {
108  m_cs.lock();
109  has_to_unlock = true;
110  }
111  const TRuntimeClassId *ret = registeredClasses[name];
112  if (has_to_unlock) m_cs.unlock();
113  return ret;
114  }
115 
116  std::vector<const TRuntimeClassId*> getListOfAllRegisteredClasses()
117  {
118  std::unique_lock<std::mutex> lk(m_cs);
119 
120  std::vector<const TRuntimeClassId*> ret;
121  for (TClassnameToRuntimeId::iterator it=registeredClasses.begin();it!=registeredClasses.end();++it)
122  ret.push_back( it->second );
123  return ret;
124  }
125 
126  private:
127  // PRIVATE constructor
128  CClassRegistry() : m_being_modified(false)
129  {
130  }
131  // PRIVATE destructor
133 
134  // This must be static since we can be called from C startup
135  // functions and it cannot be assured that classesKeeper will be
136  // initialized before other classes that call it...
138  std::mutex m_cs;
139  std::atomic<bool> m_being_modified;
140 
141  };
142 
143  } // End of namespace
144 } // End of namespace
145 
146 
147 /** Register all pending classes - to be called just before de-serializing an object, for example.
148 */
150 {
151  if (!pending_class_registers_modified) return; // Quick return
152 
153  while( pending_class_registers_count()!=0 )
154  {
155  TRegisterFunction *ptrToPtr = pending_class_registers().get();
157 
158  // Call it:
159  if((*ptrToPtr)!=NULL)
160  {
161  (*(*ptrToPtr)) ();
162  delete ptrToPtr;
163  }
164  }
166 }
167 
168 
169 
170 /*---------------------------------------------------------------
171  RegisterClass
172  ---------------------------------------------------------------*/
173 void utils::registerClass(const TRuntimeClassId* pNewClass)
174 {
175  // Register it:
176  CClassRegistry::Instance().Add( std::string( pNewClass->className ), *pNewClass);
177 
178  // Automatically register all classes when the first one is registered.
180 }
181 
182 /** For internal use within mrpt sources, and only in exceptional cases (CMultiMetricMaps, CImage,...)
183  */
184 void utils::registerClassCustomName(const char*customName, const TRuntimeClassId* pNewClass)
185 {
186  // Register it:
187  CClassRegistry::Instance().Add( customName, *pNewClass);
188 
189  // Automatically register all classes when the first one is registered.
191 }
192 
193 /*---------------------------------------------------------------
194  getAllRegisteredClasses
195  ---------------------------------------------------------------*/
196 std::vector<const TRuntimeClassId*> utils::getAllRegisteredClasses()
197 {
199 }
200 
201 std::vector<const TRuntimeClassId*> utils::getAllRegisteredClassesChildrenOf(const TRuntimeClassId* parent_id)
202 {
203  std::vector<const TRuntimeClassId*> res;
204  const auto lst = mrpt::utils::getAllRegisteredClasses();
205  for (const auto &c : lst) {
206  if (c->derivedFrom(parent_id) && c!=parent_id) {
207  res.push_back(c);
208  }
209  }
210  return res;
211 }
212 
213 /*---------------------------------------------------------------
214  findRegisteredClass
215  ---------------------------------------------------------------*/
217 {
218  return CClassRegistry::Instance().Get( className );
219 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
static std::string stripNamespace(const std::string &n)
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
static CClassRegistry & Instance()
The unique access point point to the singleton instance.
void BASE_IMPEXP registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
GLenum GLsizei n
Definition: glext.h:4618
std::vector< const mrpt::utils::TRuntimeClassId * > BASE_IMPEXP getAllRegisteredClasses()
Returns a list with all the classes registered in the system through mrpt::utils::registerClass.
Scalar * iterator
Definition: eigen_plugins.h:23
STL namespace.
std::map< std::string, const TRuntimeClassId * > TClassnameToRuntimeId
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
void BASE_IMPEXP registerClassCustomName(const char *customName, const TRuntimeClassId *pNewClass)
Mostly for internal use within mrpt sources, to handle exceptional cases with multiple serialization ...
void(* TRegisterFunction)()
const GLubyte * c
Definition: glext.h:5590
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.
const TRuntimeClassId * Get(const std::string &className)
This namespace provides multitask, synchronization utilities.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
A thread-safe template queue for object passing between threads; for a template argument of T...
GLsizei const GLchar ** string
Definition: glext.h:3919
CThreadSafeQueue< TRegisterFunction > BASE_IMPEXP & pending_class_registers()
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
volatile bool BASE_IMPEXP pending_class_registers_modified
Set to true if pending_class_registers() has been called after registerAllPendingClasses(). Startup value is false.
GLuint id
Definition: glext.h:3770
A singleton with the central registry for CSerializable run-time classes: users do not use this class...
GLuint const GLchar * name
Definition: glext.h:3891
void Add(const std::string &className, const TRuntimeClassId &id)
void BASE_IMPEXP registerClass(const mrpt::utils::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CSerializable" descendents.
std::vector< const TRuntimeClassId * > getListOfAllRegisteredClasses()
A structure that holds runtime class type information.
Definition: CObject.h:36
GLuint res
Definition: glext.h:6298
const char * className
Definition: CObject.h:38
std::vector< const TRuntimeClassId * > BASE_IMPEXP getAllRegisteredClassesChildrenOf(const TRuntimeClassId *parent_id)
Like getAllRegisteredClasses(), but filters the list to only include children clases of a given base ...
std::atomic< int > BASE_IMPEXP & pending_class_registers_count()



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020