qxLib
layered_config_variable.h
Go to the documentation of this file.
1 /**
2 
3  @file layered_config_variable.h
4  @author Khrapov
5  @date 12.05.2026
6  @copyright © Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 #pragma once
10 
13 #include <qx/meta/type_strings.h>
14 
15 namespace qx
16 {
17 
18 /**
19 
20  @class layered_config_variable
21  @brief A configuration variable that can be set from different layers
22  (default value, environment variable, command line argument, runtime get()/set()).
23  @details Supposed to be created using layered_config_variable_builder and stored as a global variable or constant
24  (in case you don't want to change it at runtime).
25  @tparam T - variable type
26  @author Khrapov
27  @date 17.05.2026
28 
29 **/
30 template<class T>
32 {
33  template<class U>
35 
36 public:
37  /**
38  @brief Get variable value.
39  If the variable is not set in any layer or has invalid value, the default value will be returned.
40  @return - variable value
41  **/
42  constexpr T get() const noexcept;
43 
44  /**
45  @brief Set variable value at runtime. It will override value from all other layers.
46  @param value - variable value
47  @return - success, or unknown_variable / type_mismatch error
48  **/
49  constexpr std::expected<void, layered_configs_manager::set_error> set(T value) noexcept;
50 
51 private:
52  constexpr layered_config_variable() noexcept = default;
53 
54 private:
55  T m_DefaultValue;
56  string_view m_svRuntimeName;
57 };
58 
59 /**
60 
61  @class layered_config_variable_builder
62  @brief A builder for creating layered_config_variable instances.
63  @tparam T - variable type
64  @author Khrapov
65  @date 17.05.2026
66 
67 **/
68 template<class T>
70 {
71 public:
72  /**
73  @brief layered_config_variable_builder object constructor
74  @param svRuntimeName - runtime name of variable
75  @param defaultValue - a value to be used if the variable is not set in any layer or has invalid value
76  **/
77  consteval layered_config_variable_builder(string_view svRuntimeName, T defaultValue = {}) noexcept;
78 
79  /**
80  @brief Add environment variable layer with specified name
81  @param svEnvName - name of environment variable
82  **/
83  consteval layered_config_variable_builder env(string_view svEnvName) noexcept;
84 
85  /**
86  @brief Add command line argument layer with specified full and short names.
87  Full name should start with "--", short name should start with "-" or be empty.
88  @param svFullName - full name of command line argument
89  @param svShortName - short name of command line argument
90  **/
91  consteval layered_config_variable_builder command_line(string_view svFullName, string_view svShortName = {});
92 
93  /**
94  @brief Set the group name for the variable.
95  @details Variables with the same group name can be displayed together in help messages.
96  @param svGroupName - group name
97  **/
98  consteval layered_config_variable_builder group(string_view svGroupName) noexcept;
99 
100  /**
101  @brief Set the description for the variable.
102  @details This description will be shown in help messages.
103  @param svDescription - description of the variable
104  **/
105  consteval layered_config_variable_builder description(string_view svDescription) noexcept;
106 
107  /**
108  @brief Mark variable as required. If a required variable is not set in any layer or has invalid value,
109  layered_configs_manager::parse() will log errors and return false.
110  **/
111  consteval layered_config_variable_builder required() noexcept;
112 
113  /**
114  @brief Finalize building layered_config_variable instance and add it to layered_configs_manager.
115  @return - built layered_config_variable instance
116  **/
118 
119 private:
121  string_view m_svRuntimeName;
122  T m_DefaultValue;
123 };
124 
125 } // namespace qx
126 
A builder for creating layered_config_variable instances.
consteval layered_config_variable_builder(string_view svRuntimeName, T defaultValue={}) noexcept
layered_config_variable_builder object constructor
consteval layered_config_variable_builder env(string_view svEnvName) noexcept
Add environment variable layer with specified name.
consteval layered_config_variable_builder command_line(string_view svFullName, string_view svShortName={})
Add command line argument layer with specified full and short names. Full name should start with "--"...
layered_config_variable< T > build() noexcept
Finalize building layered_config_variable instance and add it to layered_configs_manager.
consteval layered_config_variable_builder group(string_view svGroupName) noexcept
Set the group name for the variable.
consteval layered_config_variable_builder required() noexcept
Mark variable as required. If a required variable is not set in any layer or has invalid value,...
consteval layered_config_variable_builder description(string_view svDescription) noexcept
Set the description for the variable.
A configuration variable that can be set from different layers (default value, environment variable,...
constexpr std::expected< void, layered_configs_manager::set_error > set(T value) noexcept
Set variable value at runtime. It will override value from all other layers.
constexpr T get() const noexcept
Get variable value. If the variable is not set in any layer or has invalid value, the default value w...