-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemberwiseSerializer.h
66 lines (56 loc) · 1.9 KB
/
MemberwiseSerializer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* File: MemberwiseSerializer.h
* Author: michal
*
* Created on March 3, 2016, 12:10 AM
*/
#ifndef MEMBERWISESERIALIZER_H
#define MEMBERWISESERIALIZER_H
namespace Serialize
{
template <class Format>
struct MemberSerializer
{
MemberSerializer(IOutputStream &stream) : stream(stream) {}
Serialize::FormattedOStream<Format> stream;
template <class T>
void operator()(const T &member)
{
stream << member;
}
};
template <class Format>
struct MemberDeserializer
{
MemberDeserializer(IInputStream &stream) : stream(stream) {}
Serialize::FormattedIStream<Format> stream;
template <class T>
void operator()(T &member)
{
stream >> member;
}
};
template <class Format, class T>
struct SerializerCheckMemberwise<Format, T, true>
{
void serialize(IOutputStream &out, const T &object)
{
MemberSerializer<Format> S(out);
memberwise<serialization_purpose<Format>>(S, object);
}
};
template <class Format, class T>
struct DeserializerCheckMemberwise<Format, T, true>
{
void deserialize(IInputStream &in, T &object)
{
MemberDeserializer<Format> D(in);
memberwise<deserialization_purpose<Format>>(D, object);
}
};
}
#define SERIALIZE_MEMBERS(Format, Type, ...) template <> struct member_list<Type, serialization_purpose<Format> > : decltype(member_ptr_list<>() MEMBER_LIST_ARGS(Type, __VA_ARGS__)) {}
// only use if member list is different than one specified in SERIALIZE_MEMBERS
// or if SERIALIZE_MEMBERS is deliberately not specified
#define DESERIALIZE_MEMBERS(Format, Type, ...) template <> struct member_list<Type, deserialization_purpose<Format> > : decltype(member_ptr_list<>() MEMBER_LIST_ARGS(Type, __VA_ARGS__)) {}
#endif /* MEMBERWISESERIALIZER_H */