-
Notifications
You must be signed in to change notification settings - Fork 4
/
serialize.h
253 lines (209 loc) · 4.86 KB
/
serialize.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* 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_H_
#define _LIBP_SERIALIZE_H_
#include "buffer.h"
#include "parser.h"
#include <stdint.h>
#include <string>
#include <vector>
#include <set>
#include <map>
/*
* simple types
*/
template <class T>
void store_simple(buffer & buf, const T & v)
{
size_t was = buf.size();
buf.resize(was + sizeof(v));
*(T*)&buf[was] = v;
}
template <class T>
bool parse_simple(parser & par, T & v)
{
if (! par.has(sizeof v))
return false;
v = *(const T*)par.ptr;
par.ptr += sizeof(v);
return true;
}
inline void store(buffer & buf, const bool & v) { store_simple(buf, v); }
inline void store(buffer & buf, const uint8_t & v) { store_simple(buf, v); }
inline void store(buffer & buf, const uint16_t & v) { store_simple(buf, v); }
inline void store(buffer & buf, const uint32_t & v) { store_simple(buf, v); }
inline void store(buffer & buf, const uint64_t & v) { store_simple(buf, v); }
inline void store(buffer & buf, const float & v) { store_simple(buf, v); }
inline void store(buffer & buf, const double & v) { store_simple(buf, v); }
inline bool parse(parser & par, bool & v) { return parse_simple(par, v); }
inline bool parse(parser & par, uint8_t & v) { return parse_simple(par, v); }
inline bool parse(parser & par, uint16_t & v) { return parse_simple(par, v); }
inline bool parse(parser & par, uint32_t & v) { return parse_simple(par, v); }
inline bool parse(parser & par, uint64_t & v) { return parse_simple(par, v); }
inline bool parse(parser & par, float & v) { return parse_simple(par, v); }
inline bool parse(parser & par, double & v) { return parse_simple(par, v); }
/*
* arrays
*/
template <class T, size_t N>
//inline
void store(buffer & buf, const T (&v)[N])
{
buf.append((uint8_t*)v, N * sizeof(T));
}
template <class T, size_t N>
//inline
bool parse(parser & par, T (&v)[N])
{
size_t bytes = N * sizeof(T);
if (! par.has(bytes))
return false;
memmove(v, par.ptr, bytes);
par.ptr += bytes;
return true;
}
/*
* var-sized int - size_t
*/
void store_size(buffer & buf, size_t n);
bool parse_size(parser & par, size_t & n);
/*
* std::basic_string - buffer, string, wstring
*/
template <typename E, typename T, typename A>
//inline
void store(buffer & buf, const std::basic_string<E,T,A> & v)
{
size_t bytes = v.size() * sizeof(E);
store_size(buf, bytes);
if (bytes)
buf.append( (uint8_t*)v.data(), bytes );
}
template <typename E, typename T, typename A>
//inline
bool parse(parser & par, std::basic_string<E,T,A> & v)
{
size_t bytes;
if (! parse_size(par, bytes))
return false;
if (bytes % sizeof(E))
return par.stop();
if (! par.has(bytes))
return false;
v.assign( (E*)par.ptr, bytes/sizeof(E) );
par.ptr += bytes;
return true;
}
/*
* std::pair
*/
template <typename T1, typename T2>
inline
void store(buffer & buf, const std::pair<T1,T2> & v)
{
store(buf, v.first);
store(buf, v.second);
}
template <typename T1, typename T2>
bool parse(parser & par, std::pair<T1,T2> & v)
{
return par.ok &&
parse(par, v.first) &&
parse(par, v.second);
}
/*
* containers
*/
template <typename C>
void store_container(buffer & buf, const C & v)
{
store_size(buf, v.size());
for (auto & x : v) store(buf, x);
}
/*
* std::vector
*/
template <typename T, typename A>
//inline
void store(buffer & buf, const std::vector<T,A> & v)
{
return store_container(buf, v);
}
template <typename T, typename A>
//inline
bool parse(parser & par, std::vector<T,A> & v)
{
size_t count;
T dummy;
if (! parse_size(par, count))
return false;
v.clear();
v.reserve(count);
while (count--)
{
v.push_back(dummy);
if (! parse(par, v.back()))
return false;
}
return true;
}
/*
* std::set
*/
template <typename K, typename C, typename A>
//inline
void store(buffer & buf, const std::set<K,C,A> & v)
{
return store_container(buf, v);
}
template <typename K, typename C, typename A>
bool parse(parser & par, std::set<K,C,A> & v)
{
size_t count;
K tmp;
if (! parse_size(par, count))
return false;
v.clear();
while (count--)
{
if (! parse(par, tmp))
return false;
if (! v.insert(tmp).second)
return par.stop();
}
return true;
}
/*
* std::map
*/
template <typename K, typename D, typename C, typename A>
//inline
void store(buffer & buf, const std::map<K,D,C,A> & v)
{
return store_container(buf, v);
}
template <typename K, typename D, typename C, typename A>
//inline
bool parse(parser & par, std::map<K,D,C,A> & v)
{
size_t count;
std::pair<K,D> tmp;
if (! parse_size(par, count))
return false;
v.clear();
while (count--)
{
if (! parse(par, tmp))
return false;
if (! v.insert(tmp).second)
return par.stop();
}
return true;
}
#endif