Skip to content

Commit 982ea7b

Browse files
authored
Drop need to include ptree in ConfigurableParam.h (#11825)
1 parent d4d2179 commit 982ea7b

18 files changed

Lines changed: 170 additions & 97 deletions

File tree

Common/SimConfig/test/testSimCutParam.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <boost/test/unit_test.hpp>
1616
#include "SimConfig/SimParams.h"
1717
#include "CommonUtils/ConfigurableParam.h"
18+
#include <boost/property_tree/ptree.hpp>
1819

1920
using namespace o2::conf;
2021

Common/Utils/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ o2_add_library(CommonUtils
1414
src/RootChain.cxx src/CompStream.cxx src/ShmManager.cxx
1515
src/ValueMonitor.cxx
1616
src/StringUtils.cxx
17-
src/ConfigurableParamHelper.cxx src/ConfigurableParam.cxx src/RootSerializableKeyValueStore.cxx
17+
src/ConfigurableParamReaders.cxx src/ConfigurableParamHelper.cxx src/ConfigurableParam.cxx src/RootSerializableKeyValueStore.cxx
1818
src/KeyValParam.cxx
1919
src/FileSystemUtils.cxx
2020
src/FIFO.cxx
@@ -39,6 +39,7 @@ o2_target_root_dictionary(CommonUtils
3939
include/CommonUtils/MemFileHelper.h
4040
include/CommonUtils/ConfigurableParam.h
4141
include/CommonUtils/ConfigurableParamHelper.h
42+
include/CommonUtils/ConfigurableParamReaders.h
4243
include/CommonUtils/ConfigurationMacroHelper.h
4344
include/CommonUtils/RootSerializableKeyValueStore.h
4445
include/CommonUtils/KeyValParam.h

Common/Utils/include/CommonUtils/ConfigurableParam.h

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <cassert>
1919
#include <map>
2020
#include <unordered_map>
21-
#include <boost/property_tree/ptree.hpp>
21+
#include <boost/property_tree/ptree_fwd.hpp>
2222
#include <typeinfo>
2323
#include <iostream>
2424
#include <array>
@@ -176,15 +176,10 @@ class ConfigurableParam
176176
static void printAllRegisteredParamNames();
177177
static void printAllKeyValuePairs(bool useLogger = false);
178178

179-
static const std::string& getInputDir() { return sInputDir; }
180179
static const std::string& getOutputDir() { return sOutputDir; }
181180

182-
static void setInputDir(const std::string& d) { sInputDir = d; }
183181
static void setOutputDir(const std::string& d) { sOutputDir = d; }
184182

185-
static boost::property_tree::ptree readINI(std::string const& filepath);
186-
static boost::property_tree::ptree readJSON(std::string const& filepath);
187-
static boost::property_tree::ptree readConfigFile(std::string const& filepath);
188183
static bool configFileExists(std::string const& filepath);
189184

190185
// writes a human readable JSON file of all parameters
@@ -196,10 +191,12 @@ class ConfigurableParam
196191
template <typename T>
197192
static T getValueAs(std::string key)
198193
{
199-
if (!sIsFullyInitialized) {
200-
initialize();
201-
}
202-
return sPtree->get<T>(key);
194+
return [](auto* tree, const std::string& key) -> T {
195+
if (!sIsFullyInitialized) {
196+
initialize();
197+
}
198+
return tree->template get<T>(key);
199+
}(sPtree, key);
203200
}
204201

205202
template <typename T>
@@ -208,19 +205,21 @@ class ConfigurableParam
208205
if (!sIsFullyInitialized) {
209206
initialize();
210207
}
211-
assert(sPtree);
212-
try {
213-
auto key = mainkey + "." + subkey;
214-
if (sPtree->get_optional<std::string>(key).is_initialized()) {
215-
sPtree->put(key, x);
216-
auto changed = updateThroughStorageMap(mainkey, subkey, typeid(T), (void*)&x);
217-
if (changed != EParamUpdateStatus::Failed) {
218-
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
208+
return [&subkey, &x, &mainkey](auto* tree) -> void {
209+
assert(tree);
210+
try {
211+
auto key = mainkey + "." + subkey;
212+
if (tree->template get_optional<std::string>(key).is_initialized()) {
213+
tree->put(key, x);
214+
auto changed = updateThroughStorageMap(mainkey, subkey, typeid(T), (void*)&x);
215+
if (changed != EParamUpdateStatus::Failed) {
216+
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
217+
}
219218
}
219+
} catch (std::exception const& e) {
220+
std::cerr << "Error in setValue (T) " << e.what() << "\n";
220221
}
221-
} catch (std::exception const& e) {
222-
std::cerr << "Error in setValue (T) " << e.what() << "\n";
223-
}
222+
}(sPtree);
224223
}
225224

226225
static void setProvenance(std::string const& mainkey, std::string const& subkey, EParamProvenance p)
@@ -242,25 +241,7 @@ class ConfigurableParam
242241

243242
// specialized for std::string
244243
// which means that the type will be converted internally
245-
static void setValue(std::string const& key, std::string const& valuestring)
246-
{
247-
if (!sIsFullyInitialized) {
248-
initialize();
249-
}
250-
assert(sPtree);
251-
try {
252-
if (sPtree->get_optional<std::string>(key).is_initialized()) {
253-
sPtree->put(key, valuestring);
254-
auto changed = updateThroughStorageMapWithConversion(key, valuestring);
255-
if (changed != EParamUpdateStatus::Failed) {
256-
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
257-
}
258-
}
259-
} catch (std::exception const& e) {
260-
std::cerr << "Error in setValue (string) " << e.what() << "\n";
261-
}
262-
}
263-
244+
static void setValue(std::string const& key, std::string const& valuestring);
264245
static void setEnumValue(const std::string&, const std::string&);
265246
static void setArrayValue(const std::string&, const std::string&);
266247

@@ -321,7 +302,6 @@ class ConfigurableParam
321302
// (stored as a vector of pairs <enumValueLabel, enumValueInt>)
322303
static EnumRegistry* sEnumRegistry;
323304

324-
static std::string sInputDir;
325305
static std::string sOutputDir;
326306

327307
void setRegisterMode(bool b) { sRegisterMode = b; }
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
#ifndef O2_COMMON_UTILS_CONFIGURABLEPARAMREADERS_H_
12+
#define O2_COMMON_UTILS_CONFIGURABLEPARAMREADERS_H_
13+
14+
#include <boost/property_tree/ptree.hpp>
15+
#include <string>
16+
17+
namespace o2::conf
18+
{
19+
20+
// Helpers to read ConfigurableParam from different file formats
21+
class ConfigurableParamReaders
22+
{
23+
public:
24+
static void setInputDir(const std::string& d) { sInputDir = d; }
25+
static const std::string& getInputDir() { return sInputDir; }
26+
27+
static boost::property_tree::ptree readINI(std::string const& filepath);
28+
static boost::property_tree::ptree readJSON(std::string const& filepath);
29+
static boost::property_tree::ptree readConfigFile(std::string const& filepath);
30+
31+
private:
32+
static std::string sInputDir;
33+
};
34+
35+
} // namespace o2::conf
36+
#endif // O2_COMMON_UTILS_CONF_CONFIGURABLEPARAMREADERS_H_

Common/Utils/src/ConfigurableParam.cxx

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
// granted to it by virtue of its status as an Intergovernmental Organization
1010
// or submit itself to any jurisdiction.
1111

12-
//first version 8/2018, Sandro Wenzel
12+
// first version 8/2018, Sandro Wenzel
1313

1414
#include "CommonUtils/ConfigurableParam.h"
1515
#include "CommonUtils/StringUtils.h"
1616
#include "CommonUtils/KeyValParam.h"
17+
#include "CommonUtils/ConfigurableParamReaders.h"
1718
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
1819
#include <boost/algorithm/string/predicate.hpp>
1920
#include <boost/property_tree/ptree.hpp>
@@ -47,7 +48,6 @@ std::vector<ConfigurableParam*>* ConfigurableParam::sRegisteredParamClasses = nu
4748
boost::property_tree::ptree* ConfigurableParam::sPtree = nullptr;
4849
std::map<std::string, std::pair<std::type_info const&, void*>>* ConfigurableParam::sKeyToStorageMap = nullptr;
4950
std::map<std::string, ConfigurableParam::EParamProvenance>* ConfigurableParam::sValueProvenanceMap = nullptr;
50-
std::string ConfigurableParam::sInputDir = "";
5151
std::string ConfigurableParam::sOutputDir = "";
5252
EnumRegistry* ConfigurableParam::sEnumRegistry = nullptr;
5353

@@ -194,60 +194,28 @@ void ConfigurableParam::writeINI(std::string const& filename, std::string const&
194194

195195
bool ConfigurableParam::configFileExists(std::string const& filepath)
196196
{
197-
return std::filesystem::exists(o2::utils::Str::concat_string(sInputDir, filepath));
197+
return std::filesystem::exists(o2::utils::Str::concat_string(ConfigurableParamReaders::getInputDir(), filepath));
198198
}
199199

200200
// ------------------------------------------------------------------
201201

202-
boost::property_tree::ptree ConfigurableParam::readConfigFile(std::string const& filepath)
202+
void ConfigurableParam::setValue(std::string const& key, std::string const& valuestring)
203203
{
204-
auto inpfilename = o2::utils::Str::concat_string(sInputDir, filepath);
205-
if (!std::filesystem::exists(inpfilename)) {
206-
LOG(fatal) << inpfilename << " : config file does not exist!";
207-
}
208-
209-
boost::property_tree::ptree pt;
210-
211-
if (boost::iends_with(inpfilename, ".ini")) {
212-
pt = readINI(inpfilename);
213-
} else if (boost::iends_with(inpfilename, ".json")) {
214-
pt = readJSON(inpfilename);
215-
} else {
216-
LOG(fatal) << "Configuration file must have either .ini or .json extension";
217-
}
218-
219-
return pt;
220-
}
221-
222-
// ------------------------------------------------------------------
223-
224-
boost::property_tree::ptree ConfigurableParam::readINI(std::string const& filepath)
225-
{
226-
boost::property_tree::ptree pt;
227-
try {
228-
boost::property_tree::read_ini(filepath, pt);
229-
} catch (const boost::property_tree::ptree_error& e) {
230-
LOG(fatal) << "Failed to read INI config file " << filepath << " (" << e.what() << ")";
231-
} catch (...) {
232-
LOG(fatal) << "Unknown error when reading INI config file ";
204+
if (!sIsFullyInitialized) {
205+
initialize();
233206
}
234-
235-
return pt;
236-
}
237-
238-
// ------------------------------------------------------------------
239-
240-
boost::property_tree::ptree ConfigurableParam::readJSON(std::string const& filepath)
241-
{
242-
boost::property_tree::ptree pt;
243-
207+
assert(sPtree);
244208
try {
245-
boost::property_tree::read_json(filepath, pt);
246-
} catch (const boost::property_tree::ptree_error& e) {
247-
LOG(fatal) << "Failed to read JSON config file " << filepath << " (" << e.what() << ")";
209+
if (sPtree->get_optional<std::string>(key).is_initialized()) {
210+
sPtree->put(key, valuestring);
211+
auto changed = updateThroughStorageMapWithConversion(key, valuestring);
212+
if (changed != EParamUpdateStatus::Failed) {
213+
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
214+
}
215+
}
216+
} catch (std::exception const& e) {
217+
std::cerr << "Error in setValue (string) " << e.what() << "\n";
248218
}
249-
250-
return pt;
251219
}
252220

253221
// ------------------------------------------------------------------
@@ -258,7 +226,7 @@ void ConfigurableParam::writeJSON(std::string const& filename, std::string const
258226
LOG(info) << "ignoring writing of json file " << filename;
259227
return;
260228
}
261-
initPropertyTree(); // update the boost tree before writing
229+
initPropertyTree(); // update the boost tree before writing
262230
auto outfilename = o2::utils::Str::concat_string(sOutputDir, filename);
263231
if (!keyOnly.empty()) { // write ini for selected key only
264232
try {
@@ -409,7 +377,7 @@ void ConfigurableParam::updateFromFile(std::string const& configFile, std::strin
409377
return;
410378
}
411379

412-
boost::property_tree::ptree pt = readConfigFile(cfgfile);
380+
boost::property_tree::ptree pt = ConfigurableParamReaders::readConfigFile(cfgfile);
413381

414382
std::vector<std::pair<std::string, std::string>> keyValPairs;
415383
auto request = o2::utils::Str::tokenize(paramsList, ',', true);
@@ -522,7 +490,7 @@ void ConfigurableParam::updateFromString(std::string const& configString)
522490

523491
const auto& kv = o2::conf::KeyValParam::Instance();
524492
if (getProvenance("keyval.input_dir") != kCODE) {
525-
sInputDir = o2::utils::Str::concat_string(o2::utils::Str::rectifyDirectory(kv.input_dir));
493+
ConfigurableParamReaders::setInputDir(o2::utils::Str::concat_string(o2::utils::Str::rectifyDirectory(kv.input_dir)));
526494
}
527495
if (getProvenance("keyval.output_dir") != kCODE) {
528496
if (kv.output_dir == "/dev/null") {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
#include "CommonUtils/ConfigurableParamReaders.h"
13+
#include "CommonUtils/StringUtils.h"
14+
#include <fairlogger/Logger.h>
15+
#include <filesystem>
16+
17+
#include <boost/algorithm/string/predicate.hpp>
18+
#include <boost/property_tree/ini_parser.hpp>
19+
#include <boost/property_tree/json_parser.hpp>
20+
21+
namespace o2::conf
22+
{
23+
// ------------------------------------------------------------------
24+
25+
boost::property_tree::ptree ConfigurableParamReaders::readINI(std::string const& filepath)
26+
{
27+
boost::property_tree::ptree pt;
28+
try {
29+
boost::property_tree::read_ini(filepath, pt);
30+
} catch (const boost::property_tree::ptree_error& e) {
31+
LOG(fatal) << "Failed to read INI config file " << filepath << " (" << e.what() << ")";
32+
} catch (...) {
33+
LOG(fatal) << "Unknown error when reading INI config file ";
34+
}
35+
36+
return pt;
37+
}
38+
39+
// ------------------------------------------------------------------
40+
41+
boost::property_tree::ptree ConfigurableParamReaders::readJSON(std::string const& filepath)
42+
{
43+
boost::property_tree::ptree pt;
44+
45+
try {
46+
boost::property_tree::read_json(filepath, pt);
47+
} catch (const boost::property_tree::ptree_error& e) {
48+
LOG(fatal) << "Failed to read JSON config file " << filepath << " (" << e.what() << ")";
49+
}
50+
51+
return pt;
52+
}
53+
54+
boost::property_tree::ptree ConfigurableParamReaders::readConfigFile(std::string const& filepath)
55+
{
56+
auto inpfilename = o2::utils::Str::concat_string(sInputDir, filepath);
57+
if (!std::filesystem::exists(inpfilename)) {
58+
LOG(fatal) << inpfilename << " : config file does not exist!";
59+
}
60+
61+
boost::property_tree::ptree pt;
62+
63+
if (boost::iends_with(inpfilename, ".ini")) {
64+
pt = ConfigurableParamReaders::readINI(inpfilename);
65+
} else if (boost::iends_with(inpfilename, ".json")) {
66+
pt = ConfigurableParamReaders::readJSON(inpfilename);
67+
} else {
68+
LOG(fatal) << "Configuration file must have either .ini or .json extension";
69+
}
70+
71+
return pt;
72+
}
73+
74+
std::string ConfigurableParamReaders::sInputDir = "";
75+
76+
} // namespace o2::conf

Detectors/MUON/MCH/Raw/test/testClosureCoDec.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define BOOST_TEST_DYN_LINK
1717

1818
#include <boost/test/unit_test.hpp>
19+
#include <boost/property_tree/ptree.hpp>
1920
#include "DetectorsRaw/HBFUtils.h"
2021
#include "DetectorsRaw/RawFileWriter.h"
2122
#include "Framework/Logger.h"

Detectors/MUON/MCH/Simulation/test/testDigitizer.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "TGeoManager.h"
3232
#include "boost/format.hpp"
3333
#include <boost/test/data/test_case.hpp>
34+
#include <boost/property_tree/ptree.hpp>
3435
#include <algorithm>
3536
#include <unordered_map>
3637

Detectors/MUON/MCH/Triggering/test/testEventFinder.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <vector>
2828

2929
#include <boost/test/unit_test.hpp>
30+
#include <boost/property_tree/ptree.hpp>
3031

3132
#include "CommonUtils/ConfigurableParam.h"
3233
#include "SimulationDataFormat/MCCompLabel.h"

0 commit comments

Comments
 (0)