cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnClassRegisterMacros.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Anton Deguet
7  Created on: 2006-10-30
8 
9  (C) Copyright 2006-2012 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 
20 */
21 
22 #pragma once
23 
24 #ifndef _cmnClassRegisterMacros_h
25 #define _cmnClassRegisterMacros_h
26 
28 
113 #ifdef CMN_DECLARE_SERVICES
114 #undef CMN_DECLARE_SERVICES
115 #endif
116 #define CMN_DECLARE_SERVICES(hasDynamicCreation, lod) \
117  public: \
118  enum {HAS_DYNAMIC_CREATION = hasDynamicCreation}; \
119  enum {InitialLoD = lod}; \
120  static cmnClassServicesBase * ClassServices(void); \
121  virtual const cmnClassServicesBase * Services(void) const; \
122  private: \
123  static cmnClassServicesBase * ClassServicesPointer;
124 
125 
126 
127 #ifdef CMN_DECLARE_SERVICES_EXPORT
128 #undef CMN_DECLARE_SERVICES_EXPORT
129 #endif
130 #define CMN_DECLARE_SERVICES_EXPORT(hasDynamicCreation, lod) \
131  public: \
132  enum {HAS_DYNAMIC_CREATION = hasDynamicCreation}; \
133  enum {InitialLoD = lod}; \
134  CISST_EXPORT static cmnClassServicesBase * ClassServices(void); \
135  virtual CISST_EXPORT const cmnClassServicesBase * Services(void) const; \
136  private: \
137  static cmnClassServicesBase * ClassServicesPointer;
138 
139 #ifdef CMN_DECLARE_SERVICES_EXPORT_ALWAYS
140 #undef CMN_DECLARE_SERVICES_EXPORT_ALWAYS
141 #endif
142 #if WIN32 && _MSC_VER && CISST_DLL
143 #define CMN_DECLARE_SERVICES_EXPORT_ALWAYS(hasDynamicCreation, lod) \
144  public: \
145  enum {HAS_DYNAMIC_CREATION = hasDynamicCreation}; \
146  enum {InitialLoD = lod}; \
147  _declspec(dllexport) static cmnClassServicesBase * ClassServices(void); \
148  virtual _declspec(dllexport) const cmnClassServicesBase * Services(void) const; \
149  private: \
150  static cmnClassServicesBase * ClassServicesPointer;
151 #else
152 #define CMN_DECLARE_SERVICES_EXPORT_ALWAYS(hasDynamicCreation, lod) CMN_DECLARE_SERVICES_EXPORT(hasDynamicCreation, lod)
153 #endif
154 
156 
157 
198 
199 #ifdef CMN_DECLARE_SERVICES_INSTANTIATION
200 #undef CMN_DECLARE_SERVICES_INSTANTIATION
201 #endif
202 #define CMN_DECLARE_SERVICES_INSTANTIATION(className) \
203 template<> \
204 cmnClassServicesBase * cmnClassServicesInstantiate<className>(void);
205 
206 #ifdef CMN_DECLARE_SERVICES_INSTANTIATION_EXPORT
207 #undef CMN_DECLARE_SERVICES_INSTANTIATION_EXPORT
208 #endif
209 #define CMN_DECLARE_SERVICES_INSTANTIATION_EXPORT(className) \
210 template<> CISST_EXPORT \
211 cmnClassServicesBase * cmnClassServicesInstantiate<className>(void);
212 
213 
214 
215 
239 #ifndef LIBRARY_NAME_FOR_CISST_REGISTER
240 #define LIBRARY_NAME_FOR_CISST_REGISTER ""
241 #endif
242 
244 #ifdef CMN_IMPLEMENT_SERVICES
245 #undef CMN_IMPLEMENT_SERVICES
246 #endif
247 #define CMN_IMPLEMENT_SERVICES_INTERNAL(className, parentServices, argType) \
248 cmnClassServicesBase * className::ClassServices(void) \
249 { \
250  static cmnClassServicesBase * classServices = cmnClassServicesInstantiate<className>(); \
251  return classServices; \
252 } \
253 cmnClassServicesBase * className::ClassServicesPointer = className::ClassServices(); \
254 const cmnClassServicesBase * className::Services(void) const \
255 { \
256  return this->ClassServices(); \
257 } \
258 template<> \
259 cmnClassServicesBase * cmnClassServicesInstantiate<className>(void) \
260 { \
261  static cmnClassServices<className::HAS_DYNAMIC_CREATION, className, argType> \
262  classServices(#className, &typeid(className), parentServices, LIBRARY_NAME_FOR_CISST_REGISTER, className::InitialLoD); \
263  return static_cast<cmnClassServicesBase *>(&classServices); \
264 } \
265 static cmnClassServicesBase * className##ClassServicesPointer = className::ClassServices();
266 
267 #define CMN_IMPLEMENT_SERVICES(className) \
268  CMN_IMPLEMENT_SERVICES_INTERNAL(className, 0, className)
269 
270 #define CMN_IMPLEMENT_SERVICES_DERIVED(className, parentName) \
271  CMN_IS_DERIVED_FROM_ASSERT(className, parentName) \
272  CMN_IMPLEMENT_SERVICES_INTERNAL(className, parentName::ClassServices(), className)
273 
274 #ifdef CMN_IMPLEMENT_SERVICES_TEMPLATED
275 #undef CMN_IMPLEMENT_SERVICES_TEMPLATED
276 #endif
277 #define CMN_IMPLEMENT_SERVICES_TEMPLATED_INTERNAL(className, parentServices, argType) \
278 template<> \
279 cmnClassServicesBase * className::ClassServices(void) \
280 { \
281  static cmnClassServicesBase * classServices = cmnClassServicesInstantiate<className>(); \
282  return classServices; \
283 } \
284 template<> \
285 cmnClassServicesBase * className::ClassServicesPointer = className::ClassServices(); \
286 template<> \
287 const cmnClassServicesBase * className::Services(void) const \
288 { \
289  return this->ClassServices(); \
290 } \
291 template<> \
292 cmnClassServicesBase * cmnClassServicesInstantiate<className>(void) \
293 { \
294  static cmnClassServices<className::HAS_DYNAMIC_CREATION, className, argType> \
295  classServices(#className, &typeid(className), parentServices, LIBRARY_NAME_FOR_CISST_REGISTER, className::InitialLoD); \
296  return static_cast<cmnClassServicesBase *>(&classServices); \
297 } \
298 static cmnClassServicesBase * className##ClassServicesPointer = className::ClassServices();
299 
300 #define CMN_IMPLEMENT_SERVICES_TEMPLATED(className) \
301  CMN_IMPLEMENT_SERVICES_TEMPLATED_INTERNAL(className, 0, className)
302 
303 #define CMN_IMPLEMENT_SERVICES_DERIVED_TEMPLATED(className, parentName) \
304  CMN_IS_DERIVED_FROM_ASSERT(className, parentName) \
305  CMN_IMPLEMENT_SERVICES_TEMPLATED_INTERNAL(className, parentName::ClassServices(), className)
306 
308 
309 
314 /* This parameter allows to either register a class without dynamic
315  creation (just for logging, usually a "processing" class) or
316  register the class with dynamic creation (the log is still
317  available, mostly for "data" objects). Dynamic creation can be used
318  to create an object of a given type based on its name provided as a
319  string.
320 
321  It is important to note that a class registered to allow dynamic
322  creation must provide a default constructor. The C++ compiler will
323  not be able to compile the code generated if the default constructor
324  is missing.
325 */
326 
327 // Possible options for dynamic creation
328 const int CMN_NO_DYNAMIC_CREATION = 0; // no dynamic creation
329 const int CMN_DYNAMIC_CREATION_DEFAULT = 1; // dynamic creation with default constructor
330 const int CMN_DYNAMIC_CREATION_COPY = 2; // dynamic creation with copy constructor
331 const int CMN_DYNAMIC_CREATION = 3; // dynamic creation with default and/or copy constructor (backward compatibility)
332 const int CMN_DYNAMIC_CREATION_SETNAME = 5; // dynamic creation with default constructor and SetName method
333 const int CMN_DYNAMIC_CREATION_ONEARG = 8; // dynamic creation with one argument constructor (but not copy constructor)
334 
336 
337 #endif // _cmnClassRegisterMacros_h
338 
const int CMN_DYNAMIC_CREATION_DEFAULT
Definition: cmnClassRegisterMacros.h:329
const int CMN_DYNAMIC_CREATION_ONEARG
Definition: cmnClassRegisterMacros.h:333
Declaration of the class cmnTypeTraits.
const int CMN_NO_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:328
const int CMN_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:331
const int CMN_DYNAMIC_CREATION_COPY
Definition: cmnClassRegisterMacros.h:330
const int CMN_DYNAMIC_CREATION_SETNAME
Definition: cmnClassRegisterMacros.h:332