DD4hep  1.28.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 #include <stdexcept>
22 #include <type_traits>
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 
32 #ifndef M_PI
33 #define M_PI 3.14159265358979323846
34 #endif
35 
37 namespace dd4hep {
38 
39  // Forward declarations
40  class NamedObject;
41 
43  bool set_allow_variable_redefine(bool value);
44 
47 
49  void warning_deprecated_xml_factory(const char* name);
50 
51 
53  inline unsigned long long int magic_word() {
54  return 0xFEEDAFFEDEADFACEULL;
55  }
56 
58 
84  template <typename T> class Handle {
85  public:
87  typedef T Object;
90  typedef Handle<T> Base;
91 
93  T* m_element {nullptr};
94 
96  Handle() = default;
98  Handle(Handle<T>&& element) = default;
100  Handle(const Handle<T>& element) = default;
102  Handle(T* element) : m_element(element) { }
104  template <typename Q> Handle(Q* element)
105  : m_element(element ? detail::safe_cast<T>::cast(element) : 0)
106  { }
108  template <typename Q> Handle(const Handle<Q>& element)
109  : m_element(element.m_element ? detail::safe_cast<T>::cast(element.m_element) : 0)
110  { }
112  Handle<T>& operator=(Handle<T>&& element) = default;
114  Handle<T>& operator=(const Handle<T>& element) = default;
116  bool operator==(const Handle<T>& element) const {
117  return m_element == element.m_element;
118  }
120  bool operator<(const Handle<T>& element) const {
121  return m_element < element.m_element;
122  }
124  bool operator>(const Handle<T>& element) const {
125  return m_element > element.m_element;
126  }
128  bool isValid() const {
129  return 0 != m_element;
130  }
132  bool operator!() const {
133  return 0 == m_element;
134  }
137  m_element = 0;
138  return *this;
139  }
141  T* operator->() const {
142  return m_element;
143  }
145  operator T&() const {
146  return *m_element;
147  }
149  T& operator*() const {
150  return *m_element;
151  }
153  T* ptr() const {
154  return m_element;
155  }
157  template <typename Q> Q* _ptr() const {
158  return (Q*) m_element;
159  }
161  template <typename Q> Q* data() const {
162  return (Q*) m_element;
163  }
165  template <typename Q> Q& object() const {
166  return *(Q*) m_element;
167  }
169 
171  T* access() const;
173  const char* name() const;
175  void assign(Object* n, const std::string& nam, const std::string& title);
177  void destroy();
179  static void bad_assignment(const std::type_info& from, const std::type_info& to);
180  };
183  namespace detail {
185  template <typename T> inline void destroyHandle(T& handle) {
186  deletePtr(handle.m_element);
187  }
189  template <typename T> class DestroyHandle {
190  public:
191  void operator()(T ptr) const { destroyHandle(ptr); }
192  };
194  template <typename M> class DestroyHandles {
195  public:
197  M& object;
199  DestroyHandles(M& obj) : object(obj) { }
201  void operator()(const std::pair<typename M::key_type, typename M::mapped_type>& arg) const
202  { DestroyHandle<typename M::mapped_type>()(arg.second); }
203  };
205  template <typename M> void destroyHandles(M& arg) {
206  for_each(arg.begin(), arg.end(), DestroyHandles<M>(arg));
207  arg.clear();
208  }
209 
211  template <typename T> inline void releaseHandle(T& handle) {
212  releasePtr(handle.m_element);
213  }
215  template <typename T> class ReleaseHandle {
216  public:
217  void operator()(T handle) const { releaseHandle(handle); }
218  };
220  template <typename M> class ReleaseHandles {
221  public:
223  M& object;
225  ReleaseHandles(M& obj) : object(obj) { }
227  void operator()(const std::pair<typename M::key_type, typename M::mapped_type>& arg) const
228  { ReleaseHandle<typename M::mapped_type>()(arg.second); }
229  };
231  template <typename M> void releaseHandles(M& arg) {
232  for_each(arg.begin(), arg.end(), ReleaseHandles<M>(arg));
233  arg.clear();
234  }
235  }
236 
238  std::string remove_whitespace(const std::string& v);
239 
241  std::string _toString(bool value);
243  std::string _toString(short value, const char* fmt = "%d");
245  std::string _toString(int value, const char* fmt = "%d");
247  std::string _toString(unsigned long value, const char* fmt = "%ld");
249  std::string _toString(float value, const char* fmt = "%.17e");
251  std::string _toString(double value, const char* fmt = "%.17e");
253  std::string _ptrToString(const void* p, const char* fmt = "%p");
255  template <typename T> std::string _toString(const T* p, const char* fmt = "%p")
256  { return _ptrToString((void*)p, fmt); }
257 
259  template <typename T> T _toType(const std::string& value);
260 
262  bool _toBool(const std::string& value);
264  short _toShort(const std::string& value);
266  int _toInt(const std::string& value);
268  long _toLong(const std::string& value);
270  unsigned short _toUShort(const std::string& value);
272  unsigned int _toUInt(const std::string& value);
274  unsigned long _toULong(const std::string& value);
276  float _toFloat(const std::string& value);
278  double _toDouble(const std::string& value);
279 
281  inline bool _toBool(bool value) {
282  return value;
283  }
285  inline short _toShort(short value) {
286  return value;
287  }
289  inline int _toInt(int value) {
290  return value;
291  }
293  inline long _toLong(long value) {
294  return value;
295  }
297  inline unsigned short _toUShort(unsigned short value) {
298  return value;
299  }
301  inline unsigned int _toUInt(unsigned int value) {
302  return value;
303  }
305  inline unsigned long _toULong(unsigned long value) {
306  return value;
307  }
309  inline float _toFloat(float value) {
310  return value;
311  }
313  inline double _toDouble(double value) {
314  return value;
315  }
316 
318  template <class T> T _multiply(const std::string& left, T right);
320  template <class T> T _multiply(T left, const std::string& right);
322  template <class T> T _multiply(const std::string& left, const std::string& right);
323 
325  template <> char _multiply<char>(const std::string& left, const std::string& right);
328  template <> inline char _multiply<char>(char left, const std::string& right) {
329  return left * _toInt(right);
330  }
332  template <> inline char _multiply<char>(const std::string& left, char right) {
333  return _toInt(left) * right;
334  }
335 
337  template <> unsigned char _multiply<unsigned char>(const std::string& left, const std::string& right);
340  template <> inline unsigned char _multiply<unsigned char>(unsigned char left, const std::string& right) {
341  return left * _toInt(right);
342  }
344  template <> inline unsigned char _multiply<unsigned char>(const std::string& left, unsigned char right) {
345  return _toInt(left) * right;
346  }
347 
349  template <> short _multiply<short>(const std::string& left, const std::string& right);
351  template <> inline short _multiply<short>(short left, const std::string& right) {
352  return left * _toInt(right);
353  }
355  template <> inline short _multiply<short>(const std::string& left, short right) {
356  return _toInt(left) * right;
357  }
358 
360  template <> unsigned short _multiply<unsigned short>(const std::string& left, const std::string& right);
363  template <> inline unsigned short _multiply<unsigned short>(unsigned short left, const std::string& right) {
364  return left * _toInt(right);
365  }
367  template <> inline unsigned short _multiply<unsigned short>(const std::string& left, unsigned short right) {
368  return _toInt(left) * right;
369  }
370 
372  template <> int _multiply<int>(const std::string& left, const std::string& right);
375  template <> inline int _multiply<int>(int left, const std::string& right) {
376  return left * _toInt(right);
377  }
379  template <> inline int _multiply<int>(const std::string& left, int right) {
380  return _toInt(left) * right;
381  }
382 
384  template <> unsigned int _multiply<unsigned int>(const std::string& left, const std::string& right);
387  template <> inline unsigned int _multiply<unsigned int>(unsigned int left, const std::string& right) {
388  return left * _toInt(right);
389  }
391  template <> inline unsigned int _multiply<unsigned int>(const std::string& left, unsigned int right) {
392  return _toInt(left) * right;
393  }
394 
396  template <> long _multiply<long>(const std::string& left, const std::string& right);
399  template <> inline long _multiply<long>(long left, const std::string& right) {
400  return left * _toLong(right);
401  }
403  template <> inline long _multiply<long>(const std::string& left, long right) {
404  return _toLong(left) * right;
405  }
406 
408  template <> unsigned long _multiply<unsigned long>(const std::string& left, const std::string& right);
411  template <> inline unsigned long _multiply<unsigned long>(unsigned long left, const std::string& right) {
412  return left * _toLong(right);
413  }
415  template <> inline unsigned long _multiply<unsigned long>(const std::string& left, unsigned long right) {
416  return _toLong(left) * right;
417  }
418 
420  template <> float _multiply<float>(const std::string& left, const std::string& right);
423  template <> inline float _multiply<float>(float left, const std::string& right) {
424  return left * _toFloat(right);
425  }
427  template <> inline float _multiply<float>(const std::string& left, float right) {
428  return _toFloat(left) * right;
429  }
430 
432  template <> double _multiply<double>(const std::string& left, const std::string& right);
435  template <> inline double _multiply<double>(const std::string& left, double right) {
436  return _toDouble(left) * right;
437  }
439  template <> inline double _multiply<double>(double left, const std::string& right) {
440  return left * _toDouble(right);
441  }
442 
444  void _toDictionary(const std::string& name, const std::string& value);
446  void _toDictionary(const std::string& name, const std::string& value, const std::string& typ);
447 
449  namespace detail {
450  using dd4hep::Handle;
451  using dd4hep::Ref_t;
452  } /* End namespace detail */
453 
454  // Forward declarations
455  class Detector;
456 } /* End namespace dd4hep */
457 #endif // DD4HEP_HANDLE_H
458 
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:231
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:199
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:185
dd4hep::detail::ReleaseHandles::object
M & object
Container reference.
Definition: Handle.h:223
dd4hep::detail::ReleaseHandles
map Functor to release handles
Definition: Handle.h:220
v
View * v
Definition: MultiView.cpp:28
dd4hep::Handle::Object
T Object
Extern accessible definition of the contained element type.
Definition: Handle.h:88
dd4hep::Handle::operator>
bool operator>(const Handle< T > &element) const
Boolean operator > used for RB tree insertions.
Definition: Handle.h:124
dd4hep::Handle::object
Q & object() const
Access to an unrelated object type.
Definition: Handle.h:165
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:132
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:120
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:128
dd4hep::Handle
Handle: a templated class like a shared pointer, which allows specialized access to tgeometry objects...
Definition: Handle.h:84
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:53
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:201
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:108
dd4hep::Handle::operator*
T & operator*() const
Access the held object using the * operator.
Definition: Handle.h:149
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:136
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:141
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:104
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:161
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:205
dd4hep::detail::ReleaseHandle::operator()
void operator()(T handle) const
Definition: Handle.h:217
dd4hep::detail::releaseHandle
void releaseHandle(T &handle)
Helper to delete objects from heap and reset the handle.
Definition: Handle.h:211
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:194
dd4hep::Handle::m_element
T * m_element
Single and only data member: Reference to the actual element.
Definition: Handle.h:93
dd4hep::Handle::_ptr
Q * _ptr() const
Access to an unrelated object type.
Definition: Handle.h:157
dd4hep::Handle::operator==
bool operator==(const Handle< T > &element) const
Boolean operator == used for RB tree insertions.
Definition: Handle.h:116
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:102
dd4hep::Handle::Base
Handle< T > Base
Self type: used by sub-classes.
Definition: Handle.h:90
dd4hep::detail::DestroyHandle::operator()
void operator()(T ptr) const
Definition: Handle.h:191
dd4hep::detail::ReleaseHandle
Functor to destroy handles and delete the cached object.
Definition: Handle.h:215
Primitives.h
dd4hep::Ref_t
Handle< NamedObject > Ref_t
Default Ref_t definition describing named objects.
Definition: Handle.h:182
dd4hep::detail::DestroyHandles::object
M & object
Container reference.
Definition: Handle.h:197
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:153
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:189
dd4hep::detail::ReleaseHandles::ReleaseHandles
ReleaseHandles(M &obj)
Initializing constructor.
Definition: Handle.h:225
dd4hep::detail::ReleaseHandles::operator()
void operator()(const std::pair< typename M::key_type, typename M::mapped_type > &arg) const
Action operator.
Definition: Handle.h:227
handle
void handle(const O *o, const C &c, F pmf)
Definition: LCDDConverter.cpp:1100
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.