-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerdes.h
More file actions
416 lines (343 loc) · 13.8 KB
/
Serdes.h
File metadata and controls
416 lines (343 loc) · 13.8 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#pragma once
#include <algorithm>
#include <vector>
#include <span>
#include <stdexcept>
#include <functional>
class ISerdes
{
public:
ISerdes() = default;
ISerdes(const ISerdes&) = delete;
ISerdes(ISerdes&&) = delete;
bool operator=(const ISerdes&) const = delete;
bool operator=(ISerdes&&) const = delete;
// Begin virtual methods
virtual ~ISerdes() = default;
virtual size_t Offset() = 0;
virtual size_t BytesRemaining() = 0;
virtual void Begin(std::string_view name) = 0;
virtual void Begin(int name) = 0;
virtual void End() = 0;
virtual void NewLine() = 0;
virtual void Comment(std::string_view comment) = 0;
virtual void UInt8(std::string_view name, uint8_t& value) = 0;
virtual void UInt16(std::string_view name, uint16_t& value) = 0;
virtual void UInt32(std::string_view name, uint32_t& value) = 0;
virtual void UInt64(std::string_view name, uint64_t& value) = 0;
virtual void Int8(std::string_view name, int8_t& value) = 0;
virtual void Int16(std::string_view name, int16_t& value) = 0;
virtual void Int32(std::string_view name, int32_t& value) = 0;
virtual void Int64(std::string_view name, int64_t& value) = 0;
virtual void String(std::string_view name, std::string& value) = 0;
virtual void UInt8(int name, uint8_t& value) = 0;
virtual void UInt16(int name, uint16_t& value) = 0;
virtual void UInt32(int name, uint32_t& value) = 0;
virtual void UInt64(int name, uint64_t& value) = 0;
virtual void Int8(int name, int8_t& value) = 0;
virtual void Int16(int name, int16_t& value) = 0;
virtual void Int32(int name, int32_t& value) = 0;
virtual void Int64(int name, int64_t& value) = 0;
virtual void String(int name, std::string& value) = 0;
virtual void Bytes(std::string_view name, std::vector<uint8_t>& buffer) = 0;
// End virtual methods
template<class Tname> // Tname = std::string_view or int
void Bool(Tname name, bool& value)
{
uint8_t val = value ? 1 : 0;
UInt8(name, val);
value = (val != 0);
}
template<class T, class Tname> // Tname = std::string_view or int
void Int8Enum(Tname name, T& value)
{
int8_t val = static_cast<int8_t>(value);
Int8(name, val);
value = static_cast<T>(val);
}
template<class T, class Tname> // Tname = std::string_view or int
void Int16Enum(Tname name, T& value)
{
int16_t val = static_cast<int16_t>(value);
Int16(name, val);
value = static_cast<T>(val);
}
template<class T, class Tname> // Tname = std::string_view or int
void Int32Enum(Tname name, T& value)
{
int32_t val = static_cast<int32_t>(value);
Int32(name, val);
value = static_cast<T>(val);
}
template<class T, class Tname> // Tname = std::string_view or int
void UInt8Enum(Tname name, T& value)
{
uint8_t val = static_cast<uint8_t>(value);
UInt8(name, val);
value = static_cast<T>(val);
}
template<class T, class Tname> // Tname = std::string_view or int
void UInt16Enum(Tname name, T& value)
{
uint16_t val = static_cast<uint16_t>(value);
UInt16(name, val);
value = static_cast<T>(val);
}
template<class T, class Tname> // Tname = std::string_view or int
void UInt32Enum(Tname name, T& value)
{
uint32_t val = static_cast<uint32_t>(value);
UInt32(name, val);
value = static_cast<T>(val);
}
template<typename T, typename Tname> // Tname = std::string_view or int
void Array(Tname name, std::vector<T>& value)
{
Begin(name);
uint32_t count = static_cast<uint32_t>(value.size());
UInt32("count", count);
value.resize(count);
for (int i = 0; i < static_cast<int>(value.size()); ++i)
{
T& item = value[i];
item.Serdes(i, *this);
}
End();
}
template<typename T, typename Tname> // Tname = std::string_view or int
void Array(Tname name, std::vector<T>& value, std::function<void(int, T&, ISerdes&)> serdes)
{
Begin(name);
uint32_t count = static_cast<uint32_t>(value.size());
UInt32("count", count);
value.resize(count);
for (int i = 0; i < static_cast<int>(value.size()); ++i)
{
T& item = value[i];
serdes(i, item, *this);
}
End();
}
};
class SerdesWrite final : public ISerdes
{
std::vector<uint8_t>& m_buffer;
public:
explicit SerdesWrite(std::vector<uint8_t>& buffer): m_buffer(buffer) { }
SerdesWrite(const SerdesWrite&) = delete;
SerdesWrite(SerdesWrite&&) = delete;
SerdesWrite& operator=(const SerdesWrite&) = delete;
SerdesWrite& operator=(SerdesWrite&&) = delete;
~SerdesWrite() override = default;
size_t Offset() override { return m_buffer.size(); }
size_t BytesRemaining() override { return std::numeric_limits<size_t>::max(); }
void Begin([[maybe_unused]] std::string_view name) override {}
void Begin([[maybe_unused]] int name) override {}
void End() override {}
void NewLine() override {}
void Comment([[maybe_unused]] std::string_view comment) override {}
void UInt8([[maybe_unused]] std::string_view name, uint8_t& value) override { UInt8(0, value); }
void UInt8([[maybe_unused]] int name, uint8_t& value) override
{
m_buffer.push_back(value);
}
void UInt16([[maybe_unused]] std::string_view name, uint16_t& value) override { UInt16(0, value); }
void UInt16([[maybe_unused]] int name, uint16_t& value) override
{
m_buffer.push_back(static_cast<uint8_t>(value & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 8 & 0xFF));
}
void UInt32([[maybe_unused]] std::string_view name, uint32_t& value) override { UInt32(0, value); }
void UInt32([[maybe_unused]] int name, uint32_t& value) override
{
m_buffer.push_back(static_cast<uint8_t>(value & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 8 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 16 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 24 & 0xFF));
}
void UInt64([[maybe_unused]] std::string_view name, uint64_t& value) override { UInt64(0, value); }
void UInt64([[maybe_unused]] int name, uint64_t& value) override
{
m_buffer.push_back(static_cast<uint8_t>(value & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 8 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 16 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 24 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 32 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 40 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 48 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 56 & 0xFF));
}
void Int8([[maybe_unused]] std::string_view name, int8_t& value) override { Int8(0, value); }
void Int8([[maybe_unused]] int name, int8_t& value) override
{
m_buffer.push_back(static_cast<uint8_t>(value));
}
void Int16([[maybe_unused]] std::string_view name, int16_t& value) override { Int16(0, value); }
void Int16([[maybe_unused]] int name, int16_t& value) override
{
m_buffer.push_back(static_cast<uint8_t>(value & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 8 & 0xFF));
}
void Int32([[maybe_unused]] std::string_view name, int32_t& value) override { Int32(0, value); }
void Int32([[maybe_unused]] int name, int32_t& value) override
{
m_buffer.push_back(static_cast<uint8_t>(value & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 8 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 16 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 24 & 0xFF));
}
void Int64([[maybe_unused]] std::string_view name, int64_t& value) override { Int64(0, value); }
void Int64([[maybe_unused]] int name, int64_t& value) override
{
m_buffer.push_back(static_cast<uint8_t>(value & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 8 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 16 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 24 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 32 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 40 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 48 & 0xFF));
m_buffer.push_back(static_cast<uint8_t>(value >> 56 & 0xFF));
}
void String([[maybe_unused]] std::string_view name, std::string& value) override { String(0, value); }
void String([[maybe_unused]] int name, std::string& value) override
{
uint32_t len = static_cast<uint32_t>(value.size());
UInt32(name, len);
m_buffer.insert(m_buffer.end(), value.begin(), value.end());
}
void Bytes([[maybe_unused]] std::string_view name, std::vector<uint8_t>& buffer) override
{
uint32_t len = static_cast<uint32_t>(buffer.size());
UInt32(std::string(name) + "Length", len);
m_buffer.insert(m_buffer.end(), buffer.begin(), buffer.end());
}
};
class SerdesRead final : public ISerdes
{
std::span<uint8_t> m_buffer;
std::span<uint8_t> m_current;
public:
explicit SerdesRead(const std::span<uint8_t>& buffer) : m_buffer(buffer), m_current(buffer) {}
SerdesRead(const SerdesRead&) = delete;
SerdesRead(SerdesRead&&) = delete;
SerdesRead& operator=(const SerdesRead&) = delete;
SerdesRead& operator=(SerdesRead&&) = delete;
~SerdesRead() override = default;
size_t Offset() override { return m_buffer.size() - m_current.size(); }
size_t BytesRemaining() override { return m_current.size(); }
// These are only relevant for commenting proxy types.
void Begin([[maybe_unused]] std::string_view name) override {}
void Begin([[maybe_unused]] int name) override {}
void End() override {}
void NewLine() override {}
void Comment([[maybe_unused]] std::string_view comment) override {}
void UInt8([[maybe_unused]] std::string_view name, uint8_t& value) override { UInt8(0, value); }
void UInt8([[maybe_unused]] int name, uint8_t& value) override
{
if (m_current.empty())
throw std::runtime_error("Insufficient data for UInt8 deserialization");
value = m_current[0];
m_current = m_current.subspan(1);
}
void UInt16([[maybe_unused]] std::string_view name, uint16_t& value) override { UInt16(0, value); }
void UInt16([[maybe_unused]] int name, uint16_t& value) override
{
if (m_current.size() < 2)
throw std::runtime_error("Insufficient data for UInt16 deserialization");
value = static_cast<uint16_t>(m_current[0]) | static_cast<uint16_t>(m_current[1]) << 8;
m_current = m_current.subspan(2);
}
void UInt32([[maybe_unused]] std::string_view name, uint32_t& value) override { UInt32(0, value); }
void UInt32([[maybe_unused]] int name, uint32_t& value) override
{
if (m_current.size() < 4)
throw std::runtime_error("Insufficient data for UInt32 deserialization");
value =
static_cast<uint32_t>(m_current[0]) |
static_cast<uint32_t>(m_current[1]) << 8 |
static_cast<uint32_t>(m_current[2]) << 16 |
static_cast<uint32_t>(m_current[3]) << 24;
m_current = m_current.subspan(4);
}
void UInt64([[maybe_unused]] std::string_view name, uint64_t& value) override { UInt64(0, value); }
void UInt64([[maybe_unused]] int name, uint64_t& value) override
{
if (m_current.size() < 8)
throw std::runtime_error("Insufficient data for UInt64 deserialization");
value =
static_cast<uint64_t>(m_current[0]) |
static_cast<uint64_t>(m_current[1]) << 8 |
static_cast<uint64_t>(m_current[2]) << 16 |
static_cast<uint64_t>(m_current[3]) << 24 |
static_cast<uint64_t>(m_current[4]) << 32 |
static_cast<uint64_t>(m_current[5]) << 40 |
static_cast<uint64_t>(m_current[6]) << 48 |
static_cast<uint64_t>(m_current[7]) << 56;
m_current = m_current.subspan(8);
}
void Int8([[maybe_unused]] std::string_view name, int8_t& value) override { Int8(0, value); }
void Int8([[maybe_unused]] int name, int8_t& value) override
{
if (m_current.empty())
throw std::runtime_error("Insufficient data for Int8 deserialization");
value = static_cast<int8_t>(m_current[0]);
m_current = m_current.subspan(1);
}
void Int16([[maybe_unused]] std::string_view name, int16_t& value) override { Int16(0, value); }
void Int16([[maybe_unused]] int name, int16_t& value) override
{
if (m_current.size() < 2)
throw std::runtime_error("Insufficient data for Int16 deserialization");
value = static_cast<int16_t>(m_current[0] | (m_current[1] << 8));
m_current = m_current.subspan(2);
}
void Int32([[maybe_unused]] std::string_view name, int32_t& value) override { Int32(0, value); }
void Int32([[maybe_unused]] int name, int32_t& value) override
{
if (m_current.size() < 4)
throw std::runtime_error("Insufficient data for Int32 deserialization");
value =
static_cast<int32_t>(m_current[0]) |
static_cast<int32_t>(m_current[1]) << 8 |
static_cast<int32_t>(m_current[2]) << 16 |
static_cast<int32_t>(m_current[3]) << 24;
m_current = m_current.subspan(4);
}
void Int64([[maybe_unused]] std::string_view name, int64_t& value) override { Int64(0, value); }
void Int64([[maybe_unused]] int name, int64_t& value) override
{
if (m_current.size() < 8)
throw std::runtime_error("Insufficient data for Int64 deserialization");
value =
static_cast<int64_t>(m_current[0]) |
static_cast<int64_t>(m_current[1]) << 8 |
static_cast<int64_t>(m_current[2]) << 16 |
static_cast<int64_t>(m_current[3]) << 24 |
static_cast<int64_t>(m_current[4]) << 32 |
static_cast<int64_t>(m_current[5]) << 40 |
static_cast<int64_t>(m_current[6]) << 48 |
static_cast<int64_t>(m_current[7]) << 56;
m_current = m_current.subspan(8);
}
void String([[maybe_unused]] std::string_view name, std::string& value) override { String(0, value); }
void String([[maybe_unused]] int name, std::string& value) override
{
if (m_current.size() < 4)
throw std::runtime_error("Insufficient data for String deserialization");
uint32_t len;
UInt32(name, len);
if (len > m_current.size())
throw std::runtime_error("String length exceeds remaining data");
value.assign(reinterpret_cast<const char*>(m_current.data()), len);
m_current = m_current.subspan(len);
}
void Bytes([[maybe_unused]] std::string_view name, std::vector<uint8_t>& buffer) override
{
uint32_t length = 0;
UInt32(0, length);
buffer.resize(length);
auto toCopy = m_current.subspan(0, length);
buffer.assign(toCopy.begin(), toCopy.end());
m_current = m_current.subspan(length);
}
};