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, const bool verbose=false)
| 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.
template<typename MutatorFunc>
void | mutate (MutatorFunc &&mutator)
void | reset ()

## Static Public Member Functions

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

## Private Attributes

T | m_content
T | m_content_orig
std::mutex | m_content_mutex
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

  T | The configuration structure type. Must satisfy `IsConfigSchema`.

Examples
  Defining a config struct and using `Config`:
  #include "fourdst/config/config.h"
  struct MySettings {
  int threads = 4;
  double timeout = 30.5;
  };
  int main() {
  fourdst::config::Config<MySettings> cfg;
  // 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;
  }
  fourdst::config::Config
  Wrapper class for managing strongly-typed configuration structures.
  Definition base.h:117
  fourdst::config::Config::main
  const T & main() const
  Explicit accessor for the main configuration content.
  Definition base.h:152
  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
  fourdst::config::Config::save_schema
  static void save_schema(const std::string &path)
  Generates and saves a JSON schema for the configuration structure.
  Definition base.h:328
  config.h
  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,
|  | const bool | verbose = false )

inline

Loads configuration from a TOML file.

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

Parameters

  path | The file path to read from.

Exceptions

  exceptions::ConfigLoadError | If the config is already loaded, file doesn't exist, or root name mismatch (under KEEP_CURRENT policy).
  exceptions::ConfigParseError | If the file content is invalid TOML or doesn't match the schema.

Examples
  try {
  cfg.load("config.toml");
  } catch (const fourdst::config::exceptions::ConfigError& e) {
  std::cerr << "Error loading config: " << e.what() << std::endl;
  }
  fourdst::config::exceptions::ConfigError
  Base exception class for all configuration-related errors.
  Definition exceptions.h:20
  fourdst::config::exceptions::ConfigError::what
  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.

## mutate()

template<IsConfigSchema T>
template<typename MutatorFunc>

void fourdst::config::Config< T >::mutate | ( | MutatorFunc && | mutator | ) |

inline

## 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.

## reset()

template<IsConfigSchema T>

void fourdst::config::Config< T >::reset | ( |  | ) |

inline

## 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

  path | The file path to write to.

Exceptions

  exceptions::ConfigSaveError | If 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

  path | The path to save the schema file to.

Exceptions

  exceptions::SchemaSaveError | If 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

  name | The 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

  policy | The 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_content_mutex

template<IsConfigSchema T>

std::mutex fourdst::config::Config< T >::m_content_mutex

private

## m_content_orig

template<IsConfigSchema T>

T fourdst::config::Config< T >::m_content_orig

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:

- src/config/include/fourdst/config/base.h