alpaqa 1.0.0a16
Nonconvex constrained optimization
Loading...
Searching...
No Matches
json.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <alpaqa/export.h>
5#include <nlohmann/json_fwd.hpp>
6#include <functional>
7#include <string>
8#include <vector>
9
10namespace alpaqa::params {
11
12using nlohmann::json;
13
14/// Update/overwrite the first argument based on the JSON object @p j.
15template <class T>
16void ALPAQA_EXPORT set_param(T &, const json &j); /* deliberately undefined */
17
18/// Get the first argument as a JSON object @p j.
19template <class T>
20void ALPAQA_EXPORT get_param(const T &, json &j); /* deliberately undefined */
21
22template <class T>
23struct attribute_accessor<T, json> {
24 template <class T_actual, class A>
26 : set([attr](T &t, const json &s) { return set_param(t.*attr, s); }),
27 get([attr](const T &t, json &s) { return get_param(t.*attr, s); }) {}
28 std::function<void(T &, const json &)> set;
29 std::function<void(const T &, json &)> get;
30};
31
32template <class T>
33struct enum_accessor<T, json> {
34 enum_accessor(T value, std::string_view = "") : value{value} {}
36};
37
38/// Custom parameter parsing exception.
39struct ALPAQA_EXPORT invalid_json_param : std::invalid_argument {
40 using std::invalid_argument::invalid_argument;
41 std::vector<std::string> backtrace;
42};
43
44} // namespace alpaqa::params
void get_param(const T &t, json &j)
Get the first argument as a JSON object j.
Definition json.tpp:103
void set_param(T &t, const json &j)
Update/overwrite the first argument based on the JSON object j.
Definition json.tpp:24
Function wrapper to set attributes of a struct, type-erasing the type of the attribute.
Definition structs.hpp:9
Function wrapper access the enumerators of an enum, type-erasing the type of the enum.
Definition structs.hpp:63
constexpr const auto inf
Definition config.hpp:98
std::function< void(T &, const json &)> set
Definition json.hpp:28
std::function< void(const T &, json &)> get
Definition json.hpp:29
attribute_accessor(A T_actual::*attr, std::string_view="")
Definition json.hpp:25
enum_accessor(T value, std::string_view="")
Definition json.hpp:34
Custom parameter parsing exception.
Definition json.hpp:39
std::vector< std::string > backtrace
Definition json.hpp:41