Loading...
Searching...
No Matches
config.h File Reference

Main entry point for the fourdst::config library. More...

`#include "fourdst/config/base.h"`
`#include "fourdst/config/exceptions/exceptions.h"`
`#include "fourdst/config/cli.h"`
Include dependency graph for config.h:

## Detailed Description

Main entry point for the fourdst::config library.

This header includes all necessary components of the configuration library, providing a unified interface for defining, loading, saving, and integrating configuration structures.

# Features

- Type-safe Configuration: Define configs using standard C++ structs.
- Serialization: Built-in support for TOML loading and saving via `reflect-cpp`.
- Schema Generation: Generate JSON schemas for editor autocompletion (VS Code, etc.).
- CLI Integration: Seamlessly expose config fields as command-line arguments (supports CLI11).
- Error Handling: Comprehensive exception hierarchy for parsing and I/O errors.

Examples

1. Basic Definition and I/O

#include "fourdst/config/config.h"
struct Physics {
double gravity = 9.81;
bool enable_drag = true;
};
struct AppConfig {
std::string name = "My Simulation";
int max_steps = 1000;
Physics physics;
};
int main() {
fourdst::config::Config<AppConfig> cfg;
// Access defaults
if (cfg->physics.enable_drag) { ... }
// Save to file
cfg.save("config.toml");
// Load from file
cfg.load("config.toml");
}
fourdst::config::Config
Wrapper class for managing strongly-typed configuration structures.
Definition base.h:117
fourdst::config::Config::load
void load(const std::string_view path, const bool verbose=false)
Loads configuration from a TOML file.
Definition base.h:253
fourdst::config::Config::save
void save(std::string_view path) const
Saves the current configuration to a TOML file.
Definition base.h:168
config.h
Main entry point for the fourdst::config library.

2. CLI Integration (CLI11)

#include "CLI/CLI.hpp"
#include "fourdst/config/config.h"
int main(int argc, char** argv) {
CLI::App app("Simulation App");
fourdst::config::Config<AppConfig> cfg;
// Automatically registers flags like --name, --max_steps, --physics.gravity
fourdst::config::register_as_cli(cfg, app);
CLI11_PARSE(app, argc, argv);
std::cout << "Starting simulation: " << cfg->name << "\n";
}
fourdst::config::register_as_cli
void register_as_cli(T &config, CliApp &app, const std::string &prefix="")
Registers configuration structure fields as CLI options.
Definition cli.h:114

3. Error Handling

try {
cfg.load("missing_file.toml");
} catch (const fourdst::config::exceptions::ConfigLoadError& e) {
std::cerr << "Could not load config: " << e.what() << "\n";
// Falls back to default values
} catch (const fourdst::config::exceptions::ConfigParseError& e) {
std::cerr << "Invalid config file format: " << e.what() << "\n";
return 1;
}
fourdst::config::exceptions::ConfigError::what
const char * what() const noexcept override
Returns the error message.
Definition exceptions.h:32
fourdst::config::exceptions::ConfigLoadError
Thrown when loading the configuration from a file fails.
Definition exceptions.h:54
fourdst::config::exceptions::ConfigParseError
Thrown when parsing the configuration file fails.
Definition exceptions.h:64