alpaqa 1.0.0a13
Nonconvex constrained optimization
Loading...
Searching...
No Matches
params.tpp
Go to the documentation of this file.
2
5
6#include <charconv>
7#include <chrono>
8#include <concepts>
9#include <map>
10#include <numeric>
11#include <stdexcept>
12#include <string_view>
13#include <system_error>
14#include <tuple>
15#include <type_traits>
16
17namespace alpaqa::params {
18
20
21/// Throw a meaningful error when `s.key` is not empty, to indicate that
22/// the given type @p T is not of struct type and cannot be indexed into.
23template <class T>
25 if (!s.key.empty())
26 throw invalid_param("Type '" + demangled_typename(typeid(T)) +
27 "' cannot be indexed in '" +
28 std::string(s.full_key) + "'");
29}
30
31/// Throw a meaningful error to indicate that parameters of type @p T are not
32/// supported or implemented.
33template <class T>
35 throw invalid_param("Unknown parameter type '" +
36 demangled_typename(typeid(T)) + "' in '" +
37 std::string(s.full_key) + "'");
38}
39
40template <>
41void set_param(bool &b, ParamString s);
42
43template <>
44void set_param(std::string_view &v, ParamString s);
45
46template <>
47void set_param(std::string &v, ParamString s);
48
49template <class T>
50 requires((std::floating_point<T> || std::integral<T>) && !std::is_enum_v<T>)
51void set_param(T &f, ParamString s);
52
53#ifdef ALPAQA_WITH_QUAD_PRECISION
54template <>
55void set_param(__float128 &f, ParamString s);
56#endif
57
58template <>
59void set_param(vec<config_t> &v, ParamString s);
60
61template <>
62void set_param(vec_from_file<config_t> &v, ParamString s);
63
64template <class Rep, class Period>
65void set_param(std::chrono::duration<Rep, Period> &t, ParamString s);
66
67/// Return a function that applies @ref set_param to the given attribute of a
68/// value of type @p T.
69template <class T, class T_actual, class A>
71 return [attr](T &t, ParamString s) { return set_param(t.*attr, s); };
72}
73
74/// Function wrapper to set attributes of a struct, type-erasing the type of the
75/// attribute.
76template <class T>
78 template <class T_actual, class A>
80 std::function<void(T &, ParamString)> set;
81 void operator()(T &t, ParamString s) const { return set(t, s); }
82};
83
84/// Dictionary that maps struct attribute names to type-erased functions that
85/// set those attributes.
86template <class T>
88 std::map<std::string_view, param_setter_fun_t<T>>;
89
90/// Specialize this type to define the attribute name to attribute setters
91/// dictionaries for a struct type @p T.
92template <class T>
94
95/// Return a string enumerating the possible attribute names for the struct type
96/// @p T.
97template <class T>
100 if (tbl.empty())
101 return std::string{};
102 auto penult = std::prev(tbl.end());
103 auto quote_concat = [](std::string &&a, auto b) {
104 return a + "'" + std::string(b.first) + "', ";
105 };
106 return std::accumulate(tbl.begin(), penult, std::string{}, quote_concat) +
107 "'" + std::string(penult->first) + "'";
108}
109
110/// Use @p s to index into the struct type @p T and overwrite the attribute
111/// given by @p s.key.
112template <class T>
113 requires requires { dict_to_struct_table<T>::table; }
115 const auto &m = dict_to_struct_table<T>::table;
116 auto [key, remainder] = split_key(s.key);
117 auto it = m.find(key);
118 if (it == m.end())
119 throw invalid_param("Invalid key '" + std::string(key) +
120 "' for type '" + demangled_typename(typeid(T)) +
121 "' in '" + std::string(s.full_key) +
122 "',\n possible keys are: " + possible_keys<T>());
123 s.key = remainder;
124 it->second.set(t, s);
125}
126
127/// Helper macro to easily specialize @ref alpaqa::params::dict_to_struct_table.
128#define PARAMS_TABLE(type_, ...) \
129 template <> \
130 struct dict_to_struct_table<type_> { \
131 using type = type_; \
132 inline static const dict_to_struct_table_t<type> table{__VA_ARGS__}; \
133 }
134
135/// Helper macro to easily initialize a
136/// @ref alpaqa::params::dict_to_struct_table_t.
137#define PARAMS_MEMBER(name) \
138 { #name, &type::name }
139
140} // namespace alpaqa::params
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
auto param_setter(A T_actual::*attr)
Return a function that applies set_param to the given attribute of a value of type T.
Definition params.tpp:70
void set_param(bool &b, ParamString s)
Definition params.cpp:26
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:34
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:24
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::map< std::string_view, param_setter_fun_t< T > > dict_to_struct_table_t
Dictionary that maps struct attribute names to type-erased functions that set those attributes.
Definition params.tpp:88
auto possible_keys()
Return a string enumerating the possible attribute names for the struct type T.
Definition params.tpp:98
Represents a parameter value encoded as a string in the format abc.def.key=value.
Definition params.hpp:16
Specialize this type to define the attribute name to attribute setters dictionaries for a struct type...
Definition params.tpp:93
EigenConfigd DefaultConfig
Definition config.hpp:25
constexpr const auto inf
Definition config.hpp:85
typename Conf::vec vec
Definition config.hpp:66
Double-precision double configuration.
Definition config.hpp:135
Custom parameter parsing exception.
Definition params.hpp:26
Function wrapper to set attributes of a struct, type-erasing the type of the attribute.
Definition params.tpp:77
param_setter_fun_t(A T_actual::*attr)
Definition params.tpp:79
std::function< void(T &, ParamString)> set
Definition params.tpp:80
void operator()(T &t, ParamString s) const
Definition params.tpp:81