BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
datastream.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <string.h>
3 #include <stdint.h>
4 
5 namespace fc {
6 
7 namespace detail
8 {
9  [[noreturn]] void throw_datastream_range_error( const char* file, size_t len, int64_t over );
10 }
11 
19 template<typename T>
20 class datastream {
21  public:
22  datastream( T start, size_t s )
23  :_start(start),_pos(start),_end(start+s){};
24 
25 
26  inline void skip( size_t s ){ _pos += s; }
27  inline bool read( char* d, size_t s ) {
28  if( size_t(_end - _pos) >= (size_t)s ) {
29  memcpy( d, _pos, s );
30  _pos += s;
31  return true;
32  }
33  detail::throw_datastream_range_error( "read", _end-_start, int64_t(-((_end-_pos) - 1)));
34  }
35 
36  inline bool write( const char* d, size_t s ) {
37  if( _end - _pos >= (int32_t)s ) {
38  memcpy( _pos, d, s );
39  _pos += s;
40  return true;
41  }
42  detail::throw_datastream_range_error( "write", _end-_start, int64_t(-((_end-_pos) - 1)));
43  }
44 
45  inline bool put(char c) {
46  if( _pos < _end ) {
47  *_pos = c;
48  ++_pos;
49  return true;
50  }
51  detail::throw_datastream_range_error( "put", _end-_start, int64_t(-((_end-_pos) - 1)));
52  }
53 
54  inline bool get( unsigned char& c ) { return get( *(char*)&c ); }
55  inline bool get( char& c )
56  {
57  if( _pos < _end ) {
58  c = *_pos;
59  ++_pos;
60  return true;
61  }
62  detail::throw_datastream_range_error( "get", _end-_start, int64_t(-((_end-_pos) - 1)));
63  }
64 
65  T pos()const { return _pos; }
66  inline bool valid()const { return _pos <= _end && _pos >= _start; }
67  inline bool seekp(size_t p) { _pos = _start + p; return _pos <= _end; }
68  inline size_t tellp()const { return _pos - _start; }
69  inline size_t remaining()const { return _end - _pos; }
70  private:
71  T _start;
72  T _pos;
73  T _end;
74 };
75 
76 template<>
77 class datastream<size_t> {
78  public:
79  datastream( size_t init_size = 0):_size(init_size){};
80  inline bool skip( size_t s ) { _size += s; return true; }
81  inline bool write( const char* ,size_t s ) { _size += s; return true; }
82  inline bool put(char ) { ++_size; return true; }
83  inline bool valid()const { return true; }
84  inline bool seekp(size_t p) { _size = p; return true; }
85  inline size_t tellp()const { return _size; }
86  inline size_t remaining()const { return 0; }
87  private:
88  size_t _size;
89 };
90 
91 } // namespace fc
92 
fc::datastream::seekp
bool seekp(size_t p)
Definition: datastream.hpp:67
fc::datastream::remaining
size_t remaining() const
Definition: datastream.hpp:69
fc::datastream::get
bool get(unsigned char &c)
Definition: datastream.hpp:54
fc::datastream::put
bool put(char c)
Definition: datastream.hpp:45
fc::datastream::valid
bool valid() const
Definition: datastream.hpp:66
fc::datastream< size_t >::tellp
size_t tellp() const
Definition: datastream.hpp:85
fc
Definition: api.hpp:15
fc::datastream
Definition: datastream.hpp:20
fc::datastream< size_t >::write
bool write(const char *, size_t s)
Definition: datastream.hpp:81
fc::datastream::skip
void skip(size_t s)
Definition: datastream.hpp:26
fc::datastream< size_t >::skip
bool skip(size_t s)
Definition: datastream.hpp:80
fc::datastream::pos
T pos() const
Definition: datastream.hpp:65
fc::datastream< size_t >::datastream
datastream(size_t init_size=0)
Definition: datastream.hpp:79
fc::detail::throw_datastream_range_error
void throw_datastream_range_error(const char *file, size_t len, int64_t over)
Definition: datastream.cpp:4
fc::datastream::write
bool write(const char *d, size_t s)
Definition: datastream.hpp:36
fc::datastream< size_t >::put
bool put(char)
Definition: datastream.hpp:82
fc::datastream::datastream
datastream(T start, size_t s)
Definition: datastream.hpp:22
fc::datastream::get
bool get(char &c)
Definition: datastream.hpp:55
fc::datastream::read
bool read(char *d, size_t s)
Definition: datastream.hpp:27
fc::datastream< size_t >::remaining
size_t remaining() const
Definition: datastream.hpp:86
fc::datastream< size_t >::seekp
bool seekp(size_t p)
Definition: datastream.hpp:84
fc::datastream< size_t >::valid
bool valid() const
Definition: datastream.hpp:83
fc::datastream::tellp
size_t tellp() const
Definition: datastream.hpp:68