DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
ConditionAny.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_CONDITIONANY_H
14 #define DD4HEP_CONDITIONANY_H
15 
16 // Framework include files
17 #include <DD4hep/Conditions.h>
18 
19 // C/C++ include files
20 #include <any>
21 
23 namespace dd4hep {
24 
26  namespace detail {
27  class ConditionObject;
28  }
29 
31 
42  class ConditionAny : public Handle<detail::ConditionObject> {
43  public:
52 
53  private:
56 
57  public:
58 
60  ConditionAny() = default;
62  ConditionAny(ConditionAny&& c) = default;
66  ConditionAny(const ConditionAny& c) = default;
68  ConditionAny(const Condition& c);
72  template <typename Q> ConditionAny(const Handle<Q>& e);
74  ConditionAny(key_type hash_key);
76  template <typename PAYLOAD> ConditionAny(key_type hash_key, PAYLOAD&& data);
78  ConditionAny(const std::string& name, const std::string& type);
80  template <typename PAYLOAD> ConditionAny(const std::string& name, const std::string& type, PAYLOAD&& data);
81 
83  template <typename Q> ConditionAny& operator=(Handle<Q>&& c);
85  template <typename Q> ConditionAny& operator=(const Handle<Q>& c);
89  ConditionAny& operator=(const Condition& c);
93  ConditionAny& operator=(const ConditionAny& c) = default;
94 
96  const IOVType& iovType() const;
99  const IOV& iov() const;
100 
102  key_type key() const;
105  detkey_type detector_key() const;
107  itemkey_type item_key() const;
108 
110  mask_type flags() const;
112  void setFlag(mask_type option);
114  void unFlag(mask_type option);
116  bool testFlag(mask_type option) const;
117 
119  std::any& get();
121  const std::any& get() const;
122 
124  bool has_value() const;
126  const std::type_info& any_type() const;
128  const std::string any_type_name() const;
130  template <typename T> T& as();
132  template <typename T> const T& as() const;
134  template <typename T> T value();
136  template <typename T> T* pointer();
138  template <typename T> const T* pointer() const;
139  };
140 
143  this->use_data(c.ptr());
144  }
145 
148  this->use_data(c.ptr());
149  }
150 
152  template <typename PAYLOAD> inline
153  ConditionAny::ConditionAny(key_type hash_key, PAYLOAD&& payload) {
154  ConditionAny c(hash_key);
155  c.get() = std::move(payload);
156  this->m_element = c.ptr();
157  }
158 
160  template <typename PAYLOAD> inline
161  ConditionAny::ConditionAny(const std::string& name, const std::string& type, PAYLOAD&& payload) {
162  ConditionAny c(name, type);
163  c.get() = std::move(payload);
164  this->m_element = c.ptr();
165  }
166 
168  template <typename Q> inline ConditionAny& ConditionAny::operator=(Handle<Q>&& c) {
169  this->use_data(c.ptr());
170  return *this;
171  }
172 
174  template <typename Q> inline ConditionAny& ConditionAny::operator=(const Handle<Q>& c) {
175  this->use_data(c.ptr());
176  return *this;
177  }
178 
181  this->use_data(c.ptr());
182  return *this;
183  }
184 
187  this->use_data(c.ptr());
188  return *this;
189  }
190 
192  template <typename T> inline T ConditionAny::value() {
193  return std::any_cast<T>(this->get());
194  }
195 
197  template <typename T> inline T& ConditionAny::as() {
198  T* ptr_payload = std::any_cast<T>(&this->get());
199  if ( ptr_payload ) return *ptr_payload;
200  throw std::runtime_error("ConditionAny: Cannot access value of std::any as a reference to "+typeName(typeid(T)));
201  }
202 
204  template <typename T> inline const T& ConditionAny::as() const {
205  const T* ptr_payload = std::any_cast<T>(&this->get());
206  if ( ptr_payload ) return *ptr_payload;
207  throw std::runtime_error("ConditionAny: Cannot access value of std::any as a reference to "+typeName(typeid(T)));
208  }
209 
211  template <typename T> inline T* ConditionAny::pointer() {
212  return isValid() ? std::any_cast<T>(&this->get()) : nullptr;
213  }
214 
216  template <typename T> inline const T* ConditionAny::pointer() const {
217  return isValid() ? std::any_cast<const T>(&this->get()) : nullptr;
218  }
219 
220 } /* End namespace dd4hep */
221 #endif // DD4HEP_CONDITIONANY_H
dd4hep::ConditionAny::get
std::any & get()
Generic getter. Specify the exact type, not a polymorph type.
Definition: ConditionAny.cpp:115
dd4hep::ConditionAny::operator=
ConditionAny & operator=(ConditionAny &&c)=default
Assignment move operator.
dd4hep::Handle< detail::ConditionObject >::Object
detail::ConditionObject Object
Extern accessible definition of the contained element type.
Definition: Handle.h:88
dd4hep::ConditionAny::use_data
void use_data(detail::ConditionObject *obj)
Verify that underlying data are either invalid of contain an instance of std::any.
Definition: ConditionAny.cpp:53
dd4hep::Handle< detail::ConditionObject >::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::ConditionAny::detkey_type
Condition::detkey_type detkey_type
High part of the key identifies the detector element.
Definition: ConditionAny.h:47
dd4hep::ConditionAny
Main condition object handle.
Definition: ConditionAny.h:42
dd4hep::ConditionAny::flags
mask_type flags() const
Flag operations: Get condition flags.
Definition: ConditionAny.cpp:95
dd4hep::ConditionAny::testFlag
bool testFlag(mask_type option) const
Flag operations: Test for a given a conditons flag.
Definition: ConditionAny.cpp:110
dd4hep::ConditionAny::setFlag
void setFlag(mask_type option)
Flag operations: Set a conditons flag.
Definition: ConditionAny.cpp:100
dd4hep::Handle< detail::ConditionObject >::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::ConditionAny::item_key
itemkey_type item_key() const
Item part of the identifier.
Definition: ConditionAny.cpp:90
dd4hep::IOVType
Class describing the interval of validty type.
Definition: IOV.h:37
dd4hep::Condition::detkey_type
unsigned int detkey_type
High part of the key identifies the detector element.
Definition: Conditions.h:56
dd4hep::Condition
Main condition object handle.
Definition: Conditions.h:51
dd4hep::IOV
Class describing the interval of validty.
Definition: IOV.h:67
dd4hep::ConditionAny::as
T & as()
Access the contained object inside std::any.
Definition: ConditionAny.h:197
dd4hep::Handle< detail::ConditionObject >::data
Q * data() const
Access to an unrelated object type.
Definition: Handle.h:161
dd4hep::ConditionAny::value
T value()
Access a copy of the contained object inside std::any.
Definition: ConditionAny.h:192
dd4hep::ConditionAny::key
key_type key() const
Hash identifier.
Definition: ConditionAny.cpp:80
dd4hep::ConditionAny::detector_key
detkey_type detector_key() const
DetElement part of the identifier.
Definition: ConditionAny.cpp:85
dd4hep::ConditionAny::unFlag
void unFlag(mask_type option)
Flag operations: UN-Set a conditons flag.
Definition: ConditionAny.cpp:105
dd4hep::Handle< detail::ConditionObject >::m_element
detail::ConditionObject * m_element
Single and only data member: Reference to the actual element.
Definition: Handle.h:93
dd4hep::Condition::itemkey_type
unsigned int itemkey_type
Low part of the key identifies the item identifier.
Definition: Conditions.h:58
Conditions.h
dd4hep::Condition::key_type
unsigned long long int key_type
Forward definition of the key type.
Definition: Conditions.h:54
dd4hep::Condition::mask_type
unsigned int mask_type
Forward definition of the object properties.
Definition: Conditions.h:60
dd4hep::ConditionAny::ConditionAny
ConditionAny(const Handle< Q > &e)
Constructor from another handle.
dd4hep::ConditionAny::any_type_name
const std::string any_type_name() const
Access to the type information as string.
Definition: ConditionAny.cpp:148
dd4hep::ConditionAny::iov
const IOV & iov() const
Access the IOV block.
Definition: ConditionAny.cpp:75
dd4hep::detail::ConditionObject
The data class behind a conditions handle.
Definition: ConditionsInterna.h:68
dd4hep::ConditionAny::ConditionAny
ConditionAny(ConditionAny &&c)=default
Move constructor.
dd4hep::ConditionAny::any_type
const std::type_info & any_type() const
Access to the type information.
Definition: ConditionAny.cpp:138
dd4hep::ConditionAny::mask_type
Condition::mask_type mask_type
Forward definition of the object properties.
Definition: ConditionAny.h:51
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::ConditionAny::operator=
ConditionAny & operator=(const ConditionAny &c)=default
Assignment copy operator.
dd4hep::ConditionAny::pointer
T * pointer()
Access the contained object inside std::any.
Definition: ConditionAny.h:211
dd4hep::ConditionAny::has_value
bool has_value() const
Checks whether the object contains a value.
Definition: ConditionAny.cpp:133
dd4hep::ConditionAny::key_type
Condition::key_type key_type
Forward definition of the key type.
Definition: ConditionAny.h:45
dd4hep::ConditionAny::iovType
const IOVType & iovType() const
Access the IOV type.
Definition: ConditionAny.cpp:70
dd4hep::ConditionAny::operator=
ConditionAny & operator=(Handle< Q > &&c)
Assignment move operator.
Definition: ConditionAny.h:168
dd4hep::ConditionAny::ConditionAny
ConditionAny(const ConditionAny &c)=default
Copy constructor.
dd4hep::ConditionAny::ConditionAny
ConditionAny()=default
Default constructor.
dd4hep::ConditionAny::itemkey_type
Condition::itemkey_type itemkey_type
Low part of the key identifies the item identifier.
Definition: ConditionAny.h:49
dd4hep::ConditionAny::ConditionAny
ConditionAny(Object *p)
Initializing constructor.