forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurableParamHelper.h
More file actions
345 lines (283 loc) · 12.2 KB
/
ConfigurableParamHelper.h
File metadata and controls
345 lines (283 loc) · 12.2 KB
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
107
108
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
//first version 8/2018, Sandro Wenzel
#ifndef COMMON_SIMCONFIG_INCLUDE_SIMCONFIG_CONFIGURABLEPARAMHELPER_H_
#define COMMON_SIMCONFIG_INCLUDE_SIMCONFIG_CONFIGURABLEPARAMHELPER_H_
#include "CommonUtils/ConfigurableParam.h"
#include "TClass.h"
#include <type_traits>
#include <typeinfo>
#include "TFile.h"
namespace o2
{
namespace conf
{
// ----------------------------------------------------------------
// Utility structure for passing around ConfigurableParam data member info
// (where value is the string representation)
struct ParamDataMember {
std::string name;
std::string value;
std::string provenance;
std::string toString(std::string const& prefix, bool showProv, size_t padding = 0) const;
};
// ----------------------------------------------------------------
// just a (non-templated) helper with exclusively private functions
// used by ConfigurableParamHelper
class _ParamHelper
{
private:
static std::vector<ParamDataMember>* getDataMembersImpl(std::string const& mainkey, TClass* cl, void*,
std::map<std::string, ConfigurableParam::EParamProvenance> const* provmap, size_t virtualoffset);
static void fillKeyValuesImpl(std::string const& mainkey, TClass* cl, void*, boost::property_tree::ptree*,
std::map<std::string, std::pair<std::type_info const&, void*>>*,
EnumRegistry*, size_t offset);
static void printWarning(std::type_info const&);
static void assignmentImpl(std::string const& mainkey, TClass* cl, void* to, void* from,
std::map<std::string, ConfigurableParam::EParamProvenance>* provmap, size_t offset);
static void syncCCDBandRegistry(std::string const& mainkey, TClass* cl, void* to, void* from,
std::map<std::string, ConfigurableParam::EParamProvenance>* provmap, size_t offset);
static void outputMembersImpl(std::ostream& out, std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger, bool withPadding = false, bool showHash = false);
static void printMembersImpl(std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger, bool withPadding, bool showHash);
static size_t getHashImpl(std::string const& mainkey, std::vector<ParamDataMember> const* members);
template <typename P>
friend class ConfigurableParamHelper;
template <typename Base, typename P>
friend class ConfigurableParamPromoter;
};
// ----------------------------------------------------------------
// implementer (and checker) for concrete ConfigurableParam classes P
template <typename P>
class ConfigurableParamHelper : virtual public ConfigurableParam
{
public:
using ConfigurableParam::ConfigurableParam;
static const P& Instance()
{
return P::sInstance;
}
// ----------------------------------------------------------------
std::string getName() const final
{
return P::sKey;
}
// ----------------------------------------------------------------
// get the provenace of the member with given key
EParamProvenance getMemberProvenance(const std::string& key) const final
{
return getProvenance(getName() + '.' + key);
}
// ----------------------------------------------------------------
// one of the key methods, using introspection to print itself
void printKeyValues(bool showProv = true, bool useLogger = false, bool withPadding = true, bool showHash = true) const final
{
if (!isInitialized()) {
initialize();
}
auto members = getDataMembers();
_ParamHelper::printMembersImpl(getName(), members, showProv, useLogger, withPadding, showHash);
}
//
size_t getHash() const final
{
return _ParamHelper::getHashImpl(getName(), getDataMembers());
}
// ----------------------------------------------------------------
void output(std::ostream& out) const final
{
auto members = getDataMembers();
_ParamHelper::outputMembersImpl(out, getName(), members, true, false);
}
// ----------------------------------------------------------------
// Grab the list of ConfigurableParam data members
// Returns a nullptr if the TClass of the P template class cannot be created.
std::vector<ParamDataMember>* getDataMembers() const
{
// just a helper line to make sure P::sInstance is looked-up
// and that compiler complains about missing static sInstance of type P
// volatile void* ptr = (void*)&P::sInstance;
// static assert on type of sInstance:
static_assert(std::is_same<decltype(P::sInstance), P>::value,
"static instance must of same type as class");
// obtain the TClass for P and delegate further
auto cl = TClass::GetClass(typeid(P));
if (!cl) {
_ParamHelper::printWarning(typeid(P));
return nullptr;
}
return _ParamHelper::getDataMembersImpl(getName(), cl, (void*)this, sValueProvenanceMap, 0);
}
// ----------------------------------------------------------------
// fills the data structures with the initial default values
void putKeyValues(boost::property_tree::ptree* tree) final
{
auto cl = TClass::GetClass(typeid(P));
if (!cl) {
_ParamHelper::printWarning(typeid(P));
return;
}
_ParamHelper::fillKeyValuesImpl(getName(), cl, (void*)this, tree, sKeyToStorageMap, sEnumRegistry, 0);
}
// ----------------------------------------------------------------
void initFrom(TFile* file) final
{
// switch off auto registering since the readback object is
// only a "temporary" singleton
setRegisterMode(false);
P* readback = nullptr;
file->GetObject(getName().c_str(), readback);
if (readback != nullptr) {
_ParamHelper::assignmentImpl(getName(), TClass::GetClass(typeid(P)), (void*)this, (void*)readback,
sValueProvenanceMap, 0);
delete readback;
}
setRegisterMode(true);
}
// ----------------------------------------------------------------
void syncCCDBandRegistry(void* externalobj) final
{
// We may be getting an external copy from CCDB which is passed as externalobj.
// The task of this function is to
// a) update the internal registry with fields coming from CCDB
// but only if keys have not been modified via RT == command line / ini file
// b) update the external object with with fields having RT provenance
//
setRegisterMode(false);
_ParamHelper::syncCCDBandRegistry(getName(), TClass::GetClass(typeid(P)), (void*)this, (void*)externalobj,
sValueProvenanceMap, 0);
setRegisterMode(true);
}
// ----------------------------------------------------------------
void serializeTo(TFile* file) const final
{
file->WriteObjectAny((void*)this, TClass::GetClass(typeid(P)), getName().c_str());
}
};
// Promotes a simple struct Base to a configurable parameter class
// Aka implements all interfaces for a ConfigurableParam P, which shares or
// takes the fields from a Base struct
template <typename P, typename Base>
class ConfigurableParamPromoter : public Base, virtual public ConfigurableParam
{
public:
using ConfigurableParam::ConfigurableParam;
static const P& Instance()
{
return P::sInstance;
}
// extracts a copy of the underlying data struct
Base detach() const
{
static_assert(std::copyable<Base>, "Base type must be copyable.");
return static_cast<Base>(*this);
}
// ----------------------------------------------------------------
std::string getName() const final
{
return P::sKey;
}
// ----------------------------------------------------------------
// get the provenace of the member with given key
EParamProvenance getMemberProvenance(const std::string& key) const final
{
return getProvenance(getName() + '.' + key);
}
// ----------------------------------------------------------------
// one of the key methods, using introspection to print itself
void printKeyValues(bool showProv = true, bool useLogger = false, bool withPadding = true, bool showHash = true) const final
{
if (!isInitialized()) {
initialize();
}
auto members = getDataMembers();
_ParamHelper::printMembersImpl(getName(), members, showProv, useLogger, withPadding, showHash);
}
//
size_t getHash() const final
{
return _ParamHelper::getHashImpl(getName(), getDataMembers());
}
// ----------------------------------------------------------------
void output(std::ostream& out) const final
{
auto members = getDataMembers();
_ParamHelper::outputMembersImpl(out, getName(), members, true, false);
}
// ----------------------------------------------------------------
// Grab the list of ConfigurableParam data members
// Returns a nullptr if the TClass of the P template class cannot be created.
std::vector<ParamDataMember>* getDataMembers() const
{
// just a helper line to make sure P::sInstance is looked-up
// and that compiler complains about missing static sInstance of type P
// volatile void* ptr = (void*)&P::sInstance;
// static assert on type of sInstance:
static_assert(std::is_same<decltype(P::sInstance), P>::value,
"static instance must of same type as class");
// obtain the TClass for the Base type and delegate further
auto cl = TClass::GetClass(typeid(Base));
if (!cl) {
_ParamHelper::printWarning(typeid(Base));
return nullptr;
}
// we need to put an offset of 8 bytes since internally this is using data members of the Base class
// which doesn't account for the virtual table of P
return _ParamHelper::getDataMembersImpl(getName(), cl, (void*)this, sValueProvenanceMap, 8);
}
// ----------------------------------------------------------------
// fills the data structures with the initial default values
void putKeyValues(boost::property_tree::ptree* tree) final
{
auto cl = TClass::GetClass(typeid(Base));
if (!cl) {
_ParamHelper::printWarning(typeid(Base));
return;
}
_ParamHelper::fillKeyValuesImpl(getName(), cl, (void*)this, tree, sKeyToStorageMap, sEnumRegistry, 8);
}
// ----------------------------------------------------------------
void initFrom(TFile* file) final
{
// switch off auto registering since the readback object is
// only a "temporary" singleton
setRegisterMode(false);
P* readback = nullptr;
file->GetObject(getName().c_str(), readback);
if (readback != nullptr) {
_ParamHelper::assignmentImpl(getName(), TClass::GetClass(typeid(Base)), (void*)this, (void*)readback,
sValueProvenanceMap, 8);
delete readback;
}
setRegisterMode(true);
}
// ----------------------------------------------------------------
void syncCCDBandRegistry(void* externalobj) final
{
// We may be getting an external copy from CCDB which is passed as externalobj.
// The task of this function is to
// a) update the internal registry with fields coming from CCDB
// but only if keys have not been modified via RT == command line / ini file
// b) update the external object with with fields having RT provenance
//
setRegisterMode(false);
_ParamHelper::syncCCDBandRegistry(getName(), TClass::GetClass(typeid(Base)), (void*)this, (void*)externalobj,
sValueProvenanceMap, 8);
setRegisterMode(true);
}
// ----------------------------------------------------------------
void serializeTo(TFile* file) const final
{
file->WriteObjectAny((void*)this, TClass::GetClass(typeid(P)), getName().c_str());
}
};
} // namespace conf
} // namespace o2
#endif /* COMMON_SIMCONFIG_INCLUDE_SIMCONFIG_CONFIGURABLEPARAMHELPER_H_ */