-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig_value.h
More file actions
82 lines (65 loc) · 2.19 KB
/
Copy pathconfig_value.h
File metadata and controls
82 lines (65 loc) · 2.19 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
#pragma once
#include <filesystem>
#include <optional>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "config/config_key_path.h"
#include "silo/common/panic.h"
namespace silo::config {
enum class ConfigValueType { STRING, PATH, INT32, UINT32, UINT16, BOOL, LIST };
constexpr std::string_view configValueTypeToString(ConfigValueType type) {
switch (type) {
case ConfigValueType::STRING:
return "string";
case ConfigValueType::PATH:
return "path";
case ConfigValueType::INT32:
return "i32";
case ConfigValueType::UINT32:
return "u32";
case ConfigValueType::UINT16:
return "u16";
case ConfigValueType::BOOL:
return "bool";
case ConfigValueType::LIST:
return "list";
}
SILO_UNREACHABLE();
}
class ConfigValue {
explicit ConfigValue(std::variant<
std::string,
std::filesystem::path,
int32_t,
uint32_t,
uint16_t,
bool,
std::vector<std::string>> value)
: value(std::move(value)) {}
public:
std::variant<
std::string,
std::filesystem::path,
int32_t,
uint32_t,
uint16_t,
bool,
std::vector<std::string>>
value;
static ConfigValue fromString(const std::string& value) { return ConfigValue{value}; }
static ConfigValue fromPath(const std::filesystem::path& value) {
ConfigValue result{value};
SILO_ASSERT(get_if<std::filesystem::path>(&result.value) != nullptr);
return result;
}
static ConfigValue fromInt32(int32_t value) { return ConfigValue{value}; }
static ConfigValue fromUint32(uint32_t value) { return ConfigValue{value}; }
static ConfigValue fromUint16(uint16_t value) { return ConfigValue{value}; }
static ConfigValue fromBool(bool value) { return ConfigValue{value}; }
static ConfigValue fromList(const std::vector<std::string>& value) { return ConfigValue{value}; }
[[nodiscard]] ConfigValueType getValueType() const;
[[nodiscard]] std::string toString() const;
};
} // namespace silo::config