dcrud  0.0.0
Distributed data and services
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
ByteBuffer.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <util/types.h>
4 #include <io/ByteBuffer.h>
5 #include <io/socket.h>
6 
7 #include <math.h>
8 
9 #include <iostream>
10 #include <string>
11 
12 namespace io {
13 
14  enum ByteOrder {
15 
18  };
19 
20  class ByteBuffer {
21  private:
22 
23  ByteBuffer( ioByteBuffer buffer ) {
24  _buffer = buffer;
25  }
26 
27  ioByteBuffer _buffer;
28 
29  public:
30 
31  ByteBuffer( unsigned int capacity, byte * array = 0 ) {
32  if( array ) {
33  _buffer = ioByteBuffer_wrap( capacity, array );
34  }
35  else {
36  _buffer = ioByteBuffer_new( capacity );
37  }
38  }
39 
41  ioByteBuffer_delete( &_buffer );
42  }
43 
44  ByteBuffer * copy( unsigned int length ) const {
45  return new io::ByteBuffer( ioByteBuffer_copy( _buffer, length ));
46  }
47 
48  void clear( void ) {
49  ioByteBuffer_clear( _buffer );
50  }
51 
52  void mark( void ) {
53  ioByteBuffer_mark( _buffer );
54  }
55 
56  void reset( void ) {
57  ioByteBuffer_reset( _buffer );
58  }
59 
60  void flip( void ) {
61  ioByteBuffer_flip( _buffer );
62  }
63 
64  unsigned int position( void ) const {
65  return ioByteBuffer_getPosition( _buffer );
66  }
67 
68  void position( unsigned int position ) {
69  ioByteBuffer_setPosition( _buffer, position );
70  }
71 
72  unsigned int limit( void ) const {
73  return ioByteBuffer_getLimit( _buffer );
74  }
75 
76  unsigned int remaining( void ) const {
77  return ioByteBuffer_remaining( _buffer );
78  }
79 
80  void put( const byte * src, unsigned int from, unsigned int to ) {
81  ioByteBuffer_put( _buffer, src, from, to );
82  }
83 
84  void get( byte * target, unsigned int from, unsigned int to ) {
85  ioByteBuffer_get( _buffer, target, from, to );
86  }
87 
88  void putByte( byte value ) {
89  ioByteBuffer_putByte( _buffer, value );
90  }
91 
92  byte getByte( void ) {
93  byte value = 0;
94  ioByteBuffer_getByte( _buffer, &value );
95  return value;
96  }
97 
98  void putBoolean( bool value ) {
99  ioByteBuffer_putByte( _buffer, value ? 1 : 0 );
100  };
101 
102  bool getBoolean( void ) {
103  byte value = 0;
104  ioByteBuffer_getByte( _buffer, &value );
105  return value != 0;
106  }
107 
108  void putShort( unsigned short value ) {
109  ioByteBuffer_putShort( _buffer, value );
110  }
111 
112  unsigned short getShort( void ) {
113  unsigned short value = 0;
114  ioByteBuffer_getShort( _buffer, &value );
115  return value;
116  }
117 
118  void putInt( unsigned int value ) {
119  ioByteBuffer_putInt( _buffer, value );
120  }
121 
122  void putIntAt( unsigned int value, unsigned int index ) {
123  ioByteBuffer_putIntAt( _buffer, value, index );
124  }
125 
126  unsigned int getInt( void ) {
127  unsigned int value = 0;
128  ioByteBuffer_getInt( _buffer, &value );
129  return value;
130  }
131 
132  void putLong( uint64_t value ) {
133  ioByteBuffer_putLong( _buffer, value );
134  }
135 
136  uint64_t getLong( void ) {
137  uint64_t value = 0;
138  ioByteBuffer_getLong( _buffer, &value );
139  return value;
140  }
141 
142  void putFloat( float value ) {
143  ioByteBuffer_putFloat( _buffer, value );
144  }
145 
146  float getFloat( void ) {
147  float value = 0.0f;
148  ioByteBuffer_getFloat( _buffer, &value );
149  return value;
150  }
151 
152  void putDouble( double value ) {
153  ioByteBuffer_putDouble( _buffer, value );
154  }
155 
156  double getDouble( void ) {
157  double value = NAN;
158  if( ioByteBuffer_getDouble( _buffer, &value )) {
159  return value;
160  }
161  return NAN;
162  }
163 
164  void putString( const char * value ) {
165  ioByteBuffer_putString( _buffer, value );
166  }
167 
168  void getString( char * target, unsigned int sizeOfTarget ) {
169  ioByteBuffer_getString( _buffer, target, sizeOfTarget );
170  }
171 
172  void putString( const std::string & value ) {
173  ioByteBuffer_putString( _buffer, value.c_str());
174  }
175 
176  std::string getString() {
177  static const unsigned sizeOfTarget = 64*1024;
178  char target[sizeOfTarget];
179  ioByteBuffer_getString( _buffer, target, sizeOfTarget );
180  return std::string( target );
181  }
182 
183  void put( ByteBuffer & source ) {
184  ioByteBuffer_putBuffer( _buffer, source._buffer );
185  }
186 
187  void send( SOCKET sckt, struct sockaddr_in & target ) {
188  ioByteBuffer_send( _buffer, sckt, &target );
189  }
190 
191  bool receive( SOCKET sckt ) {
192  return ioByteBuffer_receive( _buffer, sckt ) == IO_STATUS_NO_ERROR;
193  }
194 
195  byte * array( void ) {
196  return ioByteBuffer_array( _buffer );
197  }
198 
199  void dump( FILE * target ) const {
200  ioByteBuffer_dump( _buffer, target );
201  }
202 
203  private:
204  ByteBuffer( const ByteBuffer & right );
205  ByteBuffer & operator = ( const ByteBuffer & );
206  };
207 }
std::string getString()
Definition: ByteBuffer.hpp:176
ByteOrder
Definition: ByteBuffer.hpp:14
unsigned int remaining(void) const
Definition: ByteBuffer.hpp:76
double getDouble(void)
Definition: ByteBuffer.hpp:156
bool receive(SOCKET sckt)
Definition: ByteBuffer.hpp:191
void flip(void)
Definition: ByteBuffer.hpp:60
void putLong(uint64_t value)
Definition: ByteBuffer.hpp:132
unsigned int position(void) const
Definition: ByteBuffer.hpp:64
bool getBoolean(void)
Definition: ByteBuffer.hpp:102
void putFloat(float value)
Definition: ByteBuffer.hpp:142
unsigned int limit(void) const
Definition: ByteBuffer.hpp:72
void put(ByteBuffer &source)
Definition: ByteBuffer.hpp:183
void position(unsigned int position)
Definition: ByteBuffer.hpp:68
byte * array(void)
Definition: ByteBuffer.hpp:195
void clear(void)
Definition: ByteBuffer.hpp:48
unsigned int getInt(void)
Definition: ByteBuffer.hpp:126
void send(SOCKET sckt, struct sockaddr_in &target)
Definition: ByteBuffer.hpp:187
void putShort(unsigned short value)
Definition: ByteBuffer.hpp:108
void put(const byte *src, unsigned int from, unsigned int to)
Definition: ByteBuffer.hpp:80
void putString(const char *value)
Definition: ByteBuffer.hpp:164
void dump(FILE *target) const
Definition: ByteBuffer.hpp:199
uint64_t getLong(void)
Definition: ByteBuffer.hpp:136
void putByte(byte value)
Definition: ByteBuffer.hpp:88
byte getByte(void)
Definition: ByteBuffer.hpp:92
void reset(void)
Definition: ByteBuffer.hpp:56
void mark(void)
Definition: ByteBuffer.hpp:52
void putBoolean(bool value)
Definition: ByteBuffer.hpp:98
void getString(char *target, unsigned int sizeOfTarget)
Definition: ByteBuffer.hpp:168
void putDouble(double value)
Definition: ByteBuffer.hpp:152
ByteBuffer * copy(unsigned int length) const
Definition: ByteBuffer.hpp:44
ByteBuffer(unsigned int capacity, byte *array=0)
Definition: ByteBuffer.hpp:31
float getFloat(void)
Definition: ByteBuffer.hpp:146
void putString(const std::string &value)
Definition: ByteBuffer.hpp:172
unsigned short getShort(void)
Definition: ByteBuffer.hpp:112
void putInt(unsigned int value)
Definition: ByteBuffer.hpp:118
void putIntAt(unsigned int value, unsigned int index)
Definition: ByteBuffer.hpp:122