libconfig v2.1.0
Reflection based C++ configuration library
Loading...
Searching...
No Matches
config.h File Reference

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

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

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() {
// Access defaults
if (cfg->physics.enable_drag) { ... }
// Save to file
cfg.save("config.toml");
// Load from file
cfg.load("config.toml");
}
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
Main entry point for the fourdst::config library.

2. CLI Integration (CLI11)

#include "CLI/CLI.hpp"
int main(int argc, char** argv) {
CLI::App app("Simulation App");
// Automatically registers flags like --name, --max_steps, --physics.gravity
CLI11_PARSE(app, argc, argv);
std::cout << "Starting simulation: " << cfg->name << "\n";
}
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");
std::cerr << "Could not load config: " << e.what() << "\n";
// Falls back to default values
std::cerr << "Invalid config file format: " << e.what() << "\n";
return 1;
}
const char * what() const noexcept override
Returns the error message.
Definition exceptions.h:32
Thrown when loading the configuration from a file fails.
Definition exceptions.h:54
Thrown when parsing the configuration file fails.
Definition exceptions.h:64