DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OpaqueData.cpp
Go to the documentation of this file.
1 // $Id$
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // Author : M.Frank
12 //
13 //==========================================================================
14 
15 // Framework includes
16 #include "DD4hep/Printout.h"
17 #include "DD4hep/Primitives.h"
18 #include "DD4hep/OpaqueData.h"
19 #include "DD4hep/InstanceCount.h"
21 
22 // C/C++ header files
23 #include <cstring>
24 
25 using namespace std;
26 using namespace DD4hep;
27 
29 OpaqueData::OpaqueData() : grammar(0), pointer(0) {
30 }
31 
33 OpaqueData::OpaqueData(const OpaqueData& c) : grammar(c.grammar), pointer(c.pointer) {
34 }
35 
38 }
39 
42  if ( &c != this ) {
43  grammar = c.grammar;
44  pointer = c.pointer;
45  }
46  return *this;
47 }
48 
50 bool OpaqueData::fromString(const string& rep) {
51  if ( pointer && grammar ) {
52  return grammar->fromString(pointer,rep);
53  }
54  throw runtime_error("Opaque data block is unbound. Cannot parse string representation.");
55 }
56 
58 string OpaqueData::str() const {
59  if ( pointer && grammar ) {
60  return grammar->str(pointer);
61  }
62  throw runtime_error("Opaque data block is unbound. Cannot create string representation.");
63 }
64 
66 const type_info& OpaqueData::typeInfo() const {
67  if ( pointer && grammar ) {
68  return grammar->type();
69  }
70  throw runtime_error("Opaque data block is unbound. Cannot determine type information!");
71 }
72 
74 const string& OpaqueData::dataType() const {
75  if ( pointer && grammar ) {
76  return grammar->type_name();
77  }
78  throw runtime_error("Opaque data block is unbound. Cannot determine type information!");
79 }
80 
82 OpaqueDataBlock::OpaqueDataBlock() : OpaqueData(), destruct(0), copy(0), type(0) {
84 }
85 
88  : OpaqueData(c), destruct(c.destruct), copy(c.copy), type(c.type) {
89  grammar = 0;
90  pointer = 0;
91  this->bind(c.grammar,c.copy,c.destruct);
92  this->copy(pointer,c.pointer);
94 }
95 
98  if ( destruct ) {
99  (*destruct)(pointer);
100  if ( (type&ALLOC_DATA) == ALLOC_DATA ) ::operator delete(pointer);
101  }
102  pointer = 0;
103  grammar = 0;
105 }
106 
109  pointer = from.pointer;
110  grammar = from.grammar;
111  ::memcpy(data,from.data,sizeof(data));
112  destruct = from.destruct;
113  copy = from.copy;
114  type = from.type;
115  ::memset(from.data,0,sizeof(data));
116  from.type = PLAIN_DATA;
117  from.destruct = 0;
118  from.copy = 0;
119  from.pointer = 0;
120  from.grammar = 0;
121  return true;
122 }
123 
126  if ( this != &c ) {
127  if ( this->grammar == c.grammar ) {
128  if ( destruct ) {
129  (*destruct)(pointer);
130  if ( (type&ALLOC_DATA) == ALLOC_DATA ) ::operator delete(pointer);
131  }
132  pointer = 0;
133  grammar = 0;
134  }
135  if ( this->grammar == 0 ) {
136  this->OpaqueData::operator=(c);
137  this->destruct = c.destruct;
138  this->copy = c.copy;
139  this->type = c.type;
140  this->grammar = 0;
141  this->bind(c.grammar,c.copy,c.destruct);
142  this->copy(pointer,c.pointer);
143  return *this;
144  }
145  except("OpaqueData","You may not bind opaque data multiple times!");
146  }
147  return *this;
148 }
149 
151 bool OpaqueDataBlock::bind(const BasicGrammar* g, void (*ctor)(void*,const void*), void (*dtor)(void*)) {
152  if ( !grammar ) {
153  size_t len = g->sizeOf();
154  grammar = g;
155  destruct = dtor;
156  copy = ctor;
157  (len > sizeof(data))
158  ? (pointer=::operator new(len),type=ALLOC_DATA)
160  return true;
161  }
162  else if ( grammar == g ) {
163  // We cannot ingore secondary requests for data bindings.
164  // This leads to memory leaks in the caller!
165  except("OpaqueData","You may not bind opaque multiple times!");
166  }
167  typeinfoCheck(grammar->type(),g->type(),"Opaque data blocks may not be assigned.");
168  return false;
169 }
170 
172 void OpaqueDataBlock::assign(const void* ptr, const type_info& typ) {
173  if ( !grammar ) {
174  except("OpaqueData","Opaque data block is unbound. Cannot copy data.");
175  }
176  else if ( grammar->type() != typ ) {
177  except("OpaqueData","Bad data binding binding");
178  }
179  (*copy)(pointer,ptr);
180 }
virtual const std::string & type_name() const =0
Access to the type information name.
Class describing an opaque conditions data block.
Definition: OpaqueData.h:81
OpaqueDataBlock & operator=(const OpaqueDataBlock &clone)
Assignment operator.
Definition: OpaqueData.cpp:125
virtual const std::type_info & type() const =0
Access to the type information.
virtual std::string str(const void *ptr) const =0
Serialize an opaque value to a string.
static void decrement(T *)
Decrement count according to type information.
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: BasicGrammar.h:37
Class describing an opaque data block.
Definition: OpaqueData.h:36
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
OpaqueDataBlock()
Standard initializing constructor.
Definition: OpaqueData.cpp:82
int except(const std::string &src, const std::string &fmt,...)
Calls the display action with ERROR and throws an std::runtime_error exception.
Definition: Printout.cpp:217
virtual size_t sizeOf() const =0
Access the object size (sizeof operator)
void(* copy)(void *, const void *)
Constructor function – only set if the object is valid.
Definition: OpaqueData.h:95
std::string str() const
Create string representation of the data block.
Definition: OpaqueData.cpp:58
bool move(OpaqueDataBlock &from)
Move the data content: 'from' will be reset to NULL.
Definition: OpaqueData.cpp:108
virtual ~OpaqueData()
Standard Destructor.
Definition: OpaqueData.cpp:37
OpaqueData()
Standard initializing constructor.
Definition: OpaqueData.cpp:29
virtual bool fromString(void *ptr, const std::string &value) const =0
Set value from serialized string. On successful data conversion TRUE is returned. ...
static const double g
Definition: DD4hepUnits.h:162
bool fromString(const std::string &rep)
Create data block from string representation.
Definition: OpaqueData.cpp:50
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
const std::string & dataType() const
Access type name of the condition data block.
Definition: OpaqueData.cpp:74
void * pointer
Pointer to object data.
Definition: OpaqueData.h:54
unsigned char data[sizeof(std::vector< void * >)]
Data buffer: plain data are allocated directly on this buffer.
Definition: OpaqueData.h:91
void assign(const void *ptr, const std::type_info &typ)
Set data value.
Definition: OpaqueData.cpp:172
const BasicGrammar * grammar
Data type.
Definition: OpaqueData.h:50
T & bind()
Bind data value.
OpaqueData & operator=(const OpaqueData &c)
Assignment operator.
Definition: OpaqueData.cpp:41
void typeinfoCheck(const std::type_info &typ1, const std::type_info &typ2, const std::string &text="")
Check type infos for equivalence (dynamic casts) using ABI information.
Definition: Primitives.cpp:213
const std::type_info & typeInfo() const
Access type id of the condition.
Definition: OpaqueData.cpp:66
~OpaqueDataBlock()
Standard Destructor.
Definition: OpaqueData.cpp:97
unsigned int type
Data buffer type: Must be a bitmap!
Definition: OpaqueData.h:99
void(* destruct)(void *)
Destructor function – only set if the object is valid.
Definition: OpaqueData.h:93