DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
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 <iomanip>
21 
22 using namespace dd4hep;
23 
24 namespace {
25  inline const BasicGrammar* any_grammar() {
26  static const BasicGrammar* s_any_grammar = &BasicGrammar::instance<std::any>();
27  return s_any_grammar;
28  }
29 }
30 
33 {
34  if ( hash_key != 0 && hash_key != ~0x0ULL ) {
35  Object* o = new Object();
36  this->assign(o, "", "");
37  o->data.bind<std::any>();
38  o->hash = hash_key;
39  return;
40  }
41  except("ConditionAny","+++ Cannot create a any-condition with an invalid key: %016llX",hash_key);
42 }
43 
45 ConditionAny::ConditionAny(const std::string& nam, const std::string& typ) : Handle<Object>()
46 {
47  Object* o = new Object();
48  this->assign(o, nam, typ);
49  o->data.bind<std::any>();
50  o->hash = 0;
51 }
52 
54  if ( obj ) {
55  if ( !obj->data.grammar ) {
56  except("ConditionAny",
57  "+++ Cannot assign unbound conditions data to handle. [Invalid operation]");
58  }
59  if ( obj->data.grammar != any_grammar() ) {
60  except("ConditionAny",
61  "+++ Cannot assign data of type " +
62  obj->data.grammar->type_name() +
63  " to handle holding std::any. [Invalid operation]");
64  }
65  }
66  this->m_element = obj;
67 }
68 
71  return *(this->access()->iovType());
72 }
73 
76  return *(this->access()->iovData());
77 }
78 
81  return this->access()->hash;
82 }
83 
87 }
88 
92 }
93 
96  return access()->flags;
97 }
98 
101  access()->setFlag(option);
102 }
103 
106  access()->unFlag(option);
107 }
108 
110 bool ConditionAny::testFlag(mask_type option) const {
111  return access()->testFlag(option);
112 }
113 
115 std::any& ConditionAny::get() {
116  OpaqueData& o = this->access()->data;
117  if ( o.grammar && (o.grammar == any_grammar()) ) {
118  return *(std::any*)o.ptr();
119  }
120  throw std::bad_cast();
121 }
122 
124 const std::any& ConditionAny::get() const {
125  const OpaqueData& o = this->access()->data;
126  if ( o.grammar && (o.grammar == any_grammar()) ) {
127  return *(std::any*)o.ptr();
128  }
129  throw std::bad_cast();
130 }
131 
134  return this->get().has_value();
135 }
136 
138 const std::type_info& ConditionAny::any_type() const {
139  const Object* o = this->ptr();
140  if ( o && o->data.grammar && (o->data.grammar == any_grammar()) ) {
141  const std::any* a = (const std::any*)o->data.ptr();
142  return a->type();
143  }
144  return typeid(void);
145 }
146 
148 const std::string ConditionAny::any_type_name() const {
149  const Object* o = this->ptr();
150  if ( o && o->data.grammar && (o->data.grammar == any_grammar()) ) {
151  const std::any* a = (const std::any*)o->data.ptr();
152  return typeName(a->type());
153  }
154  return typeName(typeid(void));
155 }
dd4hep::ConditionAny::get
std::any & get()
Generic getter. Specify the exact type, not a polymorph type.
Definition: ConditionAny.cpp:115
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:88
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:53
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: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::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::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:90
dd4hep::IOVType
Class describing the interval of validty type.
Definition: IOV.h:37
dd4hep::BasicGrammar::type_name
const std::string & type_name() const
Access to the type information name.
Definition: Grammar.h:130
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:67
ConditionAny.h
dd4hep::BasicGrammar
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: Grammar.h:56
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:80
dd4hep::OpaqueData
Class describing an opaque data block.
Definition: OpaqueData.h:39
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::OpaqueDataBlock::ptr
void * ptr() const
Write access to the data buffer. Is only valid after call to bind<T>()
Definition: OpaqueData.h:133
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::OpaqueData::ptr
const void * ptr() const
Access to the data buffer (read only!). Is only valid after call to bind<T>()
Definition: OpaqueData.h:69
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: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::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:138
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:153
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:53
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::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