libconfig
libconfig is the unified configuration module for SERiF and related projects
This has been broken out of the main serif project to allow for more modularity
Building
In order to build libconfig you need meson>=1.5.0. This can be installed with pip
pip install "meson>=1.5.0"
Then from the root libconfig directory it is as simple as
meson setup build --buildtype=release
meson compile -C build
meson test -C build
this will auto generate a pkg-config file for you so that linking other libraries to libconfig is easy.
Usage
libconfig makes use of reflect-cpp to provide compile time reflection and serialization/deserialization of configuration structs. This allows for config options to be defined in code and strongly typed.
Basic Usage
#include <string>
#include <print>
struct MyPhysicsOptions {
int gravity = 10;
float friction = 0.5f;
bool enable_wind = false;
};
struct MyControlOptions {
double time_step = 0.01;
double max_time = 100.0;
};
struct MySimulationConfig {
std::string name = "my_simulation";
MyPhysicsOptions physics;
MyControlOptions control;
};
int main() {
cfg.
save(
"default_config.toml");
try {
cfg.
load(
"my_config.toml");
std::println(
"Error loading config: {}", e.
what());
}
std::println("My Simulation Name: {}, My Simulation Gravity: {}", cfg->name, cfg->physics.gravity);
}
Wrapper class for managing strongly-typed configuration structures.
Definition base.h:113
void save(std::string_view path) const
Saves the current configuration to a TOML file.
Definition base.h:164
void load(const std::string_view path)
Loads configuration from a TOML file.
Definition base.h:249
static void save_schema(const std::string &path)
Generates and saves a JSON schema for the configuration structure.
Definition base.h:300
Base exception class for all configuration-related errors.
Definition exceptions.h:20
const char * what() const noexcept override
Returns the error message.
Definition exceptions.h:32
Main entry point for the fourdst::config library.
CLI Integration
libconfig integrates with CLI11 to automatically expose configuration fields as command-line arguments.
#include "CLI/CLI.hpp"
int main(int argc, char** argv) {
CLI::App app("My Application");
CLI11_PARSE(app, argc, argv);
return 0;
}
void register_as_cli(T &config, CliApp &app, const std::string &prefix="")
Registers configuration structure fields as CLI options.
Definition cli.h:114
Example output TOML
[main]
name = "my_simulation"
[main.physics]
gravity = 10
friction = 0.5
enable_wind = false
[main.control]
time_step = 0.01
max_time = 100.0