qxLib
Public Types | Public Member Functions | List of all members
qx::layered_configs_manager Class Referencefinal

A singleton that manages layered configuration variables. More...

#include <layered_configs_manager.h>

Inheritance diagram for qx::layered_configs_manager:
singleton< layered_configs_manager >

Public Types

enum class  get_error
 Errors returned when reading a registered variable.
 
enum class  set_error
 Errors returned when writing a variable; failed writes preserve the current value.
 

Public Member Functions

void set_app_description (string sAppDescription) noexcept
 Set app description. It will be shown in help message. More...
 
void set_args (int argc, char *argv[]) noexcept
 Copy command line arguments, converting narrow input with to_string(). More...
 
bool show_help () const noexcept
 Show help message if "--help" or "-h" argument is present in command line arguments. More...
 
bool parse () noexcept
 Parse command line arguments, environment variables, and default values. More...
 
template<class T >
std::expected< T, get_error > get (string_view svRuntimeName) const noexcept
 Get variable value by runtime name. More...
 
template<class T >
std::expected< void, set_error > set (string_view svRuntimeName, T value) noexcept
 Set variable value by runtime name. Only set if the variable type matches. More...
 
std::expected< std::reference_wrapper< const std::any >, get_error > get (string_view svRuntimeName) const noexcept
 Read the current value without knowing its C++ type. More...
 
std::expected< void, set_error > set (string_view svRuntimeName, std::any value) noexcept
 Set a type-erased value using the variable's exact registered C++ type. More...
 
std::expected< string, get_error > get_string (string_view svRuntimeName) const noexcept
 Read a value through its registered string formatter without knowing its C++ type. More...
 
std::expected< void, set_error > set_string (string_view svRuntimeName, string_view svValue) noexcept
 Set a variable from text using its existing registered parser. More...
 
std::vector< layered_config_variable_info > get_variables () const noexcept
 Enumerate registered variables without exposing converters or stored values. More...
 
void reset () noexcept
 Clear all variables and command line arguments. You should call parse() after that to set variables again.
 

Detailed Description

A singleton that manages layered configuration variables.

Doesn't support multithreading

Author
Khrapov
Date
17.05.2026

Definition at line 65 of file layered_configs_manager.h.

Member Function Documentation

◆ get() [1/2]

template<class T >
std::expected< T, layered_configs_manager::get_error > qx::layered_configs_manager::get ( string_view  svRuntimeName) const
inlinenoexcept

Get variable value by runtime name.

Template Parameters
T- type of variable
Parameters
svRuntimeName- runtime name of variable
Returns
- value, or unknown_variable / type_mismatch error

Definition at line 279 of file layered_configs_manager.inl.

◆ get() [2/2]

std::expected< std::reference_wrapper< const std::any >, layered_configs_manager::get_error > qx::layered_configs_manager::get ( string_view  svRuntimeName) const
inlinenoexcept

Read the current value without knowing its C++ type.

The reference is valid until the stored value is removed or the manager is reset. Setting or parsing values may change which value is current.

Parameters
svRuntimeName- registered runtime name, matched exactly
Returns
- reference to the stored value, or unknown_variable error

Definition at line 303 of file layered_configs_manager.inl.

◆ get_string()

std::expected< string, layered_configs_manager::get_error > qx::layered_configs_manager::get_string ( string_view  svRuntimeName) const
inlinenoexcept

Read a value through its registered string formatter without knowing its C++ type.

Parameters
svRuntimeName- registered runtime name, matched exactly
Returns
- owned string for the current value or default, or unknown_variable error

Definition at line 332 of file layered_configs_manager.inl.

◆ get_variables()

std::vector< layered_config_variable_info > qx::layered_configs_manager::get_variables ( ) const
inlinenoexcept

Enumerate registered variables without exposing converters or stored values.

Returns
- metadata sorted by runtime name; views retain the registered names' and descriptions' lifetime

Definition at line 357 of file layered_configs_manager.inl.

◆ parse()

bool qx::layered_configs_manager::parse ( )
inlinenoexcept

Parse command line arguments, environment variables, and default values.

Should be called after set_args() and before get()/set() of any variable. You can call it multiple times during the app lifetime, for example, to load variables from different DLLs.

Returns
- true all required variables are present and have valid values

Definition at line 155 of file layered_configs_manager.inl.

◆ set() [1/2]

std::expected< void, layered_configs_manager::set_error > qx::layered_configs_manager::set ( string_view  svRuntimeName,
std::any  value 
)
inlinenoexcept

Set a type-erased value using the variable's exact registered C++ type.

Parameters
svRuntimeName- registered runtime name
value- owned value; a mismatched type leaves the variable unchanged
Returns
- success, or unknown_variable / type_mismatch error

Definition at line 316 of file layered_configs_manager.inl.

◆ set() [2/2]

template<class T >
std::expected< void, layered_configs_manager::set_error > qx::layered_configs_manager::set ( string_view  svRuntimeName,
T  value 
)
inlinenoexcept

Set variable value by runtime name. Only set if the variable type matches.

Template Parameters
T- type of variable
Parameters
svRuntimeName- runtime name of variable
value- new variable value
Returns
- success, or unknown_variable / type_mismatch error

Definition at line 294 of file layered_configs_manager.inl.

◆ set_app_description()

void qx::layered_configs_manager::set_app_description ( string  sAppDescription)
inlinenoexcept

Set app description. It will be shown in help message.

Parameters
sAppDescription- app description

Definition at line 30 of file layered_configs_manager.inl.

◆ set_args()

void qx::layered_configs_manager::set_args ( int  argc,
char *  argv[] 
)
inlinenoexcept

Copy command line arguments, converting narrow input with to_string().

Should be called once at the beginning of the app before the first parse(). Non-positive argc or null argv clears the command line arguments.

Parameters
argc- number of command line arguments
argv- array of command line arguments

Definition at line 35 of file layered_configs_manager.inl.

◆ set_string()

std::expected< void, layered_configs_manager::set_error > qx::layered_configs_manager::set_string ( string_view  svRuntimeName,
string_view  svValue 
)
inlinenoexcept

Set a variable from text using its existing registered parser.

Every registered variable is writable. Conversion rules match environment and command-line parsing. On success this uses the same runtime storage as set<T>(); no file is written. Failure leaves the previous value unchanged. Call only on the manager's owning thread.

Parameters
svRuntimeName- registered runtime name, matched exactly
svValue- text to convert to the variable's declared type
Returns
- success, or unknown_variable / invalid_value error

Definition at line 342 of file layered_configs_manager.inl.

◆ show_help()

bool qx::layered_configs_manager::show_help ( ) const
inlinenoexcept

Show help message if "--help" or "-h" argument is present in command line arguments.

Should be called after set_args().

Returns
- true if help message was shown. usually the app should exit after that

Definition at line 49 of file layered_configs_manager.inl.


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