LRAUV  revA
OutStream.h
Go to the documentation of this file.
1 
22 #ifndef OUTSTREAM_H_
23 #define OUTSTREAM_H_
24 
25 #include "utils/Str.h"
26 #include <cstdio>
41 class OutStream
42 {
43 public:
44 
46  virtual ~OutStream()
47  {}
48  ;
49 
51  virtual OutStream& write( const char* buffer, size_t num ) = 0;
52 
54  virtual OutStream& writeHex( const char* buffer, size_t num );
55 
57  virtual bool isWritable() = 0;
58 
60  virtual OutStream& write( const char* buffer )
61  {
62  return write( buffer, strlen( buffer ) );
63  }
64 
66  virtual OutStream& writeHex( const char* buffer )
67  {
68  return writeHex( buffer, strlen( buffer ) );
69  }
70 
72  OutStream& operator << ( const Str buffer )
73  {
74  return write( buffer.cStr(), buffer.length() );
75  };
76 
78  OutStream& operator << ( const char buffer[] )
79  {
80  return write( buffer );
81  }
82 
84  OutStream& operator << ( const char byte )
85  {
86  return writeChar( byte );
87  }
88 
90  OutStream& operator << ( const signed char byte )
91  {
92  return writeChar( byte );
93  }
94 
96  OutStream& operator << ( const unsigned char byte )
97  {
98  return writeChar( byte );
99  }
100 
102  OutStream& operator << ( const int number )
103  {
104  return writeInteger( number );
105  }
106 
108  OutStream& writeChar( const signed char value )
109  {
110  return write( ( char* )&value, sizeof( value ) );
111  }
112 
114  OutStream& writeCharHex( const signed char byte );
115 
117  OutStream& writeUCharHex( const unsigned char byte );
118 
120  OutStream& writeInteger( const int number, const unsigned int radix = 10 );
121  OutStream& writeNumber( const double number, const unsigned int radix = 10 );
122 
124  OutStream& writeShort( const short value )
125  {
126  return write( ( const char* )&value, sizeof( value ) );
127  }
128 
130  OutStream& writeShortHex( const short word );
131 
133  OutStream& writeUShortHex( const unsigned short word );
134 
140  OutStream& writeUShortCompact( const unsigned short& value );
141 
144  OutStream& writeInt24( const int medInt )
145  {
146  return write( ( const char* )&medInt, 3 );
147  }
148 
150  OutStream& writeInt( const int value )
151  {
152  return write( ( const char* )&value, sizeof( value ) );
153  }
154 
156  OutStream& writeLongLong( const long long value )
157  {
158  return write( ( const char* )&value, sizeof( value ) );
159  }
160 
172  OutStream& writeLongLongCompact( const long long& value );
173 
175  OutStream& writeFloat( const float value )
176  {
177  return write( ( const char* )&value, sizeof( value ) );
178  }
179 
181  OutStream& writeFloat2( const float value )
182  {
183  return write( ( ( const char* )&value ) + 2, 2 );
184  }
185 
187  OutStream& writeFloat3( const float value )
188  {
189  return write( ( ( const char* )&value ) + 1, 3 );
190  }
191 
193  OutStream& writeDouble( const double value )
194  {
195  return write( ( const char* )&value, sizeof( value ) );
196  }
197 
198  OutStream& writeDouble6( const double value )
199  {
200  return write( ( ( const char* )&value ) + 2, 6 );
201  }
202 
203  size_t bytesWritten()
204  {
205  return lastWritten_;
206  }
207 
208 protected:
209 
212  : lastWritten_( 0 )
213  {}
214  ;
215 
216  size_t lastWritten_;
217 
218  static const unsigned char HEX_CHARS[];
219 
220 private:
221  // Note that the copy constructor below is private and not given a body.
222  // Any attempt to call it will return a compiler error.
223  OutStream( const OutStream& old ); // disallow copy constructor
224 
225 };
226 
228 {
229 public:
230 
232  virtual OutStream& write( const char* buffer, unsigned int num )
233  {
234  return *this;
235  }
236 
238  virtual bool isWritable()
239  {
240  return false;
241  }
242 
243 };
244 
245 
246 #endif /*OUTSTREAM_H_*/
OutStream & writeInteger(const int number, const unsigned int radix=10)
writes an integer as a base-radix string
Definition: OutStream.cpp:46
virtual bool isWritable()=0
indicates whether the stream can actually be written to.
virtual OutStream & write(const char *buffer)
writes zero-terminated buffer to the output buffer/stream
Definition: OutStream.h:60
OutStream & writeNumber(const double number, const unsigned int radix=10)
Definition: OutStream.cpp:54
OutStream & writeUShortCompact(const unsigned short &value)
writes an unsigned short integer as 1 to 3 bytes, encoded as follows: xxxxxxx0 - 7 bit value...
Definition: OutStream.cpp:86
virtual OutStream & write(const char *buffer, unsigned int num)
doesn't write num bytes from buffer to the output buffer/stream
Definition: OutStream.h:232
virtual bool isWritable()
indicates that the stream can't actually be written to.
Definition: OutStream.h:238
OutStream & writeUCharHex(const unsigned char byte)
writes a single unsigned char to the output buffer/stream as 2 hex chars
Definition: OutStream.cpp:37
virtual OutStream & writeHex(const char *buffer)
writes zero-terminated buffer to the output buffer/stream
Definition: OutStream.h:66
OutStream & writeInt(const int value)
writes a 4-byte integer to the output buffer/stream
Definition: OutStream.h:150
OutStream & writeFloat2(const float value)
writes a 2-byte float to the output buffer/stream
Definition: OutStream.h:181
OutStream & operator<<(const Str buffer)
write a Str, using stream operator
Definition: OutStream.h:72
size_t length() const
Definition: Str.h:196
OutStream & writeLongLong(const long long value)
writes a 8-byte integer to the output buffer/stream
Definition: OutStream.h:156
virtual OutStream & writeHex(const char *buffer, size_t num)
writes num bytes from buffer to the output buffer/stream as hex chars
Definition: OutStream.cpp:18
OutStream & writeDouble(const double value)
writes a 8-byte double to the output buffer/stream
Definition: OutStream.h:193
OutStream & writeFloat(const float value)
writes a 4-byte float to the output buffer/stream
Definition: OutStream.h:175
OutStream & writeFloat3(const float value)
writes a 3-byte float to the output buffer/stream
Definition: OutStream.h:187
OutStream()
Protected Constructor for abstract class.
Definition: OutStream.h:211
OutStream & writeLongLongCompact(const long long &value)
writes a signed long long integer as 1 to 9 bytes, encoded as follows: 0xxxxxxx - 7 bit value...
Definition: OutStream.cpp:121
Replacement for standard template class string.
Definition: Str.h:12
virtual ~OutStream()
Destructor.
Definition: OutStream.h:46
OutStream & writeUShortHex(const unsigned short word)
writes a single unsigned short to the output buffer/stream as 4 hex chars
Definition: OutStream.cpp:71
const char * cStr() const
Definition: Str.h:188
virtual OutStream & write(const char *buffer, size_t num)=0
writes num bytes from buffer to the output buffer/stream
OutStream & writeChar(const signed char value)
writes a single byte to the output buffer/stream
Definition: OutStream.h:108
OutStream & writeShort(const short value)
writes a single short to the output buffer/stream
Definition: OutStream.h:124
OutStream & writeInt24(const int medInt)
writes a 3-byte integer to the output buffer/stream only works on little-endian machines! ...
Definition: OutStream.h:144
OutStream & writeCharHex(const signed char byte)
writes a single signed char to the output buffer/stream as 2 hex chars
Definition: OutStream.cpp:28
size_t bytesWritten()
Definition: OutStream.h:203
Definition: OutStream.h:227
OutStream & writeDouble6(const double value)
Definition: OutStream.h:198
This is a very abstract class that can be implemented by classes that want to send stream output...
Definition: OutStream.h:41
size_t lastWritten_
Definition: OutStream.h:214
OutStream & writeShortHex(const short word)
writes a single short to the output buffer/stream as 4 hex chars
Definition: OutStream.cpp:60
static const unsigned char HEX_CHARS[]
Definition: OutStream.h:218