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::expected<T, layered_configs_manager::get_error> value =
17  layered_configs_manager::get_instance().get<T>(m_svRuntimeName);
18  return value ? *value : m_DefaultValue;
19 }
20 
21 template<class T>
22 constexpr std::expected<void, layered_configs_manager::set_error> layered_config_variable<T>::set(T value) noexcept
23 {
24  return layered_configs_manager::get_instance().set(m_svRuntimeName, std::move(value));
25 }
26 
27 template<class T>
29  string_view svRuntimeName,
30  T defaultValue) noexcept
31  : m_svRuntimeName(svRuntimeName)
32  , m_DefaultValue(std::move(defaultValue))
33 {
34  m_Data.pStringToT = [](string_view svData) -> std::optional<std::any>
35  {
36  if constexpr (std::is_same_v<T, string_view>)
37  {
38  return string(svData);
39  }
40  else
41  {
42  // todo get rid of an allocation
43  string sData = svData;
44 
45  std::optional<T> optResult = sData.to<T>();
46  if (!optResult)
47  return std::nullopt;
48 
49  return std::move(*optResult);
50  }
51  };
52  m_Data.pTToString = [](const std::any& data) -> string
53  {
54  return convert_to_string<typename details::layered_config_variable_type<T>::type, char_type>(
55  std::any_cast<const typename details::layered_config_variable_type<T>::type&>(data));
56  };
57  m_Data.svTypeName = []() -> string_view
58  {
59  if constexpr (std::is_same_v<T, string_view>)
60  {
61  return QXT("string");
62  }
63  else
64  {
66  }
67  }();
68 }
69 
70 template<class T>
72 {
73  m_Data.svEnvName = svEnvName;
74  return *this;
75 }
76 
77 template<class T>
79  string_view svFullName,
80  string_view svShortName)
81 {
82  m_Data.svFullCommandLineName = svFullName;
83  if (!m_Data.svFullCommandLineName.starts_with(QXT("--")))
84  throw QXT("Full command line name must start with --");
85 
86  m_Data.svShortCommandLineName = svShortName;
87  if (!m_Data.svShortCommandLineName.empty() && !m_Data.svShortCommandLineName.starts_with(QXT("-")))
88  throw QXT("Short command line name must start with -");
89 
90  return *this;
91 }
92 
93 template<class T>
95 {
96  m_Data.svGroupName = svGroupName;
97  return *this;
98 }
99 
100 template<class T>
102  string_view svDescription) noexcept
103 {
104  m_Data.svDescription = svDescription;
105  return *this;
106 }
107 
108 template<class T>
110 {
111  m_Data.bRequired = true;
112  return *this;
113 }
114 
115 template<class T>
117 {
118  layered_config_variable<T> layeredConfigVariable;
119  layeredConfigVariable.m_DefaultValue = m_DefaultValue;
120  layeredConfigVariable.m_svRuntimeName = m_svRuntimeName;
121 
122  layered_configs_manager::get_instance().add_variable(m_svRuntimeName, std::move(m_Data), std::move(m_DefaultValue));
123 
124  return layeredConfigVariable;
125 }
126 
127 } // namespace qx
std::optional< to_t > to(const_pointer pszFormat=nullptr) const noexcept
Convert string to specified type.
Definition: string.inl:293
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...
static constexpr string_view_type get_signature()
Get type signature (full name with template parameters)