8#include <boost/multiprecision/cpp_int.hpp>
17template <
typename Storage,
typename Enable =
void>
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*>>> {
31 :_start(start),_pos(start),_end(start+
s){};
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 ) {
44 inline bool write(
const char* d,
size_t s ) {
45 if(
size_t(_end - _pos) >= (
size_t)
s ) {
53 inline bool put(
char c) {
62 inline bool get(
unsigned char& c ) {
return get( *(
char*)&c ); }
63 inline bool get(
char& c )
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; }
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; }
99template <
typename Streambuf>
100class datastream<Streambuf, typename
std::enable_if_t<std::is_base_of_v<std::streambuf, Streambuf>>> {
105 template <
typename... Args>
107 :
buf(
std::forward<Args>(args)...) {}
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; }
118 buf.pubseekoff(off, std::ios::beg);
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>)>> {
131 Container _container;
135 template <
typename... Args>
137 : _container(
std::forward<Args>(args)...)
141 if (cur + n > _container.size()) {
143 "read datastream<std::vector<char>> of length ${len} over by ${over}",
144 (
"len", _container.size())(
"over", _container.size() - n));
146 std::copy_n(_container.begin() + cur, n,
s);
152 _container.resize(std::max(cur + n, _container.size()));
153 std::copy_n(
s, n, _container.begin() + cur);
163 size_t tellp()
const {
return cur; }
164 bool skip(
size_t p) { cur +=
p;
return true; }
171 size_t remaining()
const {
return _container.size() - cur; }
174 const Container&
storage()
const {
return _container; }
180inline datastream<ST>&
operator<<(datastream<ST>& ds,
const __int128& d) {
181 ds.write( (
const char*)&d,
sizeof(d) );
185template<
typename ST,
typename DATA>
187 ds.read((
char*)&
d,
sizeof(
d) );
192inline datastream<ST>&
operator<<(datastream<ST>& ds,
const unsigned __int128& d) {
193 ds.write( (
const char*)&d,
sizeof(d) );
197template<
typename ST,
typename DATA>
199 ds.read((
char*)&
d,
sizeof(
d) );
205 ds.write( (
const char*)&d,
sizeof(d) );
209template<
typename ST,
typename DATA>
211 ds.read((
char*)&
d,
sizeof(
d) );
217 ds.write( (
const char*)&d,
sizeof(d) );
221template<
typename ST,
typename DATA>
223 ds.read((
char*)&
d,
sizeof(
d) );
229 ds.write( (
const char*)&d,
sizeof(d) );
233template<
typename ST,
typename DATA>
235 ds.read((
char*)&
d,
sizeof(
d) );
241 ds.write( (
const char*)&d,
sizeof(d) );
245template<
typename ST,
typename DATA>
247 ds.read((
char*)&
d,
sizeof(
d) );
253 ds.write( (
const char*)&d,
sizeof(d) );
257template<
typename ST,
typename DATA>
259 ds.read((
char*)&
d,
sizeof(
d) );
265 ds.write( (
const char*)&d,
sizeof(d) );
269template<
typename ST,
typename DATA>
271 ds.read((
char*)&
d,
sizeof(
d) );
276 ds.write( (
const char*)&d,
sizeof(d) );
280template<
typename ST,
typename DATA>
282 ds.read((
char*)&
d,
sizeof(
d) );
287 ds.write( (
const char*)&d,
sizeof(d) );
291template<
typename ST,
typename DATA>
293 ds.read((
char*)&
d,
sizeof(
d) );
datastream(Args &&... args)
const Container & storage() const
size_t write(const char *s, size_t n)
size_t read(char *s, size_t n)
size_t read(char *data, size_t n)
datastream(Args &&... args)
const Streambuf & storage() const
size_t write(const char *data, size_t n)
bool write(const char *d, size_t s)
datastream(T start, size_t s)
bool read(char *d, size_t s)
bool get(unsigned char &c)
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)
datastream< ST > & operator<<(datastream< ST > &s, const sysio::chain::may_not_exist< T > &v)
datastream< ST > & operator>>(datastream< ST > &s, sysio::chain::may_not_exist< T > &v)
#define T(meth, val, expected)
unsigned __int64 uint64_t
memcpy((char *) pInfo->slotDescription, s, l)