Go to the documentation of this file.
9 #ifndef EAGINE_APPLICATION_CONFIG_HPP
10 #define EAGINE_APPLICATION_CONFIG_HPP
22 class application_config_impl;
36 if(
const auto attr{_find_comp_attr(key, tag)}) {
38 if(attr.select_value(flag, application_config_tag())) {
42 if(
const auto arg{_find_prog_arg(key)}) {
48 if(
const auto opt_val{_eval_env_var(key)}) {
49 if(
auto converted{from_string<bool>(
extract(opt_val))}) {
60 if(
const auto arg{_find_prog_arg(key)}) {
65 log_error(
"could not parse configuration value '${value}'")
70 if(
const auto opt_val{_eval_env_var(key)}) {
71 if(
auto converted{from_string<T>(
extract(opt_val))}) {
72 dest = std::move(
extract(converted));
75 log_error(
"could not convert configuration value '${value}'")
80 if(
const auto attr{_find_comp_attr(key, tag)}) {
81 if(attr.select_value(dest, application_config_tag())) {
84 log_error(
"could not fetch configuration value '${key}'")
92 template <
typename T,
typename A>
95 std::vector<T, A>& dest,
97 const auto arg_name{_prog_arg_name(key)};
98 for(
auto arg : _prog_args()) {
99 if(arg.is_tag(arg_name)) {
103 dest.emplace_back(std::move(temp));
105 log_error(
"could not parse configuration value '${value}'")
112 if(
const auto opt_val{_eval_env_var(key)}) {
113 if(
auto converted{from_string<T>(
extract(opt_val))}) {
114 dest.emplace_back(std::move(
extract(converted)));
116 log_error(
"could not convert configuration value '${value}'")
122 if(
const auto attr{_find_comp_attr(key, tag)}) {
123 const auto count = attr.value_count();
125 dest.resize(dest.size() +
std_size(count));
126 if(!attr.select_values(
127 tail(
cover(dest), count), application_config_tag())) {
128 log_error(
"could not fetch configuration values '${key}'")
138 template <
typename T,
typename P>
142 if(
fetch(key, temp, tag)) {
143 if(dest.is_valid(temp)) {
144 dest = std::move(temp);
147 log_error(
"value '${value}' is not valid for '${key}'")
156 template <
typename T>
159 const auto fetched =
fetch(key, temp);
160 return {std::move(temp), fetched};
164 template <
typename T>
166 fetch(key, initial, tag);
171 std::shared_ptr<application_config_impl> _pimpl;
172 auto _impl() noexcept -> application_config_impl*;
175 -> valtree::compound_attribute;
177 auto _prog_args() noexcept -> const program_args&;
178 auto _prog_arg_name(
string_view key) noexcept -> std::
string;
179 auto _find_prog_arg(
string_view key) noexcept -> program_arg;
184 template <typename T>
185 inline auto application_config_initial(
186 application_config& config,
190 config.init(key, initial, tag);
196 template <
typename T,
typename As = std::add_const_t<T>&>
204 T initial = {}) noexcept
205 : _value{application_config_initial(config, key, initial, tag)} {}
211 T initial = {}) noexcept
215 _value = std::move(new_value);
226 operator As() const noexcept {
240 #if !EAGINE_LINK_LIBRARY || defined(EAGINE_IMPLEMENTING_LIBRARY)
241 #include <eagine/application_config.inl>
Helper class used to initialize main context objects.
Definition: main_ctx_object.hpp:45
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
#define EAGINE_ID(NAME)
Macro for constructing instances of eagine::identifier.
Definition: identifier.hpp:353
auto fetch(string_view key, valid_if< T, P > &dest, string_view tag) noexcept -> bool
Fetches the configuration value identified by key, into dest.
Definition: application_config.hpp:139
Base class for main context objects.
Definition: main_ctx_object.hpp:71
Common code is placed in this namespace.
Definition: eagine.hpp:21
Class for reading application configuration.
Definition: application_config.hpp:29
static constexpr auto cover(T *addr, S size) noexcept -> span_if_mutable< T >
Creates a span starting at the specified pointer and specified length.
Definition: span.hpp:465
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
auto fetch(string_view key, std::vector< T, A > &dest, string_view tag={}) noexcept
Fetches the configuration values identified by key, into dest.
Definition: application_config.hpp:93
application_config_value(application_config &config, string_view key, T initial={}) noexcept
Initialization from key and optional initial value.
Definition: application_config.hpp:208
auto operator=(named_logging_object &&) noexcept -> named_logging_object &=default
Move assignment operator.
auto log_debug_stream() noexcept
Returns a log entry stream for debug messages.
Definition: logger.hpp:360
auto value() const noexcept -> As
Returns the stored value converted to the As type.
Definition: application_config.hpp:220
static constexpr auto std_size(T v) noexcept
Converts argument to std size type.
Definition: types.hpp:52
auto get(string_view key, type_identity< T >={}) -> optionally_valid< T >
Returns the configuration value or type T, identified by key.
Definition: application_config.hpp:157
Class wrapping values that can be loaded from application_config.
Definition: application_config.hpp:197
auto fetch(string_view key, T &dest, string_view tag={}) noexcept -> bool
Fetches the configuration value identified by key, into dest.
Definition: application_config.hpp:58
static constexpr auto tail(basic_span< T, P, S > s, L l) noexcept -> basic_span< T, P, S >
Returns the last l elements from the back of a span.
Definition: span_algo.hpp:132
auto is_set(string_view key, string_view tag={}) noexcept -> bool
Checks is the boolean option identified by key is set to true.
Definition: application_config.hpp:35
Template type used mostly for function type-tag dispatching.
Definition: type_identity.hpp:19
auto log_error_stream() noexcept
Returns a log entry stream for error messages.
Definition: logger.hpp:367
auto init(string_view key, T &initial, string_view tag={}) -> T
Fetches the configuration value identified by key, into init.
Definition: application_config.hpp:165
application_config_value(application_config &config, string_view key, string_view tag, T initial={}) noexcept
Initialization from key, tag and optional initial value.
Definition: application_config.hpp:200
valid_if< T, valid_flag_policy > optionally_valid
Specialization of valid_if with flag indicating validity.
Definition: decl.hpp:300
auto log_error(string_view format) noexcept
Create a log message entry for error, with specified format.
Definition: logger.hpp:317