qxLib
layered_config_variable.inl
Go to the documentation of this file.
1 /**
2 
3  @file layered_config_variable.inl
4  @author Khrapov
5  @date 12.05.2026
6  @copyright © Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 
10 namespace qx
11 {
12 
13 template<class T>
14 constexpr T layered_config_variable<T>::get() const noexcept
15 {
16  std::optional<T> optValue = layered_configs_manager::get_instance().get<T>(m_svRuntimeName);
17  return optValue ? *optValue : m_DefaultValue;
18 }
19 
20 template<class T>
21 constexpr void layered_config_variable<T>::set(T value) noexcept
22 {
23  layered_configs_manager::get_instance().set(m_svRuntimeName, std::move(value));
24 }
25 
26 template<class T>
28  cstring_view svRuntimeName,
29  T defaultValue) noexcept
30 {
31  m_svRuntimeName = svRuntimeName;
32  m_DefaultValue = std::move(defaultValue);
33  m_Data.pStringToT = [](cstring_view svData) -> std::optional<std::any>
34  {
35  if constexpr (std::is_same_v<T, cstring_view>)
36  {
37  return cstring(svData);
38  }
39  else
40  {
41  // todo get rid of an allocation
42  cstring sData = svData;
43 
44  std::optional<T> optResult = sData.to<T>();
45  if (!optResult)
46  return std::nullopt;
47 
48  return std::move(*optResult);
49  }
50  };
51  m_Data.pTToString = [](const std::any& data) -> cstring
52  {
53  return convert_to_string<T, char>(std::any_cast<T>(data));
54  };
55  m_Data.svTypeName = []() -> cstring_view
56  {
57  if constexpr (std::is_same_v<T, cstring_view>)
58  {
59  return "string";
60  }
61  else
62  {
64  }
65  }();
66 }
67 
68 template<class T>
70 {
71  m_Data.svEnvName = svEnvName;
72  return *this;
73 }
74 
75 template<class T>
77  cstring_view svFullName,
78  cstring_view svShortName) noexcept
79 {
80  m_Data.svFullCommandLineName = svFullName;
81  if (!m_Data.svFullCommandLineName.starts_with("--"))
82  throw "Full command line name must start with --";
83 
84  m_Data.svShortCommandLineName = svShortName;
85  if (!m_Data.svShortCommandLineName.empty() && !m_Data.svShortCommandLineName.starts_with("-"))
86  throw "Short command line name must start with -";
87 
88  return *this;
89 }
90 
91 template<class T>
93  cstring_view svGroupName) noexcept
94 {
95  m_Data.svGroupName = svGroupName;
96  return *this;
97 }
98 
99 template<class T>
101  cstring_view svDescription) noexcept
102 {
103  m_Data.svDescription = svDescription;
104  return *this;
105 }
106 
107 template<class T>
109 {
110  m_Data.bRequired = true;
111  return *this;
112 }
113 
114 template<class T>
116 {
118  layered_config_variable.m_DefaultValue = m_DefaultValue;
119  layered_config_variable.m_svRuntimeName = m_svRuntimeName;
120 
121  layered_configs_manager::get_instance().add_variable(m_svRuntimeName, std::move(m_Data), std::move(m_DefaultValue));
122 
124 }
125 
126 } // namespace qx
std::optional< to_t > to(const_pointer pszFormat=nullptr) const noexcept
Convert string to specified type.
Definition: string.inl:282
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...
static constexpr string_view_type get_signature()
Get type signature (full name with template parameters)