DUNE-FEM (unstable)

virtualstreams.hh
1#ifndef DUNE_FEM_VIRTUALSTREAMS_HH
2#define DUNE_FEM_VIRTUALSTREAMS_HH
3
4#include <dune/fem/io/streams/streams.hh>
5
6namespace Dune
7{
8
9 namespace Fem
10 {
11
12 // Forward Declarations
13 // --------------------
14
15 class VirtualOutStream;
16
17 class VirtualInStream;
18
19 template< class Traits >
20 VirtualOutStream virtualize ( OutStreamInterface< Traits > &stream );
21
22 template< class Traits >
23 VirtualInStream virtualize ( InStreamInterface< Traits > &stream );
24
25
26
27 // VirtualOutStreamObject
28 // ----------------------
29
30 class VirtualOutStreamObject
31 {
32 typedef VirtualOutStreamObject ThisType;
33
34 friend class VirtualOutStream;
35
36 private:
37 unsigned int refCount;
38
39 protected:
40 inline VirtualOutStreamObject ()
41 : refCount( 0 )
42 {}
43 virtual ~VirtualOutStreamObject() {}
44
45 public:
46 virtual void flush () = 0;
47 virtual void writeDouble ( double value ) = 0;
48 virtual void writeFloat ( float value ) = 0;
49 virtual void writeInt ( int value ) = 0;
50 virtual void writeSignedInt64 ( int64_t value ) = 0;
51 virtual void writeString ( const std::string &s ) = 0;
52 virtual void writeUnsignedInt ( unsigned int value ) = 0;
53 virtual void writeUnsignedInt64 ( uint64_t value ) = 0;
54 };
55
56
57
58 // VirtualOutStreamTraits
59 // ----------------------
60
61 struct VirtualOutStreamTraits
62 {
63 typedef VirtualOutStream OutStreamType;
64 };
65
66
67
68 // VirtualOutStream
69 // ----------------
70
71 class VirtualOutStream
72 : public OutStreamInterface< VirtualOutStreamTraits >
73 {
74 typedef VirtualOutStream ThisType;
76
77 template< class T >
78 friend VirtualOutStream virtualize ( OutStreamInterface< T > & );
79
80 private:
81 VirtualOutStreamObject *const stream_;
82
83 private:
84 explicit VirtualOutStream ( VirtualOutStreamObject *stream )
85 : stream_( stream )
86 {
87 ++stream_->refCount;
88 }
89
90 public:
91 VirtualOutStream ( const ThisType &other )
92 : stream_( other.stream_ )
93 {
94 ++stream_->refCount;
95 }
96
97 virtual ~VirtualOutStream ()
98 {
99 if( --stream_->refCount == 0 )
100 delete stream_;
101 }
102
103 private:
104 ThisType &operator= ( const ThisType & );
105
106 public:
107 void flush ()
108 {
109 stream_->flush();
110 }
111
112 void writeDouble ( double value )
113 {
114 stream_->writeDouble( value );
115 }
116
117 void writeFloat ( float value )
118 {
119 stream_->writeFloat( value );
120 }
121
122 void writeInt ( int value )
123 {
124 stream_->writeInt( value );
125 }
126
127 void writeSignedInt64 ( int64_t value )
128 {
129 stream_->writeSignedInt64( value );
130 }
131
132 void writeString ( const std :: string &s )
133 {
134 stream_->writeString( s );
135 }
136
137 void writeUnsignedInt ( unsigned int value )
138 {
139 stream_->writeUnsignedInt( value );
140 }
141
142 void writeUnsignedInt64 ( unsigned int value )
143 {
144 stream_->writeUnsignedInt64( value );
145 }
146 };
147
148
149
150 // VirtualInStreamObject
151 // ---------------------
152
153 class VirtualInStreamObject
154 {
155 typedef VirtualInStream ThisType;
156
157 friend class VirtualInStream;
158
159 private:
160 unsigned int refCount;
161
162 protected:
163 VirtualInStreamObject ()
164 : refCount( 0 )
165 {}
166
167 virtual ~VirtualInStreamObject() {}
168
169 public:
170 virtual void readDouble ( double &value ) = 0;
171 virtual void readFloat ( float &value ) = 0;
172 virtual void readInt ( int &value ) = 0;
173 virtual void readSignedInt64 ( int64_t &value ) = 0;
174 virtual void readString ( std::string &s ) = 0;
175 virtual void readUnsignedInt ( unsigned int &value ) = 0;
176 virtual void readUnsignedInt64 ( uint64_t &value ) = 0;
177 };
178
179
180
181 // VirtualInStreamTraits
182 // ---------------------
183
184 struct VirtualInStreamTraits
185 {
186 typedef VirtualInStream InStreamType;
187 };
188
189
190
191 // VirtualInStream
192 // ---------------
193
194 class VirtualInStream
195 : public InStreamInterface< VirtualInStreamTraits >
196 {
197 typedef VirtualInStream ThisType;
199
200 template< class T >
201 friend VirtualInStream virtualize ( InStreamInterface< T > & );
202
203 private:
204 VirtualInStreamObject *const stream_;
205
206 private:
207 explicit VirtualInStream ( VirtualInStreamObject *stream )
208 : stream_( stream )
209 {
210 ++stream_->refCount;
211 }
212
213 public:
214 VirtualInStream ( const ThisType &other )
215 : stream_( other.stream_ )
216 {
217 ++stream_->refCount;
218 }
219
220 virtual ~VirtualInStream ()
221 {
222 if( --stream_->refCount == 0 )
223 delete stream_;
224 }
225
226 private:
227 ThisType &operator= ( const ThisType & );
228
229 public:
230 void readDouble ( double &value )
231 {
232 stream_->readDouble( value );
233 }
234
235 void readFloat ( float &value )
236 {
237 stream_->readFloat( value );
238 }
239
240 void readInt ( int &value )
241 {
242 stream_->readInt( value );
243 }
244
245 void readSignedInt64 ( int64_t &value )
246 {
247 stream_->readSignedInt64( value );
248 }
249
250 void readString ( std :: string &s )
251 {
252 stream_->readString( s );
253 }
254
255 void readUnsignedInt ( unsigned int &value )
256 {
257 stream_->readUnsignedInt( value );
258 }
259
260 void readUnsignedInt64 ( uint64_t &value )
261 {
262 stream_->readUnsignedInt64( value );
263 }
264 };
265
266
267
268 // VirtualOutStreamWrapper
269 // -----------------------
270
271 template< class Traits >
272 class VirtualOutStreamWrapper
273 : public VirtualOutStreamObject
274 {
275 typedef VirtualOutStreamWrapper< Traits > ThisType;
276 typedef VirtualOutStreamObject BaseType;
277
278 template< class T >
279 friend VirtualOutStream virtualize ( OutStreamInterface< T > & );
280
281 public:
282 typedef OutStreamInterface< Traits > StreamType;
283
284 private:
285 StreamType &stream_;
286
287 private:
288 explicit VirtualOutStreamWrapper ( StreamType &stream )
289 : stream_( stream )
290 {}
291
292 VirtualOutStreamWrapper ( const ThisType & );
293 ThisType &operator= ( const ThisType & );
294
295 public:
296 virtual void flush ()
297 {
298 stream_.flush();
299 }
300
301 virtual void writeDouble ( double value )
302 {
303 stream_.writeDouble( value );
304 }
305
306 virtual void writeFloat ( float value )
307 {
308 stream_.writeFloat( value );
309 }
310
311 virtual void writeInt ( int value )
312 {
313 stream_.writeInt( value );
314 }
315
316 virtual void writeSignedInt64 ( int64_t value )
317 {
318 stream_.writeSignedInt64( value );
319 }
320
321 virtual void writeString ( const std :: string &s )
322 {
323 stream_.writeString( s );
324 }
325
326 virtual void writeUnsignedInt( unsigned int value )
327 {
328 stream_.writeUnsignedInt( value );
329 }
330
331 virtual void writeUnsignedInt64( uint64_t value )
332 {
333 stream_.writeUnsignedInt64( value );
334 }
335 };
336
337
338
339 // VirtualInStreamWrapper
340 // ----------------------
341
342 template< class Traits >
343 class VirtualInStreamWrapper
344 : public VirtualInStreamObject
345 {
346 typedef VirtualInStreamWrapper< Traits > ThisType;
347 typedef VirtualInStreamObject BaseType;
348
349 template< class T >
350 friend VirtualInStream virtualize ( InStreamInterface< T > & );
351
352 public:
353 typedef InStreamInterface< Traits > StreamType;
354
355 private:
356 StreamType &stream_;
357
358 private:
359 explicit VirtualInStreamWrapper ( StreamType &stream )
360 : stream_( stream )
361 {}
362
363 VirtualInStreamWrapper ( const ThisType & );
364 ThisType &operator= ( const ThisType & );
365
366 public:
367 virtual void readDouble ( double &value )
368 {
369 stream_.readDouble( value );
370 }
371
372 virtual void readFloat ( float &value )
373 {
374 stream_.readFloat( value );
375 }
376
377 virtual void readInt ( int &value )
378 {
379 stream_.readInt( value );
380 }
381
382 virtual void readSignedInt64 ( int64_t &value )
383 {
384 stream_.readSignedInt64( value );
385 }
386
387 virtual void readString ( std :: string &s )
388 {
389 stream_.readString( s );
390 }
391
392 virtual void readUnsignedInt( unsigned int &value )
393 {
394 stream_.readUnsignedInt( value );
395 }
396
397 virtual void readUnsignedInt64( uint64_t &value )
398 {
399 stream_.readUnsignedInt64( value );
400 }
401 };
402
403
404
405 // virtualize
406 // ----------
407
408 template< class Traits >
409 VirtualOutStream virtualize ( OutStreamInterface< Traits > &stream )
410 {
411 return VirtualOutStream( new VirtualOutStreamWrapper< Traits >( stream ) );
412 }
413
414 template< class Traits >
415 VirtualInStream virtualize ( InStreamInterface< Traits > &stream )
416 {
417 return VirtualInStream( new VirtualInStreamWrapper< Traits >( stream ) );
418 }
419
420 } // namespave Fem
421
422} // namespace Dune
423
424#endif // #ifndef DUNE_FEM_VIRTUALSTREAMS_HH
int64_t readSignedInt64()
read an int64_t from the stream
Definition: streams.hh:281
double readDouble()
read a double from the stream
Definition: streams.hh:221
float readFloat()
read a double from the stream
Definition: streams.hh:241
void readString(std::string &s)
read a string from the stream
Definition: streams.hh:333
unsigned int readUnsignedInt()
read an unsigned int from the stream
Definition: streams.hh:351
int readInt()
read an int from the stream
Definition: streams.hh:261
uint64_t readUnsignedInt64()
read an uint64_t from the stream
Definition: streams.hh:371
void writeString(const std::string &s)
write a string to the stream
Definition: streams.hh:134
void writeUnsignedInt64(uint64_t value)
write an uint64_t to the stream
Definition: streams.hh:152
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 27, 22:29, 2024)