DD4hep  1.31.0
Detector Description Toolkit for High Energy Physics
Handle.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
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/Primitives.h>
18 
19 #include <string>
20 #include <typeinfo>
21 
22 // Conversion factor from radians to degree: 360/(2*PI)
23 #ifndef RAD_2_DEGREE
24 #define RAD_2_DEGREE 57.295779513082320876798154814105
25 #endif
26 #ifndef DEGREE_2_RAD
27 #define DEGREE_2_RAD 0.0174532925199432957692369076848
28 #endif
29 
30 #ifndef M_PI
31 #define M_PI 3.14159265358979323846
32 #endif
33 
35 namespace dd4hep {
36 
37  // Forward declarations
38  class NamedObject;
39 
41  bool set_allow_variable_redefine(bool value);
42 
45 
47  void warning_deprecated_xml_factory(const char* name);
48 
49 
51  inline unsigned long long int magic_word() {
52  return 0xFEEDAFFEDEADFACEULL;
53  }
54 
56 
82  template <typename T> class Handle {
83  public:
85  typedef T Object;
88  typedef Handle<T> Base;
89 
91  T* m_element {nullptr};
92 
94  Handle() = default;
96  Handle(Handle<T>&& element) = default;
98  Handle(const Handle<T>& element) = default;
100  Handle(T* element) : m_element(element) { }
102  template <typename Q> Handle(Q* element)
103  : m_element(element ? detail::safe_cast<T>::cast(element) : 0)
104  { }
106  template <typename Q> Handle(const Handle<Q>& element)
107  : m_element(element.m_element ? detail::safe_cast<T>::cast(element.m_element) : 0)
108  { }
110  Handle<T>& operator=(Handle<T>&& element) = default;
112  Handle<T>& operator=(const Handle<T>& element) = default;
114  bool operator==(const Handle<T>& element) const {
115  return m_element == element.m_element;
116  }
118  bool operator<(const Handle<T>& element) const {
119  return m_element < element.m_element;
120  }
122  bool operator>(const Handle<T>& element) const {
123  return m_element > element.m_element;
124  }
126  bool isValid() const {
127  return 0 != m_element;
128  }
130  bool operator!() const {
131  return 0 == m_element;
132  }
135  m_element = 0;
136  return *this;
137  }
139  T* operator->() const {
140  return m_element;
141  }
143  operator T&() const {
144  return *m_element;
145  }
147  T& operator*() const {
148  return *m_element;
149  }
151  T* ptr() const {
152  return m_element;
153  }
155  template <typename Q> Q* _ptr() const {
156  return (Q*) m_element;
157  }
159  template <typename Q> Q* data() const {
160  return (Q*) m_element;
161  }
163  template <typename Q> Q& object() const {
164  return *(Q*) m_element;
165  }
167 
169  T* access() const;
171  const char* name() const;
173  void assign(Object* n, const std::string& nam, const std::string& title);
175  void destroy();
177  static void bad_assignment(const std::type_info& from, const std::type_info& to);
178  };
181  namespace detail {
183  template <typename T> inline void destroyHandle(T& handle) {
184  deletePtr(handle.m_element);
185  }
187  template <typename T> class DestroyHandle {
188  public:
189  void operator()(T ptr) const { destroyHandle(ptr); }
190  };
192  template <typename M> class DestroyHandles {
193  public:
195  M& object;
197  DestroyHandles(M& obj) : object(obj) { }
199  void operator()(const std::pair<typename M::key_type, typename M::mapped_type>& arg) const
200  { DestroyHandle<typename M::mapped_type>()(arg.second); }
201  };
203  template <typename M> void destroyHandles(M& arg) {
204  for_each(arg.begin(), arg.end(), DestroyHandles<M>(arg));
205  arg.clear();
206  }
207 
209  template <typename T> inline void releaseHandle(T& handle) {
210  releasePtr(handle.m_element);
211  }
213  template <typename T> class ReleaseHandle {
214  public:
215  void operator()(T handle) const { releaseHandle(handle); }
216  };
218  template <typename M> class ReleaseHandles {
219  public:
221  M& object;
223  ReleaseHandles(M& obj) : object(obj) { }
225  void operator()(const std::pair<typename M::key_type, typename M::mapped_type>& arg) const
226  { ReleaseHandle<typename M::mapped_type>()(arg.second); }
227  };
229  template <typename M> void releaseHandles(M& arg) {
230  for_each(arg.begin(), arg.end(), ReleaseHandles<M>(arg));
231  arg.clear();
232  }
233  }
234 
236  std::string remove_whitespace(const std::string& v);
237 
239  std::string _toString(bool value);
241  std::string _toString(short value, const char* fmt = "%d");
243  std::string _toString(int value, const char* fmt = "%d");
245  std::string _toString(unsigned long value, const char* fmt = "%ld");
247  std::string _toString(float value, const char* fmt = "%.17e");
249  std::string _toString(double value, const char* fmt = "%.17e");
251  std::string _ptrToString(const void* p, const char* fmt = "%p");
253  template <typename T> std::string _toString(const T* p, const char* fmt = "%p")
254  { return _ptrToString((void*)p, fmt); }
255 
257  template <typename T> T _toType(const std::string& value);
258 
260  bool _toBool(const std::string& value);
262  short _toShort(const std::string& value);
264  int _toInt(const std::string& value);
266  long _toLong(const std::string& value);
268  unsigned short _toUShort(const std::string& value);
270  unsigned int _toUInt(const std::string& value);
272  unsigned long _toULong(const std::string& value);
274  float _toFloat(const std::string& value);
276  double _toDouble(const std::string& value);
277 
279  inline bool _toBool(bool value) {
280  return value;
281  }
283  inline short _toShort(short value) {
284  return value;
285  }
287  inline int _toInt(int value) {
288  return value;
289  }
291  inline long _toLong(long value) {
292  return value;
293  }
295  inline unsigned short _toUShort(unsigned short value) {
296  return value;
297  }
299  inline unsigned int _toUInt(unsigned int value) {
300  return value;
301  }
303  inline unsigned long _toULong(unsigned long value) {
304  return value;
305  }
307  inline float _toFloat(float value) {
308  return value;
309  }
311  inline double _toDouble(double value) {
312  return value;
313  }
314 
316  template <class T> T _multiply(const std::string& left, T right);
318  template <class T> T _multiply(T left, const std::string& right);
320  template <class T> T _multiply(const std::string& left, const std::string& right);
321 
323  template <> char _multiply<char>(const std::string& left, const std::string& right);
326  template <> inline char _multiply<char>(char left, const std::string& right) {
327  return left * _toInt(right);
328  }
330  template <> inline char _multiply<char>(const std::string& left, char right) {
331  return _toInt(left) * right;
332  }
333 
335  template <> unsigned char _multiply<unsigned char>(const std::string& left, const std::string& right);
338  template <> inline unsigned char _multiply<unsigned char>(unsigned char left, const std::string& right) {
339  return left * _toInt(right);
340  }
342  template <> inline unsigned char _multiply<unsigned char>(const std::string& left, unsigned char right) {
343  return _toInt(left) * right;
344  }
345 
347  template <> short _multiply<short>(const std::string& left, const std::string& right);
349  template <> inline short _multiply<short>(short left, const std::string& right) {
350  return left * _toInt(right);
351  }
353  template <> inline short _multiply<short>(const std::string& left, short right) {
354  return _toInt(left) * right;
355  }
356 
358  template <> unsigned short _multiply<unsigned short>(const std::string& left, const std::string& right);
361  template <> inline unsigned short _multiply<unsigned short>(unsigned short left, const std::string& right) {
362  return left * _toInt(right);
363  }
365  template <> inline unsigned short _multiply<unsigned short>(const std::string& left, unsigned short right) {
366  return _toInt(left) * right;
367  }
368 
370  template <> int _multiply<int>(const std::string& left, const std::string& right);
373  template <> inline int _multiply<int>(int left, const std::string& right) {
374  return left * _toInt(right);
375  }
377  template <> inline int _multiply<int>(const std::string& left, int right) {
378  return _toInt(left) * right;
379  }
380 
382  template <> unsigned int _multiply<unsigned int>(const std::string& left, const std::string& right);
385  template <> inline unsigned int _multiply<unsigned int>(unsigned int left, const std::string& right) {
386  return left * _toInt(right);
387  }
389  template <> inline unsigned int _multiply<unsigned int>(const std::string& left, unsigned int right) {
390  return _toInt(left) * right;
391  }
392 
394  template <> long _multiply<long>(const std::string& left, const std::string& right);
397  template <> inline long _multiply<long>(long left, const std::string& right) {
398  return left * _toLong(right);
399  }
401  template <> inline long _multiply<long>(const std::string& left, long right) {
402  return _toLong(left) * right;
403  }
404 
406  template <> unsigned long _multiply<unsigned long>(const std::string& left, const std::string& right);
409  template <> inline unsigned long _multiply<unsigned long>(unsigned long left, const std::string& right) {
410  return left * _toLong(right);
411  }
413  template <> inline unsigned long _multiply<unsigned long>(const std::string& left, unsigned long right) {
414  return _toLong(left) * right;
415  }
416 
418  template <> float _multiply<float>(const std::string& left, const std::string& right);
421  template <> inline float _multiply<float>(float left, const std::string& right) {
422  return left * _toFloat(right);
423  }
425  template <> inline float _multiply<float>(const std::string& left, float right) {
426  return _toFloat(left) * right;
427  }
428 
430  template <> double _multiply<double>(const std::string& left, const std::string& right);
433  template <> inline double _multiply<double>(const std::string& left, double right) {
434  return _toDouble(left) * right;
435  }
437  template <> inline double _multiply<double>(double left, const std::string& right) {
438  return left * _toDouble(right);
439  }
440 
442  void _toDictionary(const std::string& name, const std::string& value);
444  void _toDictionary(const std::string& name, const std::string& value, const std::string& typ);
445 
447  namespace detail {
448  using dd4hep::Handle;
449  using dd4hep::Ref_t;
450  } /* End namespace detail */
451 
452  // Forward declarations
453  class Detector;
454 } /* End namespace dd4hep */
455 #endif // DD4HEP_HANDLE_H
456 
dd4hep::set_allow_variable_redefine
bool set_allow_variable_redefine(bool value)
Steer redefinition of variable re-definition during expression evaluation. returns old value.
Definition: Handle.cpp:56
dd4hep::detail::releaseHandles
void releaseHandles(M &arg)
Functional created of map destruction functors.
Definition: Handle.h:229
dd4hep::_toShort
short _toShort(const std::string &value)
String conversions: string to short value.
Definition: Handle.cpp:82
dd4hep::_toBool
bool _toBool(const std::string &value)
String conversions: string to boolean value.
Definition: Handle.cpp:106
dd4hep::warning_deprecated_xml_factory
void warning_deprecated_xml_factory(const char *name)
Function tp print warning about deprecated factory usage. Used by Plugin mechanism.
Definition: Handle.cpp:369
dd4hep::detail::DestroyHandles::DestroyHandles
DestroyHandles(M &obj)
Initializing constructor.
Definition: Handle.h:197
dd4hep::increment_object_validations
void increment_object_validations()
Definition: Handle.cpp:366
dd4hep::detail::destroyHandle
void destroyHandle(T &handle)
Helper to delete objects from heap and reset the handle.
Definition: Handle.h:183
dd4hep::detail::ReleaseHandles::object
M & object
Container reference.
Definition: Handle.h:221
dd4hep::detail::ReleaseHandles
map Functor to release handles
Definition: Handle.h:218
v
View * v
Definition: MultiView.cpp:28
dd4hep::Handle::Object
T Object
Extern accessible definition of the contained element type.
Definition: Handle.h:86
dd4hep::Handle::operator>
bool operator>(const Handle< T > &element) const
Boolean operator > used for RB tree insertions.
Definition: Handle.h:122
dd4hep::Handle::object
Q & object() const
Access to an unrelated object type.
Definition: Handle.h:163
dd4hep::_toDictionary
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:240
dd4hep::Handle::operator!
bool operator!() const
Check the validity of the object held by the handle.
Definition: Handle.h:130
dd4hep::_multiply< unsigned int >
unsigned int _multiply< unsigned int >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:220
dd4hep::Handle::operator=
Handle< T > & operator=(Handle< T > &&element)=default
Assignment move operator.
dd4hep::Handle::operator<
bool operator<(const Handle< T > &element) const
Boolean operator < used for RB tree insertions.
Definition: Handle.h:118
dd4hep::Handle::Handle
Handle(const Handle< T > &element)=default
Copy constructor.
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:126
dd4hep::Handle
Handle: a templated class like a shared pointer, which allows specialized access to tgeometry objects...
Definition: Handle.h:82
dd4hep::_toString
std::string _toString(bool value)
String conversions: boolean value to string.
Definition: Handle.cpp:332
dd4hep::_multiply< short >
short _multiply< short >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:196
dd4hep::magic_word
unsigned long long int magic_word()
Access to the magic word, which is protecting some objects against memory corruptions.
Definition: Handle.h:51
dd4hep::_toInt
int _toInt(const std::string &value)
String conversions: string to integer value.
Definition: Handle.cpp:90
dd4hep::_multiply< unsigned long >
unsigned long _multiply< unsigned long >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:228
dd4hep::detail::DestroyHandles::operator()
void operator()(const std::pair< typename M::key_type, typename M::mapped_type > &arg) const
Action operator.
Definition: Handle.h:199
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::_multiply< int >
int _multiply< int >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:216
dd4hep::_toULong
unsigned long _toULong(const std::string &value)
String conversions: string to long integer value.
Definition: Handle.cpp:102
dd4hep::_toLong
long _toLong(const std::string &value)
String conversions: string to long integer value.
Definition: Handle.cpp:98
dd4hep::Handle::Handle
Handle(const Handle< Q > &element)
Initializing constructor from unrelated handle with type checking.
Definition: Handle.h:106
dd4hep::Handle::operator*
T & operator*() const
Access the held object using the * operator.
Definition: Handle.h:147
dd4hep::_toUShort
unsigned short _toUShort(const std::string &value)
String conversions: string to unsigned short value.
Definition: Handle.cpp:86
dd4hep::Handle::clear
Handle< T > & clear()
Release the object held by the handle.
Definition: Handle.h:134
dd4hep::_multiply< float >
float _multiply< float >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:232
dd4hep::remove_whitespace
std::string remove_whitespace(const std::string &v)
String manipulations: Remove unconditionally all white spaces.
Definition: Handle.cpp:317
dd4hep::Handle::operator->
T * operator->() const
Access the held object using the -> operator.
Definition: Handle.h:139
dd4hep::Handle::bad_assignment
static void bad_assignment(const std::type_info &from, const std::type_info &to)
Helper routine called when unrelated types are assigned.
dd4hep::Handle::Handle
Handle()=default
Default constructor.
dd4hep::Handle::Handle
Handle(Q *element)
Initializing constructor from unrelated pointer with type checking.
Definition: Handle.h:102
dd4hep::Handle::assign
void assign(Object *n, const std::string &nam, const std::string &title)
Assign a new named object. Note: object references must be managed by the user.
dd4hep::Handle::data
Q * data() const
Access to an unrelated object type.
Definition: Handle.h:159
dd4hep::_multiply< unsigned short >
unsigned short _multiply< unsigned short >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:206
dd4hep::_multiply< unsigned char >
unsigned char _multiply< unsigned char >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:186
dd4hep::_multiply< long >
long _multiply< long >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:224
dd4hep::detail::destroyHandles
void destroyHandles(M &arg)
Functional created of map destruction functors.
Definition: Handle.h:203
dd4hep::detail::ReleaseHandle::operator()
void operator()(T handle) const
Definition: Handle.h:215
dd4hep::detail::releaseHandle
void releaseHandle(T &handle)
Helper to delete objects from heap and reset the handle.
Definition: Handle.h:209
dd4hep::_ptrToString
std::string _ptrToString(const void *p, const char *fmt="%p")
Pointer to text conversion.
Definition: Handle.cpp:356
dd4hep::detail::DestroyHandles
map Functor to destroy handles and delete the cached object
Definition: Handle.h:192
dd4hep::Handle::m_element
T * m_element
Single and only data member: Reference to the actual element.
Definition: Handle.h:91
dd4hep::Handle::_ptr
Q * _ptr() const
Access to an unrelated object type.
Definition: Handle.h:155
dd4hep::Handle::operator==
bool operator==(const Handle< T > &element) const
Boolean operator == used for RB tree insertions.
Definition: Handle.h:114
dd4hep::num_object_validations
long num_object_validations()
Definition: Handle.cpp:363
dd4hep::_multiply< char >
char _multiply< char >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:176
dd4hep::Handle::destroy
void destroy()
Destroy the underlying object (be careful here: things are not reference counted)!
dd4hep::Handle::Handle
Handle(T *element)
Initializing constructor from pointer.
Definition: Handle.h:100
dd4hep::Handle::Base
Handle< T > Base
Self type: used by sub-classes.
Definition: Handle.h:88
dd4hep::detail::DestroyHandle::operator()
void operator()(T ptr) const
Definition: Handle.h:189
dd4hep::detail::ReleaseHandle
Functor to destroy handles and delete the cached object.
Definition: Handle.h:213
Primitives.h
dd4hep::Ref_t
Handle< NamedObject > Ref_t
Default Ref_t definition describing named objects.
Definition: Handle.h:180
dd4hep::detail::DestroyHandles::object
M & object
Container reference.
Definition: Handle.h:195
dd4hep::_toFloat
float _toFloat(const std::string &value)
String conversions: string to float value.
Definition: Handle.cpp:111
dd4hep::_toDouble
double _toDouble(const std::string &value)
String conversions: string to double value.
Definition: Handle.cpp:116
dd4hep::_multiply
T _multiply(const std::string &left, T right)
Generic multiplication using the evaluator: result = left * right.
dd4hep::Handle::operator=
Handle< T > & operator=(const Handle< T > &element)=default
Assignment copy operator.
dd4hep::Handle::access
T * access() const
Checked object access. Throws invalid handle runtime exception if invalid handle.
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:151
dd4hep::_multiply< double >
double _multiply< double >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:236
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::_toType
T _toType(const std::string &value)
Generic type conversion from string to primitive value.
Definition: Handle.cpp:121
dd4hep::detail::DestroyHandle
Functor to destroy handles and delete the cached object.
Definition: Handle.h:187
dd4hep::detail::ReleaseHandles::ReleaseHandles
ReleaseHandles(M &obj)
Initializing constructor.
Definition: Handle.h:223
dd4hep::detail::ReleaseHandles::operator()
void operator()(const std::pair< typename M::key_type, typename M::mapped_type > &arg) const
Action operator.
Definition: Handle.h:225
handle
void handle(const O *o, const C &c, F pmf)
Definition: LCDDConverter.cpp:1104
dd4hep::_toUInt
unsigned int _toUInt(const std::string &value)
String conversions: string to unsigned integer value.
Definition: Handle.cpp:94
dd4hep::Handle::Handle
Handle(Handle< T > &&element)=default
Copy constructor.