1 #include <mxcomp/reflection.h>
5 using namespace mxcomp;
9 static auto set(Json::Value& jv, T& t,
const mxcomp::Field_<T, S>& field) -> decltype( jv[field.name] << field.get(t), void()) {
10 if(!jv.isObject())
throw std::runtime_error(
"Expected an object in " + field.name);
11 jv[field.name] << field.get(t);
15 static auto set(Json::Value& jv, T& t,
const Field_<T, S>& field) -> decltype(jv[field.name] = field.get(t), void()) {
16 if(!jv.isObject())
throw std::runtime_error(
"Expected an object in " + field.name);
17 jv[field.name] = field.get(t);
21 static void get(Json::Value& jv, T& t,
const Field_<T, S>& field){
22 if(!jv.isObject())
throw std::runtime_error(
"Expected an object in " + field.name);
23 jv[field.name] >> (&t)->*(field.field);
25 static void get(Json::Value& jv, T& t,
const Field_<T, bool>& field){
26 if(!jv.isObject())
throw std::runtime_error(
"Expected an object in " + field.name);
27 field.set(t, jv[field.name].asBool());
29 static void get(Json::Value& jv, T& t,
const Field_<T, double>& field){
30 if(!jv.isObject())
throw std::runtime_error(
"Expected an object in " + field.name);
31 field.set(t, jv[field.name].asDouble());
33 static void get(Json::Value& jv, T& t,
const Field_<T, unsigned int>& field){
34 if(!jv.isObject())
throw std::runtime_error(
"Expected an object in " + field.name);
35 field.set(t, jv[field.name].asUInt());
37 static void get(Json::Value& jv, T& t,
const Field_<T, int>& field){
38 if(!jv.isObject())
throw std::runtime_error(
"Expected an object in " + field.name);
39 field.set(t, jv[field.name].asInt());
41 static void get(Json::Value& jv, T& t,
const Field_<T, std::string>& field){
42 if(!jv.isObject())
throw std::runtime_error(
"Expected an object in " + field.name);
43 field.set(t, jv[field.name].asString());
49 Json::Value& jv; T& t;
53 inline void operator()(
const Field_<T, S>& field) {
56 }
catch(std::exception& ex){
57 throw std::runtime_error(
"Error in setting field '" + std::string(field.name) +
"': " + ex.what());
65 Json::Value& jv; T& t;
69 inline void operator()(
const Field_<T, S>& field) {
72 }
catch(std::exception& ex){
73 throw std::runtime_error(
"Error in getting field '" + std::string(field.name) +
"': " + ex.what());
79 static auto operator>>(Json::Value& jv, T& t) -> decltype(MetaClass_<T>::fields(), jv) {
85 static auto operator<<(Json::Value& jv, T& t) -> decltype(MetaClass_<T>::fields(), jv) {
86 mxcomp::tuples::iterate(SetJsonValues<T>(jv, t), MetaClass_<T>::fields());