5#ifndef DUNE_MMESH_MISC_OBJECTSTREAM_HH
6#define DUNE_MMESH_MISC_OBJECTSTREAM_HH
24 struct ObjectStreamTraits
27 static void copy ( T *dest,
const void *src, std::size_t n )
29 for( std::size_t i = 0; i < n; ++i )
30 dest[ i ] =
static_cast< const T *
>( src )[ i ];
34 static void copy (
void *dest,
const T *src, std::size_t n )
36 for( std::size_t i = 0; i < n; ++i )
37 static_cast< T *
>( dest )[ i ] = src[ i ];
43 using Traits = ObjectStreamTraits;
46 size_t _rb, _wb, _len;
48 const size_t _bufChunk;
55 virtual std::string what ()
const {
return "EOFException"; }
58 class OutOfMemoryException {};
60 inline ObjectStream (
size_t chunk = 0)
61 : _buf(0), _rb(0), _wb(0), _len(0), _bufChunk(chunk), _owner(true)
64 inline ObjectStream (
const ObjectStream & os)
65 : _buf(0), _rb(0), _wb(0), _len(0), _bufChunk(os._bufChunk), _owner(true)
71 inline void clear() { _wb = 0; _rb = 0; }
74 inline void resetReadPosition() { _rb = 0; }
77 void seekp(
const size_t pos )
80 assert ( _wb <= _len );
85 inline bool validToRead ()
const {
return (_wb > 0) && (_rb == 0); }
88 inline int capacity()
const {
return _len; }
91 inline int size()
const {
return _wb; }
94 inline void reserve(
size_t s)
96 const size_t newSize = _wb + s;
97 if (newSize > _len) reallocateBuffer( newSize );
101 inline ~ObjectStream () { removeObj(); }
105 inline ObjectStream & operator = (
const ObjectStream & os)
114 inline void write (
const T & a)
120 inline void writeUnchecked(
const T& a )
130 inline void put (
const signed char a) { write(a); }
133 inline void putNoChk (
const signed char a) { writeUnchecked(a); }
136 inline signed char get ()
144 bool eof ()
const {
return (this->_rb >= this->_wb); }
147 bool good ()
const {
return (this->_rb < this->_wb); }
153 inline void writeT (
const T & a,
const bool checkLength )
156 const size_t ap = _wb;
160 if (checkLength && _wb > _len)
162 reallocateBuffer(_wb);
164 assert ( _wb <= _len );
167 Traits::copy(
static_cast< void *
>( getBuff( ap ) ), &a, 1 );
172 inline void readT ( T& a,
bool checkLength )
174 const size_t ap = _rb;
176 assert ( _rb <= _wb );
179 Traits::copy( &a,
static_cast< const void *
>( getBuff( ap ) ), 1 );
186 inline void read (T & a) { readT( a,
true ); }
189 inline void readUnchecked ( T& a ) { readT( a,
false ); }
192 inline void readStream (ObjectStream & os)
198 inline void readStream (ObjectStream & os,
const size_t length)
200 if( length == 0 )
return;
202 os.write( getBuff(_rb), length);
203 removeObject(length);
207 inline void writeStream (
const ObjectStream & os)
209 write(os._buf, os._wb);
213 inline void removeObject(
const size_t length)
216 assert ( _rb <= _wb );
226 inline static void freeBuffer(
char * buffer)
229 if( buffer ) free( buffer );
233 inline void write(
const char* buff,
const size_t length )
236 if( length == 0 )
return;
238 const size_t newWb = _wb + length;
239 if (newWb > _len) reallocateBuffer(newWb);
241 memcpy( getBuff(_wb), buff, length );
246 inline void read(
char* buff,
const size_t length )
248 if( length == 0 )
return;
250 const size_t newRb = _rb + length;
251 assert ( newRb <= _wb );
253 memcpy( buff, getBuff(_rb), length );
258 inline char* raw () {
return getBuff( 0 ); }
259 inline const char* raw ()
const {
return getBuff( 0 ); }
261 inline char * getBuff (
const size_t ap) {
return (_buf + ap); }
262 inline const char * getBuff (
const size_t ap)
const {
return (_buf + ap); }
266 inline void reallocateBuffer(
size_t newSize)
270 if(_len < newSize) _len = newSize;
271 _buf = (
char *) realloc (_buf, _len);
274 perror (
"**EXCEPTION in ObjectStream :: reallocateBuffer(size_t) ");
275 throw OutOfMemoryException ();
280 inline void removeObj()
282 if( _owner ) freeBuffer( _buf );
283 _buf = 0; _len = 0; _wb = 0; _rb = 0; _owner =
true;
288 inline void assign(
const ObjectStream & os)
290 assert ( _buf == 0 );
296 const_cast<size_t &
> (_bufChunk) = os._bufChunk;
307 inline void assign(
char * buff,
const size_t length)
309 if( length == 0 )
return;