Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #pragma once
16 : :
17 : : #include <cstdint>
18 : : #include <functional>
19 : : #include <string>
20 : : #include <string_view>
21 : :
22 : : namespace hpactor::config {
23 : :
24 : : class TomlValueView {
25 : : public:
26 : : enum class Kind : uint8_t {
27 : : Missing = 0,
28 : : String,
29 : : Integer,
30 : : FloatingPoint,
31 : : Boolean,
32 : : Array,
33 : : Table,
34 : : };
35 : :
36 : 0 : TomlValueView() noexcept = default;
37 : :
38 : : Kind kind() const noexcept;
39 : : bool is_string() const noexcept;
40 : : bool is_integer() const noexcept;
41 : : bool is_floating_point() const noexcept;
42 : : bool is_boolean() const noexcept;
43 : :
44 : : std::string as_string(std::string_view fallback) const;
45 : : int64_t as_int64(int64_t fallback) const noexcept;
46 : : double as_double(double fallback) const noexcept;
47 : : bool as_bool(bool fallback) const noexcept;
48 : :
49 : : private:
50 : : friend class TomlTableView;
51 : : explicit TomlValueView(const void* node) noexcept;
52 : :
53 : : const void* node_{nullptr};
54 : : };
55 : :
56 : : class TomlTableView {
57 : : public:
58 : : using StringArrayVisitor = std::function<void(std::string_view)>;
59 : : using TableArrayVisitor = std::function<void(TomlTableView)>;
60 : : using NamedTableVisitor = std::function<void(std::string_view, TomlTableView)>;
61 : : using KeyValueVisitor = std::function<void(std::string_view, TomlValueView)>;
62 : : using IntegerArrayVisitor = std::function<void(int64_t)>;
63 : :
64 : 426 : TomlTableView() noexcept = default;
65 : :
66 : : bool valid() const noexcept;
67 : : bool contains(std::string_view key) const;
68 : : TomlValueView value(std::string_view key) const;
69 : : TomlTableView table(std::string_view key) const;
70 : :
71 : : std::string
72 : : read_string(std::string_view key, std::string_view fallback = "") const;
73 : : uint32_t read_uint32(std::string_view key, uint32_t fallback = 0) const noexcept;
74 : : bool read_bool(std::string_view key, bool fallback = false) const noexcept;
75 : : double read_double(std::string_view key, double fallback = 0.0) const noexcept;
76 : :
77 : : void for_each_string_array(std::string_view key,
78 : : const StringArrayVisitor& visitor) const;
79 : : void for_each_table_array(std::string_view key,
80 : : const TableArrayVisitor& visitor) const;
81 : : void for_each_subtable(std::string_view key,
82 : : const NamedTableVisitor& visitor) const;
83 : : void for_each_key_value(std::string_view key,
84 : : const KeyValueVisitor& visitor) const;
85 : : void for_each_integer_array(std::string_view key,
86 : : const IntegerArrayVisitor& visitor) const;
87 : : void for_each_entry(const KeyValueVisitor& visitor) const;
88 : :
89 : : private:
90 : : friend TomlTableView make_toml_table_view(const void* table) noexcept;
91 : : explicit TomlTableView(const void* table) noexcept;
92 : :
93 : : const void* table_{nullptr};
94 : : };
95 : :
96 : : TomlTableView make_toml_table_view(const void* table) noexcept;
97 : :
98 : : } // namespace hpactor::config
|