5#include <yaml-cpp/yaml.h>
12bool convert<std::optional<T>>::decode(
const Node & yaml, std::optional<T> & obj)
23Node convert<std::optional<T>>::encode(
const std::optional<T> & obj)
25 if (obj.has_value()) {
32template<
typename K,
typename V,
typename C,
typename A>
33bool convert<std::unordered_map<K, V, C, A>>::decode(
const Node & yaml, std::unordered_map<K, V, C, A> & obj)
36 for (
const auto & node : yaml) {
37 const auto key = node.first.as<std::string>();
38 if (obj.contains(key)) {
throw YAML::ParserException(yaml.Mark(),
"Double key '" + key +
"' in map"); }
39 obj[node.first.as<K>()] = node.second.as<V>();
44template<
typename K,
typename V,
typename C,
typename A>
45Node convert<std::unordered_map<K, V, C, A>>::encode(
const std::unordered_map<K, V, C, A> & obj)
48 for (
const auto & [key, val] : obj) { node[key] = YAML::Node(val); }
52inline bool convert<std::filesystem::path>::decode(
const Node & yaml, std::filesystem::path & obj)
54 obj = yaml.as<std::string>();
58inline Node convert<std::filesystem::path>::encode(
const std::filesystem::path & obj) {
return Node(obj.string()); }
60template<
intmax_t Num,
intmax_t Den>
61bool convert<std::chrono::duration<int64_t, std::ratio<Num, Den>>>::decode(
62 const Node & yaml, std::chrono::duration<int64_t, std::ratio<Num, Den>> & obj)
64 using RetType = std::chrono::duration<int64_t, std::ratio<Num, Den>>;
65 const auto str = yaml.as<std::string>();
66 if (str.ends_with(
"ms")) {
67 obj = std::chrono::duration_cast<RetType>(std::chrono::milliseconds(std::stoi(str.substr(0, str.size() - 2))));
68 }
else if (str.ends_with(
"us")) {
69 obj = std::chrono::duration_cast<RetType>(std::chrono::microseconds(std::stoi(str.substr(0, str.size() - 2))));
70 }
else if (str.ends_with(
"ns")) {
71 obj = std::chrono::duration_cast<RetType>(std::chrono::nanoseconds(std::stoi(str.substr(0, str.size() - 2))));
72 }
else if (str.ends_with(
"s")) {
73 obj = std::chrono::duration_cast<RetType>(std::chrono::seconds(std::stoi(str.substr(0, str.size() - 1))));
74 }
else if (str.ends_with(
"m")) {
75 obj = std::chrono::duration_cast<RetType>(std::chrono::minutes(std::stoi(str.substr(0, str.size() - 1))));
76 }
else if (str.ends_with(
"h")) {
77 obj = std::chrono::duration_cast<RetType>(std::chrono::hours(std::stoi(str.substr(0, str.size() - 1))));
79 throw YAML::ParserException{
81 "Could not detect suffix in '" + str +
"', expected s, ms, us, or ns",
88template<
intmax_t Num,
intmax_t Den>
89Node convert<std::chrono::duration<int64_t, std::ratio<Num, Den>>>::encode(
90 const std::chrono::duration<int64_t, std::ratio<Num, Den>> & obj)
92 using R = std::ratio<Num, Den>;
93 if constexpr (std::is_same_v<R, std::milli>) {
94 return std::to_string(obj.count()) +
"ms";
95 }
else if constexpr (std::is_same_v<R, std::micro>) {
96 return std::to_string(obj.count()) +
"us";
97 }
else if constexpr (std::is_same_v<R, std::nano>) {
98 return std::to_string(obj.count()) +
"ns";
99 }
else if constexpr (std::is_same_v<R, std::ratio<1>>) {
100 return std::to_string(obj.count()) +
"s";
101 }
else if constexpr (std::is_same_v<R, std::ratio<60>>) {
102 return std::to_string(obj.count()) +
"m";
103 }
else if constexpr (std::is_same_v<R, std::ratio<3600>>) {
104 return std::to_string(obj.count()) +
"h";
106 return std::to_string(std::chrono::duration_cast<std::chrono::nanoseconds>(obj).count()) +
"ns";
YAML forward declarations.