5#include <aegis2d/core/export.hpp>
23class AEGIS2D_CORE_EXPORT
ConfigError :
public std::runtime_error {
25 using std::runtime_error::runtime_error;
35 using Array = std::vector<ConfigValue>;
37 using Object = std::vector<ConfigMember>;
40 std::variant<std::monostate, bool, std::int64_t, double, std::string, Array, Object>
data;
79 [[nodiscard]]
bool contains(std::string_view path)
const;
85 [[nodiscard]] std::optional<std::int64_t>
integer(std::string_view path)
const;
90 [[nodiscard]] std::optional<double>
number(std::string_view path)
const;
95 [[nodiscard]] std::optional<bool>
boolean(std::string_view path)
const;
100 [[nodiscard]] std::optional<std::string>
string(std::string_view path)
const;
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)
118 const auto value = number(path);
119 return value.has_value() ?
static_cast<T
>(*value) : fallback;
127 [[nodiscard]] std::string
value_or(std::string_view path, std::string_view fallback)
const;
132 [[nodiscard]] std::optional<ConfigValue>
value(std::string_view path)
const;
155 void save(
const std::filesystem::path& path)
const;
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