libconfig v2.1.0
Reflection based C++ configuration library
Loading...
Searching...
No Matches
fourdst::config::Config< T > Class Template Reference

Wrapper class for managing strongly-typed configuration structures. More...

#include <base.h>

Collaboration diagram for fourdst::config::Config< T >:
[legend]

Public Member Functions

 Config ()=default
 Default constructor. Initializes the inner content with default values.
 
const T * operator-> () const
 Access member of the underlying configuration struct.
 
T * write () const
 Get a mutable pointer to the configuration content.
 
T & operator* ()
 Dereference operator to access the underlying configuration struct.
 
const T & operator* () const
 Dereference operator to access the underlying configuration struct.
 
const T & main () const
 Explicit accessor for the main configuration content.
 
void save (std::string_view path) const
 Saves the current configuration to a TOML file.
 
void set_root_name (const std::string_view name)
 Sets the root name/key used in the TOML file.
 
std::string_view get_root_name () const
 Gets the current root name.
 
void set_root_name_load_policy (const RootNameLoadPolicy policy)
 Sets the policy for handling root name mismatches during load.
 
RootNameLoadPolicy get_root_name_load_policy () const
 Gets the current root name load policy.
 
std::string describe_root_name_load_policy () const
 Returns a string description of the current root name load policy.
 
void load (const std::string_view path)
 Loads configuration from a TOML file.
 
ConfigState get_state () const
 Gets the current state of the configuration object.
 
std::string describe_state () const
 Returns a string description of the current configuration state.
 

Static Public Member Functions

static void save_schema (const std::string &path)
 Generates and saves a JSON schema for the configuration structure.
 

Private Attributes

m_content
 
std::string m_root_name = "main"
 
ConfigState m_state = ConfigState::DEFAULT
 
RootNameLoadPolicy m_root_name_load_policy = RootNameLoadPolicy::KEEP_CURRENT
 

Detailed Description

template<IsConfigSchema T>
class fourdst::config::Config< T >

Wrapper class for managing strongly-typed configuration structures.

The Config class wraps a user-defined aggregate struct T and provides methods to save/load it to/from TOML files, as well as generate JSON schemas.

It uses reflect-cpp to automatically inspect the fields of T.

Template Parameters
TThe configuration structure type. Must satisfy IsConfigSchema.
Examples
Defining a config struct and using Config:
struct MySettings {
int threads = 4;
double timeout = 30.5;
};
int main() {
// Access values (default)
std::cout << "Threads: " << cfg->threads << "\n";
// Save default config
cfg.save("settings.toml");
// Load from file
cfg.load("settings.toml");
// Save JSON Schema for editors
cfg.save_schema("schema.json");
return 0;
}
Wrapper class for managing strongly-typed configuration structures.
Definition base.h:113
const T & main() const
Explicit accessor for the main configuration content.
Definition base.h:148
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
Main entry point for the fourdst::config library.

Constructor & Destructor Documentation

◆ Config()

template<IsConfigSchema T>
fourdst::config::Config< T >::Config ( )
default

Default constructor. Initializes the inner content with default values.

Member Function Documentation

◆ describe_root_name_load_policy()

template<IsConfigSchema T>
std::string fourdst::config::Config< T >::describe_root_name_load_policy ( ) const
inlinenodiscard

Returns a string description of the current root name load policy.

Returns
"FROM_FILE", "KEEP_CURRENT", or "UNKNOWN".

◆ describe_state()

template<IsConfigSchema T>
std::string fourdst::config::Config< T >::describe_state ( ) const
inlinenodiscard

Returns a string description of the current configuration state.

Returns
"DEFAULT", "LOADED_FROM_FILE", or "UNKNOWN".

◆ get_root_name()

template<IsConfigSchema T>
std::string_view fourdst::config::Config< T >::get_root_name ( ) const
inlinenodiscard

Gets the current root name.

Returns
The root name string view.

◆ get_root_name_load_policy()

template<IsConfigSchema T>
RootNameLoadPolicy fourdst::config::Config< T >::get_root_name_load_policy ( ) const
inlinenodiscard

Gets the current root name load policy.

Returns
The current policy.

◆ get_state()

template<IsConfigSchema T>
ConfigState fourdst::config::Config< T >::get_state ( ) const
inlinenodiscard

Gets the current state of the configuration object.

Returns
The current state (DEFAULT or LOADED_FROM_FILE).

◆ load()

template<IsConfigSchema T>
void fourdst::config::Config< T >::load ( const std::string_view path)
inline

Loads configuration from a TOML file.

Reads the file, parses it, and updates the internal configuration state.

Parameters
pathThe file path to read from.
Exceptions
exceptions::ConfigLoadErrorIf the config is already loaded, file doesn't exist, or root name mismatch (under KEEP_CURRENT policy).
exceptions::ConfigParseErrorIf the file content is invalid TOML or doesn't match the schema.
Examples
try {
cfg.load("config.toml");
std::cerr << "Error loading config: " << e.what() << std::endl;
}
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()

template<IsConfigSchema T>
const T & fourdst::config::Config< T >::main ( ) const
inline

Explicit accessor for the main configuration content.

Returns
Reference to the constant configuration content.

◆ operator*() [1/2]

template<IsConfigSchema T>
T & fourdst::config::Config< T >::operator* ( )
inline

Dereference operator to access the underlying configuration struct.

Returns
Reference to the mutable configuration content.

◆ operator*() [2/2]

template<IsConfigSchema T>
const T & fourdst::config::Config< T >::operator* ( ) const
inline

Dereference operator to access the underlying configuration struct.

Returns
Reference to the constant configuration content.

◆ operator->()

template<IsConfigSchema T>
const T * fourdst::config::Config< T >::operator-> ( ) const
inline

Access member of the underlying configuration struct.

Returns
Pointer to the constant configuration content.

◆ save()

template<IsConfigSchema T>
void fourdst::config::Config< T >::save ( std::string_view path) const
inline

Saves the current configuration to a TOML file.

Wraps the configuration content under the current root name (default "main") and writes it to the specified path.

Parameters
pathThe file path to write to.
Exceptions
exceptions::ConfigSaveErrorIf the file cannot be opened.
Examples
cfg.save("config.toml");

◆ save_schema()

template<IsConfigSchema T>
static void fourdst::config::Config< T >::save_schema ( const std::string & path)
inlinestatic

Generates and saves a JSON schema for the configuration structure.

Useful for enabling autocompletion and validation in editors (e.g., VS Code).

Parameters
pathThe path to save the schema file to.
Exceptions
exceptions::SchemaSaveErrorIf the file cannot be opened.
Examples
Config<MyConfig>::save_schema("MyConfig.schema.json");

◆ set_root_name()

template<IsConfigSchema T>
void fourdst::config::Config< T >::set_root_name ( const std::string_view name)
inline

Sets the root name/key used in the TOML file.

The default root name is "main". This name appears as the top-level table in the TOML file (e.g., [main]).

Parameters
nameThe new root name.

◆ set_root_name_load_policy()

template<IsConfigSchema T>
void fourdst::config::Config< T >::set_root_name_load_policy ( const RootNameLoadPolicy policy)
inline

Sets the policy for handling root name mismatches during load.

Parameters
policyThe policy (FROM_FILE or KEEP_CURRENT).

◆ write()

template<IsConfigSchema T>
T * fourdst::config::Config< T >::write ( ) const
inline

Get a mutable pointer to the configuration content.

Returns
Pointer to the mutable configuration content.

Member Data Documentation

◆ m_content

template<IsConfigSchema T>
T fourdst::config::Config< T >::m_content
private

◆ m_root_name

template<IsConfigSchema T>
std::string fourdst::config::Config< T >::m_root_name = "main"
private

◆ m_root_name_load_policy

template<IsConfigSchema T>
RootNameLoadPolicy fourdst::config::Config< T >::m_root_name_load_policy = RootNameLoadPolicy::KEEP_CURRENT
private

◆ m_state

template<IsConfigSchema T>
ConfigState fourdst::config::Config< T >::m_state = ConfigState::DEFAULT
private

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