@eosrio/node-abieos 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +4 -1
  2. package/dist/abieos.node +0 -0
  3. package/lib/abieos.d.ts +19 -0
  4. package/lib/abieos.d.ts.map +1 -0
  5. package/lib/abieos.js +100 -0
  6. package/lib/abieos.ts +107 -0
  7. package/package.json +13 -8
  8. package/.idea/modules.xml +0 -8
  9. package/.idea/node-abieos.iml +0 -12
  10. package/.idea/vcs.xml +0 -9
  11. package/.idea/workspace.xml +0 -66
  12. package/CMakeLists.txt +0 -35
  13. package/abieos.d.ts +0 -21
  14. package/abieos.js +0 -59
  15. package/eosio.json +0 -1596
  16. package/include/.clang-format +0 -76
  17. package/include/eosio/abi.hpp +0 -393
  18. package/include/eosio/asset.hpp +0 -458
  19. package/include/eosio/bytes.hpp +0 -27
  20. package/include/eosio/chain_conversions.hpp +0 -450
  21. package/include/eosio/chain_types.hpp +0 -14
  22. package/include/eosio/check.hpp +0 -178
  23. package/include/eosio/convert.hpp +0 -95
  24. package/include/eosio/crypto.hpp +0 -148
  25. package/include/eosio/eosio_outcome.hpp +0 -41
  26. package/include/eosio/fixed_bytes.hpp +0 -250
  27. package/include/eosio/float.hpp +0 -35
  28. package/include/eosio/for_each_field.hpp +0 -48
  29. package/include/eosio/fpconv.c +0 -336
  30. package/include/eosio/fpconv.h +0 -41
  31. package/include/eosio/fpconv.license +0 -23
  32. package/include/eosio/from_bin.hpp +0 -272
  33. package/include/eosio/from_json.hpp +0 -749
  34. package/include/eosio/from_string.hpp +0 -28
  35. package/include/eosio/map_macro.h +0 -64
  36. package/include/eosio/murmur.hpp +0 -55
  37. package/include/eosio/name.hpp +0 -178
  38. package/include/eosio/opaque.hpp +0 -184
  39. package/include/eosio/operators.hpp +0 -71
  40. package/include/eosio/powers.h +0 -76
  41. package/include/eosio/reflection.hpp +0 -61
  42. package/include/eosio/ship_protocol.hpp +0 -971
  43. package/include/eosio/stream.hpp +0 -235
  44. package/include/eosio/symbol.hpp +0 -300
  45. package/include/eosio/time.hpp +0 -264
  46. package/include/eosio/to_bin.hpp +0 -189
  47. package/include/eosio/to_json.hpp +0 -334
  48. package/include/eosio/to_key.hpp +0 -305
  49. package/include/eosio/types.hpp +0 -103
  50. package/include/eosio/varint.hpp +0 -452
  51. package/include/outcome-basic.hpp +0 -6836
  52. package/include/rapidjson/allocators.h +0 -284
  53. package/include/rapidjson/cursorstreamwrapper.h +0 -78
  54. package/include/rapidjson/document.h +0 -2646
  55. package/include/rapidjson/encodedstream.h +0 -299
  56. package/include/rapidjson/encodings.h +0 -716
  57. package/include/rapidjson/error/en.h +0 -74
  58. package/include/rapidjson/error/error.h +0 -161
  59. package/include/rapidjson/filereadstream.h +0 -99
  60. package/include/rapidjson/filewritestream.h +0 -104
  61. package/include/rapidjson/fwd.h +0 -151
  62. package/include/rapidjson/internal/biginteger.h +0 -290
  63. package/include/rapidjson/internal/diyfp.h +0 -271
  64. package/include/rapidjson/internal/dtoa.h +0 -245
  65. package/include/rapidjson/internal/ieee754.h +0 -78
  66. package/include/rapidjson/internal/itoa.h +0 -308
  67. package/include/rapidjson/internal/meta.h +0 -186
  68. package/include/rapidjson/internal/pow10.h +0 -55
  69. package/include/rapidjson/internal/regex.h +0 -732
  70. package/include/rapidjson/internal/stack.h +0 -231
  71. package/include/rapidjson/internal/strfunc.h +0 -69
  72. package/include/rapidjson/internal/strtod.h +0 -290
  73. package/include/rapidjson/internal/swap.h +0 -46
  74. package/include/rapidjson/istreamwrapper.h +0 -113
  75. package/include/rapidjson/memorybuffer.h +0 -70
  76. package/include/rapidjson/memorystream.h +0 -71
  77. package/include/rapidjson/msinttypes/inttypes.h +0 -316
  78. package/include/rapidjson/msinttypes/stdint.h +0 -300
  79. package/include/rapidjson/ostreamwrapper.h +0 -81
  80. package/include/rapidjson/pointer.h +0 -1357
  81. package/include/rapidjson/prettywriter.h +0 -277
  82. package/include/rapidjson/rapidjson.h +0 -654
  83. package/include/rapidjson/reader.h +0 -2230
  84. package/include/rapidjson/schema.h +0 -2494
  85. package/include/rapidjson/stream.h +0 -223
  86. package/include/rapidjson/stringbuffer.h +0 -121
  87. package/include/rapidjson/writer.h +0 -710
  88. package/src/abi.cpp +0 -274
  89. package/src/abieos.cpp +0 -334
  90. package/src/abieos.h +0 -91
  91. package/src/abieos.hpp +0 -1011
  92. package/src/abieos_exception.hpp +0 -89
  93. package/src/abieos_numeric.hpp +0 -94
  94. package/src/abieos_ripemd160.hpp +0 -417
  95. package/src/crypto.cpp +0 -215
  96. package/src/main.cpp +0 -242
  97. package/src/ship.abi.cpp +0 -695
  98. package/tools/CMakeLists.txt +0 -9
  99. package/tools/name.cpp +0 -86
@@ -1,334 +0,0 @@
1
- #pragma once
2
-
3
- #include <cmath>
4
- #include "for_each_field.hpp"
5
- #include "fpconv.h"
6
- #include "stream.hpp"
7
- #include "types.hpp"
8
- #include <limits>
9
- #include <optional>
10
- #include <rapidjson/encodings.h>
11
- #include <variant>
12
- #include <map>
13
-
14
- namespace eosio {
15
-
16
- inline constexpr char hex_digits[] = "0123456789ABCDEF";
17
-
18
- // Adaptors for rapidjson
19
- struct stream_adaptor {
20
- stream_adaptor(const char* src, int sz) {
21
- int chars = std::min(sz, 4);
22
- memcpy(buf, src, chars);
23
- memset(buf + chars, 0, 4 - chars);
24
- }
25
- void Put(char ch) {}
26
- char Take() { return buf[idx++]; }
27
- char buf[4];
28
- int idx = 0;
29
- };
30
-
31
- // Replaces any invalid utf-8 bytes with ?
32
- template <typename S>
33
- void to_json(std::string_view sv, S& stream) {
34
- stream.write('"');
35
- auto begin = sv.begin();
36
- auto end = sv.end();
37
- while (begin != end) {
38
- auto pos = begin;
39
- while (pos != end && *pos != '"' && *pos != '\\' && (unsigned char)(*pos) >= 32 && *pos != 127) ++pos;
40
- while (begin != pos) {
41
- stream_adaptor s2(begin, static_cast<std::size_t>(pos - begin));
42
- if (rapidjson::UTF8<>::Validate(s2, s2)) {
43
- stream.write(begin, s2.idx);
44
- begin += s2.idx;
45
- } else {
46
- ++begin;
47
- stream.write('?');
48
- }
49
- }
50
- if (begin != end) {
51
- if (*begin == '"') {
52
- stream.write("\\\"", 2);
53
- } else if (*begin == '\\') {
54
- stream.write("\\\\", 2);
55
- } else {
56
- stream.write("\\u00", 4);
57
- stream.write(hex_digits[(unsigned char)(*begin) >> 4]);
58
- stream.write(hex_digits[(unsigned char)(*begin) & 15]);
59
- }
60
- ++begin;
61
- }
62
- }
63
- stream.write('"');
64
- }
65
-
66
- template <typename S>
67
- void to_json(const std::string& s, S& stream) {
68
- to_json(std::string_view{ s }, stream);
69
- }
70
-
71
- template <typename S>
72
- void to_json(const char* s, S& stream) {
73
- to_json(std::string_view{ s }, stream);
74
- }
75
-
76
- /*
77
- template <typename S>
78
- result<void> to_json(const shared_memory<std::string_view>& s, S& stream) {
79
- return to_json(*s, stream);
80
- }
81
- */
82
-
83
- template <typename S>
84
- void to_json(bool value, S& stream) {
85
- if (value)
86
- stream.write("true", 4);
87
- else
88
- stream.write("false", 5);
89
- }
90
-
91
- template <typename T>
92
- struct make_unsigned : std::make_unsigned<T> {};
93
-
94
- #ifndef ABIEOS_NO_INT128
95
- // some standard library does not support std::make_unsigned<__int128> yet.
96
- template <>
97
- struct make_unsigned<__int128> {
98
- using type = unsigned __int128;
99
- };
100
-
101
- template <>
102
- struct make_unsigned<unsigned __int128> {
103
- using type = unsigned __int128;
104
- };
105
- #endif
106
-
107
- template <typename T>
108
- using make_unsigned_t = typename make_unsigned<T>::type;
109
-
110
- template <typename T>
111
- char* int_to_decimal(T value, char* buffer) {
112
- char* pos = buffer;
113
- auto uvalue = make_unsigned_t<T>(value);
114
- bool neg = value < 0;
115
- if (neg)
116
- uvalue = -uvalue;
117
-
118
- do {
119
- *pos++ = '0' + (uvalue % 10);
120
- uvalue /= 10;
121
- } while (uvalue);
122
-
123
- if (neg)
124
- *pos++ = '-';
125
- std::reverse(buffer, pos);
126
- return pos;
127
- }
128
-
129
- template <typename T, typename S>
130
- void int_to_json(T value, S& stream) {
131
- // For older versions of libstdc++ (g++ version 9 and below) std::numeric_limits<__int128>::digits10
132
- // would return 0 when compiling with -std=c++17 flag
133
- const int num_digits = sizeof(T) == 16 ? 38 : std::numeric_limits<T>::digits10;
134
- small_buffer<num_digits + 4> b;
135
- if (sizeof(T) > 4)
136
- *b.pos++ = '"';
137
- b.pos = int_to_decimal(value, b.pos);
138
- if (sizeof(T) > 4)
139
- *b.pos++ = '"';
140
- stream.write(b.data, b.pos - b.data);
141
- }
142
-
143
- template <typename S>
144
- void fp_to_json(double value, S& stream) {
145
- // fpconv is not quite consistent with javascript for nans and infinities
146
- if (value == std::numeric_limits<double>::infinity()) {
147
- stream.write("\"Infinity\"", 10);
148
- } else if (value == -std::numeric_limits<double>::infinity()) {
149
- stream.write("\"-Infinity\"", 11);
150
- } else if (std::isnan(value)) {
151
- stream.write("\"NaN\"", 5);
152
- } else {
153
- small_buffer<24> b; // fpconv_dtoa generates at most 24 characters
154
- int n = fpconv_dtoa(value, b.pos);
155
- check( n > 0, convert_stream_error(stream_error::float_error) );
156
- b.pos += n;
157
- stream.write(b.data, b.pos - b.data);
158
- }
159
- }
160
-
161
- // clang-format off
162
- template <typename S> void to_json(unsigned char value, S& stream) { return int_to_json(value, stream); }
163
- template <typename S> void to_json(uint16_t value, S& stream) { return int_to_json(value, stream); }
164
- template <typename S> void to_json(uint32_t value, S& stream) { return int_to_json(value, stream); }
165
- template <typename S> void to_json(uint64_t value, S& stream) { return int_to_json(value, stream); }
166
- template <typename S> void to_json(char value, S& stream) { return int_to_json(value, stream); }
167
- template <typename S> void to_json(signed char value, S& stream) { return int_to_json(value, stream); }
168
- template <typename S> void to_json(int16_t value, S& stream) { return int_to_json(value, stream); }
169
- template <typename S> void to_json(int32_t value, S& stream) { return int_to_json(value, stream); }
170
- template <typename S> void to_json(int64_t value, S& stream) { return int_to_json(value, stream); }
171
- template <typename S> void to_json(double value, S& stream) { return fp_to_json(value, stream); }
172
- template <typename S> void to_json(float value, S& stream) { return fp_to_json(value, stream); }
173
-
174
- #ifndef ABIEOS_NO_INT128
175
- template <typename S> void to_json(unsigned __int128 value, S& stream) { return int_to_json(value, stream); }
176
- template <typename S> void to_json(__int128 value, S& stream) { return int_to_json(value, stream); }
177
- #endif
178
-
179
- // clang-format on
180
-
181
- template <typename T, typename S>
182
- void to_json(const std::vector<T>& obj, S& stream) {
183
- stream.write('[');
184
- bool first = true;
185
- for (auto& v : obj) {
186
- if (first) {
187
- increase_indent(stream);
188
- } else {
189
- stream.write(',');
190
- }
191
- write_newline(stream);
192
- first = false;
193
- to_json(v, stream);
194
- }
195
- if (!first) {
196
- decrease_indent(stream);
197
- write_newline(stream);
198
- }
199
- stream.write(']');
200
- }
201
-
202
- template <typename K, typename V, typename S>
203
- void to_json(const std::map<K, V>& obj, S& stream) {
204
- stream.write('{');
205
- bool first = true;
206
- for (const auto& [k,v] : obj) {
207
- if (first) {
208
- increase_indent(stream);
209
- } else {
210
- stream.write(',');
211
- }
212
- write_newline(stream);
213
- first = false;
214
- to_json(k, stream);
215
- stream.write(':');
216
- to_json(v, stream);
217
- }
218
- if (!first) {
219
- decrease_indent(stream);
220
- write_newline(stream);
221
- }
222
- stream.write('}');
223
- }
224
-
225
- template <typename T, typename S>
226
- void to_json(const std::optional<T>& obj, S& stream) {
227
- if (obj) {
228
- to_json(*obj, stream);
229
- } else {
230
- stream.write("null", 4);
231
- }
232
- }
233
-
234
- template <typename... T, typename S>
235
- void to_json(const std::variant<T...>& obj, S& stream) {
236
- stream.write('[');
237
- increase_indent(stream);
238
- write_newline(stream);
239
- std::visit(
240
- [&](const auto& t) { to_json(get_type_name((std::decay_t<decltype(t)>*)nullptr), stream); }, obj);
241
- stream.write(',');
242
- write_newline(stream);
243
- std::visit([&](auto& x) { return to_json(x, stream); }, obj);
244
- decrease_indent(stream);
245
- write_newline(stream);
246
- stream.write(']');
247
- }
248
-
249
- template<typename>
250
- struct is_std_optional : std::false_type {};
251
-
252
- template<typename T>
253
- struct is_std_optional<std::optional<T>> : std::true_type {
254
- using value_type = T;
255
- };
256
-
257
- template <typename T, typename S>
258
- void to_json(const T& t, S& stream) {
259
- bool first = true;
260
- stream.write('{');
261
- eosio::for_each_field<T>([&](const char* name, auto&& member) {
262
- auto addfield = [&]() {
263
- if (first) {
264
- increase_indent(stream);
265
- first = false;
266
- } else {
267
- stream.write(',');
268
- }
269
- write_newline(stream);
270
- to_json(name, stream);
271
- write_colon(stream);
272
- to_json(member(&t), stream);
273
- };
274
-
275
- auto m = member(&t);
276
- using member_type = std::decay_t<decltype(m)>;
277
- if constexpr ( not is_std_optional<member_type>::value ) {
278
- addfield();
279
- } else {
280
- // !!! Skipping empty optionals breaks the tests, because
281
- // abi serialization can't handle it.
282
- if( !!m || true )
283
- addfield();
284
- }
285
- });
286
- if (!first) {
287
- decrease_indent(stream);
288
- write_newline(stream);
289
- }
290
- stream.write('}');
291
- }
292
-
293
- template <typename S>
294
- void to_json_hex(const char* data, size_t size, S& stream) {
295
- stream.write('"');
296
- for (size_t i = 0; i < size; ++i) {
297
- unsigned char byte = data[i];
298
- stream.write(hex_digits[byte >> 4]);
299
- stream.write(hex_digits[byte & 15]);
300
- }
301
- stream.write('"');
302
- }
303
-
304
- #ifdef __eosio_cdt__
305
-
306
- template <typename S> void to_json(long double value, S& stream) {
307
- return to_json_hex(reinterpret_cast<const char*>(&value), sizeof(long double), stream);
308
- }
309
-
310
- #endif
311
-
312
- template <typename T>
313
- std::string convert_to_json(const T& t) {
314
- size_stream ss;
315
- to_json(t, ss);
316
- std::string result(ss.size, 0);
317
- fixed_buf_stream fbs(result.data(), result.size());
318
- to_json(t, fbs);
319
- check( fbs.pos == fbs.end, convert_stream_error(stream_error::underrun) );
320
- return result;
321
- }
322
-
323
- template <typename T>
324
- std::string format_json(const T& t) {
325
- pretty_stream<size_stream> ss;
326
- to_json(t, ss);
327
- std::string result(ss.size, 0);
328
- pretty_stream<fixed_buf_stream> fbs(result.data(), result.size());
329
- to_json(t, fbs);
330
- check( fbs.pos == fbs.end, convert_stream_error(stream_error::underrun) );
331
- return result;
332
- }
333
-
334
- } // namespace eosio
@@ -1,305 +0,0 @@
1
- #pragma once
2
-
3
- #include <deque>
4
- #include "for_each_field.hpp"
5
- #include "stream.hpp"
6
- #include <list>
7
- #include <map>
8
- #include <optional>
9
- #include <set>
10
- #include <tuple>
11
- #include <utility>
12
- #include <variant>
13
- #include <vector>
14
- #include <cstring>
15
- #include <limits>
16
-
17
- namespace eosio {
18
-
19
- template <typename... Ts, typename S>
20
- void to_key(const std::tuple<Ts...>& obj, S& stream);
21
-
22
- // to_key defines a conversion from a type to a sequence of bytes whose lexicograpical
23
- // ordering is the same as the ordering of the original type.
24
- //
25
- // For any two objects of type T, a and b:
26
- //
27
- // - key(a) < key(b) iff a < b
28
- // - key(a) is not a prefix of key(b)
29
- //
30
- // Overloads of to_key for user-defined types can be found by Koenig lookup.
31
- //
32
- // Abieos provides specializations of to_key for the following types
33
- // - std::string and std::string_view
34
- // - std::vector, std::list, std::deque
35
- // - std::tuple
36
- // - std::array
37
- // - std::optional
38
- // - std::variant
39
- // - Arithmetic types
40
- // - Scoped enumeration types
41
- // - Reflected structs
42
- // - All smart-contract related types defined by abieos
43
- template <typename T, typename S>
44
- void to_key(const T& obj, S& stream);
45
-
46
- template <int i, typename T, typename S>
47
- void to_key_tuple(const T& obj, S& stream) {
48
- if constexpr (i < std::tuple_size_v<T>) {
49
- to_key(std::get<i>(obj), stream);
50
- to_key_tuple<i + 1>(obj, stream);
51
- }
52
- }
53
-
54
- template <typename... Ts, typename S>
55
- void to_key(const std::tuple<Ts...>& obj, S& stream) {
56
- to_key_tuple<0>(obj, stream);
57
- }
58
-
59
- template <typename T, std::size_t N, typename S>
60
- void to_key(const std::array<T, N>& obj, S& stream) {
61
- for (const T& elem : obj) { to_key(elem, stream); }
62
- }
63
-
64
- template <typename T, typename S>
65
- void to_key_optional(const bool* obj, S& stream) {
66
- if (obj == nullptr)
67
- stream.write('\0');
68
- else if (!*obj)
69
- stream.write('\1');
70
- else
71
- stream.write('\2');
72
- }
73
-
74
- template <typename T, typename S>
75
- void to_key_optional(const T* obj, S& stream) {
76
- if constexpr (has_bitwise_serialization<T>() && sizeof(T) == 1) {
77
- if (obj == nullptr)
78
- stream.write("\0", 2);
79
- else {
80
- char buf[1];
81
- fixed_buf_stream tmp_stream(buf, 1);
82
- to_key(*obj, tmp_stream);
83
- stream.write(buf[0]);
84
- if (buf[0] == '\0')
85
- stream.write('\1');
86
- }
87
- } else {
88
- if (obj) {
89
- stream.write('\1');
90
- to_key(*obj, stream);
91
- } else {
92
- stream.write('\0');
93
- }
94
- }
95
- }
96
-
97
- template <typename T, typename U, typename S>
98
- void to_key(const std::pair<T, U>& obj, S& stream) {
99
- to_key(obj.first, stream);
100
- to_key(obj.second, stream);
101
- }
102
-
103
- template <typename T, typename S>
104
- void to_key_range(const T& obj, S& stream) {
105
- for (const auto& elem : obj) { to_key_optional(&elem, stream); }
106
- to_key_optional((decltype(&*std::begin(obj))) nullptr, stream);
107
- }
108
-
109
- template <typename T, typename S>
110
- void to_key(const std::vector<T>& obj, S& stream) {
111
- for (const T& elem : obj) { to_key_optional(&elem, stream); }
112
- to_key_optional((const T*)nullptr, stream);
113
- }
114
-
115
- template <typename S>
116
- void to_key(const std::vector<bool>& obj, S& stream) {
117
- for (bool elem : obj) { to_key_optional(&elem, stream); }
118
- to_key_optional((const bool*)nullptr, stream);
119
- }
120
-
121
- template <typename T, typename S>
122
- void to_key(const std::list<T>& obj, S& stream) {
123
- to_key_range(obj, stream);
124
- }
125
-
126
- template <typename T, typename S>
127
- void to_key(const std::deque<T>& obj, S& stream) {
128
- to_key_range(obj, stream);
129
- }
130
-
131
- template <typename T, typename S>
132
- void to_key(const std::set<T>& obj, S& stream) {
133
- to_key_range(obj, stream);
134
- }
135
-
136
- template <typename T, typename U, typename S>
137
- void to_key(const std::map<T, U>& obj, S& stream) {
138
- to_key_range(obj, stream);
139
- }
140
-
141
- template <typename T, typename S>
142
- void to_key(const std::optional<T>& obj, S& stream) {
143
- to_key_optional(obj ? &*obj : nullptr, stream);
144
- }
145
-
146
- // The first byte holds:
147
- // 0-4 1's (number of additional bytes) 0 (terminator) bits
148
- //
149
- // The number is represented as big-endian using the low order
150
- // bits of the first byte and all of the remaining bytes.
151
- //
152
- // Notes:
153
- // - values must be encoded using the minimum number of bytes,
154
- // as non-canonical representations will break the sort order.
155
- template <typename S>
156
- void to_key_varuint32(std::uint32_t obj, S& stream) {
157
- int num_bytes;
158
- if (obj < 0x80u) {
159
- num_bytes = 1;
160
- } else if (obj < 0x4000u) {
161
- num_bytes = 2;
162
- } else if (obj < 0x200000u) {
163
- num_bytes = 3;
164
- } else if (obj < 0x10000000u) {
165
- num_bytes = 4;
166
- } else {
167
- num_bytes = 5;
168
- }
169
-
170
- stream.write(
171
- static_cast<char>(~(0xFFu >> (num_bytes - 1)) | (num_bytes == 5 ? 0 : (obj >> ((num_bytes - 1) * 8)))));
172
- for (int i = num_bytes - 2; i >= 0; --i) { stream.write(static_cast<char>((obj >> i * 8) & 0xFFu)); }
173
- }
174
-
175
- // for non-negative values
176
- // The first byte holds:
177
- // 1 (signbit) 0-4 1's (number of additional bytes) 0 (terminator) bits
178
- // The value is represented as big endian
179
- // for negative values
180
- // The first byte holds:
181
- // 0 (signbit) 0-4 0's (number of additional bytes) 1 (terminator) bits
182
- // The value is adjusted to be positive based on the range that can
183
- // be represented with this number of bytes and then encoded as big endian.
184
- //
185
- // Notes:
186
- // - negative values must sort before positive values
187
- // - For negative value, numbers that need more bytes are smaller, hence
188
- // the encoding of the width must be opposite the encoding used for
189
- // non-negative values.
190
- // - A 5-byte varint can represent values in $[-2^34, 2^34)$. In this case,
191
- // the argument will be sign-extended.
192
- template <typename S>
193
- void to_key_varint32(std::int32_t obj, S& stream) {
194
- static_assert(std::is_same_v<S, void>, "to_key for varint32 has been temporarily disabled");
195
- int num_bytes;
196
- bool sign = (obj < 0);
197
- if (obj < 0x40 && obj >= -0x40) {
198
- num_bytes = 1;
199
- } else if (obj < 0x2000 && obj >= -0x2000) {
200
- num_bytes = 2;
201
- } else if (obj < 0x100000 && obj >= -0x100000) {
202
- num_bytes = 3;
203
- } else if (obj < 0x08000000 && obj >= -0x08000000) {
204
- num_bytes = 4;
205
- } else {
206
- num_bytes = 5;
207
- }
208
-
209
- unsigned char width_field;
210
- if (sign) {
211
- width_field = 0x80u >> num_bytes;
212
- } else {
213
- width_field = 0x80u | ~(0xFFu >> num_bytes);
214
- }
215
- auto uobj = static_cast<std::uint32_t>(obj);
216
- unsigned char value_mask = (0xFFu >> (num_bytes + 1));
217
- unsigned char high_byte = (num_bytes == 5 ? (sign ? 0xFF : 0) : (uobj >> ((num_bytes - 1) * 8)));
218
- stream.write(width_field | (high_byte & value_mask));
219
- for (int i = num_bytes - 2; i >= 0; --i) { stream.write(static_cast<char>((uobj >> i * 8) & 0xFFu)); }
220
- }
221
-
222
- template <typename... Ts, typename S>
223
- void to_key(const std::variant<Ts...>& obj, S& stream) {
224
- to_key_varuint32(static_cast<uint32_t>(obj.index()), stream);
225
- std::visit([&](const auto& item) { to_key(item, stream); }, obj);
226
- }
227
-
228
- template <typename S>
229
- void to_key(std::string_view obj, S& stream) {
230
- for (char ch : obj) {
231
- stream.write(ch);
232
- if (ch == '\0') {
233
- stream.write('\1');
234
- }
235
- }
236
- stream.write("\0", 2);
237
- }
238
-
239
- template <typename S>
240
- void to_key(const std::string& obj, S& stream) {
241
- to_key(std::string_view(obj), stream);
242
- }
243
-
244
- template <typename S>
245
- void to_key(bool obj, S& stream) {
246
- stream.write(static_cast<char>(obj ? 1 : 0));
247
- }
248
-
249
- template <typename UInt, typename T>
250
- UInt float_to_key(T value) {
251
- static_assert(sizeof(T) == sizeof(UInt), "Expected unsigned int of the same size");
252
- UInt result;
253
- std::memcpy(&result, &value, sizeof(T));
254
- UInt signbit = (static_cast<UInt>(1) << (std::numeric_limits<UInt>::digits - 1));
255
- UInt mask = 0;
256
- if (result == signbit)
257
- result = 0;
258
- if (result & signbit)
259
- mask = ~mask;
260
- return result ^ (mask | signbit);
261
- }
262
-
263
- template <typename T, typename S>
264
- void to_key(const T& obj, S& stream) {
265
- if constexpr (std::is_floating_point_v<T>) {
266
- if constexpr (sizeof(T) == 4) {
267
- to_key(float_to_key<uint32_t>(obj), stream);
268
- } else {
269
- static_assert(sizeof(T) == 8, "Unknown floating point type");
270
- to_key(float_to_key<uint64_t>(obj), stream);
271
- }
272
- } else if constexpr (std::is_integral_v<T>) {
273
- auto v = static_cast<std::make_unsigned_t<T>>(obj);
274
- v -= static_cast<std::make_unsigned_t<T>>(std::numeric_limits<T>::min());
275
- std::reverse(reinterpret_cast<char*>(&v), reinterpret_cast<char*>(&v + 1));
276
- stream.write_raw(v);
277
- } else if constexpr (std::is_enum_v<T>) {
278
- static_assert(!std::is_convertible_v<T, std::underlying_type_t<T>>, "Serializing unscoped enum");
279
- to_key(static_cast<std::underlying_type_t<T>>(obj), stream);
280
- } else {
281
- eosio::for_each_field(obj, [&](const auto& member) {
282
- to_key(member, stream);
283
- });
284
- }
285
- }
286
-
287
- template <typename T>
288
- void convert_to_key(const T& t, std::vector<char>& bin) {
289
- size_stream ss;
290
- to_key(t, ss);
291
- auto orig_size = bin.size();
292
- bin.resize(orig_size + ss.size);
293
- fixed_buf_stream fbs(bin.data() + orig_size, ss.size);
294
- to_key(t, fbs);
295
- check( fbs.pos == fbs.end, convert_stream_error(stream_error::underrun) );
296
- }
297
-
298
- template <typename T>
299
- std::vector<char> convert_to_key(const T& t) {
300
- std::vector<char> result;
301
- convert_to_key(t, result);
302
- return result;
303
- }
304
-
305
- } // namespace eosio