DD4hep  1.28.0
Detector Description Toolkit for High Energy Physics
OpaqueData.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_OPAQUEDATA_H
14 #define DD4HEP_OPAQUEDATA_H
15 
16 // Framework include files
17 #include "DD4hep/Grammar.h"
18 
19 // C/C++ include files
20 #include <typeinfo>
21 #include <vector>
22 #include <string>
23 
25 namespace dd4hep {
26 
27  // Forward declarations
28  class BasicGrammar;
29 
31 
39  class OpaqueData {
40  private:
41  protected:
43  OpaqueData() = default;
45  virtual ~OpaqueData() = default;
47  OpaqueData(const OpaqueData& copy) = default;
49  OpaqueData& operator=(const OpaqueData& copy) = default;
50 
51  public:
53  const BasicGrammar* grammar = 0;
54 
55  protected:
57  void* pointer = 0;
58 
59  public:
61  bool fromString(const std::string& rep);
63  std::string str() const;
65  const std::type_info& typeInfo() const;
67  const std::string& dataType() const;
69  const void* ptr() const { return pointer; }
71  bool is_bound() const { return 0 != pointer; }
73  template <typename T> T& get();
75  template <typename T> const T& get() const;
77  template <typename T> T& as();
79  template <typename T> const T& as() const;
80  };
81 
82 
84 
92  class OpaqueDataBlock : public OpaqueData {
93 
94  public:
96  constexpr static const size_t BUFFER_SIZE = 40;
97 
98  protected:
100 
108  unsigned char data[BUFFER_SIZE];
109 
110  public:
111  enum _DataTypes {
112  PLAIN_DATA = 1<<0,
113  ALLOC_DATA = 1<<1,
114  STACK_DATA = 1<<2,
115  BOUND_DATA = 1<<3,
116  EXTERN_DATA = 1<<4
117  };
119  unsigned int type;
120 
121  public:
123  OpaqueDataBlock();
125  template <typename OBJECT> OpaqueDataBlock(OBJECT&& data);
133  void* ptr() const { return pointer; }
135  void* bind(const BasicGrammar* grammar);
137  void* bind(void* ptr, size_t len, const BasicGrammar* grammar);
139  void bindExtern(void* ptr, const BasicGrammar* grammar);
141  template <typename T, typename... Args> T& construct(Args... args);
143  template <typename T> T& bind();
145  template <typename T> T& bind(void* ptr, size_t len);
147  template <typename T> T& bind(const std::string& value);
149  template <typename T> T& bind(void* ptr, size_t len, const std::string& value);
151  template <typename T> T& bind(T&& data);
153  template <typename T> void bindExtern(T* ptr);
154  };
155 
157  template <typename T> inline T& OpaqueData::get() {
158  if (!grammar || !grammar->equals(typeid(T))) { throw std::bad_cast(); }
159  return *(T*)pointer;
160  }
161 
163  template <typename T> inline const T& OpaqueData::get() const {
164  if (!grammar || !grammar->equals(typeid(T))) { throw std::bad_cast(); }
165  return *(T*)pointer;
166  }
167 
169  template <typename T> inline T& OpaqueData::as() {
170  if ( grammar ) {
171  T* obj = (T*)(grammar->cast().apply_dynCast(Cast::instance<T>(), this->pointer));
172  if ( obj ) return *obj;
173  }
174  throw std::bad_cast();
175  }
176 
178  template <typename T> inline const T& OpaqueData::as() const {
179  if ( grammar ) {
180  const T* obj = (const T*)(grammar->cast().apply_dynCast(Cast::instance<T>(), this->pointer));
181  if ( obj ) return *obj;
182  }
183  throw std::bad_cast();
184  }
185 
187  template <typename OBJECT> OpaqueDataBlock::OpaqueDataBlock(OBJECT&& obj) {
188  this->bind(&BasicGrammar::instance<OBJECT>());
189  new(this->pointer) OBJECT(std::move(obj));
190  }
191 
193  template <typename T, typename... Args> inline T& OpaqueDataBlock::construct(Args... args) {
194  this->bind(&BasicGrammar::instance<T>());
195  return *(new(this->pointer) T(std::forward<Args>(args)...));
196  }
197 
199  template <typename T> inline T& OpaqueDataBlock::bind() {
200  this->bind(&BasicGrammar::instance<T>());
201  return *(new(this->pointer) T());
202  }
203 
205  template <typename T> inline T& OpaqueDataBlock::bind(T&& obj) {
206  this->bind(&BasicGrammar::instance<T>());
207  new(this->pointer) T(std::move(obj));
208  }
209 
211  template <typename T> inline T& OpaqueDataBlock::bind(void* ptr, size_t len) {
212  this->bind(ptr,len,&BasicGrammar::instance<T>());
213  return *(new(this->pointer) T());
214  }
215 
217  template <typename T> inline T& OpaqueDataBlock::bind(const std::string& value) {
218  T& ret = this->bind<T>();
219  if ( !value.empty() && !this->fromString(value) ) {
220  throw std::runtime_error("OpaqueDataBlock::set> Failed to bind type "+
221  typeName(typeid(T))+" to condition data block.");
222  }
223  return ret;
224  }
225 
227  template <typename T> inline T& OpaqueDataBlock::bind(void* ptr, size_t len, const std::string& value) {
228  T& ret = this->bind<T>(ptr, len);
229  if ( !value.empty() && !this->fromString(value) ) {
230  throw std::runtime_error("OpaqueDataBlock::set> Failed to bind type "+
231  typeName(typeid(T))+" to condition data block.");
232  }
233  return ret;
234  }
236  template <typename T> inline void OpaqueDataBlock::bindExtern(T* ptr) {
237  bindExtern(ptr, &BasicGrammar::instance<T>());
238  }
239 
240 } /* End namespace dd4hep */
241 #endif // DD4HEP_OPAQUEDATA_H
dd4hep::OpaqueData::~OpaqueData
virtual ~OpaqueData()=default
Standard Destructor.
dd4hep::BasicGrammar::equals
virtual bool equals(const std::type_info &other_type) const =0
Access to the type information.
dd4hep::OpaqueData::OpaqueData
OpaqueData()=default
Standard initializing constructor.
dd4hep::OpaqueDataBlock::BOUND_DATA
@ BOUND_DATA
Definition: OpaqueData.h:115
dd4hep::OpaqueDataBlock::BUFFER_SIZE
constexpr static const size_t BUFFER_SIZE
Buffer size of the in-place data buffer.
Definition: OpaqueData.h:96
dd4hep::OpaqueDataBlock::construct
T & construct(Args... args)
Construct conditions object and bind the data.
Definition: OpaqueData.h:193
dd4hep::OpaqueDataBlock::type
unsigned int type
Data buffer type: Must be a bitmap of enum _DataTypes!
Definition: OpaqueData.h:119
dd4hep::OpaqueData::OpaqueData
OpaqueData(const OpaqueData &copy)=default
Copy constructor.
dd4hep::OpaqueDataBlock::data
unsigned char data[BUFFER_SIZE]
Data buffer: plain data are allocated directly on this buffer.
Definition: OpaqueData.h:108
dd4hep::OpaqueDataBlock::_DataTypes
_DataTypes
Definition: OpaqueData.h:111
dd4hep::OpaqueDataBlock
Class describing an opaque conditions data block.
Definition: OpaqueData.h:92
dd4hep::OpaqueData::fromString
bool fromString(const std::string &rep)
No ROOT persistency.
Definition: OpaqueData.cpp:26
dd4hep::OpaqueData::is_bound
bool is_bound() const
Check if object is already bound....
Definition: OpaqueData.h:71
dd4hep::OpaqueDataBlock::EXTERN_DATA
@ EXTERN_DATA
Definition: OpaqueData.h:116
dd4hep::OpaqueDataBlock::OpaqueDataBlock
OpaqueDataBlock()
Standard initializing constructor.
Definition: OpaqueData.cpp:58
dd4hep::OpaqueData::as
T & as()
Generic getter. Resolves polymorph types. It is mandatory that the datatype is polymorph!
Definition: OpaqueData.h:169
dd4hep::BasicGrammar
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: Grammar.h:56
dd4hep::OpaqueDataBlock::STACK_DATA
@ STACK_DATA
Definition: OpaqueData.h:114
dd4hep::OpaqueData::operator=
OpaqueData & operator=(const OpaqueData &copy)=default
Assignment operator.
dd4hep::OpaqueData
Class describing an opaque data block.
Definition: OpaqueData.h:39
dd4hep::OpaqueData::get
T & get()
Generic getter. Specify the exact type, not a polymorph type.
Definition: OpaqueData.h:157
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::OpaqueDataBlock::bind
T & bind()
Bind data value.
Definition: OpaqueData.h:199
dd4hep::OpaqueDataBlock::ALLOC_DATA
@ ALLOC_DATA
Definition: OpaqueData.h:113
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::OpaqueData::typeInfo
const std::type_info & typeInfo() const
Access type id of the condition.
Definition: OpaqueData.cpp:42
dd4hep::OpaqueDataBlock::operator=
OpaqueDataBlock & operator=(const OpaqueDataBlock &copy)
Assignment operator (Required by ROOT dictionaries)
Definition: OpaqueData.cpp:90
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::BasicGrammar::cast
virtual const Cast & cast() const
Access ABI object cast.
Definition: Grammar.cpp:253
dd4hep::OpaqueDataBlock::PLAIN_DATA
@ PLAIN_DATA
Definition: OpaqueData.h:112
dd4hep::OpaqueDataBlock::bindExtern
void bindExtern(void *ptr, const BasicGrammar *grammar)
Bind external data value to the pointer.
Definition: OpaqueData.cpp:146
dd4hep::OpaqueData::grammar
const BasicGrammar * grammar
Data type.
Definition: OpaqueData.h:53
dd4hep::OpaqueData::dataType
const std::string & dataType() const
Access type name of the condition data block.
Definition: OpaqueData.cpp:50
dd4hep::detail::tools::copy
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
Definition: AlignmentTools.cpp:43
dd4hep::OpaqueData::str
std::string str() const
Create string representation of the data block.
Definition: OpaqueData.cpp:34
dd4hep::OpaqueData::pointer
void * pointer
No ROOT persistency.
Definition: OpaqueData.h:57
dd4hep::OpaqueDataBlock::~OpaqueDataBlock
~OpaqueDataBlock()
Standard Destructor.
Definition: OpaqueData.cpp:79
Grammar.h