alpaqa cmake-targets
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
43/// Use @p s to index into the struct type @p T and overwrite the attribute
44/// given by @p s.key.
45template <class T>
46 requires requires { attribute_table<T, ParamString>::table; }
47void set_param(T &t, ParamString s) {
49 auto [key, remainder] = split_key(s.key);
50 auto it = m.find(key);
51 if (it == m.end()) {
52 auto keys = std::views::keys(m);
53 std::vector<std::string> sorted_keys{keys.begin(), keys.end()};
55 throw invalid_param(
56 "Invalid key '" + std::string(key) + "' for type '" +
57 demangled_typename(typeid(T)) + "' in '" + std::string(s.full_key) +
58 "',\n possible keys are: " +
59 util::join(sorted_keys, {.sep = ", ", .empty = "∅"}));
60 }
61 s.key = remainder;
62 it->second.set(t, s);
63}
64
65} // 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
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
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:25
constexpr const auto inf
Definition config.hpp:85
std::function< void(T &, const ParamString &)> set
Definition params.tpp:40
Custom parameter parsing exception.
Definition params.hpp:26