Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
database_utils.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <fc/io/raw.hpp>
5#include <softfloat.hpp>
6
7namespace sysio { namespace chain {
8
9 template<typename ...Indices>
10 class index_set;
11
12 template<typename Index>
14 public:
15 using index_t = Index;
16
17 template<typename F>
18 static void walk( const chainbase::database& db, F function ) {
19 auto const& index = db.get_index<Index>().indices();
20 const auto& first = index.begin();
21 const auto& last = index.end();
22 for (auto itr = first; itr != last; ++itr) {
23 function(*itr);
24 }
25 }
26
27 template<typename Secondary, typename Key, typename F>
28 static void walk_range( const chainbase::database& db, const Key& begin_key, const Key& end_key, F function ) {
29 const auto& idx = db.get_index<Index, Secondary>();
30 auto begin_itr = idx.lower_bound(begin_key);
31 auto end_itr = idx.lower_bound(end_key);
32 for (auto itr = begin_itr; itr != end_itr; ++itr) {
33 function(*itr);
34 }
35 }
36
37 template<typename Secondary, typename Key>
38 static size_t size_range( const chainbase::database& db, const Key& begin_key, const Key& end_key ) {
39 const auto& idx = db.get_index<Index, Secondary>();
40 auto begin_itr = idx.lower_bound(begin_key);
41 auto end_itr = idx.lower_bound(end_key);
42 size_t res = 0;
43 while (begin_itr != end_itr) {
44 res++; ++begin_itr;
45 }
46 return res;
47 }
48
49 template<typename F>
50 static void create( chainbase::database& db, F cons ) {
51 db.create<typename index_t::value_type>(cons);
52 }
53 };
54
55 template<typename Index>
56 class index_set<Index> {
57 public:
58 static void add_indices( chainbase::database& db ) {
59 db.add_index<Index>();
60 }
61
62 template<typename F>
63 static void walk_indices( F function ) {
64 function( index_utils<Index>() );
65 }
66 };
67
68 template<typename FirstIndex, typename ...RemainingIndices>
69 class index_set<FirstIndex, RemainingIndices...> {
70 public:
75
76 template<typename F>
77 static void walk_indices( F function ) {
80 }
81 };
82
83 template<typename DataStream>
84 DataStream& operator << ( DataStream& ds, const shared_blob& b ) {
85 fc::raw::pack(ds, static_cast<const shared_string&>(b));
86 return ds;
87 }
88
89 template<typename DataStream>
90 DataStream& operator >> ( DataStream& ds, shared_blob& b ) {
91 fc::raw::unpack(ds, static_cast<shared_string &>(b));
92 return ds;
93 }
94} }
95
96namespace fc {
97
98 // overloads for to/from_variant
99 template<typename OidType>
101 v = variant(oid._id);
102 }
103
104 template<typename OidType>
106 from_variant(v, oid._id);
107 }
108
109 inline
110 void float64_to_double (const float64_t& f, double& d) {
111 memcpy(&d, &f, sizeof(d));
112 }
113
114 inline
115 void double_to_float64 (const double& d, float64_t& f) {
116 memcpy(&f, &d, sizeof(f));
117 }
118
119 inline
121 memcpy(&u, &f, sizeof(u));
122 }
123
124 inline
126 memcpy(&f, &u, sizeof(f));
127 }
128
129 inline
130 void to_variant( const float64_t& f, variant& v ) {
131 double double_f;
132 float64_to_double(f, double_f);
133 v = variant(double_f);
134 }
135
136 inline
137 void from_variant( const variant& v, float64_t& f ) {
138 double double_f;
139 from_variant(v, double_f);
140 double_to_float64(double_f, f);
141 }
142
143 inline
144 void to_variant( const float128_t& f, variant& v ) {
145 // Assumes platform is little endian and hex representation of 128-bit integer is in little endian order.
146 char as_bytes[sizeof(sysio::chain::uint128_t)];
147 memcpy(as_bytes, &f, sizeof(as_bytes));
148 std::string s = "0x";
149 s.append( to_hex( as_bytes, sizeof(as_bytes) ) );
150 v = s;
151 }
152
153 inline
154 void from_variant( const variant& v, float128_t& f ) {
155 // Temporarily hold the binary in uint128_t before casting it to float128_t
156 char temp[sizeof(sysio::chain::uint128_t)];
157 memset(temp, 0, sizeof(temp));
158 auto s = v.as_string();
159 FC_ASSERT( s.size() == 2 + 2 * sizeof(temp) && s.find("0x") == 0, "Failure in converting hex data into a float128_t");
160 auto sz = from_hex( s.substr(2), temp, sizeof(temp) );
161 // Assumes platform is little endian and hex representation of 128-bit integer is in little endian order.
162 FC_ASSERT( sz == sizeof(temp), "Failure in converting hex data into a float128_t" );
163 memcpy(&f, temp, sizeof(f));
164 }
165
166 inline
168 v = variant(std::string(s.begin(), s.end()));
169 }
170
171 inline
173 string _s;
174 from_variant(v, _s);
175 s = sysio::chain::shared_string(_s.begin(), _s.end(), s.get_allocator());
176 }
177
178 inline
180 v = variant(base64_encode(b.data(), b.size()));
181 }
182
183 inline
185 string _s = base64_decode(v.as_string());
186 b = sysio::chain::shared_blob(_s.begin(), _s.end(), b.get_allocator());
187 }
188
189 inline
190 void to_variant( const blob& b, variant& v ) {
191 v = variant(base64_encode(b.data.data(), b.data.size()));
192 }
193
194 inline
195 void from_variant( const variant& v, blob& b ) {
196 string _s = base64_decode(v.as_string());
197 b.data = std::vector<char>(_s.begin(), _s.end());
198 }
199
200 template<typename T>
202 to_variant(std::vector<T>(sv.begin(), sv.end()), v);
203 }
204
205 template<typename T>
207 std::vector<T> _v;
208 from_variant(v, _v);
209 sv = sysio::chain::shared_vector<T>(_v.begin(), _v.end(), sv.get_allocator());
210 }
211}
212
213namespace chainbase {
214 // overloads for OID packing
215 template<typename DataStream, typename OidType>
216 DataStream& operator << ( DataStream& ds, const oid<OidType>& oid ) {
217 fc::raw::pack(ds, oid._id);
218 return ds;
219 }
220
221 template<typename DataStream, typename OidType>
222 DataStream& operator >> ( DataStream& ds, oid<OidType>& oid ) {
224 return ds;
225 }
226}
227
228// overloads for softfloat packing
229template<typename DataStream>
230DataStream& operator << ( DataStream& ds, const float64_t& v ) {
231 double double_v;
232 fc::float64_to_double(v, double_v);
233 fc::raw::pack(ds, double_v);
234 return ds;
235}
236
237template<typename DataStream>
238DataStream& operator >> ( DataStream& ds, float64_t& v ) {
239 double double_v;
240 fc::raw::unpack(ds, double_v);
241 fc::double_to_float64(double_v, v);
242 return ds;
243}
244
245template<typename DataStream>
246DataStream& operator << ( DataStream& ds, const float128_t& v ) {
247 sysio::chain::uint128_t uint128_v;
248 fc::float128_to_uint128(v, uint128_v);
249 fc::raw::pack(ds, uint128_v);
250 return ds;
251}
252
253template<typename DataStream>
254DataStream& operator >> ( DataStream& ds, float128_t& v ) {
255 sysio::chain::uint128_t uint128_v;
256 fc::raw::unpack(ds, uint128_v);
257 fc::uint128_to_float128(uint128_v, v);
258 return ds;
259}
const generic_index< MultiIndexType > & get_index() const
const ObjectType & create(Constructor &&con)
const allocator_type & get_allocator() const
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
string as_string() const
Definition variant.cpp:469
static void add_indices(chainbase::database &db)
static void walk_indices(F function)
static void walk(const chainbase::database &db, F function)
static void create(chainbase::database &db, F cons)
static void walk_range(const chainbase::database &db, const Key &begin_key, const Key &end_key, F function)
static size_t size_range(const chainbase::database &db, const Key &begin_key, const Key &end_key)
DataStream & operator<<(DataStream &ds, const float64_t &v)
DataStream & operator>>(DataStream &ds, float64_t &v)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
DataStream & operator<<(DataStream &ds, const oid< OidType > &oid)
DataStream & operator>>(DataStream &ds, oid< OidType > &oid)
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
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
Definition base64.cpp:92
void uint128_to_float128(const sysio::chain::uint128_t &u, float128_t &f)
uint8_t from_hex(char c)
Definition hex.cpp:6
void double_to_float64(const double &d, float64_t &f)
fc::string to_hex(const char *d, uint32_t s)
Definition hex.cpp:17
void float128_to_uint128(const float128_t &f, sysio::chain::uint128_t &u)
datastream< ST > & operator<<(datastream< ST > &s, const sysio::chain::may_not_exist< T > &v)
Definition abi_def.hpp:146
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
datastream< ST > & operator>>(datastream< ST > &s, sysio::chain::may_not_exist< T > &v)
Definition abi_def.hpp:152
std::string base64_decode(const std::string &encoded_string)
Definition base64.cpp:152
void float64_to_double(const float64_t &f, double &d)
unsigned __int128 uint128_t
Definition types.hpp:242
boost::interprocess::vector< T, allocator< T > > shared_vector
Definition types.hpp:85
chainbase::shared_string shared_string
Definition types.hpp:83
std::vector< char > data
Definition variant.hpp:44
CK_ULONG d
char * s
memset(pInfo->slotDescription, ' ', 64)
memcpy((char *) pInfo->slotDescription, s, l)