Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
datastream.hpp
Go to the documentation of this file.
1#pragma once
2#include <fc/utility.hpp>
4#include <string.h>
5#include <stdint.h>
6#include <type_traits>
7
8#include <boost/multiprecision/cpp_int.hpp>
9
10namespace fc {
11
12namespace detail
13{
14 NO_RETURN void throw_datastream_range_error( const char* file, size_t len, int64_t over );
15}
16
17template <typename Storage, typename Enable = void>
19
27template<typename T>
28class datastream<T, std::enable_if_t<std::is_same_v<T, char*> || std::is_same_v<T, const char*> || std::is_same_v<T, const unsigned char*>>> {
29 public:
30 datastream( T start, size_t s )
31 :_start(start),_pos(start),_end(start+s){};
32
33
34 inline void skip( size_t s ){ _pos += s; }
35 inline bool read( char* d, size_t s ) {
36 if( size_t(_end - _pos) >= (size_t)s ) {
37 memcpy( d, _pos, s );
38 _pos += s;
39 return true;
40 }
41 detail::throw_datastream_range_error( "read", _end-_start, int64_t(-((_end-_pos) - 1)));
42 }
43
44 inline bool write( const char* d, size_t s ) {
45 if( size_t(_end - _pos) >= (size_t)s ) {
46 memcpy( _pos, d, s );
47 _pos += s;
48 return true;
49 }
50 detail::throw_datastream_range_error( "write", _end-_start, int64_t(-((_end-_pos) - 1)));
51 }
52
53 inline bool put(char c) {
54 if( _pos < _end ) {
55 *_pos = c;
56 ++_pos;
57 return true;
58 }
59 detail::throw_datastream_range_error( "put", _end-_start, int64_t(-((_end-_pos) - 1)));
60 }
61
62 inline bool get( unsigned char& c ) { return get( *(char*)&c ); }
63 inline bool get( char& c )
64 {
65 if( _pos < _end ) {
66 c = *_pos;
67 ++_pos;
68 return true;
69 }
70 detail::throw_datastream_range_error( "get", _end-_start, int64_t(-((_end-_pos) - 1)));
71 }
72
73 T pos()const { return _pos; }
74 inline bool valid()const { return _pos <= _end && _pos >= _start; }
75 inline bool seekp(size_t p) { _pos = _start + p; return _pos <= _end; }
76 inline size_t tellp()const { return _pos - _start; }
77 inline size_t remaining()const { return _end - _pos; }
78 private:
79 T _start;
80 T _pos;
81 T _end;
82};
83
84template<>
85class datastream<size_t, void> {
86 public:
87 datastream( size_t init_size = 0):_size(init_size){};
88 inline bool skip( size_t s ) { _size += s; return true; }
89 inline bool write( const char* ,size_t s ) { _size += s; return true; }
90 inline bool put(char ) { ++_size; return true; }
91 inline bool valid()const { return true; }
92 inline bool seekp(size_t p) { _size = p; return true; }
93 inline size_t tellp()const { return _size; }
94 inline size_t remaining()const { return 0; }
95 private:
96 size_t _size;
97};
98
99template <typename Streambuf>
100class datastream<Streambuf, typename std::enable_if_t<std::is_base_of_v<std::streambuf, Streambuf>>> {
101 private:
102 Streambuf buf;
103
104 public:
105 template <typename... Args>
106 datastream(Args&&... args)
107 : buf(std::forward<Args>(args)...) {}
108
109 size_t read(char* data, size_t n) { return buf.sgetn(data, n); }
110 size_t write(const char* data, size_t n) { return buf.sputn(data, n); }
111 size_t tellp() { return this->pubseekoff(0, std::ios::cur); }
112 bool skip(size_t p) { this->pubseekoff(p, std::ios::cur); return true; }
113 bool get(char& c) {
114 c = buf.sbumpc();
115 return true;
116 }
117 bool seekp(size_t off) {
118 buf.pubseekoff(off, std::ios::beg);
119 return true;
120 }
121 bool remaining() { return buf.in_avail(); }
122
123 Streambuf& storage() { return buf; }
124 const Streambuf& storage() const { return buf; }
125};
126
127template <typename Container>
128class datastream<Container, typename std::enable_if_t<(std::is_same_v<std::vector<char>, Container> ||
129 std::is_same_v<std::deque<char>, Container>)>> {
130 private:
131 Container _container;
132 size_t cur;
133
134 public:
135 template <typename... Args>
136 datastream(Args&&... args)
137 : _container(std::forward<Args>(args)...)
138 , cur(0) {}
139
140 size_t read(char* s, size_t n) {
141 if (cur + n > _container.size()) {
142 FC_THROW_EXCEPTION(out_of_range_exception,
143 "read datastream<std::vector<char>> of length ${len} over by ${over}",
144 ("len", _container.size())("over", _container.size() - n));
145 }
146 std::copy_n(_container.begin() + cur, n, s);
147 cur += n;
148 return n;
149 }
150
151 size_t write(const char* s, size_t n) {
152 _container.resize(std::max(cur + n, _container.size()));
153 std::copy_n(s, n, _container.begin() + cur);
154 cur += n;
155 return n;
156 }
157
158 bool seekp(size_t off) {
159 cur = off;
160 return true;
161 }
162
163 size_t tellp() const { return cur; }
164 bool skip(size_t p) { cur += p; return true; }
165
166 bool get(char& c) {
167 this->read(&c, 1);
168 return true;
169 }
170
171 size_t remaining() const { return _container.size() - cur; }
172
173 Container& storage() { return _container; }
174 const Container& storage() const { return _container; }
175};
176
177
178
179template<typename ST>
180inline datastream<ST>& operator<<(datastream<ST>& ds, const __int128& d) {
181 ds.write( (const char*)&d, sizeof(d) );
182 return ds;
183}
184
185template<typename ST, typename DATA>
186inline datastream<ST>& operator>>(datastream<ST>& ds, __int128& d) {
187 ds.read((char*)&d, sizeof(d) );
188 return ds;
189}
190
191template<typename ST>
192inline datastream<ST>& operator<<(datastream<ST>& ds, const unsigned __int128& d) {
193 ds.write( (const char*)&d, sizeof(d) );
194 return ds;
195}
196
197template<typename ST, typename DATA>
198inline datastream<ST>& operator>>(datastream<ST>& ds, unsigned __int128& d) {
199 ds.read((char*)&d, sizeof(d) );
200 return ds;
201}
202
203template<typename ST>
204inline datastream<ST>& operator<<(datastream<ST>& ds, const int64_t& d) {
205 ds.write( (const char*)&d, sizeof(d) );
206 return ds;
207}
208
209template<typename ST, typename DATA>
211 ds.read((char*)&d, sizeof(d) );
212 return ds;
213}
214
215template<typename ST>
216inline datastream<ST>& operator<<(datastream<ST>& ds, const uint64_t& d) {
217 ds.write( (const char*)&d, sizeof(d) );
218 return ds;
219}
220
221template<typename ST, typename DATA>
223 ds.read((char*)&d, sizeof(d) );
224 return ds;
225}
226
227template<typename ST>
228inline datastream<ST>& operator<<(datastream<ST>& ds, const int32_t& d) {
229 ds.write( (const char*)&d, sizeof(d) );
230 return ds;
231}
232
233template<typename ST, typename DATA>
235 ds.read((char*)&d, sizeof(d) );
236 return ds;
237}
238
239template<typename ST>
240inline datastream<ST>& operator<<(datastream<ST>& ds, const uint32_t& d) {
241 ds.write( (const char*)&d, sizeof(d) );
242 return ds;
243}
244
245template<typename ST, typename DATA>
247 ds.read((char*)&d, sizeof(d) );
248 return ds;
249}
250
251template<typename ST>
252inline datastream<ST>& operator<<(datastream<ST>& ds, const int16_t& d) {
253 ds.write( (const char*)&d, sizeof(d) );
254 return ds;
255}
256
257template<typename ST, typename DATA>
259 ds.read((char*)&d, sizeof(d) );
260 return ds;
261}
262
263template<typename ST>
264inline datastream<ST>& operator<<(datastream<ST>& ds, const uint16_t& d) {
265 ds.write( (const char*)&d, sizeof(d) );
266 return ds;
267}
268
269template<typename ST, typename DATA>
271 ds.read((char*)&d, sizeof(d) );
272 return ds;
273}
274template<typename ST>
275inline datastream<ST>& operator<<(datastream<ST>& ds, const int8_t& d) {
276 ds.write( (const char*)&d, sizeof(d) );
277 return ds;
278}
279
280template<typename ST, typename DATA>
282 ds.read((char*)&d, sizeof(d) );
283 return ds;
284}
285template<typename ST>
286inline datastream<ST>& operator<<(datastream<ST>& ds, const uint8_t& d) {
287 ds.write( (const char*)&d, sizeof(d) );
288 return ds;
289}
290
291template<typename ST, typename DATA>
293 ds.read((char*)&d, sizeof(d) );
294 return ds;
295}
296/*
297template<typename ST, typename T>
298inline datastream<ST>& operator<<(datastream<ST>& ds, const boost::multiprecision::number<T>& n) {
299 unsigned char data[(std::numeric_limits<decltype(n)>::digits+1)/8];
300 ds.read( (char*)data, sizeof(data) );
301 boost::multiprecision::import_bits( n, data, data + sizeof(data), 1 );
302}
303
304template<typename ST, typename T>
305inline datastream<ST>& operator>>(datastream<ST>& ds, boost::multiprecision::number<T>& n) {
306 unsigned char data[(std::numeric_limits<decltype(n)>::digits+1)/8];
307 boost::multiprecision::export_bits( n, data, 1 );
308 ds.write( (const char*)data, sizeof(data) );
309}
310*/
311
312
313} // namespace fc
const mie::Vuint & p
Definition bn.cpp:27
datastream(size_t init_size=0)
bool write(const char *, size_t s)
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
NO_RETURN void throw_datastream_range_error(const char *file, size_t len, int64_t over)
Definition datastream.cpp:4
namespace sysio::chain
Definition authority.cpp:3
datastream< ST > & operator<<(datastream< ST > &s, const sysio::chain::may_not_exist< T > &v)
Definition abi_def.hpp:146
datastream< ST > & operator>>(datastream< ST > &s, sysio::chain::may_not_exist< T > &v)
Definition abi_def.hpp:152
Definition name.hpp:106
#define T(meth, val, expected)
signed short int16_t
Definition stdint.h:122
unsigned short uint16_t
Definition stdint.h:125
signed __int64 int64_t
Definition stdint.h:135
unsigned int uint32_t
Definition stdint.h:126
signed int int32_t
Definition stdint.h:123
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
signed char int8_t
Definition stdint.h:121
#define NO_RETURN
Definition utility.hpp:15
CK_ULONG d
char * s
size_t len
uint8_t buf[2048]
memcpy((char *) pInfo->slotDescription, s, l)