forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurableParam.cxx
More file actions
1001 lines (903 loc) · 29.9 KB
/
ConfigurableParam.cxx
File metadata and controls
1001 lines (903 loc) · 29.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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
#include "CommonUtils/ConfigurableParam.h"
#include "CommonUtils/StringUtils.h"
#include "CommonUtils/KeyValParam.h"
#include "CommonUtils/ConfigurableParamReaders.h"
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/algorithm/string/predicate.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
#include <algorithm>
#include <array>
#include <limits>
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>
#include <iostream>
#include <string>
#include <fairlogger/Logger.h>
#include <typeinfo>
#include "TDataMember.h"
#include "TDataType.h"
#include "TFile.h"
#include "TEnum.h"
#include "TEnumConstant.h"
#include <filesystem>
namespace o2
{
namespace conf
{
std::vector<ConfigurableParam*>* ConfigurableParam::sRegisteredParamClasses = nullptr;
boost::property_tree::ptree* ConfigurableParam::sPtree = nullptr;
std::map<std::string, std::pair<std::type_info const&, void*>>* ConfigurableParam::sKeyToStorageMap = nullptr;
std::map<std::string, ConfigurableParam::EParamProvenance>* ConfigurableParam::sValueProvenanceMap = nullptr;
std::string ConfigurableParam::sOutputDir = "";
EnumRegistry* ConfigurableParam::sEnumRegistry = nullptr;
bool ConfigurableParam::sIsFullyInitialized = false;
bool ConfigurableParam::sRegisterMode = true;
// ------------------------------------------------------------------
std::ostream& operator<<(std::ostream& out, ConfigurableParam const& param)
{
param.output(out);
return out;
}
// Does the given key exist in the boost property tree?
bool keyInTree(boost::property_tree::ptree* pt, const std::string& key)
{
if (key.size() == 0 || pt == nullptr) {
return false;
}
bool reply = false;
try {
reply = pt->get_optional<std::string>(key).is_initialized();
} catch (std::exception const& e) {
LOG(error) << "ConfigurableParam: Exception when checking for key " << key << " : " << e.what();
}
return reply;
}
// Convert a type info to the appropiate literal suffix
std::string getLiteralSuffixFromType(const std::type_info& type)
{
if (type == typeid(float)) {
return "f";
}
if (type == typeid(long double)) {
return "l";
}
if (type == typeid(unsigned int)) {
return "u";
}
if (type == typeid(unsigned long)) {
return "ul";
}
if (type == typeid(long long)) {
return "ll";
}
if (type == typeid(unsigned long long)) {
return "ull";
}
return "";
}
// ------------------------------------------------------------------
void EnumRegistry::add(const std::string& key, const TDataMember* dm)
{
if (!dm->IsEnum() || this->contains(key)) {
return;
}
EnumLegalValues legalVals;
auto enumtype = TEnum::GetEnum(dm->GetTypeName());
assert(enumtype != nullptr);
auto constantlist = enumtype->GetConstants();
assert(constantlist != nullptr);
if (enumtype) {
for (int i = 0; i < constantlist->GetEntries(); ++i) {
auto e = (TEnumConstant*)(constantlist->At(i));
std::pair<std::string, int> val(e->GetName(), (int)e->GetValue());
legalVals.vvalues.push_back(val);
}
}
// The other method of fetching enum constants from TDataMember->GetOptions
// stopped working with ROOT6-18-0:
// auto opts = dm->GetOptions();
// for (int i = 0; i < opts->GetEntries(); ++i) {
// auto opt = (TOptionListItem*)opts->At(i);
// std::pair<std::string, int> val(opt->fOptName, (int)opt->fValue);
// legalVals.vvalues.push_back(val);
// LOG(info) << "Adding legal value " << val.first << " " << val.second;
// }
auto entry = std::pair<std::string, EnumLegalValues>(key, legalVals);
this->entries.insert(entry);
}
std::string EnumRegistry::toString() const
{
std::string out = "";
for (auto& entry : entries) {
out.append(entry.first + " => ");
out.append(entry.second.toString());
out.append("\n");
}
return out;
}
std::string EnumLegalValues::toString() const
{
std::string out = "";
for (auto& value : vvalues) {
out.append("[");
out.append(value.first);
out.append(" | ");
out.append(std::to_string(value.second));
out.append("] ");
}
return out;
}
// getIntValue takes a string value which is supposed to be
// a legal enum value and tries to cast it to an int.
// If it succeeds, and if the int value is legal, it is returned.
// If it fails, and if it is a legal string enum value, we look up
// and return the equivalent int value. In any case, if it is not
// a legal value we return -1 to indicate this fact.
int EnumLegalValues::getIntValue(const std::string& value) const
{
try {
int val = boost::lexical_cast<int>(value);
if (isLegal(val)) {
return val;
}
} catch (const boost::bad_lexical_cast& e) {
if (isLegal(value)) {
for (auto& pair : vvalues) {
if (pair.first == value) {
return pair.second;
}
}
}
}
return -1;
}
// -----------------------------------------------------------------
void ConfigurableParam::write(std::string const& filename, std::string const& keyOnly)
{
if (o2::utils::Str::endsWith(filename, ".ini")) {
writeINI(filename, keyOnly);
} else if (o2::utils::Str::endsWith(filename, ".json")) {
writeJSON(filename, keyOnly);
} else {
throw std::invalid_argument(fmt::format("ConfigurabeParam output file name {} extension is neither .json nor .ini", filename));
}
}
// -----------------------------------------------------------------
void ConfigurableParam::writeINI(std::string const& filename, std::string const& keyOnly)
{
if (sOutputDir == "/dev/null") {
LOG(debug) << "ignoring writing of ini file " << filename;
return;
}
auto outfilename = o2::utils::Str::concat_string(sOutputDir, filename);
initPropertyTree(); // update the boost tree before writing
if (!keyOnly.empty()) { // write ini for selected key only
try {
boost::property_tree::ptree kTree;
auto keys = o2::utils::Str::tokenize(keyOnly, " ,;", true, true);
for (const auto& k : keys) {
kTree.add_child(k, sPtree->get_child(k));
}
boost::property_tree::write_ini(outfilename, kTree);
} catch (const boost::property_tree::ptree_bad_path& err) {
LOG(fatal) << "non-existing key " << keyOnly << " provided to writeINI";
}
} else {
boost::property_tree::write_ini(outfilename, *sPtree);
}
}
// ------------------------------------------------------------------
bool ConfigurableParam::configFileExists(std::string const& filepath)
{
return std::filesystem::exists(o2::utils::Str::concat_string(ConfigurableParamReaders::getInputDir(), filepath));
}
// ------------------------------------------------------------------
void ConfigurableParam::setValue(std::string const& key, std::string const& valuestring)
{
if (!sIsFullyInitialized) {
initialize();
}
assert(sPtree);
auto setValueImpl = [&](std::string const& value) {
sPtree->put(key, value);
auto changed = updateThroughStorageMapWithConversion(key, value);
if (changed != EParamUpdateStatus::Failed) {
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
}
};
try {
if (sPtree->get_optional<std::string>(key).is_initialized()) {
try {
// try first setting value without stripping a literal suffix
setValueImpl(valuestring);
} catch (...) {
// try second stripping the expected literal suffix value for fundamental types
auto iter = sKeyToStorageMap->find(key);
if (iter == sKeyToStorageMap->end()) {
std::cerr << "Error in setValue (string) key is not known\n";
return;
}
const auto expectedSuffix = getLiteralSuffixFromType(iter->second.first);
if (!expectedSuffix.empty()) {
auto valuestringLower = valuestring;
std::transform(valuestring.cbegin(), valuestring.cend(), valuestringLower.begin(), tolower);
if (valuestringLower.ends_with(expectedSuffix)) {
std::string strippedValue = valuestringLower.substr(0, valuestringLower.length() - expectedSuffix.length());
setValueImpl(strippedValue);
} else {
// check if it has a different suffix and throw
for (const auto& suffix : {"f", "l", "u", "ul", "ll", "ull"}) {
if (valuestringLower.ends_with(suffix) && suffix != expectedSuffix) {
throw std::invalid_argument("Wrong type suffix: expected " + expectedSuffix + " but got " + suffix);
}
}
throw; // just rethrow the original exception
}
}
}
}
} catch (std::exception const& e) {
std::cerr << "Error in setValue (string) " << e.what() << "\n";
}
}
// ------------------------------------------------------------------
void ConfigurableParam::writeJSON(std::string const& filename, std::string const& keyOnly)
{
if (sOutputDir == "/dev/null") {
LOG(info) << "ignoring writing of json file " << filename;
return;
}
initPropertyTree(); // update the boost tree before writing
auto outfilename = o2::utils::Str::concat_string(sOutputDir, filename);
if (!keyOnly.empty()) { // write ini for selected key only
try {
boost::property_tree::ptree kTree;
auto keys = o2::utils::Str::tokenize(keyOnly, " ,;", true, true);
for (const auto& k : keys) {
kTree.add_child(k, sPtree->get_child(k));
}
boost::property_tree::write_json(outfilename, kTree);
} catch (const boost::property_tree::ptree_bad_path& err) {
LOG(fatal) << "non-existing key " << keyOnly << " provided to writeJSON";
}
} else {
boost::property_tree::write_json(outfilename, *sPtree);
}
}
// ------------------------------------------------------------------
void ConfigurableParam::initPropertyTree()
{
sPtree->clear();
for (auto p : *sRegisteredParamClasses) {
p->putKeyValues(sPtree);
}
}
// ------------------------------------------------------------------
void ConfigurableParam::printAllKeyValuePairs(bool useLogger)
{
if (!sIsFullyInitialized) {
initialize();
}
std::cout << "####\n";
for (auto p : *sRegisteredParamClasses) {
p->printKeyValues(true, useLogger);
}
std::cout << "----\n";
}
// ------------------------------------------------------------------
ConfigurableParam::EParamProvenance ConfigurableParam::getProvenance(const std::string& key)
{
if (!sIsFullyInitialized) {
initialize();
}
auto iter = sValueProvenanceMap->find(key);
if (iter == sValueProvenanceMap->end()) {
throw std::runtime_error(fmt::format("provenace of unknown {:s} parameter is requested", key));
}
return iter->second;
}
// ------------------------------------------------------------------
// evidently this could be a local file or an OCDB server
// ... we need to generalize this ... but ok for demonstration purposes
void ConfigurableParam::toCCDB(std::string filename)
{
if (!sIsFullyInitialized) {
initialize();
}
TFile file(filename.c_str(), "RECREATE");
for (auto p : *sRegisteredParamClasses) {
p->serializeTo(&file);
}
file.Close();
}
// ------------------------------------------------------------------
void ConfigurableParam::fromCCDB(std::string filename)
{
if (!sIsFullyInitialized) {
initialize();
}
TFile file(filename.c_str(), "READ");
for (auto p : *sRegisteredParamClasses) {
p->initFrom(&file);
}
file.Close();
}
// ------------------------------------------------------------------
ConfigurableParam::ConfigurableParam()
{
if (sRegisteredParamClasses == nullptr) {
sRegisteredParamClasses = new std::vector<ConfigurableParam*>;
}
if (sPtree == nullptr) {
sPtree = new boost::property_tree::ptree;
}
if (sKeyToStorageMap == nullptr) {
sKeyToStorageMap = new std::map<std::string, std::pair<std::type_info const&, void*>>;
}
if (sValueProvenanceMap == nullptr) {
sValueProvenanceMap = new std::map<std::string, ConfigurableParam::EParamProvenance>;
}
if (sEnumRegistry == nullptr) {
sEnumRegistry = new EnumRegistry();
}
if (sRegisterMode == true) {
sRegisteredParamClasses->push_back(this);
}
}
// ------------------------------------------------------------------
void ConfigurableParam::initialize()
{
initPropertyTree();
// initialize the provenance map
// initially the values come from code
for (auto& key : *sKeyToStorageMap) {
sValueProvenanceMap->insert(std::pair<std::string, ConfigurableParam::EParamProvenance>(key.first, kCODE));
}
sIsFullyInitialized = true;
}
// ------------------------------------------------------------------
void ConfigurableParam::printAllRegisteredParamNames()
{
for (auto p : *sRegisteredParamClasses) {
std::cout << p->getName() << "\n";
}
}
// ------------------------------------------------------------------
// Update the storage map of params from the given configuration file.
// It can be in JSON or INI format.
// If nonempty comma-separated paramsList is provided, only those params will
// be updated, absence of data for any of requested params will lead to fatal
// If unchangedOnly is true, then only those parameters whose provenance is kCODE will be updated
// (to allow prefernce of run-time settings)
void ConfigurableParam::updateFromFile(std::string const& configFile, std::string const& paramsList, bool unchangedOnly)
{
if (!sIsFullyInitialized) {
initialize();
}
auto cfgfile = o2::utils::Str::trim_copy(configFile);
if (cfgfile.length() == 0) {
return;
}
boost::property_tree::ptree pt = ConfigurableParamReaders::readConfigFile(cfgfile);
std::vector<std::pair<std::string, std::string>> keyValPairs;
auto request = o2::utils::Str::tokenize(paramsList, ',', true);
std::unordered_map<std::string, int> requestMap;
for (const auto& par : request) {
if (!par.empty()) {
requestMap[par] = 0;
}
}
try {
for (auto& section : pt) {
std::string mainKey = section.first;
if (requestMap.size()) {
if (requestMap.find(mainKey) == requestMap.end()) {
continue; // if something was requested, ignore everything else
} else {
requestMap[mainKey] = 1;
}
}
for (auto& subKey : section.second) {
auto name = subKey.first;
auto value = subKey.second.get_value<std::string>();
std::string key = mainKey + "." + name;
if (!unchangedOnly || getProvenance(key) == kCODE) {
std::pair<std::string, std::string> pair = std::make_pair(key, o2::utils::Str::trim_copy(value));
keyValPairs.push_back(pair);
}
}
}
} catch (std::exception const& error) {
LOG(error) << "Error while updating params " << error.what();
} catch (...) {
LOG(error) << "Unknown while updating params ";
}
// make sure all requested params were retrieved
for (const auto& req : requestMap) {
if (req.second == 0) {
throw std::runtime_error(fmt::format("Param {:s} was not found in {:s}", req.first, configFile));
}
}
try {
setValues(keyValPairs);
} catch (std::exception const& error) {
LOG(error) << "Error while setting values " << error.what();
}
}
// ------------------------------------------------------------------
// ------------------------------------------------------------------
void ConfigurableParam::updateFromString(std::string const& configString)
{
if (!sIsFullyInitialized) {
initialize();
}
auto cfgStr = o2::utils::Str::trim_copy(configString);
if (cfgStr.length() == 0) {
return;
}
// Take a vector of strings with elements of form a=b, and
// return a vector of pairs with each pair of form <a, b>
auto toKeyValPairs = [](std::vector<std::string>& tokens) {
std::vector<std::pair<std::string, std::string>> pairs;
for (auto& token : tokens) {
auto s = token.find('=');
if (s == 0 || s == std::string::npos || s == token.size() - 1) {
LOG(fatal) << "Illegal command-line key/value string: " << token;
continue;
}
pairs.emplace_back(token.substr(0, s), token.substr(s + 1, token.size()));
}
return pairs;
};
// Simple check that the string starts/ends with an open square bracket
// Find the maximum index of a given key with array value.
// We store string keys for arrays as a[0]...a[size_of_array]
/*
auto maxIndex = [](std::string baseName) {
bool isFound = true;
int index = -1;
do {
index++;
std::string key = baseName + "[" + std::to_string(index) + "]";
isFound = keyInTree(sPtree, key);
} while (isFound);
return index;
};
*/
// ---- end of helper functions --------------------
// Command-line string is a ;-separated list of key=value params
auto params = o2::utils::Str::tokenize(configString, ';', true);
// Now split each key=value string into its std::pair<key, value> parts
auto keyValues = toKeyValPairs(params);
setValues(keyValues);
const auto& kv = o2::conf::KeyValParam::Instance();
if (getProvenance("keyval.input_dir") != kCODE) {
ConfigurableParamReaders::setInputDir(o2::utils::Str::concat_string(o2::utils::Str::rectifyDirectory(kv.input_dir)));
}
if (getProvenance("keyval.output_dir") != kCODE) {
if (kv.output_dir == "/dev/null") {
sOutputDir = kv.output_dir;
} else {
sOutputDir = o2::utils::Str::concat_string(o2::utils::Str::rectifyDirectory(kv.output_dir));
}
}
}
// setValues takes a vector of pairs where each pair is a key and value
// to be set in the storage map
void ConfigurableParam::setValues(std::vector<std::pair<std::string, std::string>> const& keyValues)
{
auto isArray = [](std::string& el) {
return el.size() > 0 && (el.at(0) == '[') && (el.at(el.size() - 1) == ']');
};
bool nonFatal = getenv("ALICEO2_CONFIGURABLEPARAM_WRONGKEYISNONFATAL") != nullptr;
// Take a vector of param key/value pairs
// and update the storage map for each of them by calling setValue.
// 1. For string/scalar types this is simple.
// 2. For array values we need to iterate over each array element
// and call setValue on the element, using an appropriately constructed key.
// 3. For enum types we check for the existence of the key in the enum registry
// and also confirm that the value is in the list of legal values
for (auto& keyValue : keyValues) {
std::string key = keyValue.first;
std::string value = o2::utils::Str::trim_copy(keyValue.second);
if (!keyInTree(sPtree, key)) {
if (nonFatal) {
LOG(warn) << "Ignoring non-existent ConfigurableParam key: " << key;
continue;
}
LOG(fatal) << "Inexistant ConfigurableParam key: " << key;
}
if (sEnumRegistry->contains(key)) {
setEnumValue(key, value);
} else if (isArray(value)) {
setArrayValue(key, value);
} else {
assert(sKeyToStorageMap->find(key) != sKeyToStorageMap->end());
// If the value is given as a boolean true|false, change to 1|0 int equivalent
if (value == "true") {
value = "1";
} else if (value == "false") {
value = "0";
}
// TODO: this will trap complex types like maps and structs.
// These need to be broken into their own cases, so that we only
// get strings and scalars here.
setValue(key, value);
}
}
}
void ConfigurableParam::setArrayValue(const std::string& key, const std::string& value)
{
// We remove the lead/trailing square bracket
// value.erase(0, 1).pop_back();
auto elems = o2::utils::Str::tokenize(value.substr(1, value.length() - 2), ',', true);
// TODO:
// 1. Should not assume each array element is a scalar/string. We may need to recurse.
// 2. Should not assume each array element - even if not complex - is correctly written. Validate.
// 3. Validation should include finding same types as in provided defaults.
for (int i = 0; i < elems.size(); ++i) {
std::string indexKey = key + "[" + std::to_string(i) + "]";
setValue(indexKey, elems[i]);
}
}
void ConfigurableParam::setEnumValue(const std::string& key, const std::string& value)
{
int val = (*sEnumRegistry)[key]->getIntValue(value);
if (val == -1) {
LOG(fatal) << "Illegal value "
<< value << " for enum " << key
<< ". Legal string|int values:\n"
<< (*sEnumRegistry)[key]->toString() << std::endl;
}
setValue(key, std::to_string(val));
}
void unsupp() { std::cerr << "currently unsupported\n"; }
template <typename T>
bool isMemblockDifferent(void const* block1, void const* block2)
{
// loop over thing in elements of bytes
for (int i = 0; i < sizeof(T) / sizeof(char); ++i) {
if (((char*)block1)[i] != ((char*)block2)[i]) {
return true;
}
}
return false;
}
// copies data from one place to other and returns
// true of data was actually changed
template <typename T>
ConfigurableParam::EParamUpdateStatus Copy(void const* addr, void* targetaddr)
{
if (isMemblockDifferent<T>(addr, targetaddr)) {
std::memcpy(targetaddr, addr, sizeof(T));
return ConfigurableParam::EParamUpdateStatus::Changed;
}
return ConfigurableParam::EParamUpdateStatus::Unchanged;
}
ConfigurableParam::EParamUpdateStatus ConfigurableParam::updateThroughStorageMap(std::string mainkey, std::string subkey, std::type_info const& tinfo,
void* addr)
{
// check if key_exists
auto key = mainkey + "." + subkey;
auto iter = sKeyToStorageMap->find(key);
if (iter == sKeyToStorageMap->end()) {
LOG(warn) << "Cannot update parameter " << key << " not found";
return ConfigurableParam::EParamUpdateStatus::Failed;
}
// the type we need to convert to
int type = TDataType::GetType(tinfo);
// check that type matches
if (iter->second.first != tinfo) {
LOG(warn) << "Types do not match; cannot update value";
return ConfigurableParam::EParamUpdateStatus::Failed;
}
auto targetaddress = iter->second.second;
switch (type) {
case kChar_t: {
return Copy<char>(addr, targetaddress);
break;
}
case kUChar_t: {
return Copy<unsigned char>(addr, targetaddress);
break;
}
case kShort_t: {
return Copy<short>(addr, targetaddress);
break;
}
case kUShort_t: {
return Copy<unsigned short>(addr, targetaddress);
break;
}
case kInt_t: {
return Copy<int>(addr, targetaddress);
break;
}
case kUInt_t: {
return Copy<unsigned int>(addr, targetaddress);
break;
}
case kLong_t: {
return Copy<long>(addr, targetaddress);
break;
}
case kULong_t: {
return Copy<unsigned long>(addr, targetaddress);
break;
}
case kFloat_t: {
return Copy<float>(addr, targetaddress);
break;
}
case kDouble_t: {
return Copy<double>(addr, targetaddress);
break;
}
case kDouble32_t: {
return Copy<double>(addr, targetaddress);
break;
}
case kchar: {
unsupp();
break;
}
case kBool_t: {
return Copy<bool>(addr, targetaddress);
break;
}
case kLong64_t: {
return Copy<long long>(addr, targetaddress);
break;
}
case kULong64_t: {
return Copy<unsigned long long>(addr, targetaddress);
break;
}
case kOther_t: {
unsupp();
break;
}
case kNoType_t: {
unsupp();
break;
}
case kFloat16_t: {
unsupp();
break;
}
case kCounter: {
unsupp();
break;
}
case kCharStar: {
return Copy<char*>(addr, targetaddress);
break;
}
case kBits: {
unsupp();
break;
}
case kVoid_t: {
unsupp();
break;
}
case kDataTypeAliasUnsigned_t: {
unsupp();
break;
}
/*
case kDataTypeAliasSignedChar_t: {
unsupp();
break;
}
case kNumDataTypes: {
unsupp();
break;
}*/
default: {
unsupp();
break;
}
}
return ConfigurableParam::EParamUpdateStatus::Failed;
}
template <typename T>
ConfigurableParam::EParamUpdateStatus ConvertAndCopy(std::string const& valuestring, void* targetaddr)
{
auto addr = boost::lexical_cast<T>(valuestring);
if (isMemblockDifferent<T>(targetaddr, (void*)&addr)) {
std::memcpy(targetaddr, (void*)&addr, sizeof(T));
return ConfigurableParam::EParamUpdateStatus::Changed;
}
return ConfigurableParam::EParamUpdateStatus::Unchanged;
}
// special version for std::string
template <>
ConfigurableParam::EParamUpdateStatus ConvertAndCopy<std::string>(std::string const& valuestring, void* targetaddr)
{
std::string& target = *((std::string*)targetaddr);
if (target.compare(valuestring) != 0) {
// the targetaddr is a std::string to which we can simply assign
// and all the magic will happen internally
target = valuestring;
return ConfigurableParam::EParamUpdateStatus::Changed;
}
return ConfigurableParam::EParamUpdateStatus::Unchanged;
}
// special version for char and unsigned char since we are interested in the numeric
// meaning of char as an 8-bit integer (boost lexical cast is assigning the string as a character i// nterpretation
template <>
ConfigurableParam::EParamUpdateStatus ConvertAndCopy<char>(std::string const& valuestring, void* targetaddr)
{
int intvalue = boost::lexical_cast<int>(valuestring);
if (intvalue > std::numeric_limits<char>::max() || intvalue < std::numeric_limits<char>::min()) {
LOG(error) << "Cannot assign " << valuestring << " to a char variable";
return ConfigurableParam::EParamUpdateStatus::Failed;
}
char addr = intvalue;
if (isMemblockDifferent<char>(targetaddr, (void*)&addr)) {
std::memcpy(targetaddr, (void*)&addr, sizeof(char));
return ConfigurableParam::EParamUpdateStatus::Changed;
}
return ConfigurableParam::EParamUpdateStatus::Unchanged;
}
template <>
ConfigurableParam::EParamUpdateStatus ConvertAndCopy<unsigned char>(std::string const& valuestring, void* targetaddr)
{
unsigned int intvalue = boost::lexical_cast<int>(valuestring);
if (intvalue > std::numeric_limits<unsigned char>::max() || intvalue < std::numeric_limits<unsigned char>::min()) {
LOG(error) << "Cannot assign " << valuestring << " to an unsigned char variable";
return ConfigurableParam::EParamUpdateStatus::Failed;
}
unsigned char addr = intvalue;
if (isMemblockDifferent<unsigned char>(targetaddr, (void*)&addr)) {
std::memcpy(targetaddr, (void*)&addr, sizeof(unsigned char));
return ConfigurableParam::EParamUpdateStatus::Changed;
}
return ConfigurableParam::EParamUpdateStatus::Unchanged;
}
ConfigurableParam::EParamUpdateStatus ConfigurableParam::updateThroughStorageMapWithConversion(std::string const& key, std::string const& valuestring)
{
// check if key_exists
auto iter = sKeyToStorageMap->find(key);
if (iter == sKeyToStorageMap->end()) {
LOG(warn) << "Cannot update parameter " << key << " (parameter not found) ";
return ConfigurableParam::EParamUpdateStatus::Failed;
}
auto targetaddress = iter->second.second;
// treat some special cases first:
// the type is actually a std::string
if (iter->second.first == typeid(std::string)) {
return ConvertAndCopy<std::string>(valuestring, targetaddress);
}
// the type (aka ROOT::EDataType which the type identification in the map) we need to convert to
int targettype = TDataType::GetType(iter->second.first);
switch (targettype) {
case kChar_t: {
return ConvertAndCopy<char>(valuestring, targetaddress);
break;
}
case kUChar_t: {
return ConvertAndCopy<unsigned char>(valuestring, targetaddress);
break;
}
case kShort_t: {
return ConvertAndCopy<short>(valuestring, targetaddress);
break;
}
case kUShort_t: {
return ConvertAndCopy<unsigned short>(valuestring, targetaddress);
break;
}
case kInt_t: {
return ConvertAndCopy<int>(valuestring, targetaddress);
break;
}
case kUInt_t: {
return ConvertAndCopy<unsigned int>(valuestring, targetaddress);
break;
}
case kLong_t: {
return ConvertAndCopy<long>(valuestring, targetaddress);
break;
}
case kULong_t: {
return ConvertAndCopy<unsigned long>(valuestring, targetaddress);
break;
}
case kFloat_t: {
return ConvertAndCopy<float>(valuestring, targetaddress);
break;
}
case kDouble_t: {
return ConvertAndCopy<double>(valuestring, targetaddress);
break;
}
case kDouble32_t: {
return ConvertAndCopy<double>(valuestring, targetaddress);
break;
}
case kchar: {
unsupp();
break;
}
case kBool_t: {
return ConvertAndCopy<bool>(valuestring, targetaddress);
break;
}
case kLong64_t: {
return ConvertAndCopy<long long>(valuestring, targetaddress);
break;
}
case kULong64_t: {
return ConvertAndCopy<unsigned long long>(valuestring, targetaddress);
break;
}
case kOther_t: {
unsupp();
break;
}
case kNoType_t: {
unsupp();
break;
}
case kFloat16_t: {
unsupp();
break;
}
case kCounter: {
unsupp();
break;
}
case kCharStar: {
unsupp();
// return ConvertAndCopy<char*>(valuestring, targetaddress);
break;
}
case kBits: {
unsupp();
break;
}
case kVoid_t: {
unsupp();
break;
}
case kDataTypeAliasUnsigned_t: {
unsupp();
break;
}
/*
case kDataTypeAliasSignedChar_t: {
unsupp();
break;
}
case kNumDataTypes: {
unsupp();
break;
}*/
default: {
unsupp();
break;
}
}
return ConfigurableParam::EParamUpdateStatus::Failed;
}
} // namespace conf