5#include <guanaqo/any-ptr.hpp>
6#include <guanaqo/demangled-typename.hpp>
7#include <guanaqo/string-util.hpp>
17 throw invalid_param(
"Type '" + guanaqo::demangled_typename(
typeid(T)) +
18 "' cannot be indexed in '" +
27 guanaqo::demangled_typename(
typeid(T)) +
"' in '" +
28 std::string(s.full_key) +
"'");
35 template <
class T,
class T_actual,
class A>
37 return {{[attr](
const guanaqo::any_ptr &t,
const ParamString &s) {
38 return set_param(t.template cast<T>()->*attr, s);
53 std::string &error_msg)
54 -> std::optional<typename attribute_table_t<S>::const_iterator> {
55 auto it = m.find(key);
57 auto keys = std::views::keys(m);
58 std::vector<std::string> sorted_keys{keys.begin(), keys.end()};
59 guanaqo::sort_case_insensitive(sorted_keys);
60 error_msg = guanaqo::join(sorted_keys, {.sep =
", ", .empty =
"∅"});
63 return std::make_optional(it);
70 requires requires { attribute_table<T, ParamString>::table; }
74 std::string error_msg;
78 "Invalid key '" + std::string(key) +
"' for type '" +
79 guanaqo::demangled_typename(
typeid(T)) +
"' in '" +
80 std::string(s.
full_key) +
"',\n possible keys are: " + error_msg);
82 (*param)->second.set(&t, s);
87 requires requires { enum_table<T, ParamString>::table; }
91 auto it = m.find(s.
value);
93 auto vals = std::views::keys(m);
94 std::vector<std::string> sorted_vals{vals.begin(), vals.end()};
95 guanaqo::sort_case_insensitive(sorted_vals);
97 "Invalid value '" + std::string(s.
value) +
"' for enum '" +
98 guanaqo::demangled_typename(
typeid(T)) +
"' in '" +
99 std::string(s.
full_key) +
"',\n possible value are: " +
100 guanaqo::join(sorted_vals, {.sep =
", ", .empty =
"∅"}));
102 t = it->second.value;
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 >
void set_param_default(T &t, const json &j)
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.
void unsupported_type(T &, ParamString s)
Throw a meaningful error to indicate that parameters of type T are not supported or implemented.
std::string_view key
The subkey to resolve next.
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 ...
std::string_view full_key
Full key string, used for diagnostics.
auto split_key(std::string_view full, char tok='.')
Split the string full on the first occurrence of tok.
void set_param(T &, const json &j)
Update/overwrite the first argument based on the JSON object j.
std::string_view value
The value of the parameter to store.
Represents a parameter value encoded as a string in the format abc.def.key=value.
Function wrapper to set attributes of a struct, type-erasing the type of the attribute.
Specialize this type to define the attribute name to attribute setters dictionaries for a struct type...
Specialize this type to define the enumerator name to value dictionaries for an enum type T.
EigenConfigd DefaultConfig
static attribute_accessor make(A T_actual::*attr, std::string_view="")
std::function< void(const guanaqo::any_ptr &, const ParamString &)> set
enum_accessor(T value, std::string_view="")
Custom parameter parsing exception.