|
1 | 1 | #pragma once |
2 | | -#include <yaml-cpp/yaml.h> |
3 | 2 |
|
4 | | -#include <filesystem> |
5 | | -#include <iostream> |
6 | | -#include <optional> |
7 | | -#include <type_traits> |
8 | | - |
9 | | -#include "utils/str_expected.hpp" |
10 | | - |
11 | | -namespace sim { |
12 | | - |
13 | | -class ConfigNode; |
14 | | - |
15 | | -using ConfigNodeExpected = utils::StrExpected<ConfigNode>; |
16 | | - |
17 | | -class ConfigNode { |
18 | | -public: |
19 | | - explicit ConfigNode(YAML::Node a_node = YAML::Node(YAML::NodeType::Null), |
20 | | - std::optional<std::string> a_name = std::nullopt); |
21 | | - |
22 | | - // Some functional over yaml-cpp |
23 | | - |
24 | | - [[nodiscard]] const YAML::Node& get_node() const noexcept; |
25 | | - |
26 | | - [[nodiscard]] const std::optional<std::string>& get_name() const noexcept; |
27 | | - |
28 | | - [[nodiscard]] const std::string& get_name_or_throw() const; |
29 | | - |
30 | | - [[nodiscard]] std::runtime_error create_parsing_error( |
31 | | - std::string_view error) const; |
32 | | - |
33 | | - friend std::ostream& operator<<(std::ostream& out, const ConfigNode& node); |
34 | | - |
35 | | - std::string to_string() const; |
36 | | - |
37 | | - // yaml-cpp functional |
38 | | - |
39 | | - [[nodiscard]] YAML::NodeType::value Type() const; |
40 | | - [[nodiscard]] bool IsNull() const noexcept; |
41 | | - [[nodiscard]] bool IsScalar() const noexcept; |
42 | | - [[nodiscard]] bool IsSequence() const noexcept; |
43 | | - [[nodiscard]] bool IsMap() const noexcept; |
44 | | - |
45 | | - // access |
46 | | - template <typename T> |
47 | | - [[nodiscard]] utils::StrExpected<T> as() const noexcept { |
48 | | - try { |
49 | | - return m_node.as<T>(); |
50 | | - } catch (const std::exception& e) { |
51 | | - return std::unexpected(e.what()); |
52 | | - } catch (...) { |
53 | | - return std::unexpected( |
54 | | - "Undefined exception while calling `YAML::Node::as`"); |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - template <typename T> |
59 | | - T as_or_throw() const { |
60 | | - return as<T>().value_or_throw(); |
61 | | - } |
62 | | - |
63 | | - [[nodiscard]] const std::string& Tag() const noexcept; |
64 | | - |
65 | | - [[nodiscard]] std::size_t size() const noexcept; |
66 | | - |
67 | | - class Iterator { |
68 | | - public: |
69 | | - Iterator(YAML::const_iterator a_it); |
70 | | - |
71 | | - Iterator& operator++(); |
72 | | - |
73 | | - Iterator operator++(int); |
74 | | - |
75 | | - bool operator==(const Iterator& rhs) const; |
76 | | - |
77 | | - bool operator!=(const Iterator& rhs) const; |
78 | | - |
79 | | - ConfigNode operator*() const; |
80 | | - |
81 | | - private: |
82 | | - // Invariant: m_stacktrace_node is not null |
83 | | - YAML::const_iterator m_iterator; |
84 | | - }; |
85 | | - |
86 | | - Iterator begin() const; |
87 | | - Iterator end() const; |
88 | | - |
89 | | - // indexing |
90 | | - ConfigNodeExpected operator[](std::string_view key) const; |
91 | | - |
92 | | -private: |
93 | | - // Always correct |
94 | | - const YAML::Node m_node; |
95 | | - |
96 | | - const std::optional<std::string> m_name; |
97 | | -}; |
98 | | - |
99 | | -ConfigNode load_file(std::filesystem::path path); |
100 | | - |
101 | | -} // namespace sim |
| 3 | +#include "./config_node_impl.hpp" |
| 4 | +#include "./config_node_expected.hpp" |
0 commit comments