DD4hep  1.32.1
Detector Description Toolkit for High Energy Physics
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  // make sure we get a compiler error if this is used in a context where T::Object
185  // is not complete, as this is undefined behavior.
186  // If you see this, you are probably missing an include in the file where you
187  // call destroyHandle. You need the internal object behind the handle to be
188  // fully defined. E.g. you need to include DD4hep/detail/DetectorInterna.h
189  // (despite the name) to be able to destroy a DetElement.
190  // Not doing this leads to undefined behavior and practically with gcc lack
191  // of destruction of the internal object and thus memory leak
192  static_assert( sizeof(typename T::Object) > 0, "destroyHandle called on incomplete type. Missing include ?");
193  deletePtr(handle.m_element);
194  }
196  template <typename T> class DestroyHandle {
197  public:
198  void operator()(T ptr) const { destroyHandle(ptr); }
199  };
201  template <typename M> class DestroyHandles {
202  public:
204  M& object;
206  DestroyHandles(M& obj) : object(obj) { }
208  void operator()(const std::pair<typename M::key_type, typename M::mapped_type>& arg) const
209  { DestroyHandle<typename M::mapped_type>()(arg.second); }
210  };
212  template <typename M> void destroyHandles(M& arg) {
213  for_each(arg.begin(), arg.end(), DestroyHandles<M>(arg));
214  arg.clear();
215  }
216 
218  template <typename T> inline void releaseHandle(T& handle) {
219  releasePtr(handle.m_element);
220  }
222  template <typename T> class ReleaseHandle {
223  public:
224  void operator()(T handle) const { releaseHandle(handle); }
225  };
227  template <typename M> class ReleaseHandles {
228  public:
230  M& object;
232  ReleaseHandles(M& obj) : object(obj) { }
234  void operator()(const std::pair<typename M::key_type, typename M::mapped_type>& arg) const
235  { ReleaseHandle<typename M::mapped_type>()(arg.second); }
236  };
238  template <typename M> void releaseHandles(M& arg) {
239  for_each(arg.begin(), arg.end(), ReleaseHandles<M>(arg));
240  arg.clear();
241  }
242  }
243 
245  std::string remove_whitespace(const std::string& v);
246 
248  std::string _toString(bool value);
250  std::string _toString(short value, const char* fmt = "%d");
252  std::string _toString(int value, const char* fmt = "%d");
254  std::string _toString(unsigned long value, const char* fmt = "%ld");
256  std::string _toString(float value, const char* fmt = "%.17e");
258  std::string _toString(double value, const char* fmt = "%.17e");
260  std::string _ptrToString(const void* p, const char* fmt = "%p");
262  template <typename T> std::string _toString(const T* p, const char* fmt = "%p")
263  { return _ptrToString((void*)p, fmt); }
264 
266  template <typename T> T _toType(const std::string& value);
267 
269  bool _toBool(const std::string& value);
271  short _toShort(const std::string& value);
273  int _toInt(const std::string& value);
275  long _toLong(const std::string& value);
277  unsigned short _toUShort(const std::string& value);
279  unsigned int _toUInt(const std::string& value);
281  unsigned long _toULong(const std::string& value);
283  float _toFloat(const std::string& value);
285  double _toDouble(const std::string& value);
286 
288  inline bool _toBool(bool value) {
289  return value;
290  }
292  inline short _toShort(short value) {
293  return value;
294  }
296  inline int _toInt(int value) {
297  return value;
298  }
300  inline long _toLong(long value) {
301  return value;
302  }
304  inline unsigned short _toUShort(unsigned short value) {
305  return value;
306  }
308  inline unsigned int _toUInt(unsigned int value) {
309  return value;
310  }
312  inline unsigned long _toULong(unsigned long value) {
313  return value;
314  }
316  inline float _toFloat(float value) {
317  return value;
318  }
320  inline double _toDouble(double value) {
321  return value;
322  }
323 
325  template <class T> T _multiply(const std::string& left, T right);
327  template <class T> T _multiply(T left, const std::string& right);
329  template <class T> T _multiply(const std::string& left, const std::string& right);
330 
332  template <> char _multiply<char>(const std::string& left, const std::string& right);
335  template <> inline char _multiply<char>(char left, const std::string& right) {
336  return left * _toInt(right);
337  }
339  template <> inline char _multiply<char>(const std::string& left, char right) {
340  return _toInt(left) * right;
341  }
342 
344  template <> unsigned char _multiply<unsigned char>(const std::string& left, const std::string& right);
347  template <> inline unsigned char _multiply<unsigned char>(unsigned char left, const std::string& right) {
348  return left * _toInt(right);
349  }
351  template <> inline unsigned char _multiply<unsigned char>(const std::string& left, unsigned char right) {
352  return _toInt(left) * right;
353  }
354 
356  template <> short _multiply<short>(const std::string& left, const std::string& right);
358  template <> inline short _multiply<short>(short left, const std::string& right) {
359  return left * _toInt(right);
360  }
362  template <> inline short _multiply<short>(const std::string& left, short right) {
363  return _toInt(left) * right;
364  }
365 
367  template <> unsigned short _multiply<unsigned short>(const std::string& left, const std::string& right);
370  template <> inline unsigned short _multiply<unsigned short>(unsigned short left, const std::string& right) {
371  return left * _toInt(right);
372  }
374  template <> inline unsigned short _multiply<unsigned short>(const std::string& left, unsigned short right) {
375  return _toInt(left) * right;
376  }
377 
379  template <> int _multiply<int>(const std::string& left, const std::string& right);
382  template <> inline int _multiply<int>(int left, const std::string& right) {
383  return left * _toInt(right);
384  }
386  template <> inline int _multiply<int>(const std::string& left, int right) {
387  return _toInt(left) * right;
388  }
389 
391  template <> unsigned int _multiply<unsigned int>(const std::string& left, const std::string& right);
394  template <> inline unsigned int _multiply<unsigned int>(unsigned int left, const std::string& right) {
395  return left * _toInt(right);
396  }
398  template <> inline unsigned int _multiply<unsigned int>(const std::string& left, unsigned int right) {
399  return _toInt(left) * right;
400  }
401 
403  template <> long _multiply<long>(const std::string& left, const std::string& right);
406  template <> inline long _multiply<long>(long left, const std::string& right) {
407  return left * _toLong(right);
408  }
410  template <> inline long _multiply<long>(const std::string& left, long right) {
411  return _toLong(left) * right;
412  }
413 
415  template <> unsigned long _multiply<unsigned long>(const std::string& left, const std::string& right);
418  template <> inline unsigned long _multiply<unsigned long>(unsigned long left, const std::string& right) {
419  return left * _toLong(right);
420  }
422  template <> inline unsigned long _multiply<unsigned long>(const std::string& left, unsigned long right) {
423  return _toLong(left) * right;
424  }
425 
427  template <> float _multiply<float>(const std::string& left, const std::string& right);
430  template <> inline float _multiply<float>(float left, const std::string& right) {
431  return left * _toFloat(right);
432  }
434  template <> inline float _multiply<float>(const std::string& left, float right) {
435  return _toFloat(left) * right;
436  }
437 
439  template <> double _multiply<double>(const std::string& left, const std::string& right);
442  template <> inline double _multiply<double>(const std::string& left, double right) {
443  return _toDouble(left) * right;
444  }
446  template <> inline double _multiply<double>(double left, const std::string& right) {
447  return left * _toDouble(right);
448  }
449 
451  void _toDictionary(const std::string& name, const std::string& value);
453  void _toDictionary(const std::string& name, const std::string& value, const std::string& typ);
454 
456  namespace detail {
457  using dd4hep::Handle;
458  using dd4hep::Ref_t;
459  } /* End namespace detail */
460 
461  // Forward declarations
462  class Detector;
463 } /* End namespace dd4hep */
464 #endif // DD4HEP_HANDLE_H
465 
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:238
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:206
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:230
dd4hep::detail::ReleaseHandles
map Functor to release handles
Definition: Handle.h:227
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:208
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:212
dd4hep::detail::ReleaseHandle::operator()
void operator()(T handle) const
Definition: Handle.h:224
dd4hep::detail::releaseHandle
void releaseHandle(T &handle)
Helper to delete objects from heap and reset the handle.
Definition: Handle.h:218
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:201
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:198
dd4hep::detail::ReleaseHandle
Functor to destroy handles and delete the cached object.
Definition: Handle.h:222
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:204
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:196
dd4hep::detail::ReleaseHandles::ReleaseHandles
ReleaseHandles(M &obj)
Initializing constructor.
Definition: Handle.h:232
dd4hep::detail::ReleaseHandles::operator()
void operator()(const std::pair< typename M::key_type, typename M::mapped_type > &arg) const
Action operator.
Definition: Handle.h:234
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.