Skip to content

Commit 48757bd

Browse files
committed
DPL: refactor ConfigParamRegistry to use C++20
1 parent 651d9e5 commit 48757bd

File tree

3 files changed

+179
-88
lines changed

3 files changed

+179
-88
lines changed

Framework/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ o2_add_library(Framework
5252
src/ConfigParamDiscovery.cxx
5353
src/ConfigParamStore.cxx
5454
src/ConfigParamsHelper.cxx
55+
src/ConfigParamRegistry.cxx
5556
src/ChannelParamSpec.cxx
5657
src/DDSConfigHelpers.cxx
5758
src/DataAllocator.cxx

Framework/Core/include/Framework/ConfigParamRegistry.h

Lines changed: 47 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,38 @@
1111
#ifndef O2_FRAMEWORK_CONFIGPARAMREGISTRY_H_
1212
#define O2_FRAMEWORK_CONFIGPARAMREGISTRY_H_
1313

14-
#include "Framework/ParamRetriever.h"
14+
#include "Array2D.h"
1515
#include "Framework/ConfigParamStore.h"
16+
#include <boost/property_tree/ptree.hpp>
1617
#include "Framework/Traits.h"
17-
#include "Framework/VariantPropertyTreeHelpers.h"
1818

19-
#include <boost/property_tree/ptree_fwd.hpp>
19+
#include <concepts>
2020
#include <memory>
2121
#include <string>
2222
#include <cassert>
2323

24-
namespace
25-
{
2624
template <typename T>
27-
constexpr auto isSimpleType()
28-
{
29-
return std::is_same_v<T, int> ||
30-
std::is_same_v<T, int8_t> ||
31-
std::is_same_v<T, int16_t> ||
32-
std::is_same_v<T, uint8_t> ||
33-
std::is_same_v<T, uint16_t> ||
34-
std::is_same_v<T, uint32_t> ||
35-
std::is_same_v<T, uint64_t> ||
36-
std::is_same_v<T, int64_t> ||
37-
std::is_same_v<T, long> ||
38-
std::is_same_v<T, float> ||
39-
std::is_same_v<T, double> ||
40-
std::is_same_v<T, bool>;
41-
}
42-
} // namespace
25+
concept SimpleConfigValueType = std::same_as<T, int> ||
26+
std::same_as<T, int8_t> ||
27+
std::same_as<T, int16_t> ||
28+
std::same_as<T, uint8_t> ||
29+
std::same_as<T, uint16_t> ||
30+
std::same_as<T, uint32_t> ||
31+
std::same_as<T, uint64_t> ||
32+
std::same_as<T, int64_t> ||
33+
std::same_as<T, long> ||
34+
std::same_as<T, float> ||
35+
std::same_as<T, double> ||
36+
std::same_as<T, bool>;
37+
38+
template <typename T>
39+
concept StringConfigValueType = std::same_as<T, std::string>;
40+
41+
template <typename T>
42+
concept PtreeConfigValueType = std::same_as<T, boost::property_tree::ptree> || std::constructible_from<T, boost::property_tree::ptree>;
43+
44+
template <typename T>
45+
concept ConfigValueType = SimpleConfigValueType<T> || StringConfigValueType<T> || o2::framework::base_of_template<std::vector, T> || o2::framework::base_of_template<o2::framework::Array2D, T> || o2::framework::base_of_template<o2::framework::LabeledArray, T>;
4346

4447
namespace o2::framework
4548
{
@@ -54,87 +57,43 @@ class ConfigParamStore;
5457
class ConfigParamRegistry
5558
{
5659
public:
57-
ConfigParamRegistry(std::unique_ptr<ConfigParamStore> store)
58-
: mStore{std::move(store)}
59-
{
60-
}
60+
ConfigParamRegistry(std::unique_ptr<ConfigParamStore> store);
6161

62-
bool isSet(const char* key) const
63-
{
64-
return mStore->store().count(key);
65-
}
62+
bool isSet(const char* key) const;
6663

67-
bool hasOption(const char* key) const
68-
{
69-
return mStore->store().get_child_optional(key).is_initialized();
70-
}
64+
bool hasOption(const char* key) const;
7165

72-
bool isDefault(const char* key) const
73-
{
74-
return mStore->store().count(key) > 0 && mStore->provenance(key) != "default";
75-
}
66+
bool isDefault(const char* key) const;
7667

77-
[[nodiscard]] std::vector<ConfigParamSpec> const& specs() const
78-
{
79-
return mStore->specs();
80-
}
68+
[[nodiscard]] std::vector<ConfigParamSpec> const& specs() const;
8169

82-
template <typename T>
83-
T get(const char* key) const
84-
{
85-
assert(mStore.get());
86-
try {
87-
if constexpr (isSimpleType<T>()) {
88-
return mStore->store().get<T>(key);
89-
} else if constexpr (std::is_same_v<T, std::string>) {
90-
return mStore->store().get<std::string>(key);
91-
} else if constexpr (std::is_same_v<T, std::string_view>) {
92-
return std::string_view{mStore->store().get<std::string>(key)};
93-
} else if constexpr (base_of_template<std::vector, T>) {
94-
return vectorFromBranch<typename T::value_type>(mStore->store().get_child(key));
95-
} else if constexpr (base_of_template<o2::framework::Array2D, T>) {
96-
return array2DFromBranch<typename T::element_t>(mStore->store().get_child(key));
97-
} else if constexpr (base_of_template<o2::framework::LabeledArray, T>) {
98-
return labeledArrayFromBranch<typename T::element_t>(mStore->store().get_child(key));
99-
} else if constexpr (std::is_same_v<T, boost::property_tree::ptree>) {
100-
return mStore->store().get_child(key);
101-
} else if constexpr (std::is_constructible_v<T, boost::property_tree::ptree>) {
102-
return T{mStore->store().get_child(key)};
103-
} else if constexpr (std::is_constructible_v<T, boost::property_tree::ptree> == false) {
104-
static_assert(std::is_constructible_v<T, boost::property_tree::ptree> == false,
105-
"Not a basic type and no constructor from ptree provided");
106-
}
107-
} catch (std::exception& e) {
108-
throw std::invalid_argument(std::string("missing option: ") + key + " (" + e.what() + ")");
109-
} catch (...) {
110-
throw std::invalid_argument(std::string("error parsing option: ") + key);
111-
}
112-
throw std::invalid_argument(std::string("bad type for option: ") + key);
113-
}
70+
template <ConfigValueType T>
71+
T get(const char* key) const;
11472

11573
template <typename T>
116-
void override(const char* key, const T& val) const
117-
{
118-
assert(mStore.get());
119-
try {
120-
mStore->store().put(key, val);
121-
} catch (std::exception& e) {
122-
throw std::invalid_argument(std::string("failed to store an option: ") + key + " (" + e.what() + ")");
123-
} catch (...) {
124-
throw std::invalid_argument(std::string("failed to store an option: ") + key);
125-
}
126-
}
74+
T get(const char* key) const;
75+
76+
void override(const char* key, ConfigValueType auto const& val) const;
12777

12878
// Load extra parameters discovered while we process data
129-
void loadExtra(std::vector<ConfigParamSpec>& extras)
130-
{
131-
mStore->load(extras);
132-
}
79+
void loadExtra(std::vector<ConfigParamSpec>& extras);
13380

13481
private:
13582
std::unique_ptr<ConfigParamStore> mStore;
13683
};
13784

85+
template <typename T>
86+
T ConfigParamRegistry::get(const char* key) const
87+
{
88+
try {
89+
return T{mStore->store().get_child(key)};
90+
} catch (std::exception& e) {
91+
throw std::invalid_argument(std::string("missing option: ") + key + " (" + e.what() + ")");
92+
} catch (...) {
93+
throw std::invalid_argument(std::string("error parsing option: ") + key);
94+
}
95+
}
96+
13897
} // namespace o2::framework
13998

14099
#endif // O2_FRAMEWORK_CONFIGPARAMREGISTRY_H_
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
#include "Framework/ConfigParamRegistry.h"
12+
#include "Framework/VariantPropertyTreeHelpers.h"
13+
14+
namespace o2::framework
15+
{
16+
17+
ConfigParamRegistry::ConfigParamRegistry(std::unique_ptr<ConfigParamStore> store)
18+
: mStore{std::move(store)}
19+
{
20+
}
21+
22+
bool ConfigParamRegistry::isSet(const char* key) const
23+
{
24+
return mStore->store().count(key);
25+
}
26+
27+
bool ConfigParamRegistry::hasOption(const char* key) const
28+
{
29+
return mStore->store().get_child_optional(key).is_initialized();
30+
}
31+
32+
bool ConfigParamRegistry::isDefault(const char* key) const
33+
{
34+
return mStore->store().count(key) > 0 && mStore->provenance(key) != "default";
35+
}
36+
37+
namespace
38+
{
39+
template <SimpleConfigValueType T>
40+
T getImpl(boost::property_tree::ptree const& tree, const char* key)
41+
{
42+
return tree.get<T>(key);
43+
}
44+
45+
template <StringConfigValueType T>
46+
T getImpl(boost::property_tree::ptree const& tree, const char* key)
47+
{
48+
return tree.get<std::string>(key);
49+
}
50+
51+
template <typename T>
52+
requires base_of_template<std::vector, T>
53+
auto getImpl(boost::property_tree::ptree const& tree, const char* key)
54+
{
55+
return o2::framework::vectorFromBranch<typename T::value_type>(tree.get_child(key));
56+
}
57+
58+
template <typename T>
59+
requires base_of_template<o2::framework::Array2D, T>
60+
auto getImpl(boost::property_tree::ptree& tree, const char* key)
61+
{
62+
return array2DFromBranch<typename T::element_t>(tree.get_child(key));
63+
}
64+
65+
template <typename T>
66+
requires base_of_template<o2::framework::LabeledArray, T>
67+
auto getImpl(boost::property_tree::ptree& tree, const char* key)
68+
{
69+
return labeledArrayFromBranch<typename T::element_t>(tree.get_child(key));
70+
}
71+
} // namespace
72+
73+
template <ConfigValueType T>
74+
T ConfigParamRegistry::get(const char* key) const
75+
{
76+
try {
77+
return getImpl<T>(this->mStore->store(), key);
78+
} catch (std::exception& e) {
79+
throw std::invalid_argument(std::string("missing option: ") + key + " (" + e.what() + ")");
80+
} catch (...) {
81+
throw std::invalid_argument(std::string("error parsing option: ") + key);
82+
}
83+
}
84+
85+
void ConfigParamRegistry::override(const char* key, ConfigValueType auto const& val) const
86+
{
87+
try {
88+
mStore->store().put(key, val);
89+
} catch (std::exception& e) {
90+
throw std::invalid_argument(std::string("failed to store an option: ") + key + " (" + e.what() + ")");
91+
} catch (...) {
92+
throw std::invalid_argument(std::string("failed to store an option: ") + key);
93+
}
94+
}
95+
96+
// Load extra parameters discovered while we process data
97+
void ConfigParamRegistry::loadExtra(std::vector<ConfigParamSpec>& extras)
98+
{
99+
mStore->load(extras);
100+
}
101+
102+
[[nodiscard]] std::vector<ConfigParamSpec> const& ConfigParamRegistry::specs() const
103+
{
104+
return mStore->specs();
105+
}
106+
107+
template int8_t ConfigParamRegistry::get<int8_t>(const char* key) const;
108+
template int16_t ConfigParamRegistry::get<int16_t>(const char* key) const;
109+
template int32_t ConfigParamRegistry::get<int32_t>(const char* key) const;
110+
template int64_t ConfigParamRegistry::get<int64_t>(const char* key) const;
111+
template uint8_t ConfigParamRegistry::get<uint8_t>(const char* key) const;
112+
template uint32_t ConfigParamRegistry::get<uint32_t>(const char* key) const;
113+
template uint16_t ConfigParamRegistry::get<uint16_t>(const char* key) const;
114+
template uint64_t ConfigParamRegistry::get<uint64_t>(const char* key) const;
115+
template LabeledArray<std::string> ConfigParamRegistry::get<LabeledArray<std::string>>(const char* key) const;
116+
template LabeledArray<double> ConfigParamRegistry::get<LabeledArray<double>>(const char* key) const;
117+
template LabeledArray<float> ConfigParamRegistry::get<LabeledArray<float>>(const char* key) const;
118+
template LabeledArray<int> ConfigParamRegistry::get<LabeledArray<int>>(const char* key) const;
119+
template Array2D<std::string> ConfigParamRegistry::get<Array2D<std::string>>(const char* key) const;
120+
template Array2D<double> ConfigParamRegistry::get<Array2D<double>>(const char* key) const;
121+
template Array2D<float> ConfigParamRegistry::get<Array2D<float>>(const char* key) const;
122+
template Array2D<int> ConfigParamRegistry::get<Array2D<int>>(const char* key) const;
123+
template std::vector<std::string> ConfigParamRegistry::get<std::vector<std::string>>(const char* key) const;
124+
template std::vector<double> ConfigParamRegistry::get<std::vector<double>>(const char* key) const;
125+
template std::vector<float> ConfigParamRegistry::get<std::vector<float>>(const char* key) const;
126+
template std::vector<int> ConfigParamRegistry::get<std::vector<int>>(const char* key) const;
127+
template float ConfigParamRegistry::get<float>(const char* key) const;
128+
template double ConfigParamRegistry::get<double>(const char* key) const;
129+
template std::string ConfigParamRegistry::get<std::string>(const char* key) const;
130+
template bool ConfigParamRegistry::get<bool>(const char* key) const;
131+
} // namespace o2::framework

0 commit comments

Comments
 (0)