alpaqa pi-pico
Nonconvex constrained optimization
Loading...
Searching...
No Matches
params.tpp
Go to the documentation of this file.
2
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 <>
35 template <class T, class T_actual, class A>
37 return {{[attr](const any_ptr &t, const ParamString &s) {
38 return set_param(t.template cast<T>()->*attr, s);
39 }}};
40 }
41 std::function<void(const any_ptr &, const ParamString &)> set;
42};
43
44template <class T>
46 enum_accessor(T value, std::string_view = "") : value{value} {}
48};
49
50namespace detail {
51template <class S>
52auto find_param(const attribute_table_t<S> &m, std::string_view key,
53 std::string &error_msg)
54 -> std::optional<typename attribute_table_t<S>::const_iterator> {
55 auto it = m.find(key);
56 if (it == m.end()) {
57 auto keys = std::views::keys(m);
58 std::vector<std::string> sorted_keys{keys.begin(), keys.end()};
60 error_msg = util::join(sorted_keys, {.sep = ", ", .empty = "∅"});
61 return std::nullopt;
62 }
63 return std::make_optional(it);
64}
65} // namespace detail
66
67/// Use @p s to index into the struct type @p T and overwrite the attribute
68/// given by @p s.key.
69template <class T>
70 requires requires { attribute_table<T, ParamString>::table; }
73 auto [key, remainder] = split_key(s.key);
74 std::string error_msg;
75 auto param = detail::find_param(m, key, error_msg);
76 if (!param)
77 throw invalid_param("Invalid key '" + std::string(key) +
78 "' for type '" + demangled_typename(typeid(T)) +
79 "' in '" + std::string(s.full_key) +
80 "',\n possible keys are: " + error_msg);
81 s.key = remainder;
82 (*param)->second.set(&t, s);
83}
84
85/// Set @p t to the value of @p s.value.
86template <class T>
87 requires requires { enum_table<T, ParamString>::table; }
91 auto it = m.find(s.value);
92 if (it == m.end()) {
93 auto vals = std::views::keys(m);
94 std::vector<std::string> sorted_vals{vals.begin(), vals.end()};
96 throw invalid_param(
97 "Invalid value '" + std::string(s.value) + "' for enum '" +
98 demangled_typename(typeid(T)) + "' in '" + std::string(s.full_key) +
99 "',\n possible value are: " +
100 util::join(sorted_vals, {.sep = ", ", .empty = "∅"}));
101 }
102 t = it->second.value;
103}
104
105} // namespace alpaqa::params
Like std::any, but storing just the pointer, without any dynamic allocation.
Definition any-ptr.hpp:10
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
auto find_param(const attribute_table_t< S > &m, std::string_view key, std::string &error_msg) -> std::optional< typename attribute_table_t< S >::const_iterator >
Definition params.tpp:52
std::map< std::string_view, attribute_accessor< S > > attribute_table_t
Dictionary that maps struct attribute names to type-erased functions that set those attributes.
Definition structs.hpp:16
void set_param_default(T &t, const json &j)
Definition json.tpp:114
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:14
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 &, const json &j)
Update/overwrite the first argument based on the JSON object j.
Definition json.cpp:249
std::string_view value
The value of the parameter to store.
Definition params.hpp:22
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:11
Specialize this type to define the attribute name to attribute setters dictionaries for a struct type...
Definition structs.hpp:21
Function wrapper access the enumerators of an enum, type-erasing the type of the enum.
Definition structs.hpp:81
Specialize this type to define the enumerator name to value dictionaries for an enum type T.
Definition structs.hpp:90
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:31
constexpr const auto inf
Definition config.hpp:112
static attribute_accessor make(A T_actual::*attr, std::string_view="")
Definition params.tpp:36
std::function< void(const any_ptr &, const ParamString &)> set
Definition params.tpp:41
enum_accessor(T value, std::string_view="")
Definition params.tpp:46
Custom parameter parsing exception.
Definition params.hpp:26