forked from Harrm/scale-codec
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscale_encoder_stream.hpp
More file actions
378 lines (342 loc) · 10.6 KB
/
scale_encoder_stream.hpp
File metadata and controls
378 lines (342 loc) · 10.6 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
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef SCALE_CORE_SCALE_SCALE_ENCODER_STREAM_HPP
#define SCALE_CORE_SCALE_SCALE_ENCODER_STREAM_HPP
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
#include <deque>
#include <optional>
#include <boost/variant.hpp>
#include <gsl/span>
#include <scale/detail/fixed_width_integer.hpp>
namespace scale {
/**
* @class ScaleEncoderStream designed to scale-encode data to stream
*/
class ScaleEncoderStream {
public:
// special tag to differentiate encoding streams from others
static constexpr auto is_encoder_stream = true;
ScaleEncoderStream();
/**
* Stream initialization
* @param drop_data - when true will only count encoded data size while
* omitting the data itself
*/
explicit ScaleEncoderStream(bool drop_data);
/**
* @return vector of bytes containing encoded data
*/
std::vector<uint8_t> to_vector() const;
/**
* Get amount of encoded data written to the stream
* @return size in bytes
*/
size_t size() const;
/**
* @brief scale-encodes std::vector
* @tparam T type of item
* @param c collection to encode
* @return reference to stream
*/
template <typename T>
ScaleEncoderStream &operator<<(const std::vector<T> &c) {
return encodeDynamicCollection(std::size(c), std::begin(c), std::end(c));
}
/**
* @brief scale-encodes std::deque
* @tparam T type of item
* @param c collection to encode
* @return reference to stream
*/
template <typename T>
ScaleEncoderStream &operator<<(const std::deque<T> &c) {
return encodeDynamicCollection(std::size(c), std::begin(c), std::end(c));
}
/**
* @brief scale-encodes std::list
* @tparam T type of item
* @param c collection to encode
* @return reference to stream
*/
template <typename T>
ScaleEncoderStream &operator<<(const std::list<T> &c) {
return encodeDynamicCollection(std::size(c), std::begin(c), std::end(c));
}
/**
* @brief scale-encodes std::map
* @tparam K key type
* @tparam V mapped type
* @param c collection to encode
* @return reference to stream
*/
template <typename K, typename V>
ScaleEncoderStream &operator<<(const std::map<K, V> &c) {
return encodeDynamicCollection(std::size(c), std::begin(c), std::end(c));
}
/**
* @brief scale-encodes gsl::span
* @tparam T type of item
* @tparam S container size (-1 for dynamic)
* @param span span to encode
* @return reference to stream
*/
template <typename T, ssize_t S>
ScaleEncoderStream &operator<<(const gsl::span<T, S> &span) {
if constexpr (S == -1) {
return encodeDynamicCollection(
std::size(span), std::begin(span), std::end(span));
} else {
return encodeStaticCollection(span);
}
}
/**
* @brief scale-encodes collection of same type items, requires bool tag
* is_static_collection
* @tparam C container type, T type of item
* @param c collection to encode
* @return reference to stream
*/
template <class C,
typename T = typename C::value_type,
typename = std::enable_if_t<C::is_static_collection
|| !C::is_static_collection>>
ScaleEncoderStream &operator<<(const C &c) {
if constexpr (C::is_static_collection) {
return encodeStaticCollection(c);
} else {
return encodeDynamicCollection(
std::size(c), std::begin(c), std::end(c));
}
}
ScaleEncoderStream &operator<<(const std::vector<bool> &v) {
*this << CompactInteger{v.size()};
for (bool el : v) {
*this << el;
}
return *this;
}
/**
* @brief scale-encodes pair of values
* @tparam F first value type
* @tparam S second value type
* @param p pair of values to encode
* @return reference to stream
*/
template <class F, class S>
ScaleEncoderStream &operator<<(const std::pair<F, S> &p) {
return *this << p.first << p.second;
}
/**
* @brief scale-encodes tuple
* @tparam T enumeration of types
* @param v tuple
* @return reference to stream
*/
template <class... Ts>
ScaleEncoderStream &operator<<(const std::tuple<Ts...> &v) {
if constexpr (sizeof...(Ts) > 0) {
encodeElementOfTuple<0>(v);
}
return *this;
}
/**
* @brief scale-encodes variant value
* @tparam T type list
* @param v value to encode
* @return reference to stream
*/
template <class... T>
ScaleEncoderStream &operator<<(const boost::variant<T...> &v) {
tryEncodeAsOneOfVariant<0>(v);
return *this;
}
/**
* @brief scale-encodes sharead_ptr value
* @tparam T type list
* @param v value to encode
* @return reference to stream
*/
template <class T>
ScaleEncoderStream &operator<<(const std::shared_ptr<T> &v) {
if (v == nullptr) {
raise(EncodeError::DEREF_NULLPOINTER);
}
return *this << *v;
}
/**
* @brief scale-encodes unique_ptr value
* @tparam T type list
* @param v value to encode
* @return reference to stream
*/
template <class T>
ScaleEncoderStream &operator<<(const std::unique_ptr<T> &v) {
if (v == nullptr) {
raise(EncodeError::DEREF_NULLPOINTER);
}
return *this << *v;
}
/**
* @brief scale-encodes optional value
* @tparam T value type
* @param v value to encode
* @return reference to stream
*/
template <class T>
ScaleEncoderStream &operator<<(const std::optional<T> &v) {
// optional bool is a special case of optional values
// it should be encoded using one byte instead of two
// as described in specification
if constexpr (std::is_same_v<T, bool>) {
return encodeOptionalBool(v);
}
if (!v.has_value()) {
return putByte(0u);
}
return putByte(1u) << *v;
}
/**
* @brief scale-encodes array of items
* @tparam T item type
* @tparam size of the array
* @param a reference to the array
* @return reference to stream
*/
template <typename T, size_t size>
ScaleEncoderStream &operator<<(const std::array<T, size> &a) {
return encodeStaticCollection(a);
}
/**
* @brief scale-encodes std::reference_wrapper of a type
* @tparam T underlying type
* @param v value to encode
* @return reference to stream;
*/
template <class T>
ScaleEncoderStream &operator<<(const std::reference_wrapper<T> &v) {
return *this << static_cast<const T &>(v);
}
/**
* @brief scale-encodes a string view
* @param sv string_view item
* @return reference to stream
*/
ScaleEncoderStream &operator<<(std::string_view sv) {
return encodeDynamicCollection(sv.size(), sv.begin(), sv.end());
}
/**
* @brief scale-encodes any integral type including bool
* @tparam T integral type
* @param v value of integral type
* @return reference to stream
*/
template <typename T,
typename I = std::decay_t<T>,
typename = std::enable_if_t<std::is_integral_v<I>>>
ScaleEncoderStream &operator<<(T &&v) {
// encode bool
if constexpr (std::is_same_v<I, bool>) {
uint8_t byte = (v ? 1u : 0u);
return putByte(byte);
}
// put byte
if constexpr (sizeof(T) == 1u) {
// to avoid infinite recursion
return putByte(static_cast<uint8_t>(v));
}
// encode any other integer
detail::encodeInteger<I>(v, *this);
return *this;
}
/**
* @brief scale-encodes CompactInteger value as compact integer
* @param v value to encode
* @return reference to stream
*/
ScaleEncoderStream &operator<<(const CompactInteger &v);
protected:
template <size_t I, class... Ts>
void encodeElementOfTuple(const std::tuple<Ts...> &v) {
*this << std::get<I>(v);
if constexpr (sizeof...(Ts) > I + 1) {
encodeElementOfTuple<I + 1>(v);
}
}
template <uint8_t I, class... Ts>
void tryEncodeAsOneOfVariant(const boost::variant<Ts...> &v) {
using T = std::tuple_element_t<I, std::tuple<Ts...>>;
if (v.type() == typeid(T)) {
*this << I << boost::get<T>(v);
return;
}
if constexpr (sizeof...(Ts) > I + 1) {
tryEncodeAsOneOfVariant<I + 1>(v);
}
}
/**
* @brief scale-encodes any dynamic collection
* @tparam It iterator over collection of bytes
* @param size size of the collection
* @param begin iterator pointing to the begin of collection
* @param end iterator pointing to the end of collection
* @return reference to stream
*/
template <class It>
ScaleEncoderStream &encodeDynamicCollection(const CompactInteger &size,
It &&begin,
It &&end) {
*this << size;
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
for (auto &&it = begin; it != end; ++it) {
*this << *it;
}
return *this;
}
/**
* @brief scale-encodes any fixed-size collection (std::array)
* @tparam C collection type
* @param c collection
* @return reference to stream
*/
template <class C>
ScaleEncoderStream &encodeStaticCollection(const C &c) {
for (const auto &e : c) {
*this << e;
}
return *this;
}
/**
* @brief puts a byte to buffer
* @param v byte value
* @return reference to stream
*/
ScaleEncoderStream &putByte(uint8_t v);
private:
ScaleEncoderStream &encodeOptionalBool(const std::optional<bool> &v);
const bool drop_data_;
std::deque<uint8_t> stream_;
size_t bytes_written_;
};
/**
* @brief scale-encodes any enum type as its underlying type
* Defined outside ScaleEncoderStream to allow custom overloads for
* specific enum types.
* @tparam T enum type
* @param v value of the enum type
* @return reference to stream
*/
template <typename S,
typename T,
typename E = std::decay_t<T>,
typename = std::enable_if_t<S::is_encoder_stream>,
typename = std::enable_if_t<std::is_enum_v<E>>>
S &operator<<(S &s, const T &v) {
return s << static_cast<std::underlying_type_t<E>>(v);
}
} // namespace scale
#endif // SCALE_CORE_SCALE_SCALE_ENCODER_STREAM_HPP