dune-mmesh (unstable)

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