5#ifndef DUNE_MMESH_MISC_OBJECTSTREAM_HH
6#define DUNE_MMESH_MISC_OBJECTSTREAM_HH
22struct ObjectStreamTraits {
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];
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];
35 using Traits = ObjectStreamTraits;
39 size_t _rb, _wb, _len;
42 const size_t _bufChunk;
48 virtual std::string what()
const {
return "EOFException"; }
51 class OutOfMemoryException {};
53 inline ObjectStream(
size_t chunk = 0)
54 : _buf(0), _rb(0), _wb(0), _len(0), _bufChunk(chunk), _owner(true) {}
56 inline ObjectStream(
const ObjectStream& os)
61 _bufChunk(os._bufChunk),
73 inline void resetReadPosition() { _rb = 0; }
76 void seekp(
const size_t pos) {
83 inline bool validToRead()
const {
return (_wb > 0) && (_rb == 0); }
86 inline int capacity()
const {
return _len; }
89 inline int size()
const {
return _wb; }
92 inline void reserve(
size_t s) {
93 const size_t newSize = _wb + s;
94 if (newSize > _len) reallocateBuffer(newSize);
98 inline ~ObjectStream() { removeObj(); }
102 inline ObjectStream& operator=(
const ObjectStream& os) {
110 inline void write(
const T& a) {
115 inline void writeUnchecked(
const T& a) {
124 inline void put(
const signed char a) { write(a); }
127 inline void putNoChk(
const signed char a) { writeUnchecked(a); }
130 inline signed char get() {
137 bool eof()
const {
return (this->_rb >= this->_wb); }
140 bool good()
const {
return (this->_rb < this->_wb); }
146 inline void writeT(
const T& a,
const bool checkLength) {
148 const size_t ap = _wb;
152 if (checkLength && _wb > _len) {
153 reallocateBuffer(_wb);
158 Traits::copy(
static_cast<void*
>(getBuff(ap)), &a, 1);
163 inline void readT(T& a,
bool checkLength) {
164 const size_t ap = _rb;
169 Traits::copy(&a,
static_cast<const void*
>(getBuff(ap)), 1);
176 inline void read(T& a) {
181 inline void readUnchecked(T& a) {
186 inline void readStream(ObjectStream& os) { readStream(os, _wb); }
189 inline void readStream(ObjectStream& os,
const size_t length) {
190 if (length == 0)
return;
192 os.write(getBuff(_rb), length);
193 removeObject(length);
197 inline void writeStream(
const ObjectStream& os) { write(os._buf, os._wb); }
200 inline void removeObject(
const size_t length) {
206 inline void reset() { removeObj(); }
209 inline static void freeBuffer(
char* buffer) {
211 if (buffer) free(buffer);
215 inline void write(
const char* buff,
const size_t length) {
217 if (length == 0)
return;
219 const size_t newWb = _wb + length;
220 if (newWb > _len) reallocateBuffer(newWb);
222 memcpy(getBuff(_wb), buff, length);
227 inline void read(
char* buff,
const size_t length) {
228 if (length == 0)
return;
230 const size_t newRb = _rb + length;
231 assert(newRb <= _wb);
233 memcpy(buff, getBuff(_rb), length);
238 inline char* raw() {
return getBuff(0); }
239 inline const char* raw()
const {
return getBuff(0); }
241 inline char* getBuff(
const size_t ap) {
return (_buf + ap); }
242 inline const char* getBuff(
const size_t ap)
const {
return (_buf + ap); }
246 inline void reallocateBuffer(
size_t newSize) {
249 if (_len < newSize) _len = newSize;
250 _buf = (
char*)realloc(_buf, _len);
252 perror(
"**EXCEPTION in ObjectStream :: reallocateBuffer(size_t) ");
253 throw OutOfMemoryException();
258 inline void removeObj() {
259 if (_owner) freeBuffer(_buf);
269 inline void assign(
const ObjectStream& os) {
275 const_cast<size_t&
>(_bufChunk) = os._bufChunk;
286 inline void assign(
char* buff,
const size_t length) {
287 if (length == 0)
return;