-
Notifications
You must be signed in to change notification settings - Fork 2
/
byte_stream.hpp
309 lines (253 loc) · 9.44 KB
/
byte_stream.hpp
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
// Original: https://github.com/mohitmv/quick @ include/quick/byte_stream.hpp
// Author: Mohit Saini ([email protected])
#ifndef QUICK_BYTE_STREAM_HPP_
#define QUICK_BYTE_STREAM_HPP_
// ByteStream is super intuitive, safe, reliable and easy to use utility for
// binary serialisation and deserialization of complex and deeply nested C++
// objects.
// Learn more at README.md and byte_stream_test.cpp
// Sample Use Case:
//
// int x1 = 4;
// vector<std::set<std::string>> v1 = {{"ABC", "D"}, {"Q"}};
// quick::OByteStream obs;
// obs << x1 << v1;
// std::vector<std::byte> content = obs.Buffer();
// quick::IByteStream ibs(content);
// int x2;
// vector<std::set<std::string>> v2;
// ibs >> x2 >> v2;
// assert(x1 == x2 && v1 == v2);
#include <type_traits>
#include <cstdint>
#include <string>
#include <string_view>
#include <cstring>
#include <tuple>
#include <utility>
#include <vector>
namespace quick {
static_assert(std::endian::native == std::endian::little, "");
namespace bytestream_impl {
enum class Status {OK, INVALID_READ};
template<typename T>
std::enable_if_t<(std::is_fundamental<T>::value ||
std::is_enum<T>::value)>
WritePrimitiveType(void* dst, T value) {
std::memcpy(dst, &value, sizeof(T));
}
void WriteBuffer(void* dst, const void* buffer_ptr, size_t buffer_size) {
std::memcpy(dst, buffer_ptr, buffer_size);
}
template<typename T, typename = void> struct has_iterator : std::false_type { };
template<typename T>
struct has_iterator<T, std::void_t<typename T::iterator>> : std::true_type { };
template<typename T, typename = void> struct has_to_bytestream : std::false_type { };
template<typename T>
struct has_to_bytestream<T, std::void_t<decltype(&T::ToByteStream)>> : std::true_type { };
template<typename T, typename = void> struct has_from_bytestream : std::false_type { };
template<typename T>
struct has_from_bytestream<T, std::void_t<decltype(&T::FromByteStream)>> : std::true_type { };
template<typename T, typename = void> struct can_insert : std::false_type { };
template<typename T>
struct can_insert<T, std::void_t<decltype(
std::declval<T>().insert(
std::declval<typename T::value_type&&>()))>> : std::true_type { };
template<typename T, typename = void> struct can_push_back : std::false_type { };
template<typename T>
struct can_push_back<T, std::void_t<decltype(
std::declval<T>().push_back(
std::declval<typename T::value_type&&>()))>> : std::true_type { };
template<typename T>
struct ConstCastValueType { using type = T; };
template<typename T1, typename T2>
struct ConstCastValueType<std::pair<const T1, T2>> { using type = std::pair<T1, T2>; };
} // namespace bytestream_impl
class OByteStream {
public:
const std::vector<std::byte>& GetBytes() const { return output_bytes; }
std::vector<std::byte>& GetMutableBytes() { return output_bytes; }
template<typename T>
OByteStream& operator<<(const T& input) { Write(input); return *this; }
const std::vector<std::byte>& Buffer() const { return output_bytes; }
std::vector<std::byte>& Buffer() { return output_bytes; }
private:
void Write(const std::string_view& input) {
size_t size0 = output_bytes.size();
output_bytes.resize(size0 + sizeof(size_t) + input.size());
bytestream_impl::WritePrimitiveType(&output_bytes[size0], input.size());
bytestream_impl::WriteBuffer(&output_bytes[size0 + sizeof(size_t)], input.data(), input.size());
}
template<typename T>
std::enable_if_t<(std::is_fundamental<T>::value ||
std::is_enum<T>::value)>
Write(const std::vector<T>& input) {
size_t size0 = output_bytes.size();
size_t real_input_size = sizeof(T) * input.size();
output_bytes.resize(size0 + sizeof(size_t) + real_input_size);
bytestream_impl::WritePrimitiveType(&output_bytes[size0], input.size());
bytestream_impl::WriteBuffer(&output_bytes[size0 + sizeof(size_t)], input.data(), real_input_size);
}
template<typename T>
std::enable_if_t<bytestream_impl::has_iterator<T>::value>
Write(const T& container) {
Write(container.size());
for (auto& item : container) {
Write(item);
}
}
template<typename T>
std::enable_if_t<(std::is_fundamental<T>::value || std::is_enum<T>::value)>
Write(T input) {
size_t size0 = output_bytes.size();
output_bytes.resize(size0 + sizeof(T));
bytestream_impl::WritePrimitiveType(&output_bytes[size0], input);
}
template<typename... Ts>
void WriteTuple(
const std::tuple<Ts...>&,
std::index_sequence<sizeof...(Ts)>) { }
template<std::size_t I, typename... Ts>
std::enable_if_t<(I < sizeof...(Ts))>
WriteTuple(const std::tuple<Ts...>& input,
std::index_sequence<I>) {
Write(std::get<I>(input));
WriteTuple(input, std::index_sequence<I+1>());
}
template<typename... Ts>
void Write(const std::tuple<Ts...>& input) {
WriteTuple(input, std::index_sequence<0>());
}
template<typename T1, typename T2>
void Write(const std::pair<T1, T2>& input) {
Write(input.first);
Write(input.second);
}
template<typename T>
std::enable_if_t<bytestream_impl::has_to_bytestream<T>::value>
Write(const T& input) {
input.ToByteStream(*this);
}
std::vector<std::byte> output_bytes;
};
class IByteStream {
public:
using Status = bytestream_impl::Status;
IByteStream(const std::byte* buffer, size_t len): buffer(buffer), buffer_len(len) { }
IByteStream(const std::vector<std::byte>& buffer_vec)
: buffer(buffer_vec.data()),
buffer_len(buffer_vec.size()) { }
IByteStream(const std::string_view& str_view)
: buffer(reinterpret_cast<const std::byte*>(str_view.data())),
buffer_len(str_view.size()) { }
template<typename T>
IByteStream& operator>>(T& output) {
if (Read(output)) return *this;
status = Status::INVALID_READ;
return *this;
}
Status GetStatus() const { return status; }
bool Ok() const { return status == Status::OK; }
bool End() const { return read_ptr == buffer_len; }
private:
bool Read(std::string& output) {
size_t string_size;
if (not Read(string_size)) return false;
if (read_ptr + string_size > buffer_len) return false;
output.resize(string_size);
std::memcpy(output.data(), buffer + read_ptr, string_size);
read_ptr += string_size;
return true;
}
bool Read(std::string_view& output) {
const std::byte* buffer;
size_t string_size;
if (not Read(string_size)) return false;
if (read_ptr + string_size > buffer_len) return false;
output = std::string_view(reinterpret_cast<const char*>(buffer + read_ptr), string_size);
read_ptr += string_size;
return true;
}
template<typename T>
bool Read(std::vector<T>& output) {
size_t vec_size;
if (not Read(vec_size)) return false;
output.resize(vec_size);
if constexpr (std::is_fundamental<T>::value || std::is_enum<T>::value) {
// std::memcpy at once is faster than for-loop on individual item.
size_t to_copy = vec_size * sizeof(T);
if (read_ptr + to_copy > buffer_len) return false;
std::memcpy(output.data(), buffer + read_ptr, to_copy);
read_ptr += to_copy;
} else {
for (size_t i = 0; i < vec_size; ++i) {
if (not Read(output[i])) return false;
}
}
return true;
}
template<typename T>
std::enable_if_t<(std::is_fundamental<T>::value ||
std::is_enum<T>::value), bool>
Read(T& output) {
if (read_ptr + sizeof(T) > buffer_len) return false;
std::memcpy(&output, buffer + read_ptr, sizeof(T));
read_ptr += sizeof(T);
return true;
}
template<typename... Ts>
bool ReadTuple(
std::tuple<Ts...>&,
std::index_sequence<sizeof...(Ts)>) { return true; }
template<std::size_t I, typename... Ts>
std::enable_if_t<(I < sizeof...(Ts)), bool>
ReadTuple(std::tuple<Ts...>& output, std::index_sequence<I>) {
return Read(std::get<I>(output)) &&
ReadTuple(output, std::index_sequence<I+1>());
}
template<typename... Ts>
bool Read(std::tuple<Ts...>& output) {
return ReadTuple(output, std::index_sequence<0>());
}
template<typename T1, typename T2>
bool Read(std::pair<T1, T2>& output) {
return Read(output.first) && Read(output.second);
}
template<typename T>
std::enable_if_t<bytestream_impl::has_from_bytestream<T>::value, bool>
Read(T& output) {
output.FromByteStream(*this);
return status == Status::OK;
}
template<typename T>
std::enable_if_t<bytestream_impl::can_insert<T>::value, bool>
Read(T& output) {
return ReadContainer(output, [](T& container, typename T::value_type&& value_type) {
container.insert(std::move(value_type));
});
}
template<typename T>
std::enable_if_t<bytestream_impl::can_push_back<T>::value, bool>
Read(T& output) {
return ReadContainer(output, [&](T& container, typename T::value_type&& value_type) {
container.push_back(std::move(value_type));
});
}
template<typename T, typename Inserter>
bool ReadContainer(T& output, Inserter inserter) {
size_t container_size;
if (not Read(container_size)) return false;
for (size_t i = 0; i < container_size; ++i) {
typename bytestream_impl::ConstCastValueType<typename T::value_type>::type value_type;
if (not Read(value_type)) return false;
inserter(output, std::move(value_type));
}
return true;
}
Status status = Status::OK;
const std::byte* buffer = nullptr;
size_t read_ptr = 0;
size_t buffer_len = 0;
};
} // namespace quick
#endif // QUICK_BYTE_STREAM_HPP_