-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathverified_config_attributes.cpp
More file actions
114 lines (100 loc) · 3.89 KB
/
Copy pathverified_config_attributes.cpp
File metadata and controls
114 lines (100 loc) · 3.89 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
#include "config/verified_config_attributes.h"
#include <spdlog/spdlog.h>
#include "config/source/yaml_file.h"
#include "silo/common/panic.h"
namespace {
using silo::config::ConfigKeyPath;
using silo::config::ConfigValue;
using silo::config::ConfigValueType;
using silo::config::YamlFile;
template <typename T, ConfigValueType ExpectedType>
std::optional<T> getValue(
const ConfigKeyPath& config_key_path,
const std::unordered_map<ConfigKeyPath, ConfigValue>& config_values
) {
auto value_it = config_values.find(config_key_path);
if (value_it != config_values.end()) {
const ConfigValue& value = value_it->second;
if (value.getValueType() != ExpectedType) {
SILO_PANIC(
"Called getValue with type {} on a ConfigKeyPath ('{}') that belongs to a value of "
"another type ({}).",
configValueTypeToString(ExpectedType),
YamlFile::configKeyPathToString(config_key_path),
configValueTypeToString(value.getValueType())
);
}
SPDLOG_TRACE(
"Using for key `{}` the value {}",
YamlFile::configKeyPathToString(config_key_path),
value.toString()
);
return std::get<T>(value.value);
}
return std::nullopt;
}
} // namespace
namespace silo::config {
std::optional<std::string> VerifiedConfigAttributes::getString(const ConfigKeyPath& config_key_path
) const {
return getValue<std::string, ConfigValueType::STRING>(config_key_path, config_values);
}
std::optional<std::filesystem::path> VerifiedConfigAttributes::getPath(
const ConfigKeyPath& config_key_path
) const {
return getValue<std::filesystem::path, ConfigValueType::PATH>(config_key_path, config_values);
}
std::optional<int32_t> VerifiedConfigAttributes::getInt32(const ConfigKeyPath& config_key_path
) const {
return getValue<int32_t, ConfigValueType::INT32>(config_key_path, config_values);
}
std::optional<uint32_t> VerifiedConfigAttributes::getUint32(const ConfigKeyPath& config_key_path
) const {
return getValue<uint32_t, ConfigValueType::UINT32>(config_key_path, config_values);
}
std::optional<uint16_t> VerifiedConfigAttributes::getUint16(const ConfigKeyPath& config_key_path
) const {
return getValue<uint16_t, ConfigValueType::UINT16>(config_key_path, config_values);
}
std::optional<bool> VerifiedConfigAttributes::getBool(const ConfigKeyPath& config_key_path) const {
auto value_it = config_values.find(config_key_path);
if (value_it != config_values.end()) {
const ConfigValue& value = value_it->second;
if (value.getValueType() != ConfigValueType::BOOL) {
SILO_PANIC(
"Called getBool on a ConfigKeyPath ('{}') that belongs to a value of another "
"type ({}).",
YamlFile::configKeyPathToString(config_key_path),
configValueTypeToString(value.getValueType())
);
}
SPDLOG_TRACE(
"Using for key `{}` the value {}",
YamlFile::configKeyPathToString(config_key_path),
value.toString()
);
return get<bool>(value.value);
}
return std::nullopt;
}
std::optional<std::vector<std::string>> VerifiedConfigAttributes::getList(
const ConfigKeyPath& config_key_path
) const {
return getValue<std::vector<std::string>, ConfigValueType::LIST>(config_key_path, config_values);
}
VerifiedCommandLineArguments VerifiedCommandLineArguments::askingForHelp() {
VerifiedCommandLineArguments result;
result.asks_for_help = true;
return result;
}
VerifiedCommandLineArguments VerifiedCommandLineArguments::fromConfigValuesAndPositionalArguments(
std::unordered_map<ConfigKeyPath, ConfigValue> config_values,
std::vector<std::string> positional_arguments
) {
VerifiedCommandLineArguments result;
result.config_values = std::move(config_values);
result.positional_arguments = std::move(positional_arguments);
result.asks_for_help = false;
return result;
}
} // namespace silo::config