forked from Dr15Jones/root_serialization
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathConfigurationParameters.h
More file actions
86 lines (64 loc) · 2.1 KB
/
ConfigurationParameters.h
File metadata and controls
86 lines (64 loc) · 2.1 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
#if !defined(ConfigurationParameters_h)
#define ConfigurationParameters_h
#include <map>
#include <vector>
#include <string>
#include <string_view>
#include <optional>
namespace cce::tf {
class ConfigurationParameters {
public:
using KeyValueMap = std::map<std::string, std::string, std::less<>>;
explicit ConfigurationParameters(KeyValueMap iKeyValues):
keyValues_{std::move(iKeyValues)} {
unusedKeys_.reserve(keyValues_.size());
for(auto const& kv: keyValues_) {
unusedKeys_.push_back(kv.first);
}
}
ConfigurationParameters() = delete;
template<typename T>
std::optional<T> get(std::string_view iName) const {
auto itFound = keyValues_.find(iName);
if(itFound == keyValues_.end()) {
return std::nullopt;
}
keyUsed(iName);
return convert<T>(itFound->second);
}
template<typename T>
T get(std::string_view iName, T iDefault) const {
auto v = get<T>(iName);
if(v) {
return *v;
}
return iDefault;
}
std::vector<std::string_view> unusedKeys() const {
return unusedKeys_;
}
private:
template<typename T> static T convert(std::string const& iValue);
void keyUsed(std::string_view iName) const {
auto itUsed = std::lower_bound(unusedKeys_.begin(), unusedKeys_.end(), iName);
if(itUsed != unusedKeys_.end() and *itUsed == iName) {
unusedKeys_.erase(itUsed);
}
}
KeyValueMap keyValues_;
mutable std::vector<std::string_view> unusedKeys_;
};
template<>
std::string ConfigurationParameters::convert<std::string>(std::string const& iValue);
template<>
int ConfigurationParameters::convert<int>(std::string const& iValue);
template<>
unsigned int ConfigurationParameters::convert<unsigned int>(std::string const& iValue);
template<>
bool ConfigurationParameters::convert<bool>(std::string const& iValue);
template<>
float ConfigurationParameters::convert<float>(std::string const& iValue);
template<>
std::size_t ConfigurationParameters::convert<std::size_t>(std::string const& iValue);
}
#endif