DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
OpaqueData.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/Primitives.h>
17 #include <DD4hep/OpaqueData.h>
18 #include <DD4hep/InstanceCount.h>
19 
20 // C/C++ header files
21 #include <cstring>
22 
23 using namespace dd4hep;
24 
26 bool OpaqueData::fromString(const std::string& rep) {
27  if ( pointer && grammar ) {
28  return grammar->fromString(pointer,rep);
29  }
30  throw std::runtime_error("Opaque data block is unbound. Cannot parse string representation.");
31 }
32 
34 std::string OpaqueData::str() const {
35  if ( pointer && grammar ) {
36  return grammar->str(pointer);
37  }
38  throw std::runtime_error("Opaque data block is unbound. Cannot create string representation.");
39 }
40 
42 const std::type_info& OpaqueData::typeInfo() const {
43  if ( pointer && grammar ) {
44  return grammar->type();
45  }
46  throw std::runtime_error("Opaque data block is unbound. Cannot determine type information!");
47 }
48 
50 const std::string& OpaqueData::dataType() const {
51  if ( pointer && grammar ) {
52  return grammar->type_name();
53  }
54  throw std::runtime_error("Opaque data block is unbound. Cannot determine type information!");
55 }
56 
60 }
61 
64  : OpaqueData(c), type(c.type) {
65  grammar = 0;
66  pointer = 0;
67  this->bind(c.grammar);
68  if ( this->grammar->specialization.copy ) {
70  }
71  else {
72  except("OpaqueDataBlock","Grammar type %s does not support object copy. Operation not allowed.",
73  this->grammar->type_name().c_str());
74  }
76 }
77 
80  if ( pointer && (type&EXTERN_DATA) != EXTERN_DATA ) {
82  if ( (type&ALLOC_DATA) == ALLOC_DATA ) ::operator delete(pointer);
83  }
84  pointer = 0;
85  grammar = 0;
87 }
88 
91  if ( this != &c ) {
92  if ( grammar == c.grammar ) {
93  if ( pointer ) {
95  if ( (type&ALLOC_DATA) == ALLOC_DATA ) ::operator delete(pointer);
96  }
97  pointer = 0;
98  grammar = 0;
99  }
100  if ( grammar == 0 ) {
101  this->OpaqueData::operator=(c);
102  type = c.type;
103  grammar = 0;
104  if ( c.grammar ) {
105  if ( !c.grammar->specialization.copy ) {
106  except("OpaqueDataBlock","Grammar type %s does not support object copy. Operation not allowed.",
107  c.grammar->type_name().c_str());
108  }
109  bind(c.grammar);
111  return *this;
112  }
113  else if ( (c.type&EXTERN_DATA) == EXTERN_DATA ) {
114  pointer = c.pointer;
115  return *this;
116  }
117  }
118  except("OpaqueData","You may not bind opaque data multiple times!");
119  }
120  return *this;
121 }
122 
125  if ( (type&EXTERN_DATA) == EXTERN_DATA ) {
126  except("OpaqueData","Extern data may not be bound!");
127  }
128  else if ( !grammar ) {
129  size_t len = g->sizeOf();
130  grammar = g;
131  (len > sizeof(data))
132  ? (pointer=::operator new(len),type=ALLOC_DATA)
134  return pointer;
135  }
136  else if ( grammar == g ) {
137  // We cannot ingore secondary requests for data bindings.
138  // This leads to memory leaks in the caller!
139  except("OpaqueData","You may not bind opaque multiple times!");
140  }
141  typeinfoCheck(grammar->type(),g->type(),"Opaque data blocks may not be assigned.");
142  return 0;
143 }
144 
146 void OpaqueDataBlock::bindExtern(void* ptr, const BasicGrammar* gr) {
147  if ( grammar != 0 && type != EXTERN_DATA ) {
148  // We cannot ingore secondary requests for data bindings.
149  // This leads to memory leaks in the caller!
150  except("OpaqueData","You may not bind opaque data multiple times!");
151  }
152  pointer = ptr;
153  grammar = gr;
154  type = EXTERN_DATA;
155 }
156 
158 void* OpaqueDataBlock::bind(void* ptr, size_t size, const BasicGrammar* g) {
159  if ( (type&EXTERN_DATA) == EXTERN_DATA ) {
160  except("OpaqueData","Extern data may not be bound!");
161  }
162  else if ( !grammar ) {
163  size_t len = g->sizeOf();
164  grammar = g;
165  if ( len <= size )
167  else if ( len <= sizeof(data) )
169  else
170  pointer=::operator new(len),type=ALLOC_DATA;
171  return pointer;
172  }
173  else if ( grammar == g ) {
174  // We cannot ingore secondary requests for data bindings.
175  // This leads to memory leaks in the caller!
176  except("OpaqueData","You may not bind opaque data multiple times!");
177  }
178  typeinfoCheck(grammar->type(),g->type(),"Opaque data blocks may not be assigned.");
179  return 0;
180 }
181 
182 #include <DD4hep/GrammarUnparsed.h>
183 static auto s_registry = GrammarRegistry::pre_note<OpaqueDataBlock>(1);
dd4hep::BasicGrammar::sizeOf
virtual size_t sizeOf() const =0
Access the object size (sizeof operator)
dd4hep::OpaqueDataBlock::type
unsigned int type
Data buffer type: Must be a bitmap of enum _DataTypes!
Definition: OpaqueData.h:119
dd4hep::BasicGrammar::specialization_t::copy
void(* copy)(void *to, const void *from)=0
Opaque copy constructor.
Definition: Grammar.h:78
dd4hep::OpaqueDataBlock::data
unsigned char data[BUFFER_SIZE]
Data buffer: plain data are allocated directly on this buffer.
Definition: OpaqueData.h:108
dd4hep::BasicGrammar::str
virtual std::string str(const void *ptr) const
Serialize an opaque value to a string.
Definition: Grammar.cpp:261
dd4hep::OpaqueDataBlock
Class describing an opaque conditions data block.
Definition: OpaqueData.h:92
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::OpaqueData::fromString
bool fromString(const std::string &rep)
No ROOT persistency.
Definition: OpaqueData.cpp:26
dd4hep::BasicGrammar::fromString
virtual bool fromString(void *ptr, const std::string &value) const
Set value from serialized string. On successful data conversion TRUE is returned.
Definition: Grammar.cpp:269
dd4hep::BasicGrammar::type_name
const std::string & type_name() const
Access to the type information name.
Definition: Grammar.h:130
dd4hep::OpaqueDataBlock::EXTERN_DATA
@ EXTERN_DATA
Definition: OpaqueData.h:116
dd4hep::OpaqueDataBlock::OpaqueDataBlock
OpaqueDataBlock()
Standard initializing constructor.
Definition: OpaqueData.cpp:58
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::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::OpaqueData
Class describing an opaque data block.
Definition: OpaqueData.h:39
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
GrammarUnparsed.h
OpaqueData.h
dd4hep::BasicGrammar::specialization
struct dd4hep::BasicGrammar::specialization_t specialization
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::BasicGrammar::type
virtual const std::type_info & type() const =0
Access to the type information.
Primitives.h
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
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::BasicGrammar::destruct
virtual void destruct(void *pointer) const =0
Opaque object destructor.
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
InstanceCount.h
dd4hep::OpaqueDataBlock::~OpaqueDataBlock
~OpaqueDataBlock()
Standard Destructor.
Definition: OpaqueData.cpp:79
Printout.h