BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface 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 
datastream(T start, size_t s)
Definition: datastream.hpp:22
size_t remaining() const
Definition: datastream.hpp:86
bool seekp(size_t p)
Definition: datastream.hpp:67
size_t tellp() const
Definition: datastream.hpp:85
void skip(size_t s)
Definition: datastream.hpp:26
bool write(const char *, size_t s)
Definition: datastream.hpp:81
bool put(char c)
Definition: datastream.hpp:45
bool write(const char *d, size_t s)
Definition: datastream.hpp:36
bool read(char *d, size_t s)
Definition: datastream.hpp:27
void throw_datastream_range_error(const char *file, size_t len, int64_t over)
Definition: datastream.cpp:4
bool valid() const
Definition: datastream.hpp:66
T pos() const
Definition: datastream.hpp:65
size_t tellp() const
Definition: datastream.hpp:68
bool seekp(size_t p)
Definition: datastream.hpp:84
Definition: api.hpp:15
size_t remaining() const
Definition: datastream.hpp:69
datastream(size_t init_size=0)
Definition: datastream.hpp:79