Skip to content

Commit 0aa3291

Browse files
committed
Drop need to include ptree in ConfigurableParam.h
1 parent 892ef49 commit 0aa3291

14 files changed

Lines changed: 86 additions & 79 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/include/CommonUtils/ConfigurableParam.h

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,23 @@
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
#ifndef COMMON_SIMCONFIG_INCLUDE_SIMCONFIG_CONFIGURABLEPARAM_H_
1515
#define COMMON_SIMCONFIG_INCLUDE_SIMCONFIG_CONFIGURABLEPARAM_H_
1616

1717
#include <vector>
1818
#include <map>
1919
#include <unordered_map>
20-
#include <boost/property_tree/ptree.hpp>
20+
#include <boost/property_tree/ptree_fwd.hpp>
2121
#include <typeinfo>
2222
#include <iostream>
2323

2424
class TFile;
2525
class TRootIOCtor;
2626
class TDataMember;
2727

28-
namespace o2
29-
{
30-
namespace conf
28+
namespace o2::conf
3129
{
3230
// Base class for a configurable parameter.
3331
//
@@ -88,7 +86,7 @@ namespace conf
8886
struct EnumLegalValues {
8987
std::vector<std::pair<std::string, int>> vvalues;
9088

91-
bool isLegal(const std::string& value) const
89+
[[nodiscard]] bool isLegal(const std::string& value) const
9290
{
9391
for (auto& v : vvalues) {
9492
if (v.first == value) {
@@ -98,7 +96,7 @@ struct EnumLegalValues {
9896
return false;
9997
}
10098

101-
bool isLegal(int value) const
99+
[[nodiscard]] bool isLegal(int value) const
102100
{
103101
for (auto& v : vvalues) {
104102
if (v.second == value) {
@@ -108,21 +106,21 @@ struct EnumLegalValues {
108106
return false;
109107
}
110108

111-
std::string toString() const;
112-
int getIntValue(const std::string& value) const;
109+
[[nodiscard]] std::string toString() const;
110+
[[nodiscard]] int getIntValue(const std::string& value) const;
113111
};
114112

115113
class EnumRegistry
116114
{
117115
public:
118116
void add(const std::string& key, const TDataMember* dm);
119117

120-
bool contains(const std::string& key) const
118+
[[nodiscard]] bool contains(const std::string& key) const
121119
{
122120
return entries.count(key) > 0;
123121
}
124122

125-
std::string toString() const;
123+
[[nodiscard]] std::string toString() const;
126124

127125
const EnumLegalValues* operator[](const std::string& key) const
128126
{
@@ -157,17 +155,17 @@ class ConfigurableParam
157155
}
158156

159157
// get the name of the configurable Parameter
160-
virtual std::string getName() const = 0;
158+
[[nodiscard]] virtual std::string getName() const = 0;
161159

162160
// print the current keys and values to screen (optionally with provenance information)
163161
virtual void printKeyValues(bool showprov = true, bool useLogger = false) const = 0;
164162

165163
// get a single size_t hash_value of this parameter (can be used as a checksum to see
166164
// if object changed or different)
167-
virtual size_t getHash() const = 0;
165+
[[nodiscard]] virtual size_t getHash() const = 0;
168166

169167
// return the provenance of the member key
170-
virtual EParamProvenance getMemberProvenance(const std::string& key) const = 0;
168+
[[nodiscard]] virtual EParamProvenance getMemberProvenance(const std::string& key) const = 0;
171169

172170
static EParamProvenance getProvenance(const std::string& key);
173171

@@ -180,9 +178,6 @@ class ConfigurableParam
180178
static void setInputDir(const std::string& d) { sInputDir = d; }
181179
static void setOutputDir(const std::string& d) { sOutputDir = d; }
182180

183-
static boost::property_tree::ptree readINI(std::string const& filepath);
184-
static boost::property_tree::ptree readJSON(std::string const& filepath);
185-
static boost::property_tree::ptree readConfigFile(std::string const& filepath);
186181
static bool configFileExists(std::string const& filepath);
187182

188183
// writes a human readable JSON file of all parameters
@@ -194,10 +189,12 @@ class ConfigurableParam
194189
template <typename T>
195190
static T getValueAs(std::string key)
196191
{
197-
if (!sIsFullyInitialized) {
198-
initialize();
199-
}
200-
return sPtree->get<T>(key);
192+
return [](auto* tree, const std::string& key) -> T {
193+
if (!sIsFullyInitialized) {
194+
initialize();
195+
}
196+
return tree->template get<T>(key);
197+
}(sPtree, key);
201198
}
202199

203200
template <typename T>
@@ -206,42 +203,26 @@ class ConfigurableParam
206203
if (!sIsFullyInitialized) {
207204
initialize();
208205
}
209-
assert(sPtree);
210-
try {
211-
auto key = mainkey + "." + subkey;
212-
if (sPtree->get_optional<std::string>(key).is_initialized()) {
213-
sPtree->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
206+
return [&subkey, &x, &mainkey](auto* tree) -> void {
207+
assert(tree);
208+
try {
209+
auto key = mainkey + "." + subkey;
210+
if (tree->template get_optional<std::string>(key).is_initialized()) {
211+
tree->put(key, x);
212+
auto changed = updateThroughStorageMap(mainkey, subkey, typeid(T), (void*)&x);
213+
if (changed != EParamUpdateStatus::Failed) {
214+
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
215+
}
217216
}
217+
} catch (std::exception const& e) {
218+
std::cerr << "Error in setValue (T) " << e.what() << "\n";
218219
}
219-
} catch (std::exception const& e) {
220-
std::cerr << "Error in setValue (T) " << e.what() << "\n";
221-
}
220+
}(sPtree);
222221
}
223222

224223
// specialized for std::string
225224
// which means that the type will be converted internally
226-
static void setValue(std::string const& key, std::string const& valuestring)
227-
{
228-
if (!sIsFullyInitialized) {
229-
initialize();
230-
}
231-
assert(sPtree);
232-
try {
233-
if (sPtree->get_optional<std::string>(key).is_initialized()) {
234-
sPtree->put(key, valuestring);
235-
auto changed = updateThroughStorageMapWithConversion(key, valuestring);
236-
if (changed != EParamUpdateStatus::Failed) {
237-
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
238-
}
239-
}
240-
} catch (std::exception const& e) {
241-
std::cerr << "Error in setValue (string) " << e.what() << "\n";
242-
}
243-
}
244-
225+
static void setValue(std::string const& key, std::string const& valuestring);
245226
static void setEnumValue(const std::string&, const std::string&);
246227
static void setArrayValue(const std::string&, const std::string&);
247228

@@ -306,7 +287,7 @@ class ConfigurableParam
306287
static std::string sOutputDir;
307288

308289
void setRegisterMode(bool b) { sRegisterMode = b; }
309-
bool isInitialized() const { return sIsFullyInitialized; }
290+
[[nodiscard]] bool isInitialized() const { return sIsFullyInitialized; }
310291

311292
// friend class o2::ccdb::CcdbApi;
312293
private:
@@ -318,8 +299,7 @@ class ConfigurableParam
318299
static bool sRegisterMode; //! (flag to enable/disable autoregistering of child classes)
319300
};
320301

321-
} // end namespace conf
322-
} // end namespace o2
302+
} // namespace o2::conf
323303

324304
// a helper macro for boilerplate code in parameter classes
325305
#define O2ParamDef(classname, key) \

Common/Utils/include/CommonUtils/ConfigurableParamHelper.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
#include <typeinfo>
2121
#include "TFile.h"
2222

23-
namespace o2
24-
{
25-
namespace conf
23+
namespace o2::conf
2624
{
2725

2826
// ----------------------------------------------------------------
@@ -34,7 +32,7 @@ struct ParamDataMember {
3432
std::string value;
3533
std::string provenance;
3634

37-
std::string toString(std::string const& prefix, bool showProv) const;
35+
[[nodiscard]] std::string toString(std::string const& prefix, bool showProv) const;
3836
};
3937

4038
// ----------------------------------------------------------------
@@ -82,14 +80,14 @@ class ConfigurableParamHelper : virtual public ConfigurableParam
8280

8381
// ----------------------------------------------------------------
8482

85-
std::string getName() const final
83+
[[nodiscard]] std::string getName() const final
8684
{
8785
return P::sKey;
8886
}
8987

9088
// ----------------------------------------------------------------
9189
// get the provenace of the member with given key
92-
EParamProvenance getMemberProvenance(const std::string& key) const final
90+
[[nodiscard]] EParamProvenance getMemberProvenance(const std::string& key) const final
9391
{
9492
return getProvenance(getName() + '.' + key);
9593
}
@@ -107,7 +105,7 @@ class ConfigurableParamHelper : virtual public ConfigurableParam
107105
}
108106

109107
//
110-
size_t getHash() const final
108+
[[nodiscard]] size_t getHash() const final
111109
{
112110
return _ParamHelper::getHashImpl(getName(), getDataMembers());
113111
}
@@ -124,7 +122,7 @@ class ConfigurableParamHelper : virtual public ConfigurableParam
124122

125123
// Grab the list of ConfigurableParam data members
126124
// Returns a nullptr if the TClass of the P template class cannot be created.
127-
std::vector<ParamDataMember>* getDataMembers() const
125+
[[nodiscard]] std::vector<ParamDataMember>* getDataMembers() const
128126
{
129127
// just a helper line to make sure P::sInstance is looked-up
130128
// and that compiler complains about missing static sInstance of type P
@@ -197,7 +195,6 @@ class ConfigurableParamHelper : virtual public ConfigurableParam
197195
}
198196
};
199197

200-
} // namespace conf
201198
} // namespace o2
202199

203200
#endif /* COMMON_SIMCONFIG_INCLUDE_SIMCONFIG_CONFIGURABLEPARAMHELPER_H_ */

Common/Utils/src/ConfigurableParam.cxx

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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"
@@ -39,10 +39,12 @@
3939
#include "TEnumConstant.h"
4040
#include <filesystem>
4141

42-
namespace o2
43-
{
44-
namespace conf
42+
namespace o2::conf
4543
{
44+
boost::property_tree::ptree readINI(std::string const& filepath);
45+
boost::property_tree::ptree readJSON(std::string const& filepath);
46+
boost::property_tree::ptree readConfigFile(std::string const& filepath);
47+
4648
std::vector<ConfigurableParam*>* ConfigurableParam::sRegisteredParamClasses = nullptr;
4749
boost::property_tree::ptree* ConfigurableParam::sPtree = nullptr;
4850
std::map<std::string, std::pair<std::type_info const&, void*>>* ConfigurableParam::sKeyToStorageMap = nullptr;
@@ -199,9 +201,9 @@ bool ConfigurableParam::configFileExists(std::string const& filepath)
199201

200202
// ------------------------------------------------------------------
201203

202-
boost::property_tree::ptree ConfigurableParam::readConfigFile(std::string const& filepath)
204+
boost::property_tree::ptree readConfigFile(std::string const& inputDir, std::string const& filepath)
203205
{
204-
auto inpfilename = o2::utils::Str::concat_string(sInputDir, filepath);
206+
auto inpfilename = o2::utils::Str::concat_string(inputDir, filepath);
205207
if (!std::filesystem::exists(inpfilename)) {
206208
LOG(fatal) << inpfilename << " : config file does not exist!";
207209
}
@@ -219,9 +221,28 @@ boost::property_tree::ptree ConfigurableParam::readConfigFile(std::string const&
219221
return pt;
220222
}
221223

224+
void ConfigurableParam::setValue(std::string const& key, std::string const& valuestring)
225+
{
226+
if (!sIsFullyInitialized) {
227+
initialize();
228+
}
229+
assert(sPtree);
230+
try {
231+
if (sPtree->get_optional<std::string>(key).is_initialized()) {
232+
sPtree->put(key, valuestring);
233+
auto changed = updateThroughStorageMapWithConversion(key, valuestring);
234+
if (changed != EParamUpdateStatus::Failed) {
235+
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
236+
}
237+
}
238+
} catch (std::exception const& e) {
239+
std::cerr << "Error in setValue (string) " << e.what() << "\n";
240+
}
241+
}
242+
222243
// ------------------------------------------------------------------
223244

224-
boost::property_tree::ptree ConfigurableParam::readINI(std::string const& filepath)
245+
boost::property_tree::ptree readINI(std::string const& filepath)
225246
{
226247
boost::property_tree::ptree pt;
227248
try {
@@ -237,7 +258,7 @@ boost::property_tree::ptree ConfigurableParam::readINI(std::string const& filepa
237258

238259
// ------------------------------------------------------------------
239260

240-
boost::property_tree::ptree ConfigurableParam::readJSON(std::string const& filepath)
261+
boost::property_tree::ptree readJSON(std::string const& filepath)
241262
{
242263
boost::property_tree::ptree pt;
243264

@@ -258,7 +279,7 @@ void ConfigurableParam::writeJSON(std::string const& filename, std::string const
258279
LOG(info) << "ignoring writing of json file " << filename;
259280
return;
260281
}
261-
initPropertyTree(); // update the boost tree before writing
282+
initPropertyTree(); // update the boost tree before writing
262283
auto outfilename = o2::utils::Str::concat_string(sOutputDir, filename);
263284
if (!keyOnly.empty()) { // write ini for selected key only
264285
try {
@@ -409,7 +430,7 @@ void ConfigurableParam::updateFromFile(std::string const& configFile, std::strin
409430
return;
410431
}
411432

412-
boost::property_tree::ptree pt = readConfigFile(cfgfile);
433+
boost::property_tree::ptree pt = readConfigFile(ConfigurableParam::sInputDir, cfgfile);
413434

414435
std::vector<std::pair<std::string, std::string>> keyValPairs;
415436
auto request = o2::utils::Str::tokenize(paramsList, ',', true);
@@ -953,5 +974,4 @@ ConfigurableParam::EParamUpdateStatus ConfigurableParam::updateThroughStorageMap
953974
return ConfigurableParam::EParamUpdateStatus::Failed;
954975
}
955976

956-
} // namespace conf
957-
} // namespace o2
977+
} // 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)