Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
flat.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <boost/container/flat_map.hpp>
6#include <boost/container/flat_set.hpp>
7#include <fc/crypto/hex.hpp>
8
9namespace fc {
10
11 namespace raw {
12
13 template<typename Stream, typename T, typename A>
14 void pack( Stream& s, const boost::container::vector<T, A>& value ) {
16 pack( s, unsigned_int((uint32_t)value.size()) );
17 if( !std::is_fundamental<T>::value ) {
18 for( const auto& item : value ) {
19 pack( s, item );
20 }
21 } else if( value.size() ) {
22 s.write( (const char*)value.data(), value.size() );
23 }
24 }
25
26 template<typename Stream, typename T, typename A>
27 void unpack( Stream& s, boost::container::vector<T, A>& value ) {
28 unsigned_int size;
29 unpack( s, size );
31 value.clear();
32 value.resize( size.value );
33 if( !std::is_fundamental<T>::value ) {
34 for( auto& item : value ) {
35 unpack( s, item );
36 }
37 } else if( value.size() ) {
38 s.read( (char*)value.data(), value.size() );
39 }
40 }
41
42 template<typename Stream, typename A>
43 void pack( Stream& s, const boost::container::vector<char, A>& value ) {
45 pack( s, unsigned_int((uint32_t)value.size()) );
46 if( value.size() )
47 s.write( (const char*)value.data(), value.size() );
48 }
49
50 template<typename Stream, typename A>
51 void unpack( Stream& s, boost::container::vector<char, A>& value ) {
52 unsigned_int size;
53 unpack( s, size );
55 value.clear();
56 value.resize( size.value );
57 if( value.size() )
58 s.read( (char*)value.data(), value.size() );
59 }
60
61 template<typename Stream, typename T, typename... U>
62 void pack( Stream& s, const flat_set<T, U...>& value ) {
64 }
65
66 template<typename Stream, typename T, typename... U>
67 void unpack( Stream& s, flat_set<T, U...>& value ) {
69 }
70
71 template<typename Stream, typename T, typename... U>
72 void pack( Stream& s, const flat_multiset<T, U...>& value ) {
74 }
75
76 template<typename Stream, typename T, typename... U>
77 void unpack( Stream& s, flat_multiset<T, U...>& value ) {
79 }
80
81 template<typename Stream, typename K, typename V, typename... U>
82 void pack( Stream& s, const flat_map<K, V, U...>& value ) {
84 }
85
86 template<typename Stream, typename K, typename V, typename... U>
87 void unpack( Stream& s, flat_map<K, V, U...>& value ) {
89 }
90
91 template<typename Stream, typename K, typename V, typename... U>
92 void pack( Stream& s, const flat_multimap<K, V, U...>& value ) {
94 }
95
96 template<typename Stream, typename K, typename V, typename... U>
97 void unpack( Stream& s, flat_multimap<K, V, U...>& value ) {
99 }
100
101 } // namespace raw
102
103 template<typename T, typename... U>
104 void to_variant( const boost::container::vector<T, U...>& vec, fc::variant& vo ) {
105 FC_ASSERT( vec.size() <= MAX_NUM_ARRAY_ELEMENTS );
106 variants vars;
107 vars.reserve( vec.size() );
108 for( const auto& item : vec ) {
109 vars.emplace_back( item );
110 }
111 vo = std::move(vars);
112 }
113
114 template<typename T, typename... U>
115 void from_variant( const fc::variant& v, boost::container::vector<T, U...>& vec ) {
116 const variants& vars = v.get_array();
117 FC_ASSERT( vars.size() <= MAX_NUM_ARRAY_ELEMENTS );
118 vec.clear();
119 vec.resize( vars.size() );
120 for( uint32_t i = 0; i < vars.size(); ++i ) {
121 from_variant( vars[i], vec[i] );
122 }
123 }
124
125 template<typename... U>
126 void to_variant( const boost::container::vector<char, U...>& vec, fc::variant& vo ) {
127 FC_ASSERT( vec.size() <= MAX_SIZE_OF_BYTE_ARRAYS );
128 if( vec.size() )
129 vo = variant( fc::to_hex( vec.data(), vec.size() ) );
130 else
131 vo = "";
132 }
133
134 template<typename... U>
135 void from_variant( const fc::variant& v, boost::container::vector<char, U...>& vec )
136 {
137 const auto& str = v.get_string();
138 FC_ASSERT( str.size() <= 2*MAX_SIZE_OF_BYTE_ARRAYS ); // Doubled because hex strings needs two characters per byte
139 vec.resize( str.size() / 2 );
140 if( vec.size() ) {
141 size_t r = fc::from_hex( str, vec.data(), vec.size() );
142 FC_ASSERT( r == vec.size() );
143 }
144 }
145
146 template<typename T, typename... U>
147 void to_variant( const flat_set< T, U... >& s, fc::variant& vo ) {
149 }
150
151 template<typename T, typename... U>
152 void from_variant( const fc::variant& v, flat_set< T, U... >& s ) {
154 }
155
156 template<typename T, typename... U>
157 void to_variant( const flat_multiset< T, U... >& s, fc::variant& vo ) {
159 }
160
161 template<typename T, typename... U>
162 void from_variant( const fc::variant& v, flat_multiset< T, U... >& s ) {
164 }
165
166 template<typename K, typename V, typename... U >
167 void to_variant( const flat_map< K, V, U... >& m, fc::variant& vo ) {
169 }
170
171 template<typename K, typename V, typename... U>
172 void from_variant( const variant& v, flat_map<K, V, U...>& m ) {
174 }
175
176 template<typename K, typename V, typename... U >
177 void to_variant( const flat_multimap< K, V, U... >& m, fc::variant& vo ) {
179 }
180
181 template<typename K, typename V, typename... U>
182 void from_variant( const variant& v, flat_multimap<K, V, U...>& m ) {
184 }
185
186}
const mie::Vuint & r
Definition bn.cpp:28
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
const string & get_string() const
Definition variant.cpp:606
variants & get_array()
Definition variant.cpp:496
Concept for reading and writing characters.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
const uint64 K
Definition make_512.cpp:78
void to_variant_from_set(const Set< T, U... > &s, fc::variant &vo)
void from_variant_to_flat_set(const fc::variant &v, Set< T, U... > &s)
void from_variant_to_flat_map(const variant &v, Map< K, V, U... > &m)
void to_variant_from_map(const Map< K, V, U... > &m, fc::variant &vo)
void pack_map(Stream &s, const Map< K, V, U... > &value)
void pack_set(Stream &s, const Set< T, U... > &value)
void unpack_flat_map(Stream &s, Map< K, V, U... > &value)
void unpack_flat_set(Stream &s, Set< T, U... > &value)
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
namespace sysio::chain
Definition authority.cpp:3
uint8_t from_hex(char c)
Definition hex.cpp:6
std::vector< fc::variant > variants
Definition variant.hpp:173
fc::string to_hex(const char *d, uint32_t s)
Definition hex.cpp:17
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
#define value
Definition pkcs11.h:157
#define T(meth, val, expected)
unsigned int uint32_t
Definition stdint.h:126
Definition test_zm.cpp:19
uint32_t value
Definition varint.hpp:17
size_t size() const
Definition zm.h:519
Definition dtoa.c:306
#define MAX_SIZE_OF_BYTE_ARRAYS
Definition utility.hpp:19
#define MAX_NUM_ARRAY_ELEMENTS
Definition utility.hpp:18
char * s