Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//==========================================================================
// AIDA Detector description implementation for LCD
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// All rights reserved.
//
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
//
// Author : M.Frank
//
//==========================================================================
// Framework includes
#include "DD4hep/Printout.h"
#include "DD4hep/Primitives.h"
#include "DD4hep/OpaqueData.h"
#include "DD4hep/InstanceCount.h"
#include "DD4hep/objects/OpaqueData_inl.h"
// C/C++ header files
#include <cstring>
using namespace std;
using namespace DD4hep;
/// Create data block from string representation
bool OpaqueData::fromString(const string& rep) {
if ( pointer && grammar ) {
return grammar->fromString(pointer,rep);
}
throw runtime_error("Opaque data block is unbound. Cannot parse string representation.");
}
/// Create string representation of the data block
string OpaqueData::str() const {
if ( pointer && grammar ) {
return grammar->str(pointer);
}
throw runtime_error("Opaque data block is unbound. Cannot create string representation.");
}
/// Access type id of the condition
const type_info& OpaqueData::typeInfo() const {
if ( pointer && grammar ) {
return grammar->type();
}
throw runtime_error("Opaque data block is unbound. Cannot determine type information!");
}
/// Access type name of the condition data block
const string& OpaqueData::dataType() const {
if ( pointer && grammar ) {
return grammar->type_name();
}
throw runtime_error("Opaque data block is unbound. Cannot determine type information!");
}
/// Standard initializing constructor
OpaqueDataBlock::OpaqueDataBlock() : OpaqueData(), destruct(0), copy(0), type(0) {
InstanceCount::increment(this);
}
/// Copy constructor
OpaqueDataBlock::OpaqueDataBlock(const OpaqueDataBlock& c)
: OpaqueData(c), destruct(c.destruct), copy(c.copy), type(c.type) {
grammar = 0;
pointer = 0;
this->bind(c.grammar,c.copy,c.destruct);
this->copy(pointer,c.pointer);
InstanceCount::increment(this);
}
/// Standard Destructor
OpaqueDataBlock::~OpaqueDataBlock() {
if ( destruct ) {
(*destruct)(pointer);
if ( (type&ALLOC_DATA) == ALLOC_DATA ) ::operator delete(pointer);
}
pointer = 0;
grammar = 0;
InstanceCount::decrement(this);
}
/// Move the data content: 'from' will be reset to NULL
bool OpaqueDataBlock::move(OpaqueDataBlock& from) {
pointer = from.pointer;
grammar = from.grammar;
::memcpy(data,from.data,sizeof(data));
destruct = from.destruct;
copy = from.copy;
type = from.type;
::memset(from.data,0,sizeof(data));
from.type = PLAIN_DATA;
from.destruct = 0;
from.copy = 0;
from.pointer = 0;
from.grammar = 0;
return true;
}
/// Copy constructor
OpaqueDataBlock& OpaqueDataBlock::operator=(const OpaqueDataBlock& c) {
if ( this != &c ) {
if ( this->grammar == c.grammar ) {
if ( destruct ) {
(*destruct)(pointer);
if ( (type&ALLOC_DATA) == ALLOC_DATA ) ::operator delete(pointer);
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
}
pointer = 0;
grammar = 0;
}
if ( this->grammar == 0 ) {
this->OpaqueData::operator=(c);
this->destruct = c.destruct;
this->copy = c.copy;
this->type = c.type;
this->grammar = 0;
this->bind(c.grammar,c.copy,c.destruct);
this->copy(pointer,c.pointer);
return *this;
}
except("OpaqueData","You may not bind opaque data multiple times!");
}
return *this;
}
/// Set data value
bool OpaqueDataBlock::bind(const BasicGrammar* g, void (*ctor)(void*,const void*), void (*dtor)(void*)) {
if ( !grammar ) {
size_t len = g->sizeOf();
grammar = g;
destruct = dtor;
copy = ctor;
(len > sizeof(data))
? (pointer=::operator new(len),type=ALLOC_DATA)
: (pointer=data,type=PLAIN_DATA);
return true;
}
else if ( grammar == g ) {
// We cannot ingore secondary requests for data bindings.
// This leads to memory leaks in the caller!
except("OpaqueData","You may not bind opaque multiple times!");
}
typeinfoCheck(grammar->type(),g->type(),"Opaque data blocks may not be assigned.");
return false;
}
/// Set data value
void OpaqueDataBlock::assign(const void* ptr, const type_info& typ) {
if ( !grammar ) {
except("OpaqueData","Opaque data block is unbound. Cannot copy data.");
}
else if ( grammar->type() != typ ) {
except("OpaqueData","Bad data binding binding");
}
(*copy)(pointer,ptr);
}