-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_node_.hpp
More file actions
105 lines (72 loc) · 2.65 KB
/
Copy pathconfig_node_.hpp
File metadata and controls
105 lines (72 loc) · 2.65 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
#pragma once
#include <yaml-cpp/yaml.h>
#include <filesystem>
#include <iostream>
#include <optional>
#include <type_traits>
#include "utils/str_expected.hpp"
namespace sim {
class ConfigNodeExpected;
class ConfigNode {
public:
explicit ConfigNode(YAML::Node a_node = YAML::Node(YAML::NodeType::Null),
std::optional<std::string> a_name = std::nullopt,
std::optional<std::string> a_path_node = std::nullopt);
// Some functional over yaml-cpp
[[nodiscard]] const YAML::Node& get_node() const noexcept;
[[nodiscard]] const std::optional<std::string>& get_name() const noexcept;
[[nodiscard]] const std::string& get_name_or_throw() const;
[[nodiscard]] std::runtime_error create_parsing_error(
std::string_view error) const;
const std::optional<std::string>& get_path_node() const;
friend std::ostream& operator<<(std::ostream& out, const ConfigNode& node);
std::string to_string() const;
// yaml-cpp functional
[[nodiscard]] YAML::NodeType::value Type() const;
[[nodiscard]] bool IsNull() const noexcept;
[[nodiscard]] bool IsScalar() const noexcept;
[[nodiscard]] bool IsSequence() const noexcept;
[[nodiscard]] bool IsMap() const noexcept;
// access
template <typename T>
[[nodiscard]] utils::StrExpected<T> as() const noexcept {
try {
return m_node.as<T>();
} catch (const std::exception& e) {
return std::unexpected(e.what());
} catch (...) {
return std::unexpected(
"Undefined exception while calling `YAML::Node::as`");
}
}
template <typename T>
T as_or_throw() const {
return as<T>().value_or_throw();
}
[[nodiscard]] const std::string& Tag() const noexcept;
[[nodiscard]] std::size_t size() const noexcept;
class Iterator {
public:
Iterator(YAML::const_iterator a_it, const ConfigNode* a_owner);
Iterator& operator++();
Iterator operator++(int);
bool operator==(const Iterator& rhs) const;
bool operator!=(const Iterator& rhs) const;
ConfigNode operator*() const;
private:
// Invariant: m_stacktrace_node is not null
YAML::const_iterator m_iterator;
const ConfigNode* m_owner;
};
Iterator begin() const;
Iterator end() const;
// indexing
ConfigNodeExpected operator[](std::string_view key) const;
private:
// Always correct
const YAML::Node m_node;
const std::optional<std::string> m_name;
mutable std::optional<std::string> m_path_node;
};
ConfigNode load_file(std::filesystem::path path);
} // namespace sim