-
Notifications
You must be signed in to change notification settings - Fork 4
/
serialize_obj.h
177 lines (151 loc) · 3.83 KB
/
serialize_obj.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* CPP Seializer - Fast and simple data serialization for C++.
*
* https://swapped.ch/cpp-serializer
*
* The code is distributed under terms of the BSD license.
* Copyright (c) 2019 Alex Pankratov. All rights reserved.
*/
#ifndef _LIBP_SERIALIZE_OBJ_H_
#define _LIBP_SERIALIZE_OBJ_H_
#include "serialize.h"
/*
* member_ptr
* make_member_ptr()
*/
template <typename C, typename M>
struct member_ptr
{
member_ptr(M C::* _raw) { raw = _raw; }
M C::* raw;
};
//
template <typename C, typename T>
//inline
member_ptr<C, T> make_member_ptr(T C::* ptr)
{
return member_ptr<C,T>(ptr);
}
/*
* object_spec ~ tuple<member_ptr, member_ptr, ...>
* make_object_spec()
*/
template <typename... Ts>
struct object_spec
{
};
template <typename T, typename... Ts>
struct object_spec<T, Ts...> : object_spec<Ts...>
{
object_spec() : object_spec( T() ) { }
object_spec(T t, Ts... ts) : object_spec<Ts...>(ts...), m(t) {}
T m;
};
//
template <typename... CMs>
//inline
auto make_object_spec(CMs... args)
{
return object_spec<CMs...>(args...);
}
/*
* get_object_spec()
* has_object_spec<>
*/
template <typename T>
auto get_object_spec()
{
static_assert( false, "This type has no serialization spec" );
return 0;
}
//
template <typename... Ts>
using __exists = void;
template <typename T, typename = void>
struct has_object_spec { static constexpr bool value = false; };
template <typename T>
struct has_object_spec<T, __exists<decltype( get_object_spec<T> )> > { static constexpr bool value = true; };
/*
* store_member()
* store_object()
* store() - generic for types that have get_object_spec<T>()
*/
template <typename C, typename... CMs>
//inline
void store_member(buffer & buf, const C & obj, object_spec<CMs...> & spec)
{}
template <typename C, typename CM, typename... CMs>
//inline
void store_member(buffer & buf, const C & obj, object_spec<CM, CMs...> & spec)
{
store(buf, obj.*spec.m.raw);
store_member(buf, obj, (object_spec<CMs...> &)spec);
}
template <typename C>
//inline
void store_object(buffer & buf, const C & obj)
{
auto spec = get_object_spec<C>();
store_member(buf, obj, spec);
}
//
template <typename C>
//inline
void store(buffer & b, const C & obj)
{
static_assert( has_object_spec<C>::value, "This type has no serialization spec and no specialized store()" );
store_object(b, obj);
}
/*
* parse_member()
* parse_object()
* parse() - generic for types that have get_object_spec<T>()
*/
template <typename C, typename... CMs>
//inline
bool parse_member(parser & par, C & obj, object_spec<CMs...> & spec)
{
return true;
}
template <typename C, typename CM, typename... CMs>
//inline
bool parse_member(parser & par, C & obj, object_spec<CM, CMs...> & spec)
{
return par.ok &&
parse(par, obj.*spec.m.raw) &&
parse_member(par, obj, (object_spec<CMs...> &)spec);
}
template <typename C>
//inline
bool parse_object(parser & par, C & obj)
{
auto spec = get_object_spec<C>();
return parse_member(par, obj, spec);
}
//
template <typename C>
//inline
bool parse(parser & par, C & obj)
{
static_assert( has_object_spec<C>::value, "This type has no serialization spec and no specialized store()" );
return parse_object(par, obj);
}
/*
* macros
*/
#define OBJECT_SPEC(T) \
\
template <> \
auto ::get_object_spec<T>() \
{ \
typedef T obj_type; \
return make_object_spec \
(
#define __f(f) \
make_member_ptr(&obj_type::f)
#define __e(f) \
make_member_ptr( (uint32_t obj_type::*) &obj_type::f)
#define END_OF_SPEC \
); \
}
#endif