Loading [MathJax]/extensions/tex2jax.js

dune-mmesh (1.4)

objectstream.hh
1// This implementation has been taken from Dune-ALUGrid!
2// (dune/alugrid/impl/serial/serialize.h)
3// Credit: Bernhard Schupp, 1997-1998 and Robert Kloefkorn 2006
4
5#ifndef DUNE_MMESH_MISC_OBJECTSTREAM_HH
6#define DUNE_MMESH_MISC_OBJECTSTREAM_HH
7
8#include <cstdio>
9#include <cstdlib>
10#include <cstring>
11#include <string>
12#include <iostream>
13#include <utility>
14
15namespace Dune
16{
17
18namespace MMeshImpl
19{
20
21 // ObjectStream
22 // ------------
23
24 struct ObjectStreamTraits
25 {
26 template< class T >
27 static void copy ( T *dest, const void *src, std::size_t n )
28 {
29 for( std::size_t i = 0; i < n; ++i )
30 dest[ i ] = static_cast< const T * >( src )[ i ];
31 }
32
33 template< class T >
34 static void copy ( void *dest, const T *src, std::size_t n )
35 {
36 for( std::size_t i = 0; i < n; ++i )
37 static_cast< T * >( dest )[ i ] = src[ i ];
38 }
39 };
40
41 class ObjectStream
42 {
43 using Traits = ObjectStreamTraits;
44 public:
45 char *_buf;
46 size_t _rb, _wb, _len;
47 protected:
48 const size_t _bufChunk;
49 mutable bool _owner;
50
51 public :
52 class EOFException
53 {
54 public:
55 virtual std::string what () const { return "EOFException"; }
56 };
57
58 class OutOfMemoryException {};
59
60 inline ObjectStream (size_t chunk = 0)
61 : _buf(0), _rb(0), _wb(0), _len(0), _bufChunk(chunk), _owner(true)
62 {}
63
64 inline ObjectStream (const ObjectStream & os)
65 : _buf(0), _rb(0), _wb(0), _len(0), _bufChunk(os._bufChunk), _owner(true)
66 {
67 assign(os);
68 }
69
70 // reset write and read postitions
71 inline void clear() { _wb = 0; _rb = 0; }
72
73 // reset read position
74 inline void resetReadPosition() { _rb = 0; }
75
77 void seekp( const size_t pos )
78 {
79 _wb = pos;
80 assert ( _wb <= _len );
81 }
82
83 // return's true if size > 0 and read position is zero
84 // i.e. a read othe stream will result some valid data
85 inline bool validToRead () const { return (_wb > 0) && (_rb == 0); }
86
87 // return size of bytes allready written to stream
88 inline int capacity() const { return _len; }
89
90 // return size of bytes allready written to stream
91 inline int size() const { return _wb; }
92
93 // make sure that s bytes memory can be wrote without reallocation
94 inline void reserve(size_t s)
95 {
96 const size_t newSize = _wb + s;
97 if (newSize > _len) reallocateBuffer( newSize );
98 }
99
100 // delete stream
101 inline ~ObjectStream () { removeObj(); }
102
104 //inline const ObjectStream & operator = (const ObjectStream & os)
105 inline ObjectStream & operator = (const ObjectStream & os)
106 {
107 removeObj();
108 assign(os);
109 return *this;
110 }
111
112 // write value to stream
113 template <class T>
114 inline void write (const T & a)
115 {
116 writeT( a, true );
117 }
118
119 template <class T>
120 inline void writeUnchecked( const T& a )
121 {
122 writeT( a, false );
123 }
124
126 // to behave like stringstream
128
129 // put char
130 inline void put (const signed char a) { write(a); }
131
132 // put char with checking buffer size (reserve size before usage)
133 inline void putNoChk (const signed char a) { writeUnchecked(a); }
134
135 // get char
136 inline signed char get ()
137 {
138 signed char a;
139 read(a);
140 return a;
141 }
142
143 // eof function
144 bool eof () const { return (this->_rb >= this->_wb); }
145
146 // good function
147 bool good () const { return (this->_rb < this->_wb); }
148
150
151 protected:
152 template <class T>
153 inline void writeT (const T & a, const bool checkLength )
154 {
155 assert ( _owner );
156 const size_t ap = _wb;
157 _wb += sizeof(T);
158
159 // if buffer is to small, reallocate
160 if (checkLength && _wb > _len)
161 {
162 reallocateBuffer(_wb);
163 }
164 assert ( _wb <= _len );
165
166 // call assignment operator of type T
167 Traits::copy( static_cast< void * >( getBuff( ap ) ), &a, 1 );
168 return;
169 }
170
171 template<class T>
172 inline void readT ( T& a, bool checkLength )
173 {
174 const size_t ap = _rb;
175 _rb += sizeof(T);
176 assert ( _rb <= _wb );
177
178 // call assignment operator of type T
179 Traits::copy( &a, static_cast< const void * >( getBuff( ap ) ), 1 );
180 return;
181 }
182
183 public:
184 // read value from stream
185 template <class T>
186 inline void read (T & a) { readT( a, true ); }
187
188 template<class T>
189 inline void readUnchecked ( T& a ) { readT( a, false ); }
190
191 // read this stream and write to os
192 inline void readStream (ObjectStream & os)
193 {
194 readStream(os,_wb);
195 }
196
197 // read length bytes from this stream and stores it to os
198 inline void readStream (ObjectStream & os, const size_t length)
199 {
200 if( length == 0 ) return;
201 // actual read position
202 os.write( getBuff(_rb), length);
203 removeObject(length);
204 }
205
206 // writes hole stream of os to this stream
207 inline void writeStream (const ObjectStream & os)
208 {
209 write(os._buf, os._wb);
210 }
211
212 // increments the read position without actualy read data
213 inline void removeObject(const size_t length)
214 {
215 _rb += length;
216 assert ( _rb <= _wb );
217 }
218
220 inline void reset()
221 {
222 removeObj();
223 }
224
225 // static free for use with all buffers here
226 inline static void freeBuffer(char * buffer)
227 {
228 // free buffer if not zero
229 if( buffer ) free( buffer );
230 }
231
232 // compatibility with ostream
233 inline void write(const char* buff, const size_t length )
234 {
235 assert ( _owner );
236 if( length == 0 ) return;
237
238 const size_t newWb = _wb + length;
239 if (newWb > _len) reallocateBuffer(newWb);
240
241 memcpy( getBuff(_wb), buff, length );
242 _wb = newWb;
243 }
244
245 // compatibility with istream
246 inline void read(char* buff, const size_t length )
247 {
248 if( length == 0 ) return;
249
250 const size_t newRb = _rb + length;
251 assert ( newRb <= _wb );
252
253 memcpy( buff, getBuff(_rb), length );
254 _rb = newRb;
255 }
256
257 // return pointer to buffer memory
258 inline char* raw () { return getBuff( 0 ); }
259 inline const char* raw () const { return getBuff( 0 ); }
260
261 inline char * getBuff (const size_t ap) { return (_buf + ap); }
262 inline const char * getBuff (const size_t ap) const { return (_buf + ap); }
263
264 protected:
265 // reallocated the buffer if necessary
266 inline void reallocateBuffer(size_t newSize)
267 {
268 assert ( _owner );
269 _len += _bufChunk;
270 if(_len < newSize) _len = newSize;
271 _buf = (char *) realloc (_buf, _len);
272 if (!_buf)
273 {
274 perror ("**EXCEPTION in ObjectStream :: reallocateBuffer(size_t) ");
275 throw OutOfMemoryException ();
276 }
277 }
278
279 // delete buffer
280 inline void removeObj()
281 {
282 if( _owner ) freeBuffer( _buf );
283 _buf = 0; _len = 0; _wb = 0; _rb = 0; _owner = true;
284 return;
285 }
286
287 // assign buffer
288 inline void assign(const ObjectStream & os)
289 {
290 assert ( _buf == 0 );
291 if( os._len > 0 )
292 {
293 _len = os._len;
294 _wb = os._wb;
295 _rb = os._rb;
296 const_cast<size_t &> (_bufChunk) = os._bufChunk;
297
298 // overtake buffer and set ownership of os to false
299 _buf = os._buf;
300 os._owner = false;
301 // we are owner now
302 _owner = true;
303 }
304 return;
305 }
306
307 inline void assign(char * buff, const size_t length)
308 {
309 if( length == 0 ) return;
310
311 // if length > 0, buff should be valid
312 assert ( buff );
313
314 // set length
315 _wb = _len = length;
316 // set buffer
317 _buf = buff;
318
319 // read status is zero
320 _rb = 0;
321
322 // we are the owner
323 _owner = true;
324 return;
325 }
326 };
327
328} // namespace MMeshImpl
329
330} // namespace Dune
331
332#endif // #ifndef DUNE_MMESH_MISC_OBJECTSTREAM_HH
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Apr 15, 23:04, 2025)