alpaqa 1.0.0a16
Nonconvex constrained optimization
Loading...
Searching...
No Matches
params.tpp
Go to the documentation of this file.
2
7
8namespace alpaqa::params {
9
11
12/// Throw a meaningful error when `s.key` is not empty, to indicate that
13/// the given type @p T is not of struct type and cannot be indexed into.
14template <class T>
16 if (!s.key.empty())
17 throw invalid_param("Type '" + demangled_typename(typeid(T)) +
18 "' cannot be indexed in '" +
19 std::string(s.full_key) + "'");
20}
21
22/// Throw a meaningful error to indicate that parameters of type @p T are not
23/// supported or implemented.
24template <class T>
26 throw invalid_param("Unknown parameter type '" +
27 demangled_typename(typeid(T)) + "' in '" +
28 std::string(s.full_key) + "'");
29}
30
31/// Function wrapper to set attributes of a struct, type-erasing the type of the
32/// attribute.
33template <class T>
35 template <class T_actual, class A>
37 : set([attr](T &t, const ParamString &s) {
38 return set_param(t.*attr, s);
39 }) {}
40 std::function<void(T &, const ParamString &)> set;
41};
42
43template <class T>
45 enum_accessor(T value, std::string_view = "") : value{value} {}
47};
48
49/// Use @p s to index into the struct type @p T and overwrite the attribute
50/// given by @p s.key.
51template <class T>
52 requires requires { attribute_table<T, ParamString>::table; }
53void set_param(T &t, ParamString s) {
55 auto [key, remainder] = split_key(s.key);
56 auto it = m.find(key);
57 if (it == m.end()) {
58 auto keys = std::views::keys(m);
59 std::vector<std::string> sorted_keys{keys.begin(), keys.end()};
61 throw invalid_param(
62 "Invalid key '" + std::string(key) + "' for type '" +
63 demangled_typename(typeid(T)) + "' in '" + std::string(s.full_key) +
64 "',\n possible keys are: " +
65 util::join(sorted_keys, {.sep = ", ", .empty = "∅"}));
66 }
67 s.key = remainder;
68 it->second.set(t, s);
69}
70
71/// Set @p t to the value of @p s.value.
72template <class T>
73 requires requires { enum_table<T, ParamString>::table; }
74void set_param(T &t, ParamString s) {
77 auto it = m.find(s.value);
78 if (it == m.end()) {
79 auto vals = std::views::keys(m);
80 std::vector<std::string> sorted_vals{vals.begin(), vals.end()};
82 throw invalid_param(
83 "Invalid value '" + std::string(s.value) + "' for enum '" +
84 demangled_typename(typeid(T)) + "' in '" + std::string(s.full_key) +
85 "',\n possible value are: " +
86 util::join(sorted_vals, {.sep = ", ", .empty = "∅"}));
87 }
88 t = it->second.value;
89}
90
91} // namespace alpaqa::params
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
void unsupported_type(T &, ParamString s)
Throw a meaningful error to indicate that parameters of type T are not supported or implemented.
Definition params.tpp:25
std::string_view key
The subkey to resolve next.
Definition params.hpp:20
void assert_key_empty(ParamString s)
Throw a meaningful error when s.key is not empty, to indicate that the given type T is not of struct ...
Definition params.tpp:15
DefaultConfig config_t
Definition json.tpp:11
std::string_view full_key
Full key string, used for diagnostics.
Definition params.hpp:18
auto split_key(std::string_view full, char tok='.')
Split the string full on the first occurrence of tok.
Definition params.hpp:32
std::string_view value
The value of the parameter to store.
Definition params.hpp:22
void set_param(T &t, const json &j)
Update/overwrite the first argument based on the JSON object j.
Definition json.tpp:24
Represents a parameter value encoded as a string in the format abc.def.key=value.
Definition params.hpp:16
Function wrapper to set attributes of a struct, type-erasing the type of the attribute.
Definition structs.hpp:9
Specialize this type to define the attribute name to attribute setters dictionaries for a struct type...
Definition structs.hpp:19
Function wrapper access the enumerators of an enum, type-erasing the type of the enum.
Definition structs.hpp:63
Specialize this type to define the enumerator name to value dictionaries for an enum type T.
Definition structs.hpp:72
std::string join(std::ranges::input_range auto strings, join_opt opt={})
Join the list of strings into a single string, using the separator given by opt.
void sort_case_insensitive(auto &range)
Sort the given range of strings in-place in a case-insensitive manner.
EigenConfigd DefaultConfig
Definition config.hpp:26
constexpr const auto inf
Definition config.hpp:98
attribute_accessor(A T_actual::*attr, std::string_view="")
Definition params.tpp:36
std::function< void(T &, const ParamString &)> set
Definition params.tpp:40
enum_accessor(T value, std::string_view="")
Definition params.tpp:45
Custom parameter parsing exception.
Definition params.hpp:26