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  @retval - 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  **/
48  constexpr void set(T value) noexcept;
49 
50 private:
51  constexpr layered_config_variable() noexcept = default;
52 
53 private:
54  T m_DefaultValue;
55  cstring_view m_svRuntimeName;
56 };
57 
58 /**
59 
60  @class layered_config_variable_builder
61  @brief A builder for creating layered_config_variable instances.
62  @tparam T - variable type
63  @author Khrapov
64  @date 17.05.2026
65 
66 **/
67 template<class T>
69 {
70 public:
71  /**
72  @brief layered_config_variable_builder object constructor
73  @param svRuntimeName - runtime name of variable
74  @param defaultValue - a value to be used if the variable is not set in any layer or has invalid value
75  **/
76  consteval layered_config_variable_builder(cstring_view svRuntimeName, T defaultValue = {}) noexcept;
77 
78  /**
79  @brief Add environment variable layer with specified name
80  @param svEnvName - name of environment variable
81  **/
82  consteval layered_config_variable_builder env(cstring_view svEnvName) noexcept;
83 
84  /**
85  @brief Add command line argument layer with specified full and short names.
86  Full name should start with "--", short name should start with "-" or be empty.
87  @param svFullName - full name of command line argument
88  @param svShortName - short name of command line argument
89  **/
91  cstring_view svFullName,
92  cstring_view svShortName = {}) noexcept;
93 
94  /**
95  @brief Set the group name for the variable.
96  @details Variables with the same group name can be displayed together in help messages.
97  @param svGroupName - group name
98  **/
99  consteval layered_config_variable_builder group(cstring_view svGroupName) noexcept;
100 
101  /**
102  @brief Set the description for the variable.
103  @details This description will be shown in help messages.
104  @param svDescription - description of the variable
105  **/
106  consteval layered_config_variable_builder description(cstring_view svDescription) noexcept;
107 
108  /**
109  @brief Mark variable as required. If a required variable is not set in any layer or has invalid value,
110  layered_configs_manager::parse() will log errors and return false.
111  **/
112  consteval layered_config_variable_builder required() noexcept;
113 
114  /**
115  @brief Finalize building layered_config_variable instance and add it to layered_configs_manager.
116  @retval - built layered_config_variable instance
117  **/
119 
120 private:
122  cstring_view m_svRuntimeName;
123  T m_DefaultValue;
124 };
125 
126 } // namespace qx
127 
A builder for creating layered_config_variable instances.
consteval layered_config_variable_builder env(cstring_view svEnvName) noexcept
Add environment variable layer with specified name.
consteval layered_config_variable_builder group(cstring_view svGroupName) noexcept
Set the group name for the variable.
layered_config_variable< T > build() noexcept
Finalize building layered_config_variable instance and add it to layered_configs_manager.
consteval layered_config_variable_builder description(cstring_view svDescription) noexcept
Set the description 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(cstring_view svRuntimeName, T defaultValue={}) noexcept
layered_config_variable_builder object constructor
consteval layered_config_variable_builder command_line(cstring_view svFullName, cstring_view svShortName={}) noexcept
Add command line argument layer with specified full and short names. Full name should start with "--"...
A configuration variable that can be set from different layers (default value, environment variable,...
constexpr void 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...