DD4hep  1.31.0
Detector Description Toolkit for High Energy Physics
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ConditionAny.cpp
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 
14 // Framework includes
15 #include <DD4hep/Printout.h>
16 #include <DD4hep/ConditionAny.h>
18 
19 // C/C++ include files
20 #include <any>
21 #include <string>
22 
23 using namespace dd4hep;
24 
25 namespace {
26  inline const BasicGrammar* any_grammar() {
27  static const BasicGrammar* s_any_grammar = &BasicGrammar::instance<std::any>();
28  return s_any_grammar;
29  }
30 }
31 
34 {
35  if ( hash_key != 0 && hash_key != ~0x0ULL ) {
36  Object* o = new Object();
37  this->assign(o, "", "");
38  o->data.bind<std::any>();
39  o->hash = hash_key;
40  return;
41  }
42  except("ConditionAny","+++ Cannot create a any-condition with an invalid key: %016llX",hash_key);
43 }
44 
46 ConditionAny::ConditionAny(const std::string& nam, const std::string& typ) : Handle<Object>()
47 {
48  Object* o = new Object();
49  this->assign(o, nam, typ);
50  o->data.bind<std::any>();
51  o->hash = 0;
52 }
53 
55  if ( obj ) {
56  if ( !obj->data.grammar ) {
57  except("ConditionAny",
58  "+++ Cannot assign unbound conditions data to handle. [Invalid operation]");
59  }
60  if ( obj->data.grammar != any_grammar() ) {
61  except("ConditionAny",
62  "+++ Cannot assign data of type " +
63  obj->data.grammar->type_name() +
64  " to handle holding std::any. [Invalid operation]");
65  }
66  }
67  this->m_element = obj;
68 }
69 
72  return *(this->access()->iovType());
73 }
74 
77  return *(this->access()->iovData());
78 }
79 
82  return this->access()->hash;
83 }
84 
88 }
89 
93 }
94 
97  return access()->flags;
98 }
99 
102  access()->setFlag(option);
103 }
104 
107  access()->unFlag(option);
108 }
109 
111 bool ConditionAny::testFlag(mask_type option) const {
112  return access()->testFlag(option);
113 }
114 
116 std::any& ConditionAny::get() {
117  OpaqueData& o = this->access()->data;
118  if ( o.grammar && (o.grammar == any_grammar()) ) {
119  return *(std::any*)o.ptr();
120  }
121  throw std::bad_cast();
122 }
123 
125 const std::any& ConditionAny::get() const {
126  const OpaqueData& o = this->access()->data;
127  if ( o.grammar && (o.grammar == any_grammar()) ) {
128  return *(std::any*)o.ptr();
129  }
130  throw std::bad_cast();
131 }
132 
135  return this->get().has_value();
136 }
137 
139 const std::type_info& ConditionAny::any_type() const {
140  const Object* o = this->ptr();
141  if ( o && o->data.grammar && (o->data.grammar == any_grammar()) ) {
142  const std::any* a = (const std::any*)o->data.ptr();
143  return a->type();
144  }
145  return typeid(void);
146 }
147 
149 const std::string ConditionAny::any_type_name() const {
150  const Object* o = this->ptr();
151  if ( o && o->data.grammar && (o->data.grammar == any_grammar()) ) {
152  const std::any* a = (const std::any*)o->data.ptr();
153  return typeName(a->type());
154  }
155  return typeName(typeid(void));
156 }
dd4hep::ConditionAny::get
std::any & get()
Generic getter. Specify the exact type, not a polymorph type.
Definition: ConditionAny.cpp:116
dd4hep::detail::ConditionObject::hash
Condition::key_type hash
Hash value of the name.
Definition: ConditionsInterna.h:85
dd4hep::Handle< detail::ConditionObject >::Object
detail::ConditionObject Object
Extern accessible definition of the contained element type.
Definition: Handle.h:86
ConditionsInterna.h
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:54
dd4hep::detail::ConditionObject::flags
Condition::mask_type flags
Flags.
Definition: ConditionsInterna.h:87
dd4hep::Handle
Handle: a templated class like a shared pointer, which allows specialized access to tgeometry objects...
Definition: Handle.h:82
dd4hep::ConditionAny::detkey_type
Condition::detkey_type detkey_type
High part of the key identifies the detector element.
Definition: ConditionAny.h:47
dd4hep::ConditionAny::flags
mask_type flags() const
Flag operations: Get condition flags.
Definition: ConditionAny.cpp:96
dd4hep::ConditionAny::testFlag
bool testFlag(mask_type option) const
Flag operations: Test for a given a conditons flag.
Definition: ConditionAny.cpp:111
dd4hep::ConditionAny::setFlag
void setFlag(mask_type option)
Flag operations: Set a conditons flag.
Definition: ConditionAny.cpp:101
dd4hep::ConditionKey::KeyMaker
Helper union to interprete conditions keys.
Definition: Conditions.h:295
dd4hep::ConditionAny::item_key
itemkey_type item_key() const
Item part of the identifier.
Definition: ConditionAny.cpp:91
dd4hep::IOVType
Class describing the interval of validty type.
Definition: IOV.h:36
dd4hep::BasicGrammar::type_name
const std::string & type_name() const
Access to the type information name.
Definition: Grammar.h:129
dd4hep::detail::ConditionObject::unFlag
void unFlag(Condition::mask_type option)
Flag operations: UN-Set a conditons flag.
Definition: ConditionsInterna.h:124
dd4hep::OpaqueDataBlock::bind
void * bind(const BasicGrammar *grammar)
Bind data value.
Definition: OpaqueData.cpp:124
dd4hep::IOV
Class describing the interval of validty.
Definition: IOV.h:66
ConditionAny.h
dd4hep::BasicGrammar
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: Grammar.h:55
dd4hep::Handle< detail::ConditionObject >::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::detail::ConditionObject::iovType
const IOVType * iovType() const
Access safely the IOV-type.
Definition: ConditionsInterna.cpp:92
dd4hep::ConditionAny::key
key_type key() const
Hash identifier.
Definition: ConditionAny.cpp:81
dd4hep::OpaqueData
Class describing an opaque data block.
Definition: OpaqueData.h:38
dd4hep::ConditionAny::detector_key
detkey_type detector_key() const
DetElement part of the identifier.
Definition: ConditionAny.cpp:86
dd4hep::ConditionAny::unFlag
void unFlag(mask_type option)
Flag operations: UN-Set a conditons flag.
Definition: ConditionAny.cpp:106
dd4hep::OpaqueDataBlock::ptr
void * ptr() const
Write access to the data buffer. Is only valid after call to bind<T>()
Definition: OpaqueData.h:132
dd4hep::Handle< detail::ConditionObject >::m_element
detail::ConditionObject * m_element
Single and only data member: Reference to the actual element.
Definition: Handle.h:91
dd4hep::OpaqueData::ptr
const void * ptr() const
Access to the data buffer (read only!). Is only valid after call to bind<T>()
Definition: OpaqueData.h:68
dd4hep::detail::ConditionObject::setFlag
void setFlag(Condition::mask_type option)
Flag operations: Set a conditons flag.
Definition: ConditionsInterna.h:122
dd4hep::detail::ConditionObject::iovData
const IOV * iovData() const
Access safely the IOV.
Definition: ConditionsInterna.cpp:85
dd4hep::ConditionAny::any_type_name
const std::string any_type_name() const
Access to the type information as string.
Definition: ConditionAny.cpp:149
dd4hep::ConditionAny::iov
const IOV & iov() const
Access the IOV block.
Definition: ConditionAny.cpp:76
dd4hep::detail::ConditionObject
The data class behind a conditions handle.
Definition: ConditionsInterna.h:68
dd4hep::Handle< detail::ConditionObject >::access
detail::ConditionObject * access() const
Checked object access. Throws invalid handle runtime exception if invalid handle.
dd4hep::ConditionAny::any_type
const std::type_info & any_type() const
Access to the type information.
Definition: ConditionAny.cpp:139
dd4hep::ConditionAny::mask_type
Condition::mask_type mask_type
Forward definition of the object properties.
Definition: ConditionAny.h:51
dd4hep::Handle< detail::ConditionObject >::ptr
detail::ConditionObject * ptr() const
Access to the held object.
Definition: Handle.h:151
dd4hep::ConditionKey::KeyMaker::det_key
Condition::detkey_type det_key
Definition: Conditions.h:303
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::hash
unsigned int hash(unsigned int initialSeed, unsigned int eventNumber, unsigned int runNumber)
calculate hash from initialSeed, eventID and runID
Definition: Geant4EventSeed.h:201
dd4hep::detail::ConditionObject::data
OpaqueDataBlock data
Data block.
Definition: ConditionsInterna.h:81
dd4hep::OpaqueData::grammar
const BasicGrammar * grammar
Data type.
Definition: OpaqueData.h:52
dd4hep::ConditionAny::has_value
bool has_value() const
Checks whether the object contains a value.
Definition: ConditionAny.cpp:134
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:71
dd4hep::ConditionAny::ConditionAny
ConditionAny()=default
Default constructor.
dd4hep::ConditionKey::KeyMaker::values
struct dd4hep::ConditionKey::KeyMaker::@2 values
dd4hep::detail::ConditionObject::testFlag
bool testFlag(Condition::mask_type option) const
Flag operations: Test for a given a conditons flag.
Definition: ConditionsInterna.h:126
dd4hep::ConditionAny::itemkey_type
Condition::itemkey_type itemkey_type
Low part of the key identifies the item identifier.
Definition: ConditionAny.h:49
Printout.h
dd4hep::ConditionKey::KeyMaker::item_key
Condition::itemkey_type item_key
Definition: Conditions.h:302