forked from Harrm/scale-codec
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcollections.hpp
More file actions
264 lines (235 loc) · 8.27 KB
/
collections.hpp
File metadata and controls
264 lines (235 loc) · 8.27 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
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @brief Provides encoding and decoding utilities for collections
* in SCALE serialization.
*
* This file defines functions to handle encoding and decoding
* of different types of collections, including static, dynamic,
* resizable, and extensible collections.
*/
#pragma once
#include <scale/detail/collections_type_traits.hpp>
#include <scale/detail/decomposable_type_traits.hpp>
#include <scale/detail/fixed_width_integer.hpp>
#include <scale/detail/tagged.hpp>
#include <scale/types.hpp>
namespace scale {
using detail::collections::DynamicCollection;
using detail::collections::DynamicSpan;
using detail::collections::ExtensibleBackCollection;
using detail::collections::RandomExtensibleCollection;
using detail::collections::ResizeableCollection;
using detail::collections::StaticCollection;
/**
* @brief Encodes a dynamic collection using SCALE encoding.
* @param collection The collection to encode.
* @param encoder The encoder instance to write to.
*/
void encode(DynamicCollection auto &&collection, Encoder &encoder)
requires NoTagged<decltype(collection)>
and (not std::same_as<std::remove_cvref_t<decltype(collection)>,
std::vector<bool>>)
{
encode(as_compact(collection.size()), encoder);
if constexpr (std::same_as<
std::remove_cvref_t<decltype(*std::begin(collection))>,
uint8_t>) {
encoder.write(collection);
return;
}
for (auto &&item : std::forward<decltype(collection)>(collection)) {
encode(std::as_const(item), encoder);
}
}
/**
* @brief Encodes a static collection using SCALE encoding.
* @param collection The collection to encode.
* @param encoder The encoder instance to write to.
*/
void encode(StaticCollection auto &&collection, Encoder &encoder)
requires NoTagged<decltype(collection)>
and (not DecomposableArray<decltype(collection)>)
{
if constexpr (std::same_as<
std::remove_cvref_t<decltype(*std::begin(collection))>,
uint8_t>) {
encoder.write(collection);
return;
}
for (auto &&item : std::forward<decltype(collection)>(collection)) {
encode(std::as_const(item), encoder);
}
}
/**
* @brief Encodes a string view using SCALE encoding.
* @param view The string view to encode.
* @param encoder The encoder instance to write to.
*/
inline void encode(const std::string_view view, Encoder &encoder) {
encode(as_compact(view.size()), encoder);
encoder.write(
{reinterpret_cast<const uint8_t *>(view.data()), view.size()});
}
/**
* @brief Encodes a vector of boolean values using SCALE encoding.
* @param vector The boolean vector to encode.
* @param encoder The encoder instance to write to.
*/
template <typename T>
requires std::same_as<std::remove_cvref_t<T>, std::vector<bool>>
void encode(T &&vector, Encoder &encoder)
requires NoTagged<decltype(vector)>
{
encode(as_compact(vector.size()), encoder);
for (const bool item : vector) {
encoder.put(static_cast<uint8_t>(item ? 1 : 0));
}
}
/**
* @brief Decoding for dynamic spans is prohibited as potentially dangerous.
*/
void decode(DynamicSpan auto &collection, Decoder &decoder) = delete;
/**
* @brief Decodes a static collection using SCALE decoding.
* @param collection The collection to decode.
* @param decoder The decoder instance to read from.
*/
void decode(StaticCollection auto &collection, Decoder &decoder)
requires NoTagged<decltype(collection)>
and (not Decomposable<decltype(collection)>)
{
using Collection = std::remove_cvref_t<decltype(collection)>;
using MutableCollection = std::conditional_t<
detail::collections::ImmutableCollection<Collection>,
detail::collections::mutable_collection_t<Collection>,
Collection>;
auto &mutable_collection = reinterpret_cast<MutableCollection &>(
const_cast<Collection &>(collection));
if constexpr (std::same_as<std::remove_cvref_t<decltype(*std::begin(
mutable_collection))>,
uint8_t>) {
if (decoder.isContinuousSource()) {
decoder.read(mutable_collection);
return;
}
}
for (decltype(auto) item : mutable_collection) {
decode(item, decoder);
}
}
/**
* @brief Decodes an extensible back collection using SCALE decoding.
* @param collection The collection to decode.
* @param decoder The decoder instance to read from.
*/
void decode(ExtensibleBackCollection auto &collection, Decoder &decoder)
requires NoTagged<decltype(collection)>
{
using size_type = typename std::decay_t<decltype(collection)>::size_type;
size_t item_count;
decode(as_compact(item_count), decoder);
if (item_count > collection.max_size()) {
raise(DecodeError::TOO_MANY_ITEMS);
}
collection.clear();
try {
collection.reserve(item_count);
} catch (const std::bad_alloc &) {
raise(DecodeError::TOO_MANY_ITEMS);
}
for (size_type i = 0u; i < item_count; ++i) {
collection.emplace_back();
decode(collection.back(), decoder);
}
}
/**
* @brief Decodes a resizable collection using SCALE decoding.
* @param collection The collection to decode.
* @param decoder The decoder instance to read from.
*/
void decode(ResizeableCollection auto &collection, Decoder &decoder)
requires NoTagged<decltype(collection)>
{
using Collection = std::remove_cvref_t<decltype(collection)>;
using MutableCollection = std::conditional_t<
detail::collections::ImmutableCollection<Collection>,
detail::collections::mutable_collection_t<Collection>,
Collection>;
auto &mutable_collection =
reinterpret_cast<MutableCollection &>(collection);
size_t item_count;
decode(as_compact(item_count), decoder);
if (item_count > collection.max_size()) {
raise(DecodeError::TOO_MANY_ITEMS);
}
try {
mutable_collection.resize(item_count);
} catch (const std::bad_alloc &) {
raise(DecodeError::TOO_MANY_ITEMS);
}
if constexpr (std::same_as<std::remove_cvref_t<decltype(*std::begin(
mutable_collection))>,
uint8_t>) {
if (decoder.isContinuousSource()) {
decoder.read(mutable_collection);
return;
}
}
for (decltype(auto) item : mutable_collection) {
decode(item, decoder);
}
}
/**
* @brief Decodes a random extensible collection using SCALE decoding.
* @note non-sequential collection, which can not be reserved space or resize,
* but each element can be emplaced while decoding
*/
void decode(RandomExtensibleCollection auto &collection, Decoder &decoder)
requires NoTagged<decltype(collection)>
{
using size_type = typename std::decay_t<decltype(collection)>::size_type;
using value_type = std::remove_const_t<
typename std::decay_t<decltype(collection)>::value_type>;
size_type item_count;
decode(as_compact(item_count), decoder);
if (item_count > collection.max_size()) {
raise(DecodeError::TOO_MANY_ITEMS);
}
collection.clear();
for (size_type i = 0u; i < item_count; ++i) {
value_type item{};
decode(item, decoder);
try {
collection.emplace(std::move(item));
} catch (const std::bad_alloc &) {
raise(DecodeError::TOO_MANY_ITEMS);
}
}
}
/**
* @brief Decodes a vector of booleans using SCALE decoding.
* @param collection The collection to decode.
* @param decoder The decoder instance to read from.
*/
inline void decode(std::vector<bool> &collection, Decoder &decoder) {
size_t item_count;
decode(as_compact(item_count), decoder);
if (item_count > collection.max_size()) {
raise(DecodeError::TOO_MANY_ITEMS);
}
try {
collection.resize(item_count);
} catch (const std::bad_alloc &) {
raise(DecodeError::TOO_MANY_ITEMS);
}
for (size_t i = 0u; i < item_count; ++i) {
bool item;
decode(item, decoder);
collection[i] = item;
}
}
} // namespace scale