forked from Harrm/scale-codec
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscale_decoder_stream.hpp
More file actions
438 lines (376 loc) · 11.9 KB
/
scale_decoder_stream.hpp
File metadata and controls
438 lines (376 loc) · 11.9 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef SCALE_CORE_SCALE_SCALE_DECODER_STREAM_HPP
#define SCALE_CORE_SCALE_SCALE_DECODER_STREAM_HPP
#include <array>
#include <iterator>
#include <optional>
#include <deque>
#include <boost/variant.hpp>
#include <gsl/span>
#include <scale/detail/fixed_width_integer.hpp>
#include <type_traits>
#include <utility>
#include "scale/types.hpp"
namespace scale {
class ScaleDecoderStream {
public:
// special tag to differentiate decoding streams from others
static constexpr auto is_decoder_stream = true;
explicit ScaleDecoderStream(gsl::span<const uint8_t> span);
/**
* @brief scale-decodes pair of values
* @tparam F first value type
* @tparam S second value type
* @param p pair of values to decode
* @return reference to stream
*/
template <class F, class S>
ScaleDecoderStream &operator>>(std::pair<F, S> &p) {
static_assert(!std::is_reference_v<F> && !std::is_reference_v<S>);
return *this >> const_cast<std::remove_const_t<F> &>(p.first) // NOLINT
>> const_cast<std::remove_const_t<S> &>(p.second); // NOLINT
}
/**
* @brief scale-decoding of tuple
* @tparam T enumeration of tuples types
* @param v reference to tuple
* @return reference to stream
*/
template <class... T>
ScaleDecoderStream &operator>>(std::tuple<T...> &v) {
if constexpr (sizeof...(T) > 0) {
decodeElementOfTuple<0>(v);
}
return *this;
}
/**
* @brief scale-decoding of variant
* @tparam T enumeration of various types
* @param v reference to variant
* @return reference to stream
*/
template <class... Ts>
ScaleDecoderStream &operator>>(boost::variant<Ts...> &v) {
// first byte means type index
uint8_t type_index = 0u;
*this >> type_index; // decode type index
// ensure that index is in [0, types_count)
if (type_index >= sizeof...(Ts)) {
raise(DecodeError::WRONG_TYPE_INDEX);
}
tryDecodeAsOneOfVariant<0>(v, type_index);
return *this;
}
/**
* @brief scale-decodes shared_ptr value
* @tparam T value type
* @param v value to decode
* @return reference to stream
*/
template <class T>
ScaleDecoderStream &operator>>(std::shared_ptr<T> &v) {
using mutableT = std::remove_const_t<T>;
static_assert(std::is_default_constructible_v<mutableT>);
v = std::make_shared<mutableT>();
return *this >> const_cast<mutableT &>(*v); // NOLINT
}
/**
* @brief scale-decodes unique_ptr value
* @tparam T value type
* @param v value to decode
* @return reference to stream
*/
template <class T>
ScaleDecoderStream &operator>>(std::unique_ptr<T> &v) {
using mutableT = std::remove_const_t<T>;
static_assert(std::is_default_constructible_v<mutableT>);
v = std::make_unique<mutableT>();
return *this >> const_cast<mutableT &>(*v); // NOLINT
}
/**
* @brief scale-decodes 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>>>
ScaleDecoderStream &operator>>(T &v) {
// check bool
if constexpr (std::is_same_v<I, bool>) {
v = decodeBool();
return *this;
}
// check byte
if constexpr (sizeof(T) == 1u) {
v = nextByte();
return *this;
}
// decode any other integer
v = detail::decodeInteger<I>(*this);
return *this;
}
/**
* @brief scale-decodes any optional value
* @tparam T type of optional value
* @param v optional value reference
* @return reference to stream
*/
template <class T>
ScaleDecoderStream &operator>>(std::optional<T> &v) {
using mutableT = std::remove_const_t<T>;
static_assert(std::is_default_constructible_v<mutableT>);
// optional bool is special case of optional values
// it is encoded as one byte instead of two
// as described in specification
if constexpr (std::is_same_v<mutableT, bool>) {
v = decodeOptionalBool();
return *this;
}
// detect if optional has value
bool has_value = false;
*this >> has_value;
if (!has_value) {
v.reset();
return *this;
}
// decode value
v.emplace();
return *this >> const_cast<mutableT &>(*v); // NOLINT
}
/**
* @brief scale-decodes compact integer value
* @param v compact integer reference
* @return
*/
ScaleDecoderStream &operator>>(CompactInteger &v);
/**
* @brief decodes custom container with is_static_collection bool class
* member
* @tparam C container type
* @param c reference to container
* @return reference to stream
*/
template <class C,
typename T = typename C::value_type,
typename S = typename C::size_type,
typename = std::enable_if_t<C::is_static_collection
|| !C::is_static_collection>>
ScaleDecoderStream &operator>>(C &c) {
using mutableT = std::remove_const_t<T>;
using size_type = S;
static_assert(std::is_default_constructible_v<mutableT>);
if constexpr (C::is_static_collection) {
C container;
for (auto &el : container) {
*this >> el;
}
c = std::move(container);
return *this;
} else {
return decodeVectorLike(c);
}
}
/**
* @brief decodes vector
* @tparam T item type
* @param v reference to container
* @return reference to stream
*/
template <typename T>
ScaleDecoderStream &operator>>(std::vector<T> &v) {
return decodeVectorLike(v);
}
/**
* @brief decodes deque
* @tparam T item type
* @param v reference to container
* @return reference to stream
*/
template <typename T>
ScaleDecoderStream &operator>>(std::deque<T> &v) {
return decodeVectorLike(v);
}
/**
* @brief decodes random access resizable container
* @tparam T item type
* @param v reference to container
* @return reference to stream
*/
template <class C,
typename T = typename C::value_type,
typename S = typename C::size_type>
ScaleDecoderStream &decodeVectorLike(C &v) {
using mutableT = std::remove_const_t<T>;
using size_type = S;
static_assert(std::is_default_constructible_v<mutableT>);
CompactInteger size{0u};
*this >> size;
auto item_count = size.convert_to<size_type>();
C container;
try {
container.resize(item_count);
} catch (const std::bad_alloc &) {
raise(DecodeError::TOO_MANY_ITEMS);
}
for (size_type i = 0u; i < item_count; ++i) {
*this >> container[i];
}
v = std::move(container);
return *this;
}
/**
* @brief Specification for vector<bool>
* @param v reference to container
* @return reference to stream
*/
ScaleDecoderStream &operator>>(std::vector<bool> &v) {
CompactInteger size{0u};
*this >> size;
auto item_count = size.convert_to<size_t>();
std::vector<bool> container;
bool el;
for (size_t i = 0u; i < item_count; ++i) {
*this >> el;
container.push_back(el);
}
v = std::move(container);
return *this;
}
/**
* @brief decodes list of items
* @tparam T item type
* @param v reference to collection
* @return reference to stream
*/
template <class T>
ScaleDecoderStream &operator>>(std::list<T> &v) {
using mutableT = std::remove_const_t<T>;
using size_type = typename std::list<T>::size_type;
static_assert(std::is_default_constructible_v<mutableT>);
CompactInteger size{0u};
*this >> size;
auto item_count = size.convert_to<size_type>();
std::list<T> lst;
try {
lst.reserve(item_count);
} catch (const std::bad_alloc &) {
raise(DecodeError::TOO_MANY_ITEMS);
}
for (size_type i = 0u; i < item_count; ++i) {
lst.emplace_back();
*this >> lst.back();
}
v = std::move(lst);
return *this;
}
template <typename, typename U = void>
struct is_map_like : std::false_type {};
template <typename T>
struct is_map_like<T,
std::void_t<typename T::key_type,
typename T::mapped_type,
decltype(std::declval<T &>()[std::declval<
const typename T::key_type &>()])>>
: std::true_type {};
/**
* @brief decodes associative containers
* @tparam C item type
* @param c reference to the map
* @return reference to stream
*/
template <class C, typename = std::enable_if_t<is_map_like<C>::value>>
ScaleDecoderStream &operator>>(C &c) {
CompactInteger size{0u};
*this >> size;
auto item_count = size.convert_to<size_t>();
C container;
typename C::value_type pair;
for (size_t i = 0u; i < item_count; ++i) {
*this >> pair;
container.emplace(pair);
}
c = std::move(container);
return *this;
}
/**
* @brief decodes array of items
* @tparam T item type
* @tparam size of the array
* @param a reference to the array
* @return reference to stream
*/
template <class T, size_t size>
ScaleDecoderStream &operator>>(std::array<T, size> &a) {
using mutableT = std::remove_const_t<T>;
for (size_t i = 0u; i < size; ++i) {
*this >> const_cast<mutableT &>(a[i]); // NOLINT
}
return *this;
}
/**
* @brief decodes string from stream
* @param v value to decode
* @return reference to stream
*/
ScaleDecoderStream &operator>>(std::string &v);
/**
* @brief hasMore Checks whether n more bytes are available
* @param n Number of bytes to check
* @return True if n more bytes are available and false otherwise
*/
bool hasMore(uint64_t n) const;
/**
* @brief takes one byte from stream and
* advances current byte iterator by one
* @return current byte
*/
uint8_t nextByte();
using ByteSpan = gsl::span<const uint8_t>;
using SpanIterator = ByteSpan::iterator;
using SizeType = ByteSpan::size_type;
ByteSpan span() const {
return span_;
}
SizeType currentIndex() const {
return current_index_;
}
private:
bool decodeBool();
/**
* @brief special case of optional values as described in specification
* @return std::optional<bool> value
*/
std::optional<bool> decodeOptionalBool();
template <size_t I, class... Ts>
void decodeElementOfTuple(std::tuple<Ts...> &v) {
using T = std::remove_const_t<std::tuple_element_t<I, std::tuple<Ts...>>>;
*this >> const_cast<T &>(std::get<I>(v)); // NOLINT
if constexpr (sizeof...(Ts) > I + 1) {
decodeElementOfTuple<I + 1>(v);
}
}
template <size_t I, class... Ts>
void tryDecodeAsOneOfVariant(boost::variant<Ts...> &v, size_t i) {
using T = std::remove_const_t<std::tuple_element_t<I, std::tuple<Ts...>>>;
static_assert(std::is_default_constructible_v<T>);
if (I == i) {
T val;
*this >> val;
v = std::forward<T>(val);
return;
}
if constexpr (sizeof...(Ts) > I + 1) {
tryDecodeAsOneOfVariant<I + 1>(v, i);
}
}
ByteSpan span_;
SpanIterator current_iterator_;
SizeType current_index_;
};
} // namespace scale
#endif // SCALE_CORE_SCALE_SCALE_DECODER_STREAM_HPP