26#include <boost/array.hpp>
27#include <boost/unordered/unordered_set.hpp>
40template<
typename Value_set>
383template<
typename... S_d_value_set>
412 "Must have even number of template args to Config_manager; use a trailing Null_value_set if needed.");
554 template<
typename... Value_set>
578 template<
typename... Value_set>
580 const fs::path& static_cfg_path,
669 const fs::path& cfg_path,
734 template<
typename... Value_set>
761 template<
typename... Value_set>
763 const fs::path& dynamic_cfg_path,
801 template<
typename... Value_set>
825 template<
typename... Value_set>
840 template<
typename Value_set>
857 template<
typename Value_set>
891 template<
typename... Value_set>
908 template<
typename Value_set>
937 template<
typename Value_set>
1010 S_STATIC_AND_DYNAMIC,
1027 template<
typename Value_set>
1040 template<
typename Value_set>
1051 template<
typename Value_set>
1070 template<
typename Value_set>
1072 bool canonicalize_else_reject,
bool* changed_or_null);
1089 template<
typename... Value_set>
1091 const fs::path& static_cfg_path,
1110 template<
typename... Value_set>
1112 const fs::path& dynamic_cfg_path,
1130 const fs::path& cfg_path,
1154 template<
typename... Value_set>
1157 const fs::path& cfg_path,
const boost::unordered_set<std::string>& all_opt_names_or_empty,
1205 template<
typename Value_set>
1207 const boost::unordered_set<std::string>& all_opt_names_or_empty,
1209 bool* skip_parsing);
1234 template<
typename Value_set>
1256 template<
typename Value_set>
1267 template<
typename Value_set>
1431template<
typename... S_d_value_set>
1447 std::list<On_dynamic_change_func>::const_iterator
m_pos;
1472template<
typename... S_d_value_set>
1477 m_nickname(nickname),
1478 m_dynamic_values_set(false),
1479 m_multi_src_update_in_progress(
Update_type::S_NONE)
1488 &&
"Must have at least 2 template args to Config_manager; otherwise what are you configuring?");
1500 bool dyn_else_st =
false;
1501 size_t value_set_idx = 0;
1502 size_t d_value_set_idx = 0;
1513 std::move(declare_opts_func_moved)}},
1521 && (opt_set<S_d_value_set>(value_set_idx)->null()
1526 =
Void_ptr{
new typename S_d_value_set::Const_ptr},
1531 dyn_else_st = !dyn_else_st
1536 "managing [" <<
S_N_S_VALUE_SETS <<
"] dynamic+static option set pairs (some may be null/unused).");
1539template<
typename... S_d_value_set>
1540template<
typename Value_set>
1546 assert(value_set_idx < S_N_VALUE_SETS);
1548 (m_s_d_opt_sets[value_set_idx].get());
1551template<
typename... S_d_value_set>
1552template<
typename Value_set>
1558 assert(d_value_set_idx < S_N_D_VALUE_SETS);
1559 return static_cast<Value_set*
>
1560 (m_d_baseline_value_sets[d_value_set_idx].get());
1563template<
typename... S_d_value_set>
1564template<
typename Value_set>
1568 return const_cast<Config_manager<S_d_value_set...
>*>(
this)->opt_set<Value_set>(value_set_idx);
1571template<
typename... S_d_value_set>
1572template<
typename Value_set>
1576 if (opt_set->
null())
1580 && (*changed_or_null =
false);
1589template<
typename... S_d_value_set>
1590template<
typename Value_set>
1591typename Config_manager<S_d_value_set...>::On_dynamic_change_func_handle
1595 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Adding dynamic-change listener for dynamic value set "
1596 "[" << d_value_set_idx <<
"].");
1598 assert((d_value_set_idx < S_N_D_VALUE_SETS) &&
"Invalid dynamic option set index.");
1599 assert((!opt_set<Value_set>((d_value_set_idx * 2) + 1)->null())
1600 &&
"Do not register dynamic change listener for Null_value_set or otherwise null value set.");
1603 m_on_dynamic_change_funcs[d_value_set_idx].emplace_back(std::move(on_dynamic_change_func_moved));
1604 const auto& callback_pos = std::prev(m_on_dynamic_change_funcs[d_value_set_idx].end());
1606 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Added dynamic-change listener [@" << &(*callback_pos) <<
1607 "] for dynamic value set [" << d_value_set_idx <<
"].");
1612template<
typename... S_d_value_set>
1617 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Removed dynamic-change listener [@" << &(*handle.
m_pos) <<
1623template<
typename... S_d_value_set>
1624template<
typename... Value_set>
1626 (
const fs::path& static_cfg_path,
1630 return apply_static_impl<Value_set...>(
false, static_cfg_path, final_validator_func..., commit);
1633template<
typename... S_d_value_set>
1634template<
typename... Value_set>
1637 const fs::path& static_cfg_path,
1641 return apply_static_impl<Value_set...>(
true, static_cfg_path, final_validator_func..., commit);
1644template<
typename... S_d_value_set>
1645template<
typename... Value_set>
1647 (
bool allow_invalid_defaults,
1648 const fs::path& static_cfg_path,
1652 using boost::unordered_set;
1655 using std::is_same_v;
1657 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Request to apply static config file [" << static_cfg_path <<
"]. "
1658 "Invalid defaults allowed if explicitly set in file? = [" << allow_invalid_defaults <<
"]. "
1659 "This application (if successful) shall end with canonicalizing values? = [" << commit <<
"]; "
1660 "multi-source update already in progress (0=none) = [" <<
int(m_multi_src_update_in_progress) <<
"].");
1662 reject_candidates_if_update_type_changed(Update_type::S_STATIC);
1667 static_assert(
sizeof...(Value_set) == S_N_S_VALUE_SETS,
1668 "You must supply N/2 `final_validator_func`s, where N is # of S_d_value_set params. "
1669 "Use null_final_validator_func() for any Null_value_set.");
1677 size_t value_set_idx;
1678 bool success =
true;
1682 if ((!allow_invalid_defaults) && (m_multi_src_update_in_progress == Update_type::S_NONE))
1685 [[maybe_unused]]
const auto dummy =
1690 opt_set<Value_set>(value_set_idx)
1691 ->validate_values(&success),
1700 "Request to apply static config file [" << static_cfg_path <<
"]: "
1701 "Will not parse file, because stringent check "
1702 "of defaults detected at least 1 individually-invalid default value. Either correct "
1703 "the default or use apply_static(allow_invalid_defaults_tag_t) overload.");
1708 unordered_set<string> all_opt_names;
1709 unordered_set<string>
const * this_opt_names;
1711 static_assert(is_same_v<
decltype(declval<Option_set<Null_value_set>>().option_names()),
1712 decltype(*this_opt_names)>,
1713 "Option_set<>::option_names() must return ref-to-const. Bug?");
1720 this_opt_names = &(opt_set<Value_set>
1721 (value_set_idx)->option_names()),
1722 all_opt_names.insert(this_opt_names->begin(), this_opt_names->end()),
1728 success = apply_static_or_dynamic_impl<Value_set...>
1730 static_cfg_path, all_opt_names, final_validator_func...);
1733 if ((!commit) && success)
1735 m_multi_src_update_in_progress = Update_type::S_STATIC;
1736 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Multi-source update has successfully begun or continued. "
1737 "Expecting more sources before we can canonicalize candidate(s).");
1753 option_set_canonicalize_or_reject(opt_set<Value_set>(value_set_idx),
1759 if (m_multi_src_update_in_progress != Update_type::S_NONE)
1761 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Multi-source update is now finished.");
1762 m_multi_src_update_in_progress = Update_type::S_NONE;
1771 assert(commit &&
"Maintenance bug? We should have returned above if `!commit` but success==true.");
1773 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: State that was just made canonical follows.");
1779template<
typename... S_d_value_set>
1780template<
typename... Value_set>
1782 (
bool dyn_else_st,
const fs::path& cfg_path,
const boost::unordered_set<std::string>& all_opt_names_or_empty,
1785 size_t s_d_value_set_idx = 0;
1786 size_t value_set_idx = dyn_else_st ? 1 : 0;
1790 bool skip_parsing =
false;
1810 success = apply_impl(opt_set<Value_set>
1813 ((m_multi_src_update_in_progress == Update_type::S_NONE)
1814 && dyn_else_st && (!opt_set<Value_set>(value_set_idx)->null()))
1815 ? d_baseline_value_set<Value_set>
1817 :
static_cast<const Value_set*
>(
nullptr),
1819 cfg_path, all_opt_names_or_empty,
1820 final_validator_func,
1822 ++s_d_value_set_idx,
1829template<
typename... S_d_value_set>
1830template<
typename Value_set>
1833 const Value_set* baseline_value_set_or_null,
1834 const fs::path& cfg_path,
1835 const boost::unordered_set<std::string>& all_opt_names_or_empty,
1840 auto& opts = *opt_set;
1853 if (baseline_value_set_or_null)
1878 const auto pre_parse_candidate = candidate_or_null ? *candidate_or_null : opt_set->
values();
1882 if (skip_parsing && (*skip_parsing))
1885 "Parsing, individual-option validation, and final-validation are being skipped "
1886 "for this value set, as requested by the caller. This file's values will not be applied. "
1887 "This has happened because this value set is successive to a value set whose "
1888 "final-validation determined that it shall not be applied. This is not an error.");
1889 opts.parse_direct_values(pre_parse_candidate);
1903 opts.parse_config_file(cfg_path,
true, &ok, all_opt_names_or_empty);
1911 opts.validate_values_candidate(&ok);
1915 "Request to apply config file [" << cfg_path <<
"] for a particular value set: "
1916 "Individual values present in file were OK, but at least 1 default value (see above) "
1917 "was invalid. Either correct the default or ensure it is properly overridden in file.");
1924 const auto final_validator_outcome = final_validator_func(*opts.values_candidate());
1925 switch (final_validator_outcome)
1934 "Request to apply config file [" << cfg_path <<
"] for a particular value set: "
1935 "Individual values present in file were OK, but the final-validator logic specified "
1936 "that this file's values not be applied (to skip the file after all). "
1937 "Rewinding the candidate value-set to pre-parse state. This is not an error.");
1938 opts.parse_direct_values(pre_parse_candidate);
1941 assert((!*skip_parsing) &&
"We should have `return`ed already.");
1942 *skip_parsing =
true;
1950template<
typename... S_d_value_set>
1952 (
const fs::path& cfg_path,
1956 return apply_static_and_dynamic_impl(
false, cfg_path, final_validator_func..., commit);
1959template<
typename... S_d_value_set>
1962 const fs::path& cfg_path,
1966 return apply_static_and_dynamic_impl(
true, cfg_path, final_validator_func..., commit);
1969template<
typename... S_d_value_set>
1971 (
bool allow_invalid_defaults,
1972 const fs::path& cfg_path,
1977 using boost::unordered_set;
1980 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Request to apply config file [" << cfg_path <<
"] as both "
1981 "static config and *baseline* dynamic config. Parsing in that order for each pair. "
1982 "Invalid defaults allowed if explicitly set in file? = [" << allow_invalid_defaults <<
"]. "
1983 "This application (if successful) shall end with canonicalizing values? = [" << commit <<
"]; "
1984 "multi-source update already in progress (0=none) = [" <<
int(m_multi_src_update_in_progress) <<
"].");
1986 reject_candidates_if_update_type_changed(Update_type::S_STATIC_AND_DYNAMIC);
1994 size_t value_set_idx;
1995 bool success =
true;
1999 if ((!allow_invalid_defaults) && (m_multi_src_update_in_progress == Update_type::S_NONE))
2002 [[maybe_unused]]
const auto dummy =
2007 opt_set<S_d_value_set>(value_set_idx)
2008 ->validate_values(&success),
2016 FLOW_LOG_WARNING(
"Config_manager [" << *
this <<
"]: Request to apply config file [" << cfg_path <<
"] as both "
2017 "static config and *baseline* dynamic config: Will not parse file, because stringent check "
2018 "of defaults detected at least 1 individually-invalid default value. Either correct "
2019 "the default or use apply_static_and_dynamic(allow_invalid_defaults_tag_t) overload.");
2024 unordered_set<string> all_opt_names;
2025 unordered_set<string> this_opt_names;
2031 bool skip_parsing =
false;
2036 this_opt_names = opt_set<S_d_value_set>(value_set_idx)->option_names(),
2037 all_opt_names.insert(this_opt_names.begin(), this_opt_names.end()),
2044 [[maybe_unused]]
const auto dummy =
2049 success = apply_impl(opt_set<S_d_value_set>(value_set_idx),
2051 static_cast<const S_d_value_set*
>(
nullptr),
2052 cfg_path, all_opt_names,
2053 final_validator_func,
2061 if ((!commit) && success)
2063 m_multi_src_update_in_progress = Update_type::S_STATIC_AND_DYNAMIC;
2064 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Multi-source update has successfully begun or continued. "
2065 "Expecting more sources before we can canonicalize candidate(s).");
2080 option_set_canonicalize_or_reject
2081 (opt_set<S_d_value_set>(value_set_idx),
2087 if (m_multi_src_update_in_progress != Update_type::S_NONE)
2089 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Multi-source update is now finished.");
2090 m_multi_src_update_in_progress = Update_type::S_NONE;
2099 assert(commit &&
"Maintenance bug? We should have returned above if `!commit` but success==true.");
2101 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: State that was just made canonical follows.");
2107 bool dyn_else_st =
false;
2109 size_t d_value_set_idx = 0;
2122 && (opt_set<S_d_value_set>(value_set_idx)->null()
2123 || (*(d_baseline_value_set<S_d_value_set>(d_value_set_idx))
2124 = opt_set<S_d_value_set>(value_set_idx)->values(),
2130 dyn_else_st = !dyn_else_st
2136 Lock_guard<
decltype(m_d_value_sets_mutex)> lock{m_d_value_sets_mutex};
2138 dyn_else_st =
false;
2145 && (save_dynamic_value_set_locked<S_d_value_set>
2146 (opt_set<S_d_value_set>(value_set_idx), value_set_idx / 2),
2149 dyn_else_st = !dyn_else_st
2155 assert((!m_dynamic_values_set)
2156 &&
"User apparently attempted apply_static_and_dynamic(), but dynamic config was already initialized OK.");
2157 m_dynamic_values_set =
true;
2160 for (d_value_set_idx = 0; d_value_set_idx != S_N_D_VALUE_SETS; ++d_value_set_idx)
2162 invoke_dynamic_change_listeners(d_value_set_idx,
true);
2168template<
typename... S_d_value_set>
2169template<
typename... Value_set>
2171 (
const fs::path& dynamic_cfg_path,
2175 return apply_dynamic_impl<Value_set...>(
false, dynamic_cfg_path, final_validator_func..., commit);
2178template<
typename... S_d_value_set>
2179template<
typename... Value_set>
2182 const fs::path& dynamic_cfg_path,
2186 return apply_dynamic_impl<Value_set...>(
true, dynamic_cfg_path, final_validator_func..., commit);
2189template<
typename... S_d_value_set>
2190template<
typename... Value_set>
2192 (
bool allow_invalid_defaults,
2193 const fs::path& dynamic_cfg_path,
2202 if (m_dynamic_values_set)
2205 "Request to apply dynamic config file [" << dynamic_cfg_path <<
"]. "
2206 "Initializing dynamic config? = [false]. "
2207 "This application (if successful) shall end with canonicalizing values? = [" << commit <<
"]; "
2208 "multi-source update already in progress "
2209 "(0=none) = [" <<
int(m_multi_src_update_in_progress) <<
"].");
2214 "Request to apply dynamic config file [" << dynamic_cfg_path <<
"]. "
2215 "Initializing dynamic config? = [true]. "
2216 "Invalid defaults allowed if explicitly set in file? = [" << allow_invalid_defaults <<
"]. "
2217 "This application (if successful) shall end with canonicalizing values? = [" << commit <<
"]; "
2218 "multi-source update already in progress "
2219 "(0=none) = [" <<
int(m_multi_src_update_in_progress) <<
"].");
2222 reject_candidates_if_update_type_changed(Update_type::S_DYNAMIC);
2230 size_t value_set_idx;
2231 bool success =
true;
2235 if ((!m_dynamic_values_set) && (!allow_invalid_defaults) && (m_multi_src_update_in_progress == Update_type::S_NONE))
2238 [[maybe_unused]]
const auto dummy =
2243 opt_set<Value_set>(value_set_idx)
2244 ->validate_values(&success),
2253 "Request to initially-apply dynamic config file [" << dynamic_cfg_path <<
"]: "
2254 "Will not parse file, because stringent check "
2255 "of defaults detected at least 1 individually-invalid default value. Either correct "
2256 "the default or use apply_dynamic(allow_invalid_defaults_tag_t) overload.");
2266 && apply_static_or_dynamic_impl<Value_set...>(
true,
2267 dynamic_cfg_path, {}, final_validator_func...);
2269 if ((!commit) && success)
2271 m_multi_src_update_in_progress = Update_type::S_DYNAMIC;
2272 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Multi-source update has successfully begun or continued. "
2273 "Expecting more sources before we can canonicalize candidate(s).");
2282 size_t d_value_set_idx = 0;
2286 array<bool, S_N_D_VALUE_SETS> changed{};
2287 bool some_changed =
false;
2291 value_set_idx = (d_value_set_idx * 2) + 1,
2294 option_set_canonicalize_or_reject(opt_set<Value_set>(value_set_idx),
2297 m_dynamic_values_set ? &changed[d_value_set_idx] :
nullptr),
2299 changed[d_value_set_idx] = (!m_dynamic_values_set) || changed[d_value_set_idx],
2301 some_changed = some_changed || changed[d_value_set_idx],
2306 if (m_multi_src_update_in_progress != Update_type::S_NONE)
2308 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Multi-source update is now finished.");
2309 m_multi_src_update_in_progress = Update_type::S_NONE;
2327 os.os() <<
"Config_manager [" << *
this <<
"]: (Dynamic) state that was just made canonical follows.\n";
2333 state_to_ostream_impl<Value_set>(value_set_idx, os.os()),
2347 Lock_guard<
decltype(m_d_value_sets_mutex)> lock{m_d_value_sets_mutex};
2348 d_value_set_idx = 0;
2352 value_set_idx = (d_value_set_idx * 2) + 1,
2355 (changed[d_value_set_idx]
2356 && (save_dynamic_value_set_locked<Value_set>
2357 (opt_set<Value_set>(value_set_idx),
2365 const bool was_dynamic_values_set = m_dynamic_values_set;
2366 m_dynamic_values_set =
true;
2370 for (d_value_set_idx = 0; d_value_set_idx != S_N_D_VALUE_SETS; ++d_value_set_idx)
2372 if (changed[d_value_set_idx])
2374 invoke_dynamic_change_listeners(d_value_set_idx, !was_dynamic_values_set);
2381template<
typename... S_d_value_set>
2384 if (m_multi_src_update_in_progress == Update_type::S_NONE)
2386 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Request to abandon multi-source update in-progress; but none "
2387 "is in progress. Doing nothing.");
2392 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: Request to abandon multi-source update in-progress. Doing so.");
2395 size_t value_set_idx = 0;
2401 option_set_canonicalize_or_reject
2402 (opt_set<S_d_value_set>(value_set_idx),
2408 m_multi_src_update_in_progress = Update_type::S_NONE;
2411template<
typename... S_d_value_set>
2414 assert(this_update_type != Update_type::S_NONE);
2415 if ((m_multi_src_update_in_progress == Update_type::S_NONE)
2416 || (m_multi_src_update_in_progress == this_update_type))
2422 FLOW_LOG_INFO(
"Config_manager [" << *
this <<
"]: An update of type [" <<
int(this_update_type) <<
"] "
2423 "is interrupting (canceling) an in-progress multi-source update of type "
2424 "[" <<
int(m_multi_src_update_in_progress) <<
"]. Reverting any uncommitted values "
2425 "from the latter. Suggest invoking reject_candidates() explicitly if intentional; this "
2426 "message will then go away, while the code will be more deliberate.");
2428 reject_candidates();
2429 assert(m_multi_src_update_in_progress == Update_type::S_NONE);
2432template<
typename... S_d_value_set>
2433template<
typename Value_set>
2437 assert(opt_set_ptr);
2438 auto& opt_set = *opt_set_ptr;
2443 *(
static_cast<typename Value_set::Const_ptr*
>(m_d_value_sets[d_value_set_idx].get()))
2445 = Value_set::const_ptr_cast(opt_set.mutable_values_copy());
2448template<
typename... S_d_value_set>
2452 "Dynamic update for dynamic option set [" << d_value_set_idx <<
"] (0-based) of "
2453 "[" << S_N_D_VALUE_SETS <<
"] (1-based); trigger was "
2454 "[" << (init ?
"initial-update" :
"change-detected") <<
"].");
2456 const auto& on_dynamic_change_funcs = m_on_dynamic_change_funcs[d_value_set_idx];
2459 for (
const auto& on_dynamic_change_func : on_dynamic_change_funcs)
2461 FLOW_LOG_INFO(
"Invoking listener [" << idx <<
"] (0-based) of [" << on_dynamic_change_funcs.size() <<
"] "
2463 on_dynamic_change_func();
2466 FLOW_LOG_INFO(
"Done invoking change listeners for dynamic option set.");
2469template<
typename... S_d_value_set>
2470template<
typename... Value_set>
2473 static_assert(
sizeof...(Value_set) == S_N_S_VALUE_SETS,
2474 "You must supply N/2 `value_set_or_null`s, where N is # of S_d_value_set params. "
2475 "Use nullptr for any Null_value_set and/or Value_set of no interest.");
2477 size_t s_value_set_idx = 0;
2478 size_t value_set_idx;
2482 value_set_idx = s_value_set_idx * 2,
2485 ((value_set_or_null && (!opt_set<Value_set>
2486 (value_set_idx)->null()))
2487 && (*value_set_or_null = &(opt_set<Value_set>(value_set_idx)->values()),
2494template<
typename... S_d_value_set>
2495template<
typename... Value_set>
2498 static_assert(
sizeof...(Value_set) == S_N_S_VALUE_SETS,
2499 "You must supply N/2 `value_set_or_null`s, where N is # of S_d_value_set params. "
2500 "Use nullptr for any Null_value_set and/or Value_set of no interest.");
2502 size_t s_value_set_idx = 0;
2503 size_t value_set_idx;
2507 value_set_idx = s_value_set_idx * 2,
2509 ((value_set_or_null && (!opt_set<Value_set>
2510 (value_set_idx)->null()))
2516 && (*value_set_or_null = opt_set<Value_set>(value_set_idx)->values_candidate()
2518 ?: &(opt_set<Value_set>(value_set_idx)->values()),
2525template<
typename... S_d_value_set>
2526template<
typename Value_set>
2529 assert((s_value_set_idx < S_N_S_VALUE_SETS) &&
"Invalid static option set index.");
2531 return opt_set<Value_set>(s_value_set_idx * 2)->values();
2534template<
typename... S_d_value_set>
2535template<
typename Value_set>
2538 assert((s_value_set_idx < S_N_S_VALUE_SETS) &&
"Invalid static option set index.");
2540 const auto opt = opt_set<Value_set>(s_value_set_idx * 2);
2546 const Value_set* values = opt->values_candidate();
2549 values = &(opt->values());
2555template<
typename... S_d_value_set>
2556template<
typename... Value_set>
2558 (
typename Value_set::Const_ptr*... value_set_or_null)
const
2562 static_assert(
sizeof...(Value_set) == S_N_D_VALUE_SETS,
2563 "You must supply N/2 `value_set_or_null`s, where N is # of S_d_value_set params. "
2564 "Use nullptr for any Null_value_set and/or Value_set of no interest.");
2567 Lock_guard<
decltype(m_d_value_sets_mutex)> lock{m_d_value_sets_mutex};
2569 size_t d_value_set_idx = 0;
2570 size_t value_set_idx;
2574 value_set_idx = (d_value_set_idx * 2) + 1,
2577 ((value_set_or_null && (!opt_set<Value_set>
2578 (value_set_idx)->null()))
2579 && (*value_set_or_null
2580 = *(
static_cast<typename Value_set::Const_ptr*
>(m_d_value_sets[d_value_set_idx].get())),
2588template<
typename... S_d_value_set>
2589template<
typename Value_set>
2590typename Value_set::Const_ptr
2595 assert((d_value_set_idx < S_N_D_VALUE_SETS) &&
"Invalid dynamic option set index.");
2598 Lock_guard<
decltype(m_d_value_sets_mutex)> lock{m_d_value_sets_mutex};
2599 return *(
static_cast<typename Value_set::Const_ptr*
>(m_d_value_sets[d_value_set_idx].get()));
2603template<
typename... S_d_value_set>
2606 os <<
"Config_manager [" << *
this <<
"]: Current state + config documentation:\n";
2608 size_t value_set_idx = 0;
2611 state_to_ostream_impl<S_d_value_set>(value_set_idx++, os)
2615template<
typename... S_d_value_set>
2616template<
typename Value_set>
2619 const auto& opts = *(opt_set<Value_set>(value_set_idx));
2626 const size_t s_d_value_set_idx = value_set_idx / 2;
2627 const bool dyn_else_st = (value_set_idx % 2) == 1;
2629 os <<
"-- [" << (dyn_else_st ?
"DYNAMIC" :
"STATIC") <<
"] [" << s_d_value_set_idx <<
"] --\n";
2633 const auto output_uncanonicalized = [&]()
2635 if (m_multi_src_update_in_progress != Update_type::S_NONE)
2639 const auto candidate_or_none = opts.values_candidate();
2640 if (candidate_or_none)
2642 os <<
" <incomplete multi-source update type [" << int(m_multi_src_update_in_progress) <<
"] in-progress / "
2643 "current candidate values follow>\n";
2644 opts.values_to_ostream(os, candidate_or_none);
2651 opts.values_to_ostream(os);
2652 output_uncanonicalized();
2657 if (m_dynamic_values_set)
2659 opts.values_to_ostream(os);
2660 output_uncanonicalized();
2670 os <<
"<no dynamic config init payload / option docs follow instead>\n";
2671 opts.help_to_ostream(os);
2673 output_uncanonicalized();
2676template<
typename... S_d_value_set>
2683 state_to_ostream(os.os());
2688template<
typename... S_d_value_set>
2691 os <<
"Config_manager [" << *
this <<
"]: Config help:\n";
2693 size_t value_set_idx = 0;
2696 help_to_ostream_impl<S_d_value_set>(value_set_idx++, os)
2700template<
typename... S_d_value_set>
2701template<
typename Value_set>
2704 const auto opts = opt_set<Value_set>(value_set_idx);
2711 const size_t s_d_value_set_idx = value_set_idx / 2;
2712 const bool dyn_else_st = (value_set_idx % 2) == 1;
2714 os <<
"-- [" << (dyn_else_st ?
"DYNAMIC" :
"STATIC") <<
"] [" << s_d_value_set_idx <<
"] --\n";
2715 opts->help_to_ostream(os);
2718template<
typename... S_d_value_set>
2725 help_to_ostream(os.os());
2730template<
typename Value_set>
2736template<
typename... S_d_value_set>
2739 return os <<
'[' << val.
m_nickname <<
"]@" << &val;
Manages a config setup, intended for a single daemon process, by maintaining 1 or more set(s) of stat...
void log_help(log::Sev sev=log::Sev::S_INFO) const
Logs what help_to_ostream() would print.
boost::array< std::list< On_dynamic_change_func >, S_N_D_VALUE_SETS > m_on_dynamic_change_funcs
List of callbacks to execute after m_d_value_sets members (the pointers) are next assigned: one per e...
void state_to_ostream(std::ostream &os) const
Prints a human-targeted long-form summary of our contents, doubling as a usage message and a dump of ...
void log_state(log::Sev sev=log::Sev::S_INFO) const
Logs what state_to_ostream() would print.
bool apply_static(const fs::path &static_cfg_path, const typename Final_validator_func< Value_set >::Type &... final_validator_func, bool commit=true)
Invoke this after construction to load the permanent set of static config from config sources includi...
bool apply_dynamic(allow_invalid_defaults_tag_t, const fs::path &dynamic_cfg_path, const typename Final_validator_func< Value_set >::Type &... final_validator_func, bool commit=true)
Identical to apply_dynamic() overload without allow_invalid_defaults_tag_t tag; except that – applica...
bool apply_dynamic(const fs::path &dynamic_cfg_path, const typename Final_validator_func< Value_set >::Type &... final_validator_func, bool commit=true)
Load the first or subsequent set of dynamic config from config source including a dynamic config file...
void option_set_canonicalize_or_reject(Option_set< Value_set > *opt_set, bool canonicalize_else_reject, bool *changed_or_null)
Helper that executes opt_set->canonicalize_candidate() or opt_set->reject_candidate().
Option_set< Value_set > * opt_set(size_t value_set_idx)
Helper that obtains the Option_set in the slot m_s_d_opt_sets[value_set_idx], which stores an Option_...
boost::array< Void_ptr, S_N_D_VALUE_SETS > m_d_value_sets
The dynamic config ref-counted handles returned by all_dynamic_values().
boost::array< Void_ptr, S_N_D_VALUE_SETS > m_d_baseline_value_sets
The baseline dynamic value sets, in the same order as S_N_D_VALUE_SETS dynamic S_d_value_set template...
void unregister_dynamic_change_listener(const On_dynamic_change_func_handle &handle)
Remove a previously registered dynamic change callback.
boost::array< Void_ptr, S_N_VALUE_SETS > m_s_d_opt_sets
The static and dynamic value sets, in the same order as the S_N_VALUE_SETS S_d_value_set template arg...
void invoke_dynamic_change_listeners(size_t d_value_set_idx, bool init) const
Invokes the registered listeners for the given dynamic config slot (synchronously).
bool apply_static_and_dynamic(allow_invalid_defaults_tag_t, const fs::path &cfg_path, const typename Final_validator_func< S_d_value_set >::Type &... final_validator_func, bool commit=true)
Identical to apply_static_and_dynamic() overload without allow_invalid_defaults_tag_t tag; but skips ...
void all_static_values_candidates(const Value_set **... value_set_or_null) const
Similar to all_static_values(), but if called from within a validator function passed to apply_static...
bool apply_static_and_dynamic_impl(bool allow_invalid_defaults, const fs::path &cfg_path, const typename Final_validator_func< S_d_value_set >::Type &... final_validator_func, bool commit)
Implements all apply_static_and_dynamic() overloads.
allow_invalid_defaults_tag_t
Tag type: indicates an apply_*() method must allow invalid defaults and only complain if the config s...
@ S_ALLOW_INVALID_DEFAULTS
Sole value for tag type allow_invalid_defaults_tag_t.
const std::string m_nickname
See nickname ctor arg.
void all_dynamic_values(typename Value_set::Const_ptr *... value_set_or_null) const
Obtain ref-counted pointers to each currently-canonical set of dynamic config; each pointed-at struct...
boost::shared_ptr< void > Void_ptr
Short-hand for shared_ptr-to-void type used to store variable-type values in internal containers.
void all_static_values(const Value_set **... value_set_or_null) const
Emit a pointer to each permanently set static config value set; the same pointers are emitted through...
const Value_set & static_values(size_t s_value_set_idx) const
Similar to all_static_values(), but obtains the static config in one specified slot as opposed to all...
void save_dynamic_value_set_locked(Option_set< Value_set > *opt_set, size_t d_value_set_idx)
Little helper that, having assumed m_d_value_sets_mutex is locked, makes a newly allocated copy of th...
bool apply_dynamic_impl(bool allow_invalid_defaults, const fs::path &dynamic_cfg_path, const typename Final_validator_func< Value_set >::Type &... final_validator_func, bool commit)
Implements all apply_dynamic() overloads.
void help_to_ostream_impl(size_t value_set_idx, std::ostream &os) const
Helper of help_to_ostream() for m_s_d_opt_sets slot value_set_idx.
On_dynamic_change_func_handle register_dynamic_change_listener(size_t d_value_set_idx, On_dynamic_change_func &&on_dynamic_change_func_moved)
Saves the given callback; next time apply_dynamic(commit = true) or apply_static_and_dynamic(commit =...
static constexpr size_t S_N_S_VALUE_SETS
The number of static value sets (including any Null_value_sets).
bool apply_static_and_dynamic(const fs::path &cfg_path, const typename Final_validator_func< S_d_value_set >::Type &... final_validator_func, bool commit=true)
If you use dynamic config, and you allow for initial values for dynamic options to be read from the s...
Update_type
Useful at least in the context of multi-source (commit == false) apply_*() methods,...
bool m_dynamic_values_set
Starts false; set to true permanently on successful apply_static_and_dynamic() or apply_dynamic().
bool apply_static_or_dynamic_impl(bool dyn_else_st, const fs::path &cfg_path, const boost::unordered_set< std::string > &all_opt_names_or_empty, const typename Final_validator_func< Value_set >::Type &... final_validator_func)
Work-horse helper that parses either all static value sets or all dynamic value sets from the specifi...
util::Mutex_non_recursive m_d_value_sets_mutex
Mutex protecting m_d_value_sets.
Value_set * d_baseline_value_set(size_t d_value_set_idx)
Helper that obtains the baseline dynamic Value_set in the slot m_d_baseline_value_sets[value_set_idx]...
void reject_candidates_if_update_type_changed(Update_type this_update_type)
Helper for the top of apply_*() that guards against a call to apply_Y() following apply_X(commit == f...
void reject_candidates()
Cancel a not-yet-canonicalized (incomplete) multi-source update, if one is in progress.
Value_set::Const_ptr dynamic_values(size_t d_value_set_idx) const
Similar to all_dynamic_values() but obtains the dynamic config in one specified slot as opposed to al...
static constexpr size_t S_N_D_VALUE_SETS
The number of dynamic value sets (including any Null_value_sets).
bool apply_impl(Option_set< Value_set > *opt_set, const Value_set *baseline_value_set_or_null, const fs::path &cfg_path, const boost::unordered_set< std::string > &all_opt_names_or_empty, const typename Final_validator_func< Value_set >::Type &final_validator_func, bool *skip_parsing)
Work-horse helper that parses into one given Option_set in m_s_d_opt_sets (from opt_set()) from the s...
Update_type m_multi_src_update_in_progress
In short, truthy if and only if a commit == false update is currently in progress,...
bool apply_static_impl(bool allow_invalid_defaults, const fs::path &static_cfg_path, const typename Final_validator_func< Value_set >::Type &... final_validator_func, bool commit)
Implements all apply_static() overloads.
static constexpr size_t S_N_VALUE_SETS
The number of template params in this Config_manager instantiation. It must be even and positive.
Config_manager(log::Logger *logger_ptr, util::String_view nickname, typename Option_set< S_d_value_set >::Declare_options_func &&... declare_opts_func_moved)
Constructs a Config_manager ready to read initial config via apply_*() and other setup methods; and f...
void help_to_ostream(std::ostream &os) const
Prints a human-targeted long-form usage message that includes all options with their descriptions and...
const Value_set & static_values_candidate(size_t s_value_set_idx) const
Similar to static_values(), but if called from within a validator function passed to apply_static() o...
bool apply_static(allow_invalid_defaults_tag_t, const fs::path &static_cfg_path, const typename Final_validator_func< Value_set >::Type &... final_validator_func, bool commit=true)
Identical to apply_static() overload without allow_invalid_defaults_tag_t tag; but skips the stringen...
const Option_set< Value_set > * opt_set(size_t value_set_idx) const
const overload of the other opt_set() helper.
void state_to_ostream_impl(size_t value_set_idx, std::ostream &os) const
Helper of state_to_ostream() for m_s_d_opt_sets slot value_set_idx.
The core config-parsing facility, which builds parsing/comparison/output capabilities on top of a giv...
const Values & values() const
Externally immutable internally stored canonical (current) config values as last constructed or parse...
void reject_candidate()
In PARSING state, returns to CANONICAL state, as if no parse attempts have occurred.
bool null() const
Return true if and only if the option-declaring function passed to the constructor declared no option...
void canonicalize_candidate(bool *change_detected=nullptr)
In PARSING state enters CANONICAL state, finalizing values() from values_candidate().
const Values * values_candidate() const
Returns null in CANONICAL state; or in PARSING state a pointer to the not-yet-canonical values after ...
void parse_direct_values(const Values &src_values)
Enters into (from CANONICAL state) or continues in PARSING state by simply setting *values_candidate(...
Convenience class that simply stores a Logger and/or Component passed into a constructor; and returns...
Logger * get_logger() const
Returns the stored Logger pointer, particularly as many FLOW_LOG_*() macros expect.
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
Convenience class template that endows the given subclass T with nested aliases Ptr and Const_ptr ali...
Similar to ostringstream but allows fast read-only access directly into the std::string being written...
#define FLOW_LOG_INFO(ARG_stream_fragment)
Logs an INFO message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
#define FLOW_LOG_WARNING(ARG_stream_fragment)
Logs a WARNING message into flow::log::Logger *get_logger() with flow::log::Component get_log_compone...
#define FLOW_LOG_WITH_CHECKING(ARG_sev, ARG_stream_fragment)
Logs a message of the specified severity into flow::log::Logger *get_logger() with flow::log::Compone...
#define FLOW_LOG_TRACE(ARG_stream_fragment)
Logs a TRACE message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
Flow module that facilitates configuring modules, such as applications and APIs, via statically and/o...
Final_validator_outcome
Result enumeration for a Final_validator_func::Type function which is used by a Config_manager user w...
@ S_ACCEPT
The holistically-checked cumulative Value_set has no problems and shall be accepted into the candidat...
@ S_SKIP
The holistically-checked cumulative Value_set has contents such that the validator function decided t...
@ S_FAIL
The holistically-checked cumulative Value_set has invalid contents; the candidate shall be rejected,...
Option_set< Null_value_set >::Declare_options_func null_declare_opts_func()
Returns a value usable as declare_opts_func_moved Config_manager ctor arg for a Null_value_set value ...
Final_validator_func< Null_value_set >::Type null_final_validator_func()
Returns a value usable as final_validator_func arg to Config_manager::apply_static() and others – for...
std::ostream & operator<<(std::ostream &os, const Option_set< Value_set > &val)
Serializes (briefly) an Option_set to a standard output stream.
Sev
Enumeration containing one of several message severity levels, ordered from highest to lowest.
@ S_INFO
Message indicates a not-"bad" condition that is not frequent enough to be of severity Sev::S_TRACE.
boost::unique_lock< Mutex > Lock_guard
Short-hand for advanced-capability RAII lock guard for any mutex, ensuring exclusive ownership of tha...
std::string ostream_op_string(T &&... ostream_args)
Equivalent to ostream_op_to_string() but returns a new string by value instead of writing to the call...
boost::mutex Mutex_non_recursive
Short-hand for non-reentrant, exclusive mutex. ("Reentrant" = one can lock an already-locked-in-that-...
Basic_string_view< char > String_view
Commonly used char-based Basic_string_view. See its doc header.
Flow_log_component
The flow::log::Component payload enumeration comprising various log components used by Flow's own int...
Opaque handle for managing a dynamic config change callback.
size_t m_d_value_set_idx
Opaque handle detail.
std::list< On_dynamic_change_func >::const_iterator m_pos
Opaque handle detail.
Utility/traits type to concisely work with final-validation functions when calling methods like Confi...
static Type null()
Returns a final-validator function that simply always returns Final_validator_outcome::S_ACCEPT.
Empty struct suitable as a *_value_set template arg for Config_manager, when a slot requires a Value_...