-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml_loader.hpp
153 lines (135 loc) · 5.45 KB
/
yaml_loader.hpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef YAML_LOADER_HPP
#define YAML_LOADER_HPP
#if __cplusplus < 201402L
#error "This code requires C++14 or higher. Please set the compiler to use C++14."
#endif
#include <iostream>
#include <string>
#include <vector>
#include <yaml-cpp/yaml.h>
#include <stdexcept>
#include <vector>
#include <type_traits>
namespace yaml_loader {
template <typename T>
struct is_vector : std::false_type {};
template <typename T, typename Alloc>
struct is_vector<std::vector<T, Alloc>> : std::true_type {};
class YamlLoader {
public:
explicit YamlLoader(const std::string& file_path) {
path_ = file_path;
std::cout << "Load config file: " << path_ << std::endl;
}
template <typename T>
bool LoadParam(const std::string& param_name, T& param_value, const T& default_value = T{},
const bool& required = false) {
return loadParamInternal(param_name, param_value, default_value, required);
}
template <class T>
bool LoadParam(const std::string& param_name, std::vector<T>& param_value,
const std::vector<T> default_value = {}, const bool& required = false) {
return loadParamInternal(param_name, param_value, default_value, required);
}
private:
std::string path_;
template <typename T>
bool loadParamInternal(const std::string& param_name, T& param_value, const T& default_value, bool required) {
const auto config_ = YAML::LoadFile(path_);
YAML::Node tmp_node;
if (containsSlash(param_name)) {
tmp_node = getNodeFromPath(param_name);
}
else {
if (config_[param_name]) {
tmp_node = config_[param_name];
}
}
if (!tmp_node.IsNull()) {
// 检查类型是否匹配
if constexpr (is_vector<T>::value) {
if (tmp_node.Type() == YAML::NodeType::Sequence) {
try {
param_value = tmp_node.as<T>();
printf("\033[0;32m Load param %s success: \033[0;0m", param_name.c_str());
printValue(param_value);
return true;
} catch (const YAML::BadConversion&) {
printf("\033[0;31m Type mismatch for param %s, using default value: \033[0;0m", param_name.c_str());
param_value = default_value;
printValue(param_value);
return false;
}
} else {
printf("\033[0;31m Type mismatch for param %s, using default value: \033[0;0m", param_name.c_str());
param_value = default_value;
printValue(param_value);
return false;
}
} else {
// 处理非 vector 类型
try {
param_value = tmp_node.as<T>();
printf("\033[0;32m Load param %s success: \033[0;0m", param_name.c_str());
printValue(param_value);
return true;
} catch (const YAML::BadConversion&) {
printf("\033[0;31m Type mismatch for param %s, using default value: \033[0;0m", param_name.c_str());
param_value = default_value;
printValue(param_value);
return false;
}
}
}
else {
printf("\033[0;33m Load param %s failed, use default value: \033[0;0m", param_name.c_str());
param_value = default_value;
printValue(param_value);
if (required) {
throw std::invalid_argument("Required param " + param_name + " not found");
}
return false;
}
}
static bool containsSlash(const std::string& str) {
return str.find('/') != std::string::npos;
}
template <typename T>
void printValue(const T& param_value) {
if constexpr (is_vector<T>::value) {
std::cout << "[";
for (const auto& elem : param_value) {
std::cout << elem << " ";
}
std::cout << "]" << std::endl;
}
else {
std::cout << param_value << std::endl;
}
}
[[nodiscard]] YAML::Node getNodeFromPath(const std::string& path) const {
auto node = YAML::LoadFile(path_);
size_t pos = 0, next_pos;
while ((next_pos = path.find('/', pos)) != std::string::npos) {
std::string key = path.substr(pos, next_pos - pos);
if (node[key]) {
node = node[key];
}
else {
return {};
}
pos = next_pos + 1;
}
if (node.IsScalar()) {
return {};
}
if (!node[path.substr(pos)].IsNull() &&
node[path.substr(pos)]) {
node = node[path.substr(pos)];
return node;
}
return {};
}
};
}
#endif //YAML_LOADER_HPP