dataclass-native 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dataclass_native/__init__.py +60 -0
- dataclass_native/__main__.py +5 -0
- dataclass_native/_arrow.py +822 -0
- dataclass_native/_json.py +783 -0
- dataclass_native/_mode.py +57 -0
- dataclass_native/_msgpack.py +207 -0
- dataclass_native/_native.py +234 -0
- dataclass_native/_runtime.py +607 -0
- dataclass_native/_version.py +24 -0
- dataclass_native/_wire.py +901 -0
- dataclass_native/arrays.py +13 -0
- dataclass_native/backends/__init__.py +0 -0
- dataclass_native/backends/bindings.py +74 -0
- dataclass_native/backends/cpp.py +574 -0
- dataclass_native/backends/templates/bindings.cpp.j2 +58 -0
- dataclass_native/backends/templates/extensions.cmake.j2 +12 -0
- dataclass_native/backends/templates/header.hpp.j2 +75 -0
- dataclass_native/cli.py +125 -0
- dataclass_native/cmake/dataclass_native-config.cmake +67 -0
- dataclass_native/include/dataclass_native/arrow.hpp +2022 -0
- dataclass_native/include/dataclass_native/json.hpp +1903 -0
- dataclass_native/include/dataclass_native/msgpack.hpp +1513 -0
- dataclass_native/include/dataclass_native/python.hpp +594 -0
- dataclass_native/include/dataclass_native/reflect.hpp +63 -0
- dataclass_native/include/dataclass_native/types.hpp +417 -0
- dataclass_native/introspect.py +811 -0
- dataclass_native/py.typed +0 -0
- dataclass_native/schema.py +553 -0
- dataclass_native/types.py +103 -0
- dataclass_native-1.0.0.dist-info/METADATA +339 -0
- dataclass_native-1.0.0.dist-info/RECORD +35 -0
- dataclass_native-1.0.0.dist-info/WHEEL +5 -0
- dataclass_native-1.0.0.dist-info/entry_points.txt +2 -0
- dataclass_native-1.0.0.dist-info/licenses/LICENSE +21 -0
- dataclass_native-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1513 @@
|
|
|
1
|
+
// Writes and reads values of the generated types as msgpack, in the wire format of WIRE_FORMAT.md:
|
|
2
|
+
//
|
|
3
|
+
// dataclass_native::Bytes data = dataclass_native::msgpack::to_msgpack(trade);
|
|
4
|
+
// auto trade = dataclass_native::msgpack::from_msgpack<Trade>(data);
|
|
5
|
+
//
|
|
6
|
+
// of a generated struct, or of any type a field of one may have. What is written is, byte for byte, what the Python
|
|
7
|
+
// implementation writes. Nothing here depends on Python, or on a msgpack library.
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <array>
|
|
11
|
+
#include <bit>
|
|
12
|
+
#include <chrono>
|
|
13
|
+
#include <concepts>
|
|
14
|
+
#include <cstddef>
|
|
15
|
+
#include <cstdint>
|
|
16
|
+
#include <limits>
|
|
17
|
+
#include <map>
|
|
18
|
+
#include <memory>
|
|
19
|
+
#include <optional>
|
|
20
|
+
#include <set>
|
|
21
|
+
#include <span>
|
|
22
|
+
#include <stdexcept>
|
|
23
|
+
#include <string>
|
|
24
|
+
#include <string_view>
|
|
25
|
+
#include <tuple>
|
|
26
|
+
#include <type_traits>
|
|
27
|
+
#include <unordered_map>
|
|
28
|
+
#include <utility>
|
|
29
|
+
#include <variant>
|
|
30
|
+
#include <vector>
|
|
31
|
+
|
|
32
|
+
#include "dataclass_native/reflect.hpp"
|
|
33
|
+
#include "dataclass_native/types.hpp"
|
|
34
|
+
|
|
35
|
+
namespace dataclass_native::msgpack
|
|
36
|
+
{
|
|
37
|
+
// What is read is not msgpack, or not the type being read as written; or a value cannot be written
|
|
38
|
+
class Error : public std::invalid_argument
|
|
39
|
+
{
|
|
40
|
+
public:
|
|
41
|
+
using std::invalid_argument::invalid_argument;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
namespace detail
|
|
45
|
+
{
|
|
46
|
+
// Thrown where what is read is not of the type expected. Caught by what is reading the field of a struct, or
|
|
47
|
+
// the whole value, which names it in an Error
|
|
48
|
+
struct Mismatch
|
|
49
|
+
{
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// The kinds of msgpack value by which the members of a plain union are told apart
|
|
53
|
+
enum class Kind
|
|
54
|
+
{
|
|
55
|
+
Nil,
|
|
56
|
+
Bool,
|
|
57
|
+
Int,
|
|
58
|
+
Float,
|
|
59
|
+
Str,
|
|
60
|
+
Bin,
|
|
61
|
+
Array,
|
|
62
|
+
Map,
|
|
63
|
+
Timestamp,
|
|
64
|
+
Reference,
|
|
65
|
+
Other,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
inline constexpr std::int8_t timestamp_type = -1;
|
|
69
|
+
// A reference to a shared object: the index of its table, and its row in it
|
|
70
|
+
inline constexpr std::int8_t reference_type = 1;
|
|
71
|
+
inline constexpr int max_depth = 256;
|
|
72
|
+
inline constexpr std::int64_t day_microseconds = 86'400'000'000;
|
|
73
|
+
|
|
74
|
+
[[noreturn]] inline void truncated()
|
|
75
|
+
{
|
|
76
|
+
throw Error("msgpack data is truncated");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
inline bool is_utf8(std::string_view text)
|
|
80
|
+
{
|
|
81
|
+
const auto* at = reinterpret_cast<const unsigned char*>(text.data());
|
|
82
|
+
const auto* end = at + text.size();
|
|
83
|
+
while (at != end)
|
|
84
|
+
{
|
|
85
|
+
const unsigned first = *at++;
|
|
86
|
+
if (first < 0x80)
|
|
87
|
+
continue;
|
|
88
|
+
// The bytes which follow, and the least code point which needs that many
|
|
89
|
+
int more = 0;
|
|
90
|
+
char32_t least = 0;
|
|
91
|
+
char32_t point = 0;
|
|
92
|
+
if ((first & 0xE0) == 0xC0)
|
|
93
|
+
more = 1, least = 0x80, point = first & 0x1F;
|
|
94
|
+
else if ((first & 0xF0) == 0xE0)
|
|
95
|
+
more = 2, least = 0x800, point = first & 0x0F;
|
|
96
|
+
else if ((first & 0xF8) == 0xF0)
|
|
97
|
+
more = 3, least = 0x10000, point = first & 0x07;
|
|
98
|
+
else
|
|
99
|
+
return false;
|
|
100
|
+
if (end - at < more)
|
|
101
|
+
return false;
|
|
102
|
+
for (; more; --more, ++at)
|
|
103
|
+
{
|
|
104
|
+
if ((*at & 0xC0) != 0x80)
|
|
105
|
+
return false;
|
|
106
|
+
point = (point << 6) | (*at & 0x3F);
|
|
107
|
+
}
|
|
108
|
+
if (point < least || point > 0x10FFFF || (point >= 0xD800 && point < 0xE000))
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
class Writer
|
|
115
|
+
{
|
|
116
|
+
public:
|
|
117
|
+
// The objects of a class which are shared, as written
|
|
118
|
+
struct Table
|
|
119
|
+
{
|
|
120
|
+
std::string_view tag;
|
|
121
|
+
std::vector<Bytes> rows;
|
|
122
|
+
// By the address of each object, its row
|
|
123
|
+
std::unordered_map<const void*, std::uint32_t> index;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
Bytes out;
|
|
127
|
+
// How many objects which are not frozen what is being written is within
|
|
128
|
+
int depth = 0;
|
|
129
|
+
// In the order first used
|
|
130
|
+
std::vector<Table> tables;
|
|
131
|
+
|
|
132
|
+
void byte(unsigned value)
|
|
133
|
+
{
|
|
134
|
+
out.push_back(static_cast<std::uint8_t>(value));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
template <std::unsigned_integral U>
|
|
138
|
+
void big_endian(U value)
|
|
139
|
+
{
|
|
140
|
+
std::uint8_t bytes[sizeof(U)];
|
|
141
|
+
for (std::size_t i = 0; i < sizeof(U); ++i)
|
|
142
|
+
bytes[i] = static_cast<std::uint8_t>(value >> ((sizeof(U) - 1 - i) * 8));
|
|
143
|
+
out.insert(out.end(), bytes, bytes + sizeof(U));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// The byte which says what follows, and that: at once, as an array is of many
|
|
147
|
+
template <std::unsigned_integral U>
|
|
148
|
+
void tagged(std::uint8_t tag, U value)
|
|
149
|
+
{
|
|
150
|
+
std::uint8_t bytes[1 + sizeof(U)]{tag};
|
|
151
|
+
for (std::size_t i = 0; i < sizeof(U); ++i)
|
|
152
|
+
bytes[1 + i] = static_cast<std::uint8_t>(value >> ((sizeof(U) - 1 - i) * 8));
|
|
153
|
+
out.insert(out.end(), bytes, bytes + sizeof(bytes));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
void nil()
|
|
157
|
+
{
|
|
158
|
+
byte(0xC0);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
void boolean(bool value)
|
|
162
|
+
{
|
|
163
|
+
byte(value ? 0xC3 : 0xC2);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// In as few bytes as hold it
|
|
167
|
+
void unsigned_integer(std::uint64_t value)
|
|
168
|
+
{
|
|
169
|
+
if (value < 0x80)
|
|
170
|
+
byte(static_cast<unsigned>(value));
|
|
171
|
+
else if (value <= 0xFF)
|
|
172
|
+
byte(0xCC), big_endian(static_cast<std::uint8_t>(value));
|
|
173
|
+
else if (value <= 0xFFFF)
|
|
174
|
+
byte(0xCD), big_endian(static_cast<std::uint16_t>(value));
|
|
175
|
+
else if (value <= 0xFFFF'FFFF)
|
|
176
|
+
byte(0xCE), big_endian(static_cast<std::uint32_t>(value));
|
|
177
|
+
else
|
|
178
|
+
byte(0xCF), big_endian(value);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
void integer(std::int64_t value)
|
|
182
|
+
{
|
|
183
|
+
if (value >= 0)
|
|
184
|
+
unsigned_integer(static_cast<std::uint64_t>(value));
|
|
185
|
+
else if (value >= -32)
|
|
186
|
+
byte(static_cast<unsigned>(value) & 0xFF);
|
|
187
|
+
else if (value >= std::numeric_limits<std::int8_t>::min())
|
|
188
|
+
byte(0xD0), big_endian(static_cast<std::uint8_t>(value));
|
|
189
|
+
else if (value >= std::numeric_limits<std::int16_t>::min())
|
|
190
|
+
byte(0xD1), big_endian(static_cast<std::uint16_t>(value));
|
|
191
|
+
else if (value >= std::numeric_limits<std::int32_t>::min())
|
|
192
|
+
byte(0xD2), big_endian(static_cast<std::uint32_t>(value));
|
|
193
|
+
else
|
|
194
|
+
byte(0xD3), big_endian(static_cast<std::uint64_t>(value));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
void float32(float value)
|
|
198
|
+
{
|
|
199
|
+
tagged(0xCA, std::bit_cast<std::uint32_t>(value));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
void float64(double value)
|
|
203
|
+
{
|
|
204
|
+
tagged(0xCB, std::bit_cast<std::uint64_t>(value));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
void str(std::string_view value)
|
|
208
|
+
{
|
|
209
|
+
if (!is_utf8(value))
|
|
210
|
+
throw Error("A str which is not UTF-8 cannot be written");
|
|
211
|
+
length(value.size(), 0xA0, 32, 0xD9);
|
|
212
|
+
out.insert(out.end(), value.begin(), value.end());
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
void bin(std::span<const std::uint8_t> value)
|
|
216
|
+
{
|
|
217
|
+
length(value.size(), 0, 0, 0xC4);
|
|
218
|
+
out.insert(out.end(), value.begin(), value.end());
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// The start of an array, which its elements follow
|
|
222
|
+
void array(std::size_t size)
|
|
223
|
+
{
|
|
224
|
+
length(size, 0x90, 16, 0xDC - 1);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// The start of a map, which its keys and values follow, alternately
|
|
228
|
+
void map(std::size_t size)
|
|
229
|
+
{
|
|
230
|
+
length(size, 0x80, 16, 0xDE - 1);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// An extension value of 4, 8 or 12 bytes, which follow
|
|
234
|
+
void extension(std::int8_t type, std::size_t size)
|
|
235
|
+
{
|
|
236
|
+
if (size == 12)
|
|
237
|
+
byte(0xC7), byte(12);
|
|
238
|
+
else
|
|
239
|
+
byte(size == 4 ? 0xD6 : 0xD7);
|
|
240
|
+
byte(static_cast<std::uint8_t>(type));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
std::uint32_t table(std::string_view tag)
|
|
244
|
+
{
|
|
245
|
+
for (std::size_t i = 0; i != tables.size(); ++i)
|
|
246
|
+
if (tables[i].tag == tag)
|
|
247
|
+
return static_cast<std::uint32_t>(i);
|
|
248
|
+
tables.push_back({tag, {}, {}});
|
|
249
|
+
return static_cast<std::uint32_t>(tables.size() - 1);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private:
|
|
253
|
+
// A length, in the first of the forms which holds it: within the type's byte (short_form, if less than
|
|
254
|
+
// short_limit), or after a byte in 1 (code, unless an array's or a map's), 2 or 4 bytes
|
|
255
|
+
void length(std::size_t size, unsigned short_form, std::size_t short_limit, unsigned code)
|
|
256
|
+
{
|
|
257
|
+
const bool one_byte = code == 0xC4 || code == 0xD9;
|
|
258
|
+
if (size < short_limit)
|
|
259
|
+
byte(short_form | static_cast<unsigned>(size));
|
|
260
|
+
else if (one_byte && size <= 0xFF)
|
|
261
|
+
byte(code), big_endian(static_cast<std::uint8_t>(size));
|
|
262
|
+
else if (size <= 0xFFFF)
|
|
263
|
+
byte(code + 1), big_endian(static_cast<std::uint16_t>(size));
|
|
264
|
+
else if (size <= 0xFFFF'FFFF)
|
|
265
|
+
byte(code + 2), big_endian(static_cast<std::uint32_t>(size));
|
|
266
|
+
else
|
|
267
|
+
throw Error("A value of more than 4294967295 elements or bytes cannot be written");
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
class Reader
|
|
272
|
+
{
|
|
273
|
+
public:
|
|
274
|
+
// A shared object, as written, and once read
|
|
275
|
+
struct Row
|
|
276
|
+
{
|
|
277
|
+
const std::uint8_t* at;
|
|
278
|
+
std::shared_ptr<const void> object;
|
|
279
|
+
bool reading = false;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
struct Table
|
|
283
|
+
{
|
|
284
|
+
std::string_view tag;
|
|
285
|
+
std::vector<Row> rows;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const std::uint8_t* at;
|
|
289
|
+
const std::uint8_t* end;
|
|
290
|
+
int depth = 0;
|
|
291
|
+
std::vector<Table> tables;
|
|
292
|
+
|
|
293
|
+
Reader(const std::uint8_t* at, const std::uint8_t* end) : at(at), end(end)
|
|
294
|
+
{
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// While one exists, what is read is within an array or a map
|
|
298
|
+
class Nested
|
|
299
|
+
{
|
|
300
|
+
public:
|
|
301
|
+
explicit Nested(Reader& reader) : reader_(reader)
|
|
302
|
+
{
|
|
303
|
+
if (++reader_.depth > max_depth)
|
|
304
|
+
throw Error("msgpack data is too deeply nested");
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
Nested(const Nested&) = delete;
|
|
308
|
+
Nested& operator=(const Nested&) = delete;
|
|
309
|
+
|
|
310
|
+
~Nested()
|
|
311
|
+
{
|
|
312
|
+
--reader_.depth;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private:
|
|
316
|
+
Reader& reader_;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
std::size_t remaining() const
|
|
320
|
+
{
|
|
321
|
+
return static_cast<std::size_t>(end - at);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
std::uint8_t peek(std::size_t offset = 0) const
|
|
325
|
+
{
|
|
326
|
+
if (remaining() <= offset)
|
|
327
|
+
truncated();
|
|
328
|
+
return at[offset];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
std::span<const std::uint8_t> take(std::size_t size)
|
|
332
|
+
{
|
|
333
|
+
if (remaining() < size)
|
|
334
|
+
truncated();
|
|
335
|
+
at += size;
|
|
336
|
+
return {at - size, size};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
template <std::unsigned_integral U>
|
|
340
|
+
U big_endian()
|
|
341
|
+
{
|
|
342
|
+
U value = 0;
|
|
343
|
+
for (const std::uint8_t byte : take(sizeof(U)))
|
|
344
|
+
value = static_cast<U>((value << 8) | byte);
|
|
345
|
+
return value;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Of the value which is next
|
|
349
|
+
Kind kind() const
|
|
350
|
+
{
|
|
351
|
+
const std::uint8_t first = peek();
|
|
352
|
+
if (first < 0x80 || first >= 0xE0 || (first >= 0xCC && first <= 0xD3))
|
|
353
|
+
return Kind::Int;
|
|
354
|
+
if (first < 0x90 || first == 0xDE || first == 0xDF)
|
|
355
|
+
return Kind::Map;
|
|
356
|
+
if (first < 0xA0 || first == 0xDC || first == 0xDD)
|
|
357
|
+
return Kind::Array;
|
|
358
|
+
if (first < 0xC0 || (first >= 0xD9 && first <= 0xDB))
|
|
359
|
+
return Kind::Str;
|
|
360
|
+
if (first == 0xC0)
|
|
361
|
+
return Kind::Nil;
|
|
362
|
+
if (first == 0xC1)
|
|
363
|
+
throw Error("msgpack data has the unused byte 0xc1");
|
|
364
|
+
if (first <= 0xC3)
|
|
365
|
+
return Kind::Bool;
|
|
366
|
+
if (first <= 0xC6)
|
|
367
|
+
return Kind::Bin;
|
|
368
|
+
if (first == 0xCA || first == 0xCB)
|
|
369
|
+
return Kind::Float;
|
|
370
|
+
// An extension: its type follows its length
|
|
371
|
+
const std::size_t type_at = first >= 0xD4 ? 1 : first == 0xC7 ? 2 : first == 0xC8 ? 3 : 5;
|
|
372
|
+
const auto type = static_cast<std::int8_t>(peek(type_at));
|
|
373
|
+
return type == timestamp_type ? Kind::Timestamp : type == reference_type ? Kind::Reference : Kind::Other;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
void expect(Kind expected) const
|
|
377
|
+
{
|
|
378
|
+
if (kind() != expected)
|
|
379
|
+
throw Mismatch{};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
bool boolean()
|
|
383
|
+
{
|
|
384
|
+
expect(Kind::Bool);
|
|
385
|
+
return take(1)[0] == 0xC3;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
template <std::integral I>
|
|
389
|
+
I integer()
|
|
390
|
+
{
|
|
391
|
+
expect(Kind::Int);
|
|
392
|
+
const std::uint8_t first = take(1)[0];
|
|
393
|
+
if (first < 0x80)
|
|
394
|
+
return in_range<I>(static_cast<std::uint64_t>(first));
|
|
395
|
+
if (first >= 0xE0)
|
|
396
|
+
return in_range<I>(static_cast<std::int64_t>(static_cast<std::int8_t>(first)));
|
|
397
|
+
switch (first)
|
|
398
|
+
{
|
|
399
|
+
case 0xCC:
|
|
400
|
+
return in_range<I>(static_cast<std::uint64_t>(big_endian<std::uint8_t>()));
|
|
401
|
+
case 0xCD:
|
|
402
|
+
return in_range<I>(static_cast<std::uint64_t>(big_endian<std::uint16_t>()));
|
|
403
|
+
case 0xCE:
|
|
404
|
+
return in_range<I>(static_cast<std::uint64_t>(big_endian<std::uint32_t>()));
|
|
405
|
+
case 0xCF:
|
|
406
|
+
return in_range<I>(big_endian<std::uint64_t>());
|
|
407
|
+
case 0xD0:
|
|
408
|
+
return in_range<I>(static_cast<std::int64_t>(static_cast<std::int8_t>(big_endian<std::uint8_t>())));
|
|
409
|
+
case 0xD1:
|
|
410
|
+
return in_range<I>(
|
|
411
|
+
static_cast<std::int64_t>(static_cast<std::int16_t>(big_endian<std::uint16_t>())));
|
|
412
|
+
case 0xD2:
|
|
413
|
+
return in_range<I>(
|
|
414
|
+
static_cast<std::int64_t>(static_cast<std::int32_t>(big_endian<std::uint32_t>())));
|
|
415
|
+
default:
|
|
416
|
+
return in_range<I>(static_cast<std::int64_t>(big_endian<std::uint64_t>()));
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Of either size
|
|
421
|
+
double floating()
|
|
422
|
+
{
|
|
423
|
+
expect(Kind::Float);
|
|
424
|
+
if (take(1)[0] == 0xCA)
|
|
425
|
+
return std::bit_cast<float>(big_endian<std::uint32_t>());
|
|
426
|
+
return std::bit_cast<double>(big_endian<std::uint64_t>());
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
std::string_view str()
|
|
430
|
+
{
|
|
431
|
+
expect(Kind::Str);
|
|
432
|
+
const std::uint8_t first = take(1)[0];
|
|
433
|
+
const auto bytes = take(first < 0xC0 ? first & 0x1F : length(first - 0xD9));
|
|
434
|
+
const std::string_view text{reinterpret_cast<const char*>(bytes.data()), bytes.size()};
|
|
435
|
+
if (!is_utf8(text))
|
|
436
|
+
throw Error("msgpack data has a str which is not UTF-8");
|
|
437
|
+
return text;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
std::span<const std::uint8_t> bin()
|
|
441
|
+
{
|
|
442
|
+
expect(Kind::Bin);
|
|
443
|
+
return take(length(take(1)[0] - 0xC4));
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// The number of elements of the array which is next: they follow
|
|
447
|
+
std::size_t array()
|
|
448
|
+
{
|
|
449
|
+
expect(Kind::Array);
|
|
450
|
+
const std::uint8_t first = take(1)[0];
|
|
451
|
+
return elements(first < 0xA0 ? first & 0x0F : length(first - 0xDC + 1), 1);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// The number of entries of the map which is next: their keys and values follow, alternately
|
|
455
|
+
std::size_t map()
|
|
456
|
+
{
|
|
457
|
+
expect(Kind::Map);
|
|
458
|
+
const std::uint8_t first = take(1)[0];
|
|
459
|
+
return elements(first < 0x90 ? first & 0x0F : length(first - 0xDE + 1), 2);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// The bytes of the extension value which is next, of either of our two types
|
|
463
|
+
std::span<const std::uint8_t> extension()
|
|
464
|
+
{
|
|
465
|
+
const std::uint8_t first = take(1)[0];
|
|
466
|
+
const std::size_t size = first >= 0xD4 ? std::size_t{1} << (first - 0xD4) : length(first - 0xC7);
|
|
467
|
+
take(1);
|
|
468
|
+
return take(size);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// The table and row which the reference which is next is to
|
|
472
|
+
std::array<std::uint32_t, 2> reference()
|
|
473
|
+
{
|
|
474
|
+
expect(Kind::Reference);
|
|
475
|
+
const auto bytes = extension();
|
|
476
|
+
if (bytes.size() != 8)
|
|
477
|
+
throw Mismatch{};
|
|
478
|
+
Reader fields{bytes.data(), bytes.data() + 8};
|
|
479
|
+
const auto table = fields.big_endian<std::uint32_t>();
|
|
480
|
+
const auto row = fields.big_endian<std::uint32_t>();
|
|
481
|
+
if (table >= tables.size() || row >= tables[table].rows.size())
|
|
482
|
+
throw Mismatch{};
|
|
483
|
+
return {table, row};
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// Passes over the value which is next
|
|
487
|
+
void skip()
|
|
488
|
+
{
|
|
489
|
+
const std::uint8_t first = peek();
|
|
490
|
+
switch (kind())
|
|
491
|
+
{
|
|
492
|
+
case Kind::Nil:
|
|
493
|
+
case Kind::Bool:
|
|
494
|
+
take(1);
|
|
495
|
+
break;
|
|
496
|
+
case Kind::Int:
|
|
497
|
+
take(first < 0xCC || first >= 0xE0 ? 1 : 1 + (std::size_t{1} << ((first - 0xCC) & 3)));
|
|
498
|
+
break;
|
|
499
|
+
case Kind::Float:
|
|
500
|
+
take(first == 0xCA ? 5 : 9);
|
|
501
|
+
break;
|
|
502
|
+
case Kind::Str:
|
|
503
|
+
str();
|
|
504
|
+
break;
|
|
505
|
+
case Kind::Bin:
|
|
506
|
+
bin();
|
|
507
|
+
break;
|
|
508
|
+
case Kind::Array:
|
|
509
|
+
case Kind::Map:
|
|
510
|
+
{
|
|
511
|
+
const Nested nested(*this);
|
|
512
|
+
const bool is_map = kind() == Kind::Map;
|
|
513
|
+
for (std::size_t values = is_map ? 2 * map() : array(); values; --values)
|
|
514
|
+
skip();
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
default:
|
|
518
|
+
extension();
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
private:
|
|
523
|
+
template <std::integral I, std::integral V>
|
|
524
|
+
static I in_range(V value)
|
|
525
|
+
{
|
|
526
|
+
if (!std::in_range<I>(value))
|
|
527
|
+
throw Mismatch{};
|
|
528
|
+
return static_cast<I>(value);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// A length held in 1, 2 or 4 bytes
|
|
532
|
+
std::size_t length(int form)
|
|
533
|
+
{
|
|
534
|
+
return form == 0 ? big_endian<std::uint8_t>()
|
|
535
|
+
: form == 1 ? big_endian<std::uint16_t>()
|
|
536
|
+
: big_endian<std::uint32_t>();
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// A number of elements, each of which is at least so many bytes: so not more than there is data for
|
|
540
|
+
std::size_t elements(std::size_t count, std::size_t each) const
|
|
541
|
+
{
|
|
542
|
+
if (count > remaining() / each)
|
|
543
|
+
truncated();
|
|
544
|
+
return count;
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
// Writes and reads a type. Each has write() and read(), and those which may be a member of a union:
|
|
549
|
+
//
|
|
550
|
+
// kind The kind of msgpack value it is written as, by which the members of a plain union are told apart
|
|
551
|
+
// tag() What names it in a discriminated union
|
|
552
|
+
//
|
|
553
|
+
// shares is whether a value may hold, however deeply, a Shared one: if so, it is written as a bundle
|
|
554
|
+
template <class T>
|
|
555
|
+
struct Codec;
|
|
556
|
+
|
|
557
|
+
template <>
|
|
558
|
+
struct Codec<bool>
|
|
559
|
+
{
|
|
560
|
+
static constexpr Kind kind = Kind::Bool;
|
|
561
|
+
static constexpr bool shares = false;
|
|
562
|
+
|
|
563
|
+
static constexpr std::string_view tag()
|
|
564
|
+
{
|
|
565
|
+
return "bool";
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
static void write(Writer& writer, bool value)
|
|
569
|
+
{
|
|
570
|
+
writer.boolean(value);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
static bool read(Reader& reader)
|
|
574
|
+
{
|
|
575
|
+
return reader.boolean();
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
template <std::integral T>
|
|
580
|
+
struct Codec<T>
|
|
581
|
+
{
|
|
582
|
+
static constexpr Kind kind = Kind::Int;
|
|
583
|
+
static constexpr bool shares = false;
|
|
584
|
+
|
|
585
|
+
static constexpr std::string_view tag()
|
|
586
|
+
{
|
|
587
|
+
constexpr std::string_view names[] = {"int8", "int16", "int32", "int",
|
|
588
|
+
"uint8", "uint16", "uint32", "uint64"};
|
|
589
|
+
return names[(std::unsigned_integral<T> ? 4 : 0) + std::countr_zero(sizeof(T))];
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
static void write(Writer& writer, T value)
|
|
593
|
+
{
|
|
594
|
+
if constexpr (std::unsigned_integral<T>)
|
|
595
|
+
writer.unsigned_integer(value);
|
|
596
|
+
else
|
|
597
|
+
writer.integer(value);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
static T read(Reader& reader)
|
|
601
|
+
{
|
|
602
|
+
return reader.integer<T>();
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
template <std::floating_point T>
|
|
607
|
+
struct Codec<T>
|
|
608
|
+
{
|
|
609
|
+
static constexpr Kind kind = Kind::Float;
|
|
610
|
+
static constexpr bool shares = false;
|
|
611
|
+
|
|
612
|
+
static constexpr std::string_view tag()
|
|
613
|
+
{
|
|
614
|
+
return sizeof(T) == 4 ? "float32" : "float";
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
static void write(Writer& writer, T value)
|
|
618
|
+
{
|
|
619
|
+
if constexpr (sizeof(T) == 4)
|
|
620
|
+
writer.float32(value);
|
|
621
|
+
else
|
|
622
|
+
writer.float64(value);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
static T read(Reader& reader)
|
|
626
|
+
{
|
|
627
|
+
return static_cast<T>(reader.floating());
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
template <>
|
|
632
|
+
struct Codec<std::string>
|
|
633
|
+
{
|
|
634
|
+
static constexpr Kind kind = Kind::Str;
|
|
635
|
+
static constexpr bool shares = false;
|
|
636
|
+
|
|
637
|
+
static constexpr std::string_view tag()
|
|
638
|
+
{
|
|
639
|
+
return "str";
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
static void write(Writer& writer, const std::string& value)
|
|
643
|
+
{
|
|
644
|
+
writer.str(value);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
static std::string read(Reader& reader)
|
|
648
|
+
{
|
|
649
|
+
return std::string(reader.str());
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
// bytes, and a tuple of UInt8
|
|
654
|
+
template <>
|
|
655
|
+
struct Codec<Bytes>
|
|
656
|
+
{
|
|
657
|
+
static constexpr Kind kind = Kind::Bin;
|
|
658
|
+
static constexpr bool shares = false;
|
|
659
|
+
|
|
660
|
+
static constexpr std::string_view tag()
|
|
661
|
+
{
|
|
662
|
+
return "bytes";
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
static void write(Writer& writer, const Bytes& value)
|
|
666
|
+
{
|
|
667
|
+
writer.bin(value);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
static Bytes read(Reader& reader)
|
|
671
|
+
{
|
|
672
|
+
const auto bytes = reader.bin();
|
|
673
|
+
return {bytes.begin(), bytes.end()};
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
// As its digits, with a point: so that it is read whatever the places of what reads it
|
|
678
|
+
template <int Places>
|
|
679
|
+
struct Codec<Decimal<Places>>
|
|
680
|
+
{
|
|
681
|
+
static constexpr Kind kind = Kind::Str;
|
|
682
|
+
static constexpr bool shares = false;
|
|
683
|
+
|
|
684
|
+
static constexpr std::string_view tag()
|
|
685
|
+
{
|
|
686
|
+
return "decimal";
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
static void write(Writer& writer, Decimal<Places> value)
|
|
690
|
+
{
|
|
691
|
+
if (!value.fits())
|
|
692
|
+
throw Error("A Decimal of more than 18 digits cannot be written");
|
|
693
|
+
writer.str(value.str());
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
static Decimal<Places> read(Reader& reader)
|
|
697
|
+
{
|
|
698
|
+
const std::optional<Decimal<Places>> value = Decimal<Places>::parse(reader.str());
|
|
699
|
+
if (!value)
|
|
700
|
+
throw Mismatch{};
|
|
701
|
+
return *value;
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
// As its 16 bytes
|
|
706
|
+
template <>
|
|
707
|
+
struct Codec<Uuid>
|
|
708
|
+
{
|
|
709
|
+
static constexpr Kind kind = Kind::Bin;
|
|
710
|
+
static constexpr bool shares = false;
|
|
711
|
+
|
|
712
|
+
static constexpr std::string_view tag()
|
|
713
|
+
{
|
|
714
|
+
return "uuid";
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
static void write(Writer& writer, const Uuid& value)
|
|
718
|
+
{
|
|
719
|
+
writer.bin(value.bytes);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
static Uuid read(Reader& reader)
|
|
723
|
+
{
|
|
724
|
+
const auto bytes = reader.bin();
|
|
725
|
+
Uuid value;
|
|
726
|
+
if (bytes.size() != value.bytes.size())
|
|
727
|
+
throw Mismatch{};
|
|
728
|
+
std::copy(bytes.begin(), bytes.end(), value.bytes.begin());
|
|
729
|
+
return value;
|
|
730
|
+
}
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
// As days since 1970-01-01
|
|
734
|
+
template <>
|
|
735
|
+
struct Codec<Date>
|
|
736
|
+
{
|
|
737
|
+
static constexpr Kind kind = Kind::Int;
|
|
738
|
+
static constexpr bool shares = false;
|
|
739
|
+
|
|
740
|
+
static constexpr std::string_view tag()
|
|
741
|
+
{
|
|
742
|
+
return "date";
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
static void write(Writer& writer, Date value)
|
|
746
|
+
{
|
|
747
|
+
writer.integer(value.time_since_epoch().count());
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
static Date read(Reader& reader)
|
|
751
|
+
{
|
|
752
|
+
return Date{std::chrono::days{reader.integer<std::chrono::days::rep>()}};
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
// As microseconds since midnight
|
|
757
|
+
template <>
|
|
758
|
+
struct Codec<TimeOfDay>
|
|
759
|
+
{
|
|
760
|
+
static constexpr Kind kind = Kind::Int;
|
|
761
|
+
static constexpr bool shares = false;
|
|
762
|
+
|
|
763
|
+
static constexpr std::string_view tag()
|
|
764
|
+
{
|
|
765
|
+
return "time";
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
static void write(Writer& writer, TimeOfDay value)
|
|
769
|
+
{
|
|
770
|
+
const std::int64_t microseconds = value.since_midnight.count();
|
|
771
|
+
if (microseconds < 0 || microseconds >= day_microseconds)
|
|
772
|
+
throw Error("A time which is not within a day cannot be written");
|
|
773
|
+
writer.integer(microseconds);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
static TimeOfDay read(Reader& reader)
|
|
777
|
+
{
|
|
778
|
+
const auto microseconds = reader.integer<std::int64_t>();
|
|
779
|
+
if (microseconds < 0 || microseconds >= day_microseconds)
|
|
780
|
+
throw Mismatch{};
|
|
781
|
+
return {std::chrono::microseconds{microseconds}};
|
|
782
|
+
}
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
// As microseconds
|
|
786
|
+
template <>
|
|
787
|
+
struct Codec<TimeDelta>
|
|
788
|
+
{
|
|
789
|
+
static constexpr Kind kind = Kind::Int;
|
|
790
|
+
static constexpr bool shares = false;
|
|
791
|
+
|
|
792
|
+
static constexpr std::string_view tag()
|
|
793
|
+
{
|
|
794
|
+
return "timedelta";
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
static void write(Writer& writer, TimeDelta value)
|
|
798
|
+
{
|
|
799
|
+
writer.integer(value.count());
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
static TimeDelta read(Reader& reader)
|
|
803
|
+
{
|
|
804
|
+
return TimeDelta{reader.integer<std::int64_t>()};
|
|
805
|
+
}
|
|
806
|
+
};
|
|
807
|
+
|
|
808
|
+
// As microseconds since 1970-01-01T00:00, wherever it is
|
|
809
|
+
template <>
|
|
810
|
+
struct Codec<NaiveDateTime>
|
|
811
|
+
{
|
|
812
|
+
static constexpr Kind kind = Kind::Int;
|
|
813
|
+
static constexpr bool shares = false;
|
|
814
|
+
|
|
815
|
+
static constexpr std::string_view tag()
|
|
816
|
+
{
|
|
817
|
+
return "naivedatetime";
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
static void write(Writer& writer, NaiveDateTime value)
|
|
821
|
+
{
|
|
822
|
+
writer.integer(value.time_since_epoch().count());
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
static NaiveDateTime read(Reader& reader)
|
|
826
|
+
{
|
|
827
|
+
return NaiveDateTime{std::chrono::microseconds{reader.integer<std::int64_t>()}};
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
// As msgpack's timestamp, in the shortest of its three forms which holds it
|
|
832
|
+
template <>
|
|
833
|
+
struct Codec<DateTime>
|
|
834
|
+
{
|
|
835
|
+
static constexpr Kind kind = Kind::Timestamp;
|
|
836
|
+
static constexpr bool shares = false;
|
|
837
|
+
|
|
838
|
+
static constexpr std::string_view tag()
|
|
839
|
+
{
|
|
840
|
+
return "datetime";
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
static void write(Writer& writer, DateTime value)
|
|
844
|
+
{
|
|
845
|
+
const std::int64_t microseconds = value.time_since_epoch().count();
|
|
846
|
+
// Rounding down, so that the nanoseconds are not negative
|
|
847
|
+
const std::int64_t seconds = microseconds / 1'000'000 - (microseconds % 1'000'000 < 0);
|
|
848
|
+
const auto nanoseconds = static_cast<std::uint32_t>((microseconds - seconds * 1'000'000) * 1000);
|
|
849
|
+
if (seconds < 0 || seconds >= std::int64_t{1} << 34)
|
|
850
|
+
{
|
|
851
|
+
writer.extension(timestamp_type, 12);
|
|
852
|
+
writer.big_endian(nanoseconds);
|
|
853
|
+
writer.big_endian(static_cast<std::uint64_t>(seconds));
|
|
854
|
+
}
|
|
855
|
+
else if (nanoseconds || seconds >= std::int64_t{1} << 32)
|
|
856
|
+
{
|
|
857
|
+
writer.extension(timestamp_type, 8);
|
|
858
|
+
writer.big_endian(std::uint64_t{nanoseconds} << 34 | static_cast<std::uint64_t>(seconds));
|
|
859
|
+
}
|
|
860
|
+
else
|
|
861
|
+
{
|
|
862
|
+
writer.extension(timestamp_type, 4);
|
|
863
|
+
writer.big_endian(static_cast<std::uint32_t>(seconds));
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
static DateTime read(Reader& reader)
|
|
868
|
+
{
|
|
869
|
+
reader.expect(Kind::Timestamp);
|
|
870
|
+
const auto bytes = reader.extension();
|
|
871
|
+
Reader fields{bytes.data(), bytes.data() + bytes.size()};
|
|
872
|
+
std::uint32_t nanoseconds = 0;
|
|
873
|
+
std::int64_t seconds = 0;
|
|
874
|
+
if (bytes.size() == 4)
|
|
875
|
+
seconds = fields.big_endian<std::uint32_t>();
|
|
876
|
+
else if (bytes.size() == 8)
|
|
877
|
+
{
|
|
878
|
+
const auto both = fields.big_endian<std::uint64_t>();
|
|
879
|
+
nanoseconds = static_cast<std::uint32_t>(both >> 34);
|
|
880
|
+
seconds = static_cast<std::int64_t>(both & ((std::uint64_t{1} << 34) - 1));
|
|
881
|
+
}
|
|
882
|
+
else if (bytes.size() == 12)
|
|
883
|
+
{
|
|
884
|
+
nanoseconds = fields.big_endian<std::uint32_t>();
|
|
885
|
+
seconds = static_cast<std::int64_t>(fields.big_endian<std::uint64_t>());
|
|
886
|
+
}
|
|
887
|
+
else
|
|
888
|
+
throw Mismatch{};
|
|
889
|
+
|
|
890
|
+
// A whole number of microseconds, which are within 64 bits
|
|
891
|
+
constexpr std::int64_t max_seconds = std::numeric_limits<std::int64_t>::max() / 1'000'000 - 1;
|
|
892
|
+
if (nanoseconds >= 1'000'000'000 || nanoseconds % 1000 || seconds < -max_seconds ||
|
|
893
|
+
seconds > max_seconds)
|
|
894
|
+
throw Mismatch{};
|
|
895
|
+
return DateTime{std::chrono::microseconds{seconds * 1'000'000 + nanoseconds / 1000}};
|
|
896
|
+
}
|
|
897
|
+
};
|
|
898
|
+
|
|
899
|
+
// By name
|
|
900
|
+
template <Enum T>
|
|
901
|
+
struct Codec<T>
|
|
902
|
+
{
|
|
903
|
+
static constexpr Kind kind = Kind::Str;
|
|
904
|
+
static constexpr bool shares = false;
|
|
905
|
+
|
|
906
|
+
static constexpr std::string_view tag()
|
|
907
|
+
{
|
|
908
|
+
return Reflect<T>::tag;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
static void write(Writer& writer, T value)
|
|
912
|
+
{
|
|
913
|
+
for (const auto& member : Reflect<T>::members)
|
|
914
|
+
if (member.value == value)
|
|
915
|
+
return writer.str(member.name);
|
|
916
|
+
throw Error("A value which is not a member of " + std::string(Reflect<T>::name) +
|
|
917
|
+
" cannot be written");
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
static T read(Reader& reader)
|
|
921
|
+
{
|
|
922
|
+
const std::string_view name = reader.str();
|
|
923
|
+
for (const auto& member : Reflect<T>::members)
|
|
924
|
+
if (member.name == name)
|
|
925
|
+
return member.value;
|
|
926
|
+
throw Mismatch{};
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
|
|
930
|
+
template <class T>
|
|
931
|
+
struct Codec<std::optional<T>>
|
|
932
|
+
{
|
|
933
|
+
static constexpr bool shares = Codec<T>::shares;
|
|
934
|
+
|
|
935
|
+
static void write(Writer& writer, const std::optional<T>& value)
|
|
936
|
+
{
|
|
937
|
+
if (value)
|
|
938
|
+
Codec<T>::write(writer, *value);
|
|
939
|
+
else
|
|
940
|
+
writer.nil();
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
static std::optional<T> read(Reader& reader)
|
|
944
|
+
{
|
|
945
|
+
if (reader.kind() != Kind::Nil)
|
|
946
|
+
return Codec<T>::read(reader);
|
|
947
|
+
reader.take(1);
|
|
948
|
+
return std::nullopt;
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
template <class T>
|
|
953
|
+
struct Codec<std::vector<T>>
|
|
954
|
+
{
|
|
955
|
+
static constexpr Kind kind = Kind::Array;
|
|
956
|
+
static constexpr bool shares = Codec<T>::shares;
|
|
957
|
+
|
|
958
|
+
static void write(Writer& writer, const std::vector<T>& value)
|
|
959
|
+
{
|
|
960
|
+
writer.array(value.size());
|
|
961
|
+
for (const auto& element : value)
|
|
962
|
+
Codec<T>::write(writer, element);
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
static std::vector<T> read(Reader& reader)
|
|
966
|
+
{
|
|
967
|
+
const Reader::Nested nested(reader);
|
|
968
|
+
std::vector<T> value;
|
|
969
|
+
std::size_t size = reader.array();
|
|
970
|
+
value.reserve(size);
|
|
971
|
+
for (; size; --size)
|
|
972
|
+
value.push_back(Codec<T>::read(reader));
|
|
973
|
+
return value;
|
|
974
|
+
}
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
// An array of numbers: as the tuple of them is, after its shape if it is of more than one dimension
|
|
978
|
+
template <class T, std::size_t N, bool W>
|
|
979
|
+
struct Codec<Array<T, N, W>>
|
|
980
|
+
{
|
|
981
|
+
static constexpr bool shares = false;
|
|
982
|
+
|
|
983
|
+
static void write(Writer& writer, const Array<T, N, W>& value)
|
|
984
|
+
{
|
|
985
|
+
const std::span<const T> numbers = *value;
|
|
986
|
+
writer.out.reserve(writer.out.size() + numbers.size() * (sizeof(T) + 1));
|
|
987
|
+
if constexpr (N > 1)
|
|
988
|
+
{
|
|
989
|
+
writer.array(2);
|
|
990
|
+
writer.array(N);
|
|
991
|
+
for (const std::size_t size : value.shape())
|
|
992
|
+
writer.unsigned_integer(size);
|
|
993
|
+
}
|
|
994
|
+
if constexpr (std::same_as<std::vector<T>, Bytes>)
|
|
995
|
+
writer.bin(numbers);
|
|
996
|
+
else
|
|
997
|
+
{
|
|
998
|
+
writer.array(numbers.size());
|
|
999
|
+
for (const T& number : numbers)
|
|
1000
|
+
Codec<T>::write(writer, number);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
static Array<T, N, W> read(Reader& reader)
|
|
1005
|
+
{
|
|
1006
|
+
if constexpr (N == 1)
|
|
1007
|
+
return Codec<std::vector<T>>::read(reader);
|
|
1008
|
+
else
|
|
1009
|
+
{
|
|
1010
|
+
const Reader::Nested nested(reader);
|
|
1011
|
+
if (reader.array() != 2 || reader.array() != N)
|
|
1012
|
+
throw Mismatch{};
|
|
1013
|
+
typename Array<T, N, W>::Shape shape;
|
|
1014
|
+
for (std::size_t& size : shape)
|
|
1015
|
+
size = reader.integer<std::size_t>();
|
|
1016
|
+
std::vector<T> values = Codec<std::vector<T>>::read(reader);
|
|
1017
|
+
if (!Array<T, N, W>::fits(shape, values.size()))
|
|
1018
|
+
throw Mismatch{};
|
|
1019
|
+
return {std::move(values), shape};
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
};
|
|
1023
|
+
|
|
1024
|
+
// In ascending order, which is that it is held in
|
|
1025
|
+
template <class T>
|
|
1026
|
+
struct Codec<std::set<T>>
|
|
1027
|
+
{
|
|
1028
|
+
static constexpr Kind kind = Kind::Array;
|
|
1029
|
+
static constexpr bool shares = false;
|
|
1030
|
+
|
|
1031
|
+
static void write(Writer& writer, const std::set<T>& value)
|
|
1032
|
+
{
|
|
1033
|
+
writer.array(value.size());
|
|
1034
|
+
for (const auto& element : value)
|
|
1035
|
+
Codec<T>::write(writer, element);
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
static std::set<T> read(Reader& reader)
|
|
1039
|
+
{
|
|
1040
|
+
const Reader::Nested nested(reader);
|
|
1041
|
+
std::set<T> value;
|
|
1042
|
+
for (std::size_t size = reader.array(); size; --size)
|
|
1043
|
+
value.insert(Codec<T>::read(reader));
|
|
1044
|
+
return value;
|
|
1045
|
+
}
|
|
1046
|
+
};
|
|
1047
|
+
|
|
1048
|
+
// In ascending order of key
|
|
1049
|
+
template <class K, class V>
|
|
1050
|
+
struct Codec<std::map<K, V>>
|
|
1051
|
+
{
|
|
1052
|
+
static constexpr Kind kind = Kind::Map;
|
|
1053
|
+
static constexpr bool shares = Codec<V>::shares;
|
|
1054
|
+
|
|
1055
|
+
static void write(Writer& writer, const std::map<K, V>& value)
|
|
1056
|
+
{
|
|
1057
|
+
writer.map(value.size());
|
|
1058
|
+
for (const auto& [key, element] : value)
|
|
1059
|
+
{
|
|
1060
|
+
Codec<K>::write(writer, key);
|
|
1061
|
+
Codec<V>::write(writer, element);
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
static std::map<K, V> read(Reader& reader)
|
|
1066
|
+
{
|
|
1067
|
+
const Reader::Nested nested(reader);
|
|
1068
|
+
std::map<K, V> value;
|
|
1069
|
+
for (std::size_t size = reader.map(); size; --size)
|
|
1070
|
+
{
|
|
1071
|
+
K key = Codec<K>::read(reader);
|
|
1072
|
+
if (!value.emplace(std::move(key), Codec<V>::read(reader)).second)
|
|
1073
|
+
throw Error("msgpack data has a map with a key twice");
|
|
1074
|
+
}
|
|
1075
|
+
return value;
|
|
1076
|
+
}
|
|
1077
|
+
};
|
|
1078
|
+
|
|
1079
|
+
template <class... Ts>
|
|
1080
|
+
struct Codec<std::tuple<Ts...>>
|
|
1081
|
+
{
|
|
1082
|
+
static constexpr Kind kind = Kind::Array;
|
|
1083
|
+
static constexpr bool shares = (Codec<Ts>::shares || ...);
|
|
1084
|
+
|
|
1085
|
+
static void write(Writer& writer, const std::tuple<Ts...>& value)
|
|
1086
|
+
{
|
|
1087
|
+
writer.array(sizeof...(Ts));
|
|
1088
|
+
std::apply([&](const Ts&... elements) { (Codec<Ts>::write(writer, elements), ...); }, value);
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
static std::tuple<Ts...> read(Reader& reader)
|
|
1092
|
+
{
|
|
1093
|
+
const Reader::Nested nested(reader);
|
|
1094
|
+
if (reader.array() != sizeof...(Ts))
|
|
1095
|
+
throw Mismatch{};
|
|
1096
|
+
// In braces, so in order
|
|
1097
|
+
return std::tuple<Ts...>{Codec<Ts>::read(reader)...};
|
|
1098
|
+
}
|
|
1099
|
+
};
|
|
1100
|
+
|
|
1101
|
+
// As its T
|
|
1102
|
+
template <class T>
|
|
1103
|
+
struct Codec<Indirect<T>>
|
|
1104
|
+
{
|
|
1105
|
+
static constexpr Kind kind = Codec<T>::kind;
|
|
1106
|
+
static constexpr bool shares = Codec<T>::shares;
|
|
1107
|
+
|
|
1108
|
+
static constexpr std::string_view tag()
|
|
1109
|
+
{
|
|
1110
|
+
return Codec<T>::tag();
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
static void write(Writer& writer, const Indirect<T>& value)
|
|
1114
|
+
{
|
|
1115
|
+
if (!value)
|
|
1116
|
+
throw Error("An empty Indirect cannot be written");
|
|
1117
|
+
Codec<T>::write(writer, *value);
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
static Indirect<T> read(Reader& reader)
|
|
1121
|
+
{
|
|
1122
|
+
return Indirect<T>(Codec<T>::read(reader));
|
|
1123
|
+
}
|
|
1124
|
+
};
|
|
1125
|
+
|
|
1126
|
+
// As its T: what was one object held twice is read as two
|
|
1127
|
+
template <class T>
|
|
1128
|
+
struct Codec<Mutable<T>>
|
|
1129
|
+
{
|
|
1130
|
+
static constexpr Kind kind = Codec<T>::kind;
|
|
1131
|
+
static constexpr bool shares = Codec<T>::shares;
|
|
1132
|
+
|
|
1133
|
+
static constexpr std::string_view tag()
|
|
1134
|
+
{
|
|
1135
|
+
return Codec<T>::tag();
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
static void write(Writer& writer, const Mutable<T>& value)
|
|
1139
|
+
{
|
|
1140
|
+
if (!value)
|
|
1141
|
+
throw Error("An empty Mutable cannot be written");
|
|
1142
|
+
// Which only an object that is not frozen may do
|
|
1143
|
+
if (++writer.depth > max_depth)
|
|
1144
|
+
throw Error("An object which holds itself, or is too deeply nested, cannot be written");
|
|
1145
|
+
Codec<T>::write(writer, *value);
|
|
1146
|
+
--writer.depth;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
static Mutable<T> read(Reader& reader)
|
|
1150
|
+
{
|
|
1151
|
+
return Mutable<T>(Codec<T>::read(reader));
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
|
|
1155
|
+
// As a reference to its T, which is written in the table of its class
|
|
1156
|
+
template <class T>
|
|
1157
|
+
struct Codec<Shared<T>>
|
|
1158
|
+
{
|
|
1159
|
+
static constexpr Kind kind = Kind::Reference;
|
|
1160
|
+
static constexpr bool shares = true;
|
|
1161
|
+
|
|
1162
|
+
static constexpr std::string_view tag()
|
|
1163
|
+
{
|
|
1164
|
+
return Reflect<T>::tag;
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
static void write(Writer& writer, const Shared<T>& value)
|
|
1168
|
+
{
|
|
1169
|
+
if (!value)
|
|
1170
|
+
throw Error("An empty Shared cannot be written");
|
|
1171
|
+
|
|
1172
|
+
const std::uint32_t table = writer.table(Reflect<T>::tag);
|
|
1173
|
+
const auto next = static_cast<std::uint32_t>(writer.tables[table].rows.size());
|
|
1174
|
+
const auto [found, added] = writer.tables[table].index.emplace(value.get(), next);
|
|
1175
|
+
const std::uint32_t row = found->second;
|
|
1176
|
+
if (added)
|
|
1177
|
+
{
|
|
1178
|
+
// Its row is taken before it is written, as it may share objects of its own class
|
|
1179
|
+
writer.tables[table].rows.emplace_back();
|
|
1180
|
+
Bytes rest = std::exchange(writer.out, {});
|
|
1181
|
+
Codec<T>::write(writer, *value);
|
|
1182
|
+
writer.tables[table].rows[row] = std::exchange(writer.out, std::move(rest));
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
writer.extension(reference_type, 8);
|
|
1186
|
+
writer.big_endian(table);
|
|
1187
|
+
writer.big_endian(row);
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
static Shared<T> read(Reader& reader)
|
|
1191
|
+
{
|
|
1192
|
+
const auto [table, row] = reader.reference();
|
|
1193
|
+
if (reader.tables[table].tag != Reflect<T>::tag)
|
|
1194
|
+
throw Mismatch{};
|
|
1195
|
+
|
|
1196
|
+
if (!reader.tables[table].rows[row].object)
|
|
1197
|
+
{
|
|
1198
|
+
// An object which shares itself, which no object written could
|
|
1199
|
+
if (std::exchange(reader.tables[table].rows[row].reading, true))
|
|
1200
|
+
throw Mismatch{};
|
|
1201
|
+
const std::uint8_t* rest = std::exchange(reader.at, reader.tables[table].rows[row].at);
|
|
1202
|
+
auto object = std::make_shared<const T>(Codec<T>::read(reader));
|
|
1203
|
+
reader.at = rest;
|
|
1204
|
+
reader.tables[table].rows[row].object = std::move(object);
|
|
1205
|
+
}
|
|
1206
|
+
return Shared<T>(std::static_pointer_cast<const T>(reader.tables[table].rows[row].object));
|
|
1207
|
+
}
|
|
1208
|
+
};
|
|
1209
|
+
|
|
1210
|
+
// As a map of each field's name and value
|
|
1211
|
+
template <Struct T>
|
|
1212
|
+
struct Codec<T>
|
|
1213
|
+
{
|
|
1214
|
+
static constexpr Kind kind = Kind::Map;
|
|
1215
|
+
static constexpr bool shares = Reflect<T>::shares;
|
|
1216
|
+
|
|
1217
|
+
static constexpr std::string_view tag()
|
|
1218
|
+
{
|
|
1219
|
+
return Reflect<T>::tag;
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
static void write(Writer& writer, const T& value)
|
|
1223
|
+
{
|
|
1224
|
+
std::apply(
|
|
1225
|
+
[&](const auto&... field)
|
|
1226
|
+
{
|
|
1227
|
+
writer.map(sizeof...(field));
|
|
1228
|
+
(write_field(writer, value, field), ...);
|
|
1229
|
+
},
|
|
1230
|
+
Reflect<T>::fields);
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
static T read(Reader& reader)
|
|
1234
|
+
{
|
|
1235
|
+
const Reader::Nested nested(reader);
|
|
1236
|
+
T value{};
|
|
1237
|
+
std::array<bool, std::tuple_size_v<decltype(Reflect<T>::fields)>> written{};
|
|
1238
|
+
for (std::size_t entries = reader.map(); entries; --entries)
|
|
1239
|
+
{
|
|
1240
|
+
// An entry which is not a field's is ignored
|
|
1241
|
+
const bool named = reader.kind() == Kind::Str;
|
|
1242
|
+
const std::string_view name = named ? reader.str() : std::string_view{};
|
|
1243
|
+
if (!named)
|
|
1244
|
+
reader.skip();
|
|
1245
|
+
std::size_t index = 0;
|
|
1246
|
+
const bool is_field = named && std::apply(
|
|
1247
|
+
[&](const auto&... field)
|
|
1248
|
+
{
|
|
1249
|
+
return ((field.name == name
|
|
1250
|
+
? (read_field(reader, value, field,
|
|
1251
|
+
written[index]),
|
|
1252
|
+
true)
|
|
1253
|
+
: (++index, false)) ||
|
|
1254
|
+
...);
|
|
1255
|
+
},
|
|
1256
|
+
Reflect<T>::fields);
|
|
1257
|
+
if (!is_field)
|
|
1258
|
+
reader.skip();
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
// A field which is not written takes its default, if it has one
|
|
1262
|
+
std::size_t index = 0;
|
|
1263
|
+
std::apply(
|
|
1264
|
+
[&](const auto&... field)
|
|
1265
|
+
{
|
|
1266
|
+
((written[index++] || field.has_default
|
|
1267
|
+
? void()
|
|
1268
|
+
: throw Error(std::string(Reflect<T>::name) + " cannot be read: no " +
|
|
1269
|
+
std::string(field.name))),
|
|
1270
|
+
...);
|
|
1271
|
+
},
|
|
1272
|
+
Reflect<T>::fields);
|
|
1273
|
+
return value;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
private:
|
|
1277
|
+
static std::string named(std::string_view field)
|
|
1278
|
+
{
|
|
1279
|
+
return std::string(Reflect<T>::name) + "." + std::string(field);
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
template <class M>
|
|
1283
|
+
static void write_field(Writer& writer, const T& value, const Field<T, M>& field)
|
|
1284
|
+
{
|
|
1285
|
+
writer.str(field.name);
|
|
1286
|
+
Codec<M>::write(writer, value.*field.member);
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
template <class M>
|
|
1290
|
+
static void read_field(Reader& reader, T& value, const Field<T, M>& field, bool& written)
|
|
1291
|
+
{
|
|
1292
|
+
if (std::exchange(written, true))
|
|
1293
|
+
throw Error(named(field.name) + " is written twice");
|
|
1294
|
+
try
|
|
1295
|
+
{
|
|
1296
|
+
value.*field.member = Codec<M>::read(reader);
|
|
1297
|
+
}
|
|
1298
|
+
catch (const Mismatch&)
|
|
1299
|
+
{
|
|
1300
|
+
throw Error(named(field.name) + " cannot be read: it is not of its type, as written");
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
};
|
|
1304
|
+
|
|
1305
|
+
// What names a type in a discriminated union, or as the class of a shared object: if anything does
|
|
1306
|
+
template <class T>
|
|
1307
|
+
constexpr std::string_view tag_of()
|
|
1308
|
+
{
|
|
1309
|
+
if constexpr (requires { Codec<T>::tag(); })
|
|
1310
|
+
return Codec<T>::tag();
|
|
1311
|
+
else
|
|
1312
|
+
return {};
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
// Reads the first member of a union for which matches is true, into value
|
|
1316
|
+
template <class Matches, class... Ts>
|
|
1317
|
+
void read_member(Reader& reader, std::variant<Ts...>& value, const Matches& matches)
|
|
1318
|
+
{
|
|
1319
|
+
const bool read =
|
|
1320
|
+
((matches(std::type_identity<Ts>{}) ? (value.template emplace<Ts>(Codec<Ts>::read(reader)), true)
|
|
1321
|
+
: false) ||
|
|
1322
|
+
...);
|
|
1323
|
+
if (!read)
|
|
1324
|
+
throw Mismatch{};
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
// A plain union: as its member, each being written as a kind of msgpack value of its own
|
|
1328
|
+
template <class... Ts>
|
|
1329
|
+
struct Codec<std::variant<Ts...>>
|
|
1330
|
+
{
|
|
1331
|
+
static constexpr bool shares = (Codec<Ts>::shares || ...);
|
|
1332
|
+
|
|
1333
|
+
static void write(Writer& writer, const std::variant<Ts...>& value)
|
|
1334
|
+
{
|
|
1335
|
+
if (value.valueless_by_exception())
|
|
1336
|
+
throw Error("A variant without a value cannot be written");
|
|
1337
|
+
std::visit(
|
|
1338
|
+
[&](const auto& member) { Codec<std::remove_cvref_t<decltype(member)>>::write(writer, member); },
|
|
1339
|
+
value);
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
static std::variant<Ts...> read(Reader& reader)
|
|
1343
|
+
{
|
|
1344
|
+
const Kind kind = reader.kind();
|
|
1345
|
+
// A reference names the class of what it is to, by its table
|
|
1346
|
+
std::string_view tag;
|
|
1347
|
+
if (kind == Kind::Reference)
|
|
1348
|
+
{
|
|
1349
|
+
const std::uint8_t* reference = reader.at;
|
|
1350
|
+
tag = reader.tables[reader.reference()[0]].tag;
|
|
1351
|
+
reader.at = reference;
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
std::variant<Ts...> value;
|
|
1355
|
+
read_member(reader, value,
|
|
1356
|
+
[&]<class M>(std::type_identity<M>)
|
|
1357
|
+
{ return Codec<M>::kind == kind && (kind != Kind::Reference || tag_of<M>() == tag); });
|
|
1358
|
+
return value;
|
|
1359
|
+
}
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
// As a map of "t", its member's tag, and "v", its member
|
|
1363
|
+
template <class... Ts>
|
|
1364
|
+
struct Codec<DiscriminatedUnion<Ts...>>
|
|
1365
|
+
{
|
|
1366
|
+
static constexpr Kind kind = Kind::Map;
|
|
1367
|
+
static constexpr bool shares = (Codec<Ts>::shares || ...);
|
|
1368
|
+
|
|
1369
|
+
static void write(Writer& writer, const DiscriminatedUnion<Ts...>& value)
|
|
1370
|
+
{
|
|
1371
|
+
if (value.valueless_by_exception())
|
|
1372
|
+
throw Error("A variant without a value cannot be written");
|
|
1373
|
+
writer.map(2);
|
|
1374
|
+
writer.str("t");
|
|
1375
|
+
std::visit(
|
|
1376
|
+
[&](const auto& member)
|
|
1377
|
+
{
|
|
1378
|
+
using M = Codec<std::remove_cvref_t<decltype(member)>>;
|
|
1379
|
+
writer.str(M::tag());
|
|
1380
|
+
writer.str("v");
|
|
1381
|
+
M::write(writer, member);
|
|
1382
|
+
},
|
|
1383
|
+
value.as_variant());
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
static DiscriminatedUnion<Ts...> read(Reader& reader)
|
|
1387
|
+
{
|
|
1388
|
+
const Reader::Nested nested(reader);
|
|
1389
|
+
if (reader.map() != 2)
|
|
1390
|
+
throw Mismatch{};
|
|
1391
|
+
|
|
1392
|
+
// In either order: so the member may be before the tag which says what it is
|
|
1393
|
+
std::optional<std::string_view> tag;
|
|
1394
|
+
const std::uint8_t* member = nullptr;
|
|
1395
|
+
for (int entry = 0; entry != 2; ++entry)
|
|
1396
|
+
{
|
|
1397
|
+
const std::string_view key = reader.str();
|
|
1398
|
+
if (key == "t" && !tag)
|
|
1399
|
+
tag = reader.str();
|
|
1400
|
+
else if (key == "v" && !member)
|
|
1401
|
+
{
|
|
1402
|
+
member = reader.at;
|
|
1403
|
+
reader.skip();
|
|
1404
|
+
}
|
|
1405
|
+
else
|
|
1406
|
+
throw Mismatch{};
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
const std::uint8_t* rest = std::exchange(reader.at, member);
|
|
1410
|
+
std::variant<Ts...> value;
|
|
1411
|
+
read_member(reader, value, [&]<class M>(std::type_identity<M>) { return Codec<M>::tag() == *tag; });
|
|
1412
|
+
reader.at = rest;
|
|
1413
|
+
return DiscriminatedUnion<Ts...>(std::move(value));
|
|
1414
|
+
}
|
|
1415
|
+
};
|
|
1416
|
+
|
|
1417
|
+
template <class T>
|
|
1418
|
+
std::string described()
|
|
1419
|
+
{
|
|
1420
|
+
if constexpr (Struct<T>)
|
|
1421
|
+
return "a " + std::string(Reflect<T>::name);
|
|
1422
|
+
else
|
|
1423
|
+
return "of the type read";
|
|
1424
|
+
}
|
|
1425
|
+
} // namespace detail
|
|
1426
|
+
|
|
1427
|
+
template <class T>
|
|
1428
|
+
Bytes to_msgpack(const T& value)
|
|
1429
|
+
{
|
|
1430
|
+
detail::Writer writer;
|
|
1431
|
+
if (!detail::Codec<T>::shares)
|
|
1432
|
+
{
|
|
1433
|
+
detail::Codec<T>::write(writer, value);
|
|
1434
|
+
return std::move(writer.out);
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
// A bundle: the value, then the objects which it shares
|
|
1438
|
+
writer.map(2);
|
|
1439
|
+
writer.str("root");
|
|
1440
|
+
detail::Codec<T>::write(writer, value);
|
|
1441
|
+
writer.str("shared");
|
|
1442
|
+
writer.array(writer.tables.size());
|
|
1443
|
+
for (std::size_t table = 0; table != writer.tables.size(); ++table)
|
|
1444
|
+
{
|
|
1445
|
+
writer.array(2);
|
|
1446
|
+
writer.str(writer.tables[table].tag);
|
|
1447
|
+
writer.array(writer.tables[table].rows.size());
|
|
1448
|
+
for (const Bytes& row : writer.tables[table].rows)
|
|
1449
|
+
writer.out.insert(writer.out.end(), row.begin(), row.end());
|
|
1450
|
+
}
|
|
1451
|
+
return std::move(writer.out);
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
// Throws Error if data is not a T, as written
|
|
1455
|
+
template <class T>
|
|
1456
|
+
T from_msgpack(std::span<const std::uint8_t> data)
|
|
1457
|
+
{
|
|
1458
|
+
using detail::Kind;
|
|
1459
|
+
detail::Reader reader{data.data(), data.data() + data.size()};
|
|
1460
|
+
try
|
|
1461
|
+
{
|
|
1462
|
+
if (!detail::Codec<T>::shares)
|
|
1463
|
+
{
|
|
1464
|
+
T value = detail::Codec<T>::read(reader);
|
|
1465
|
+
if (reader.remaining())
|
|
1466
|
+
throw Error("msgpack data has more than one value");
|
|
1467
|
+
return value;
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
// A bundle. Its tables are found before the value is read, as that refers to them
|
|
1471
|
+
const std::uint8_t* root = nullptr;
|
|
1472
|
+
bool shared = false;
|
|
1473
|
+
for (std::size_t entries = reader.map(); entries; --entries)
|
|
1474
|
+
{
|
|
1475
|
+
const bool named = reader.kind() == Kind::Str;
|
|
1476
|
+
const std::string_view name = named ? reader.str() : std::string_view{};
|
|
1477
|
+
if (name == "shared" && !shared)
|
|
1478
|
+
{
|
|
1479
|
+
shared = true;
|
|
1480
|
+
for (std::size_t tables = reader.array(); tables; --tables)
|
|
1481
|
+
{
|
|
1482
|
+
if (reader.array() != 2)
|
|
1483
|
+
throw detail::Mismatch{};
|
|
1484
|
+
detail::Reader::Table table{reader.str(), {}};
|
|
1485
|
+
for (std::size_t rows = reader.array(); rows; --rows)
|
|
1486
|
+
{
|
|
1487
|
+
table.rows.push_back({reader.at, {}});
|
|
1488
|
+
reader.skip();
|
|
1489
|
+
}
|
|
1490
|
+
reader.tables.push_back(std::move(table));
|
|
1491
|
+
}
|
|
1492
|
+
continue;
|
|
1493
|
+
}
|
|
1494
|
+
if (name == "root" && !root)
|
|
1495
|
+
root = reader.at;
|
|
1496
|
+
else if (!named)
|
|
1497
|
+
reader.skip();
|
|
1498
|
+
reader.skip();
|
|
1499
|
+
}
|
|
1500
|
+
if (reader.remaining())
|
|
1501
|
+
throw Error("msgpack data has more than one value");
|
|
1502
|
+
if (!root || !shared)
|
|
1503
|
+
throw detail::Mismatch{};
|
|
1504
|
+
|
|
1505
|
+
reader.at = root;
|
|
1506
|
+
return detail::Codec<T>::read(reader);
|
|
1507
|
+
}
|
|
1508
|
+
catch (const detail::Mismatch&)
|
|
1509
|
+
{
|
|
1510
|
+
throw Error("The data is not " + detail::described<T>() + ", as written");
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
} // namespace dataclass_native::msgpack
|