DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Handle.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 #ifndef DD4HEP_HANDLE_H
14 #define DD4HEP_HANDLE_H
15 
16 // Framework include files
17 #include "DD4hep/config.h"
18 #include "DD4hep/Primitives.h"
19 
20 #include <string>
21 #include <typeinfo>
22 #include <stdexcept>
23 
24 // Conversion factor from radians to degree: 360/(2*PI)
25 #ifndef RAD_2_DEGREE
26 #define RAD_2_DEGREE 57.295779513082320876798154814105
27 #endif
28 #ifndef DEGREE_2_RAD
29 #define DEGREE_2_RAD 0.0174532925199432957692369076848
30 #endif
31 #ifndef CM_2_MM
32 #define CM_2_MM 10.0
33 #endif
34 #ifndef MM_2_CM
35 #define MM_2_CM 0.1
36 #endif
37 
38 #ifndef M_PI
39 #define M_PI 3.14159265358979323846
40 #endif
41 
43 namespace DD4hep {
44 
45  // Forward declarations
46  class NamedObject;
47 
51  void warning_deprecated_xml_factory(const char* name);
52 
53 
55  inline unsigned long long int magic_word() {
56  return 0xFEEDAFFEDEADFACEULL;
57  }
58 
59 
61 
87  template <typename T> class Handle {
88  public:
90  typedef T Implementation;
94 
96  T* m_element = 0;
98  Handle() = default;
100  Handle(const Handle<T>& e) = default;
102  Handle(T* e) : m_element(e) { }
104  template <typename Q> Handle(Q* e) : m_element((T*)e)
105  { verifyObject(); }
107  template <typename Q> Handle(const Handle<Q>& e) : m_element((T*)e.m_element)
108  { verifyObject(); }
110  Handle<T>& operator=(const Handle<T>& e) = default;
112  bool operator==(const Handle<T>& e) const {
113  return m_element == e.m_element;
114  }
116  bool operator<(const Handle<T>& e) const {
117  return m_element < e.m_element;
118  }
120  bool operator>(const Handle<T>& e) const {
121  return m_element > e.m_element;
122  }
124  bool isValid() const {
125  return 0 != m_element;
126  }
128  bool operator!() const {
129  return 0 == m_element;
130  }
133  m_element = 0;
134  return *this;
135  }
137  T* operator->() const {
138  return m_element;
139  }
141  operator T&() const {
142  return *m_element;
143  }
145  T& operator*() const {
146  return *m_element;
147  }
149  T* ptr() const {
150  return m_element;
151  }
153  template <typename Q> Q* _ptr() const {
154  return (Q*) m_element;
155  }
157  template <typename Q> Q* data() const {
158  return (Q*) m_element;
159  }
161  template <typename Q> Q& object() const {
162  return *(Q*) m_element;
163  }
165  T* access() const;
167  void verifyObject() const;
169  const char* name() const;
171  void assign(Implementation* n, const std::string& nam, const std::string& title);
173  static void bad_assignment(const std::type_info& from, const std::type_info& to);
174  };
177 
179  template <typename T> inline void destroyHandle(T& h) {
180  deletePtr(h.m_element);
181  }
183  template <typename T> class DestroyHandle {
184  public:
185  void operator()(T p) const { destroyHandle(p); }
186  };
188  template <typename M> class DestroyHandles {
189  public:
191  M& object;
193  DestroyHandles(M& m) : object(m) { }
195  void operator()(const std::pair<typename M::key_type, typename M::mapped_type>& p) const
197  };
199  template <typename M> void destroyHandles(M& m) {
200  for_each(m.begin(), m.end(), DestroyHandles<M>(m));
201  m.clear();
202  }
203 
205  template <typename T> inline void releaseHandle(T& h) {
206  releasePtr(h.m_element);
207  }
209  template <typename T> class ReleaseHandle {
210  public:
211  void operator()(T p) const { releaseHandle(p); }
212  };
214  template <typename M> class ReleaseHandles {
215  public:
217  M& object;
219  ReleaseHandles(M& m) : object(m) { }
221  void operator()(const std::pair<typename M::key_type, typename M::mapped_type>& p) const
223  };
225  template <typename M> void releaseHandles(M& m) {
226  for_each(m.begin(), m.end(), ReleaseHandles<M>(m));
227  m.clear();
228  }
229 
231  std::string _toString(bool value);
233  std::string _toString(short value, const char* fmt = "%d");
235  std::string _toString(int value, const char* fmt = "%d");
237  std::string _toString(float value, const char* fmt = "%.17e");
239  std::string _toString(double value, const char* fmt = "%.17e");
241  std::string _ptrToString(const void* p, const char* fmt = "%p");
243  template <typename T> std::string _toString(const T* p, const char* fmt = "%p")
244  { return _ptrToString((void*)p, fmt); }
245 
247  bool _toBool(const std::string& value);
249  short _toShort(const std::string& value);
251  int _toInt(const std::string& value);
253  long _toLong(const std::string& value);
255  float _toFloat(const std::string& value);
257  double _toDouble(const std::string& value);
258 
260  inline bool _toBool(bool value) {
261  return value;
262  }
264  inline short _toShort(short value) {
265  return value;
266  }
268  inline int _toInt(int value) {
269  return value;
270  }
272  inline long _toLong(long value) {
273  return value;
274  }
276  inline unsigned short _toUShort(unsigned short value) {
277  return value;
278  }
280  inline unsigned int _toUInt(unsigned int value) {
281  return value;
282  }
284  inline unsigned long _toULong(unsigned long value) {
285  return value;
286  }
288  inline float _toFloat(float value) {
289  return value;
290  }
292  inline double _toDouble(double value) {
293  return value;
294  }
295 
297  template <class T> T _multiply(const std::string& left, T right);
299  template <class T> T _multiply(T left, const std::string& right);
301  template <class T> T _multiply(const std::string& left, const std::string& right);
302 
304  template <> char _multiply<char>(const std::string& left, const std::string& right);
307  template <> inline char _multiply<char>(char left, const std::string& right) {
308  return left * _toInt(right);
309  }
311  template <> inline char _multiply<char>(const std::string& left, char right) {
312  return _toInt(left) * right;
313  }
314 
316  template <> unsigned char _multiply<unsigned char>(const std::string& left, const std::string& right);
319  template <> inline unsigned char _multiply<unsigned char>(unsigned char left, const std::string& right) {
320  return left * _toInt(right);
321  }
323  template <> inline unsigned char _multiply<unsigned char>(const std::string& left, unsigned char right) {
324  return _toInt(left) * right;
325  }
326 
328  template <> short _multiply<short>(const std::string& left, const std::string& right);
330  template <> inline short _multiply<short>(short left, const std::string& right) {
331  return left * _toInt(right);
332  }
334  template <> inline short _multiply<short>(const std::string& left, short right) {
335  return _toInt(left) * right;
336  }
337 
339  template <> unsigned short _multiply<unsigned short>(const std::string& left, const std::string& right);
342  template <> inline unsigned short _multiply<unsigned short>(unsigned short left, const std::string& right) {
343  return left * _toInt(right);
344  }
346  template <> inline unsigned short _multiply<unsigned short>(const std::string& left, unsigned short right) {
347  return _toInt(left) * right;
348  }
349 
351  template <> int _multiply<int>(const std::string& left, const std::string& right);
354  template <> inline int _multiply<int>(int left, const std::string& right) {
355  return left * _toInt(right);
356  }
358  template <> inline int _multiply<int>(const std::string& left, int right) {
359  return _toInt(left) * right;
360  }
361 
363  template <> unsigned int _multiply<unsigned int>(const std::string& left, const std::string& right);
366  template <> inline unsigned int _multiply<unsigned int>(unsigned int left, const std::string& right) {
367  return left * _toInt(right);
368  }
370  template <> inline unsigned int _multiply<unsigned int>(const std::string& left, unsigned int right) {
371  return _toInt(left) * right;
372  }
373 
375  template <> long _multiply<long>(const std::string& left, const std::string& right);
378  template <> inline long _multiply<long>(long left, const std::string& right) {
379  return left * _toLong(right);
380  }
382  template <> inline long _multiply<long>(const std::string& left, long right) {
383  return _toLong(left) * right;
384  }
385 
387  template <> unsigned long _multiply<unsigned long>(const std::string& left, const std::string& right);
390  template <> inline unsigned long _multiply<unsigned long>(unsigned long left, const std::string& right) {
391  return left * _toLong(right);
392  }
394  template <> inline unsigned long _multiply<unsigned long>(const std::string& left, unsigned long right) {
395  return _toLong(left) * right;
396  }
397 
399  template <> float _multiply<float>(const std::string& left, const std::string& right);
402  template <> inline float _multiply<float>(float left, const std::string& right) {
403  return left * _toFloat(right);
404  }
406  template <> inline float _multiply<float>(const std::string& left, float right) {
407  return _toFloat(left) * right;
408  }
409 
411  template <> double _multiply<double>(const std::string& left, const std::string& right);
414  template <> inline double _multiply<double>(const std::string& left, double right) {
415  return _toDouble(left) * right;
416  }
418  template <> inline double _multiply<double>(double left, const std::string& right) {
419  return left * _toDouble(right);
420  }
421 
423  void _toDictionary(const std::string& name, const std::string& value);
425  void _toDictionary(const std::string& name, const std::string& value, const std::string& typ);
426 
428  namespace Geometry {
429  using DD4hep::Handle;
430  using DD4hep::Ref_t;
431 
432  // Forward declarations
433  class LCDD;
434  } /* End namespace Geometry */
435 } /* End namespace DD4hep */
436 #endif /* DD4HEP_HANDLE_H */
437 
map Functor to release handles
Definition: Handle.h:214
bool operator!() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
Handle(const Handle< Q > &e)
Initializing constructor from unrelated handle with type checking.
Definition: Handle.h:107
void warning_deprecated_xml_factory(const char *name)
Function tp print warning about deprecated factory usage. Used by Plugin mechanism.
Definition: Handle.cpp:246
void operator()(T p) const
Definition: Handle.h:211
unsigned long _multiply< unsigned long >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:165
void destroyHandle(T &h)
Helper to delete objects from heap and reset the handle.
Definition: Handle.h:179
bool operator==(const Handle< T > &e) const
Boolean operator == used for RB tree insertions.
Definition: Handle.h:112
long _multiply< long >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:161
const char * name() const
Access the object name (or "" if not supported by the object)
Definition: Handle.inl:36
void deletePtr(T *&p)
Helper to delete objects from heap and reset the pointer. Saves many many lines of code...
Definition: Primitives.h:234
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:124
DestroyHandles(M &m)
Initializing constructor.
Definition: Handle.h:193
Handle< T > & clear()
Release the object held by the handle.
Definition: Handle.h:132
short _toShort(const std::string &value)
String conversions: string to integer value.
Definition: Handle.cpp:41
T & operator*() const
Access the held object using the * operator.
Definition: Handle.h:145
Q * data() const
Access to an unrelated object type.
Definition: Handle.h:157
Functor to destroy handles and delete the cached object.
Definition: Handle.h:209
std::string _toString(bool value)
String conversions: boolean value to string.
Definition: Handle.cpp:213
unsigned long _toULong(unsigned long value)
Void helper function to support formalisms.
Definition: Handle.h:284
short _multiply< short >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:133
unsigned short _multiply< unsigned short >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:143
Handle()=default
Defaulot constructor.
unsigned char _multiply< unsigned char >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:123
Handle(Q *e)
Initializing constructor from unrelated pointer with type checking.
Definition: Handle.h:104
T Implementation
Extern accessible definition of the contained element type.
Definition: Handle.h:91
ReleaseHandles(M &m)
Initializing constructor.
Definition: Handle.h:219
T _multiply(const std::string &left, T right)
Generic multiplication using the evaluator: result = left * right.
Handle(T *e)
Initializing constructor from pointer.
Definition: Handle.h:102
unsigned int _toUInt(unsigned int value)
Void helper function to support formalisms.
Definition: Handle.h:280
return e
Definition: Volumes.cpp:297
double _multiply< double >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:173
map Functor to destroy handles and delete the cached object
Definition: Handle.h:188
T * ptr() const
Access to the held object.
Definition: Handle.h:149
void operator()(const std::pair< typename M::key_type, typename M::mapped_type > &p) const
Action operator.
Definition: Handle.h:221
float _multiply< float >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:169
Handle< T > & operator=(const Handle< T > &e)=default
Assignment operator.
void releasePtr(T &p)
Helper to delete objects from heap and reset the pointer. Saves many many lines of code...
Definition: Primitives.h:316
void verifyObject() const
Verify the object type after a (re-)assignment.
Functor to destroy handles and delete the cached object.
Definition: Handle.h:183
M & object
Container reference.
Definition: Handle.h:217
Q * _ptr() const
Access to an unrelated object type.
Definition: Handle.h:153
void assign(Implementation *n, const std::string &nam, const std::string &title)
Assign a new named object. Note: object references must be managed by the user.
Definition: Handle.inl:27
T * access() const
Checked object access. Throws invalid handle runtime exception.
Definition: Handle.inl:41
Q & object() const
Access to an unrelated object type.
Definition: Handle.h:161
unsigned short _toUShort(unsigned short value)
Void helper function to support formalisms.
Definition: Handle.h:276
void releaseHandles(M &m)
Functional created of map destruction functors.
Definition: Handle.h:225
bool _toBool(const std::string &value)
String conversions: string to boolean value.
Definition: Handle.cpp:89
char _multiply< char >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:113
void releaseHandle(T &h)
Helper to delete objects from heap and reset the handle.
Definition: Handle.h:205
int _multiply< int >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:153
Handle< NamedObject > Ref_t
Default Ref_t definition describing named objects.
Definition: Handle.h:176
Handle< Implementation > handle_t
Declaration of 'self'.
Definition: Handle.h:93
unsigned int _multiply< unsigned int >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:157
int _toInt(const std::string &value)
String conversions: string to integer value.
Definition: Handle.cpp:57
double _toDouble(const std::string &value)
String conversions: string to double value.
Definition: Handle.cpp:103
M & object
Container reference.
Definition: Handle.h:191
void increment_object_validations()
Definition: Handle.cpp:243
float _toFloat(const std::string &value)
String conversions: string to float value.
Definition: Handle.cpp:93
T * operator->() const
Access the held object using the -> operator.
Definition: Handle.h:137
std::string _ptrToString(const void *p, const char *fmt="%p")
Pointer to text conversion.
Definition: Handle.cpp:233
Handle: a templated class like a shared pointer, which allows specialized access to tgeometry objects...
Definition: Handle.h:87
long num_object_validations()
Definition: Handle.cpp:240
void destroyHandles(M &m)
Functional created of map destruction functors.
Definition: Handle.h:199
void _toDictionary(const std::string &name, const std::string &value)
Enter name value pair to the dictionary. "value" must be a numerical expression, which is evaluated...
Definition: Handle.cpp:177
T * m_element
Single and only data member: Reference to the actual element.
Definition: Handle.h:96
void operator()(const std::pair< typename M::key_type, typename M::mapped_type > &p) const
Action operator.
Definition: Handle.h:195
long _toLong(const std::string &value)
String conversions: string to long integer value.
Definition: Handle.cpp:73
void operator()(T p) const
Definition: Handle.h:185
bool operator>(const Handle< T > &e) const
Boolean operator > used for RB tree insertions.
Definition: Handle.h:120
static void bad_assignment(const std::type_info &from, const std::type_info &to)
Helper routine called when unrelated types are assigned.
Definition: Handle.inl:21
TGeoShape TGeoMedium * m
Definition: Volumes.cpp:294
unsigned long long int magic_word()
Access to the magic word, which is protecting some objects against memory corruptions.
Definition: Handle.h:55