3#include <guanaqo/any-ptr.hpp>
4#include <guanaqo/demangled-typename.hpp>
5#include <guanaqo/string-util.hpp>
7#include <nlohmann/json.hpp>
19 auto keys = std::views::keys(members);
20 std::vector<std::string> sorted_keys{keys.begin(), keys.end()};
21 guanaqo::sort_case_insensitive(sorted_keys);
22 return guanaqo::join(sorted_keys, {.sep =
", ", .empty =
"∅"});
29 const std::string &type_name) ->
31 auto it = members.find(key);
33 if (it == members.end()) {
35 auto alias_it = aliases.find(key);
37 if (alias_it == aliases.end()) {
39 "Invalid key '" + key +
"' for type '" + type_name +
44 it = members.find(alias_it->second);
45 if (it == members.end())
46 throw std::logic_error(
"Alias '" + std::string(alias_it->first) +
47 "' refers to nonexistent option '" +
48 std::string(alias_it->second) +
"' in '" +
57 const std::false_type &,
const std::string &type_name) ->
59 auto it = members.find(key);
61 if (it == members.end()) {
63 "Invalid key '" + key +
"' for type '" + type_name +
69template <
class Aliases>
70[[gnu::noinline]]
void set_param_json(
const guanaqo::any_ptr &t,
const json &j,
72 const Aliases &aliases,
73 const std::string &type_name) {
76 "Invalid value " + to_string(j) +
" for type '" + type_name +
77 "' (expected object, but got " + j.type_name() +
')');
79 for (
auto &&el : j.items()) {
80 const auto &key = el.key();
84 it->second.set(t, el.value());
97 template <
class T,
class T_actual,
class A>
100 .set{[attr](
const guanaqo::any_ptr &t,
const json &s) {
101 return set_param(t.template cast<T>()->*attr, s);
103 .get{[attr](
const guanaqo::any_ptr &t, json &s) {
104 return get_param(t.template cast<const T>()->*attr, s);
108 std::function<void(
const guanaqo::any_ptr &,
const json &)>
set;
109 std::function<void(
const guanaqo::any_ptr &, json &)>
get;
120 guanaqo::demangled_typename(
typeid(T)));
123 guanaqo::demangled_typename(
typeid(T)));
127 requires requires { enum_table<T, json>::table; }
131 "Invalid value " + to_string(j) +
" for enum '" +
132 guanaqo::demangled_typename(
typeid(T)) +
133 "' (expected string, but got " + j.type_name() +
')');
136 std::string value = j;
137 auto it = m.find(value);
140 "Invalid value '" + value +
"' for enum '" +
141 guanaqo::demangled_typename(
typeid(T)) +
144 t = it->second.value;
148 requires requires { enum_table<T, json>::table; }
154 requires requires { attribute_table<T, json>::table; }
158 for (
auto &&[k, v] : m)
159 v.get(&t, s[std::string{k}]);
auto find_param_or_throw(const std::string &key, const attribute_table_t< S > &members, const attribute_alias_table_t< S > &aliases, const std::string &type_name) -> typename attribute_table_t< S >::const_iterator
std::string join_sorted_keys(const auto &members)
void set_param_json(const guanaqo::any_ptr &t, const json &j, const attribute_table_t< json > &members, const Aliases &aliases, const std::string &type_name)
std::map< std::string_view, std::string_view > attribute_alias_table_t
Dictionary that maps struct attribute names to type-erased functions that set those attributes.
void get_param(const T &, json &j)
Get the first argument as a JSON object j.
void get_param_default(const T &t, json &j)
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 set_param(T &, const json &j)
Update/overwrite the first argument based on the JSON object j.
Function wrapper to set attributes of a struct, type-erasing the type of the attribute.
Specialize this type to define the alternative attribute name to attribute setters dictionaries for a...
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
constexpr const char * enum_name(LBFGSStepSize s)
static attribute_accessor make(A T_actual::*attr, std::string_view="")
std::function< void(const guanaqo::any_ptr &, json &)> get
std::function< void(const guanaqo::any_ptr &, const json &)> set
Custom parameter parsing exception.
std::vector< std::string > backtrace