Deduce class-type based on first member mapping

This commit is contained in:
G.H.O.S.T 2024-12-27 01:41:24 +01:00
parent 4cbc64a7a4
commit 02ec014f35
Signed by: G.H.O.S.T
GPG Key ID: 3BD93EABD1407B82
3 changed files with 21 additions and 6 deletions

View File

@ -140,24 +140,42 @@ namespace code::json
}; };
template<typename T, typename... Members> template<typename First, typename... Members>
struct mapping_t struct mapping_t
{ {
using T = First::T;
static
void
do_marshal(variant& v, T const& instance, marshaling_context_t* context)
{
First::marshal(v, instance, context);
((Members::marshal(v, instance, context)), ...);
}
static static
variant variant
marshal(T const& instance, marshaling_context_t* context) marshal(T const& instance, marshaling_context_t* context)
{ {
variant v{std::map<std::string, variant>{}}; variant v{std::map<std::string, variant>{}};
((Members::marshal(v, instance, context)), ...); do_marshal(v, instance, context);
return v; return v;
} }
static
void
do_unmarshal(T& instance, variant const& v, marshaling_context_t* context)
{
First::unmarshal(instance, v, context);
((Members::unmarshal(instance, v, context)), ...);
}
static static
T T
unmarshal(variant const& v, marshaling_context_t* context) unmarshal(variant const& v, marshaling_context_t* context)
{ {
T instance; T instance;
((Members::unmarshal(instance, v, context)), ...); do_unmarshal(instance, v, context);
return instance; return instance;
} }

View File

@ -26,7 +26,6 @@ struct name_t
std::string last; std::string last;
using json = mapping_t< using json = mapping_t<
name_t,
member_t<"/person/first", &name_t::first>::pointer_t, member_t<"/person/first", &name_t::first>::pointer_t,
member_t<"/person/last", &name_t::last>::pointer_t member_t<"/person/last", &name_t::last>::pointer_t
>; >;

View File

@ -18,7 +18,6 @@ struct person_name {
std::string last; std::string last;
using json = mapping_t< using json = mapping_t<
person_name,
member_t<"first", &person_name::first>, member_t<"first", &person_name::first>,
member_t<"last", &person_name::last> member_t<"last", &person_name::last>
>; >;
@ -30,7 +29,6 @@ struct person {
int age; int age;
using json = mapping_t< using json = mapping_t<
person,
member_t<"name", &person::name>, member_t<"name", &person::name>,
member_t<"age", &person::age> member_t<"age", &person::age>
>; >;