Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::core::Config Class Reference

JSON configuration file (server port, window size...) read through typed getters. More...

#include <aegis2d/core/config.hpp>

Public Member Functions

 Config ()=default
 Creates an empty configuration: every lookup fails.
 
bool contains (std::string_view path) const
 Tells whether a value exists at path, whatever its type.
 
std::optional< std::int64_t > integer (std::string_view path) const
 Reads an integer.
 
std::optional< double > number (std::string_view path) const
 Reads a number, integer or not.
 
std::optional< bool > boolean (std::string_view path) const
 Reads a boolean.
 
std::optional< std::string > string (std::string_view path) const
 Reads a string.
 
template<typename T >
requires std::is_arithmetic_v<T>
T value_or (std::string_view path, T fallback) const
 Reads a boolean, integer or floating-point value with a default.
 
std::string value_or (std::string_view path, std::string_view fallback) const
 Reads a string with a default.
 
std::optional< ConfigValue > value (std::string_view path) const
 Reads a value of any type, such as an array.
 
void set (std::string_view path, ConfigValue value)
 Sets a value, creating the objects on its path.
 
std::string to_string () const
 Writes the configuration as JSON.
 
void save (const std::filesystem::path &path) const
 Writes the configuration to a file, creating its folder.
 

Static Public Member Functions

static Config from_file (const std::filesystem::path &path)
 Reads and parses a JSON file.
 
static Config from_string (std::string_view text)
 Parses JSON text.
 

Detailed Description

JSON configuration file (server port, window size...) read through typed getters.

Values are found by a dotted path: in {"server": {"udp_port": 4242}, "levels": [{"name": "Stage 1"}]}, "server.udp_port" is 4242 and "levels.0.name" is "Stage 1" (a number indexes an array). Comments (//, /* *&zwj;/) are allowed in the file. The document is converted to ConfigValue when loaded, so the JSON library stays an implementation detail of core. Settings the player changes (key bindings, volumes) are written back with set() and save().

Constructor & Destructor Documentation

◆ Config()

aegis2d::core::Config::Config ( )
default

Creates an empty configuration: every lookup fails.

Member Function Documentation

◆ boolean()

std::optional< bool > aegis2d::core::Config::boolean ( std::string_view  path) const

Reads a boolean.

Parameters
pathDotted path of the value.
Returns
The value, or nothing if it is missing or not a boolean.

◆ contains()

bool aegis2d::core::Config::contains ( std::string_view  path) const

Tells whether a value exists at path, whatever its type.

Parameters
pathDotted path such as "window.width".
Returns
true if the path leads to a value.

◆ from_file()

static Config aegis2d::core::Config::from_file ( const std::filesystem::path &  path)
static

Reads and parses a JSON file.

Parameters
pathFile to read.
Returns
The configuration.
Exceptions
ConfigErrorif the file cannot be read, is not valid JSON or its root is not an object; the message starts with the file path.

◆ from_string()

static Config aegis2d::core::Config::from_string ( std::string_view  text)
static

Parses JSON text.

Parameters
textJSON document whose root is an object.
Returns
The configuration.
Exceptions
ConfigErrorif text is not valid JSON or its root is not an object.

◆ integer()

std::optional< std::int64_t > aegis2d::core::Config::integer ( std::string_view  path) const

Reads an integer.

Parameters
pathDotted path of the value.
Returns
The value, or nothing if it is missing, not an integer or out of the std::int64_t range.

◆ number()

std::optional< double > aegis2d::core::Config::number ( std::string_view  path) const

Reads a number, integer or not.

Parameters
pathDotted path of the value.
Returns
The value, or nothing if it is missing or not a number.

◆ save()

void aegis2d::core::Config::save ( const std::filesystem::path &  path) const

Writes the configuration to a file, creating its folder.

The text goes to <path>.tmp first, which then replaces the file, so a crash while saving never leaves a half-written file.

Parameters
pathFile to write.
Exceptions
ConfigErrorif the folder or the file cannot be written; the message starts with the file path.

◆ set()

void aegis2d::core::Config::set ( std::string_view  path,
ConfigValue  value 
)

Sets a value, creating the objects on its path.

A number indexes an existing array element. A value on the path that is neither an object nor an array (a hand-edited "input": 3) is replaced by an object.

Parameters
pathDotted path such as "input.fire".
valueNew value, such as {.data = true} or an array of key names.
Exceptions
std::invalid_argumentif path is empty or has an empty part ("a..b").
ConfigErrorif a part of the path indexes past the end of an array or is not a number while the value there is an array.

◆ string()

std::optional< std::string > aegis2d::core::Config::string ( std::string_view  path) const

Reads a string.

Parameters
pathDotted path of the value.
Returns
The value, or nothing if it is missing or not a string.

◆ to_string()

std::string aegis2d::core::Config::to_string ( ) const

Writes the configuration as JSON.

Returns
The document, indented by 4 spaces, ending with a newline; comments of the text it was read from are not kept.

◆ value()

std::optional< ConfigValue > aegis2d::core::Config::value ( std::string_view  path) const

Reads a value of any type, such as an array.

Parameters
pathDotted path of the value.
Returns
A copy of the value, or nothing if the path is missing.

◆ value_or() [1/2]

std::string aegis2d::core::Config::value_or ( std::string_view  path,
std::string_view  fallback 
) const

Reads a string with a default.

Parameters
pathDotted path of the value.
fallbackValue returned when the path is missing or is not a string.
Returns
The value at path, or fallback.

◆ value_or() [2/2]

template<typename T >
requires std::is_arithmetic_v<T>
T aegis2d::core::Config::value_or ( std::string_view  path,
T  fallback 
) const
inline

Reads a boolean, integer or floating-point value with a default.

Integers that do not fit in T (a port above 65535 read as std::uint16_t) count as missing.

Parameters
pathDotted path of the value.
fallbackValue returned when the path is missing or has another type.
Returns
The value at path, or fallback.

The documentation for this class was generated from the following file: