Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
config.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <aegis2d/core/export.hpp>
6
7#include <concepts>
8#include <cstdint>
9#include <filesystem>
10#include <optional>
11#include <stdexcept>
12#include <string>
13#include <string_view>
14#include <type_traits>
15#include <utility>
16#include <variant>
17#include <vector>
18
19namespace aegis2d::core {
20
23class AEGIS2D_CORE_EXPORT ConfigError : public std::runtime_error {
24public:
25 using std::runtime_error::runtime_error;
26};
27
28struct ConfigMember;
29
35 using Array = std::vector<ConfigValue>;
37 using Object = std::vector<ConfigMember>;
38
40 std::variant<std::monostate, bool, std::int64_t, double, std::string, Array, Object> data;
41};
42
46 std::string key;
49};
50
58class AEGIS2D_CORE_EXPORT Config {
59public:
61 Config() = default;
62
68 [[nodiscard]] static Config from_file(const std::filesystem::path& path);
69
74 [[nodiscard]] static Config from_string(std::string_view text);
75
79 [[nodiscard]] bool contains(std::string_view path) const;
80
85 [[nodiscard]] std::optional<std::int64_t> integer(std::string_view path) const;
86
90 [[nodiscard]] std::optional<double> number(std::string_view path) const;
91
95 [[nodiscard]] std::optional<bool> boolean(std::string_view path) const;
96
100 [[nodiscard]] std::optional<std::string> string(std::string_view path) const;
101
108 template <typename T>
109 requires std::is_arithmetic_v<T>
110 [[nodiscard]] T value_or(std::string_view path, T fallback) const {
111 if constexpr (std::same_as<T, bool>) {
112 return boolean(path).value_or(fallback);
113 } else if constexpr (std::integral<T>) {
114 const auto value = integer(path);
115 return value.has_value() && std::in_range<T>(*value) ? static_cast<T>(*value)
116 : fallback;
117 } else {
118 const auto value = number(path);
119 return value.has_value() ? static_cast<T>(*value) : fallback;
120 }
121 }
122
127 [[nodiscard]] std::string value_or(std::string_view path, std::string_view fallback) const;
128
132 [[nodiscard]] std::optional<ConfigValue> value(std::string_view path) const;
133
142 void set(std::string_view path, ConfigValue value);
143
147 [[nodiscard]] std::string to_string() const;
148
155 void save(const std::filesystem::path& path) const;
156
157private:
158 explicit Config(ConfigValue root) noexcept;
159
161};
162
163} // namespace aegis2d::core
Thrown when a configuration cannot be read or parsed.
Definition config.hpp:23
JSON configuration file (server port, window size...) read through typed getters.
Definition config.hpp:58
Config()=default
Creates an empty configuration: every lookup fails.
std::string to_string() const
Writes the configuration as JSON.
static Config from_file(const std::filesystem::path &path)
Reads and parses a JSON file.
std::optional< std::string > string(std::string_view path) const
Reads a string.
std::optional< bool > boolean(std::string_view path) const
Reads a boolean.
std::optional< ConfigValue > value(std::string_view path) const
Reads a value of any type, such as an array.
std::optional< std::int64_t > integer(std::string_view path) const
Reads an integer.
std::string value_or(std::string_view path, std::string_view fallback) const
Reads a string with a default.
std::optional< double > number(std::string_view path) const
Reads a number, integer or not.
T value_or(std::string_view path, T fallback) const
Reads a boolean, integer or floating-point value with a default.
Definition config.hpp:110
bool contains(std::string_view path) const
Tells whether a value exists at path, whatever its type.
void save(const std::filesystem::path &path) const
Writes the configuration to a file, creating its folder.
static Config from_string(std::string_view text)
Parses JSON text.
void set(std::string_view path, ConfigValue value)
Sets a value, creating the objects on its path.
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11
One member of a JSON object.
Definition config.hpp:44
std::string key
Member name.
Definition config.hpp:46
ConfigValue value
Member value.
Definition config.hpp:48
One value of a configuration document, independent of the JSON library.
Definition config.hpp:33
std::variant< std::monostate, bool, std::int64_t, double, std::string, Array, Object > data
The value itself.
Definition config.hpp:40
std::vector< ConfigMember > Object
JSON object, members in document order.
Definition config.hpp:37
std::vector< ConfigValue > Array
JSON array.
Definition config.hpp:35