-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializerIostreamHelper.h
338 lines (293 loc) · 7.27 KB
/
SerializerIostreamHelper.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
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
// Copyright (c) Kobe Vrijsen 2022
// Licensed under the EUPL-1.2-or-later
#pragma once
#include <fstream>
#include <string>
#include <vector>
#include <array>
namespace serializer_helper
{
namespace detail
{
template <typename Object>
struct is_parsable;
template <typename Object, typename Return>
using enable_if_parsable_t = std::enable_if_t<is_parsable<Object>::value, Return>;
}
//
// Read
//
template <typename Object, typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
auto read(Stream& stream, Object& object) -> detail::enable_if_parsable_t<Object, bool>;
//
// Write
//
template <typename Object, typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
auto write(Stream& stream, Object const& object) -> detail::enable_if_parsable_t<Object, bool>;
//
// Layout
//
template<typename ... Objects>
struct Layout
{
template <typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
static bool Read(Stream& is, Objects & ... objects)
{
return (read(is, objects) && ...);
}
template <typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
static std::tuple<Objects...> Read(Stream& is) requires std::default_initializable<std::tuple<Objects...>>
{
std::tuple<Objects...> out{};
std::apply(
[&is](auto& ... objects)
{
if (!Read(is, objects...))
throw std::runtime_error{ "Read failed" };
},
out
);
return out;
}
template <typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
static bool Write(Stream& os, Objects const& ... objects)
{
return (write(os, objects) && ...);
}
};
// ---Implementation---
#pragma region detail
namespace detail
{
constexpr bool result_fail = false;
constexpr bool result_success = true;
template <typename ... T>
bool failed(std::basic_ios<T...> const& stream)
{
constexpr auto state = std::ios_base::badbit | std::ios_base::failbit;
return bool(stream.rdstate() & state);
}
constexpr bool READ = false;
constexpr bool WRITE = true;
enum class eKind
{
invalid = 0,
trivial,
itterable,
pointer
};
template <typename, typename = void>
constexpr static bool is_iterable_v = false;
template <typename, typename = void>
constexpr static bool is_contiguous_container_v = false;
template <typename Object>
constexpr auto parse_kind() noexcept
{
if constexpr (std::is_trivially_copyable_v<Object>)
{
return eKind::trivial;
}
else if constexpr (is_iterable_v<Object>) // is_container_v
{
return eKind::itterable;
}
else if constexpr (std::is_pointer_v<Object>)
{
return eKind::pointer;
}
else return eKind::invalid;
}
template <typename Object>
struct is_parsable : std::bool_constant<parse_kind<Object>() != eKind::invalid> {};
template <bool W, typename Pod, typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
bool parse_pod(Stream& stream, Pod&& data)
{
std::array<char, sizeof(Pod)> buffer{};
if constexpr (W)
{
#if defined(__cpp_lib_bit_cast)
buffer = std::bit_cast<decltype(buffer)>(data);
#else
std::memcpy(std::data(buffer), std::addressof(data), sizeof(Pod));
#endif
stream.write(std::data(buffer), std::size(buffer));
}
else // R
{
stream.read(std::data(buffer), std::size(buffer));
#if defined(__cpp_lib_bit_cast)
data = std::bit_cast<std::remove_reference_t<Pod>>(buffer);
#else
std::memcpy(std::addressof(data), std::data(buffer), sizeof(Pod));
#endif
}
#if defined(__cpp_lib_bit_cast)
if (std::is_constant_evaluated())
return result_success; // todo: fix?
else
#endif
return /*failed(stream)
? result_fail
:*/ result_success;
}
// Array
template <bool W, typename Pod, typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
bool parse_array(Stream& stream, Pod* const data, ptrdiff_t const count)
{
if (count < 0)
return result_fail;
else
if (count == 0)
return result_success;
else
if constexpr (W)
{
if (!parse_pod<WRITE>(stream, std::make_unsigned_t<ptrdiff_t>(count)))
return result_fail;
#if defined(__cpp_lib_bit_cast)
for (ptrdiff_t i = 0; i < count; ++i)
parse_pod<WRITE>(stream, data[i]);
#else
// Undefined behaviour!!!
stream.write((char const* const)(data), count * sizeof(Pod));
#endif
}
else // R
{
#if defined(__cpp_lib_bit_cast)
for (ptrdiff_t i = 0; i < count; ++i)
parse_pod<READ>(stream, data[i]);
#else
// Undefined behaviour!!!
stream.read((char* const)(data), count * sizeof(Pod));
#endif
}
#if defined(__cpp_lib_bit_cast)
if (std::is_constant_evaluated())
return result_success; // todo: fix?
else
#endif
return /*failed(stream)
? result_fail
:*/ result_success;
}
// Containers
template <typename Cont>
constexpr static bool is_iterable_v<Cont, std::void_t<decltype(std::begin(std::declval<Cont&>()))>> = true;
template <typename Cont>
constexpr static bool is_contiguous_container_v<Cont, std::void_t<decltype(std::data(std::declval<Cont&>()))>> = is_iterable_v<Cont>;
template <bool W, typename Cont, typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
bool parse_container(Stream& stream, Cont& cont)
{
using T = std::decay_t<decltype(*begin(cont))>;
if constexpr (std::is_trivially_copyable_v<T> && is_contiguous_container_v<Cont>)
{
if constexpr (W)
{
return parse_array<WRITE>(stream, data(cont), size(cont));
}
else // R
{
decltype(size(cont)) count{};
if (!parse_pod<READ>(stream, count))
return result_fail;
cont.resize(count);
return parse_array<READ>(stream, data(cont), count);
}
}
else
{
if constexpr (W)
{
parse_pod<WRITE>(stream, size(cont));
for (auto const& el : cont)
if (!write(stream, el))
return result_fail;
return result_success;
}
else // R
{
decltype(size(cont)) count{};
if (!parse_pod<READ>(stream, count))
return result_fail;
if (count == 0)
return result_success;
auto inserter = std::inserter(cont, end(cont));
do
{
T object{};
if (!read(stream, object))
return result_fail;
inserter = std::move(object);
} while (--count);
return result_success;
}
}
}
// Any
template <bool W, typename Object, typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
auto parse_any(Stream& stream, Object& object) -> enable_if_parsable_t<Object, bool>
{
if constexpr (constexpr auto kind = parse_kind<Object>(); kind == eKind::trivial)
{
return parse_pod<W>(stream, object);
}
else if constexpr (kind == eKind::itterable)
{
return parse_container<W>(stream, object);
}
else if constexpr (kind == eKind::pointer)
{
if constexpr (W)
return write(stream, object);
else
return read(stream, object);
}
else return result_fail;
}
}
template <typename Object, typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
auto read(Stream& stream, Object& object) -> detail::enable_if_parsable_t<Object, bool>
{
return detail::parse_any<detail::READ>(stream, object);
}
template <typename Object, typename Stream>
#if defined(__cpp_lib_bit_cast)
constexpr
#endif
auto write(Stream& stream, Object const& object) -> detail::enable_if_parsable_t<Object, bool>
{
return detail::parse_any<detail::WRITE>(stream, object);
}
#pragma endregion
}