dune-fem 2.8.0
Loading...
Searching...
No Matches
standardstreams.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_STANDARDSTREAMS_HH
2#define DUNE_FEM_STANDARDSTREAMS_HH
3
4#include <fstream>
5#include <utility>
6#include <memory>
7
8#ifdef SYSTEM_ENDIAN_HEADER
9#include SYSTEM_ENDIAN_HEADER
10#endif
11
13
14namespace Dune
15{
16
17 namespace Fem
18 {
19
20 struct ByteOrder
21 {
22 // the default endianess is little, e.g. 0
23 static const char defaultEndian = 0;
24 static const char order =
25#if __BYTE_ORDER == __LITTLE_ENDIAN
26 0 ;
27#elif __BYTE_ORDER == __BIG_ENDIAN
28 1 ;
29#else
30 0 ; // default is zero (in case no endian header was found)
31#endif
32 static inline size_t map( const size_t pos,
33 const size_t size )
34 {
35 // if byte order is not little endian, swap bytes
36 return ( order == defaultEndian ) ? pos : ( size - pos - 1 );
37 }
38 };
39
40 class StandardOutStream;
41 class StandardInStream;
42
44 {
46 };
47
60 : public OutStreamInterface< StandardOutStreamTraits >
61 {
64
65 public:
68
69 protected:
71
72 public:
77 explicit StandardOutStream ( std::ostream& stream )
78 : stream_( stream )
79 {
80 if( ! stream )
81 DUNE_THROW( Dune::IOError, "Stream not valid!" );
82 }
83
85 std::ostream& stream() { return stream_; }
86
88 void flush ()
89 {
90 stream_.flush();
91 }
92
94 void writeDouble ( const double value )
95 {
96 writePrimitive( value );
97 }
98
100 void writeFloat ( const float value )
101 {
102 writePrimitive( value );
103 }
104
106 void writeInt ( const int value )
107 {
108 writePrimitive( value );
109 }
110
112 void writeChar ( const char value )
113 {
114 writePrimitive( value );
115 }
116
118 void writeBool ( const bool value )
119 {
120 writePrimitive( value );
121 }
122
124 void writeString ( const std::string &s )
125 {
126 const unsigned int length = s.length();
127 writePrimitive( length );
128 for( unsigned int i = 0; i < length; ++i )
129 writePrimitive( s[ i ] );
130 }
131
133 void writeUnsignedInt ( unsigned int value )
134 {
135 writePrimitive( value );
136 }
137
139 void writeUnsignedInt64 ( uint64_t value )
140 {
141 writePrimitive( value );
142 }
143
144 protected:
145 bool valid () const
146 {
147 return bool( stream_ );
148 }
149
150 template< class T >
151 void writePrimitive ( const T &value )
152 {
153 const size_t tsize = sizeof( T );
154 union { T value; char bytes[ tsize ]; } convert;
155
156 // copy value
157 convert.value = value;
158
159 // make sure that char is only one byte
160 assert( sizeof(char) == 1 ) ;
161
162 // write with byte order little endian
163 for( size_t i=0; i<tsize; ++i )
164 {
165 stream_.put( convert.bytes[ ByteOrder :: map( i, tsize ) ] );
166 }
167
168 if( !valid () )
169 writeError();
170 }
171
172 protected:
173 std::ostream& stream_;
174 };
175
176
178 {
180 };
181
182
195 : public InStreamInterface< StandardInStreamTraits >
196 {
199
200 public:
203
204 protected:
206
207 public:
212 explicit StandardInStream ( std::istream& stream )
213 : stream_( stream )
214 {
215 if( ! valid() )
216 DUNE_THROW( Dune::IOError, "Stream not valid!" );
217 }
218
220 std::istream& stream() { return stream_; }
221
223 void readDouble ( double &value )
224 {
225 readPrimitive( value );
226 }
227
229 void readFloat ( float &value )
230 {
231 readPrimitive( value );
232 }
233
235 void readInt ( int &value )
236 {
237 readPrimitive( value );
238 }
239
241 void readChar ( char &value )
242 {
243 readPrimitive( value );
244 }
245
247 void readBool ( bool &value )
248 {
249 readPrimitive( value );
250 }
251
253 void readString ( std::string &s )
254 {
255 unsigned int length;
256 readPrimitive( length );
257
258 // resize string
259 s.resize( length );
260 for( unsigned int i = 0; i < length; ++i )
261 {
262 readPrimitive( s[ i ] );
263 }
264 }
265
267 void readUnsignedInt ( unsigned int &value )
268 {
269 readPrimitive( value );
270 }
271
273 void readUnsignedInt64 ( uint64_t &value )
274 {
275 readPrimitive( value );
276 }
277
278 protected:
279 bool valid () const
280 {
281 return stream_.good() | stream_.eof();
282 }
283
284 template< class T >
285 void readPrimitive ( T &value )
286 {
287 const size_t tsize = sizeof( T ) ;
288 union { T value; char bytes[ tsize ]; } convert;
289
290 // char should be only 1 byte
291 assert( sizeof(char) == 1 ) ;
292
293 // read from stream with byte order little endian
294 for( size_t i=0; i<tsize; ++i )
295 {
296 convert.bytes[ ByteOrder :: map( i, tsize ) ] = stream_.get();
297 }
298
299 // store result to value
300 value = convert.value;
301
302 if( !valid() )
303 readError();
304 }
305
306 protected:
307 std::istream& stream_;
308 };
309
310 } // namespace Fem
311
312} // namespace Dune
313
314#endif // #ifndef DUNE_FEM_BINARYSTREAMS_HH
Definition: bindguard.hh:11
Definition: standardstreams.hh:21
static size_t map(const size_t pos, const size_t size)
Definition: standardstreams.hh:32
static const char defaultEndian
Definition: standardstreams.hh:23
static const char order
Definition: standardstreams.hh:24
Definition: standardstreams.hh:44
StandardOutStream OutStreamType
Definition: standardstreams.hh:45
output stream writing into a given std::ostream
Definition: standardstreams.hh:61
void flush()
flush the stream
Definition: standardstreams.hh:88
void writeUnsignedInt(unsigned int value)
write an unsigned int to the stream
Definition: standardstreams.hh:133
void writeString(const std::string &s)
write a string to the stream
Definition: standardstreams.hh:124
void writeFloat(const float value)
write a float to the stream
Definition: standardstreams.hh:100
void writeChar(const char value)
write a char to the stream
Definition: standardstreams.hh:112
void writeDouble(const double value)
write a double to the stream
Definition: standardstreams.hh:94
void writePrimitive(const T &value)
Definition: standardstreams.hh:151
void writeBool(const bool value)
write a char to the stream
Definition: standardstreams.hh:118
std::ostream & stream_
Definition: standardstreams.hh:173
bool valid() const
Definition: standardstreams.hh:145
void writeUnsignedInt64(uint64_t value)
write an uint64_t to the stream
Definition: standardstreams.hh:139
StandardOutStreamTraits Traits
type of the traits
Definition: standardstreams.hh:67
void writeInt(const int value)
write an int to the stream
Definition: standardstreams.hh:106
std::ostream & stream()
Definition: standardstreams.hh:85
StandardOutStream(std::ostream &stream)
constructor
Definition: standardstreams.hh:77
Definition: standardstreams.hh:178
StandardInStream InStreamType
Definition: standardstreams.hh:179
input stream reading from a given std::istream
Definition: standardstreams.hh:196
void readUnsignedInt(unsigned int &value)
read an unsigned int from the stream
Definition: standardstreams.hh:267
void readDouble(double &value)
read a double from the stream
Definition: standardstreams.hh:223
std::istream & stream()
Definition: standardstreams.hh:220
bool valid() const
Definition: standardstreams.hh:279
void readInt(int &value)
read an int from the stream
Definition: standardstreams.hh:235
void readFloat(float &value)
read a float from the stream
Definition: standardstreams.hh:229
void readChar(char &value)
read a char from the stream
Definition: standardstreams.hh:241
void readPrimitive(T &value)
Definition: standardstreams.hh:285
StandardInStreamTraits Traits
type of the traits
Definition: standardstreams.hh:202
std::istream & stream_
Definition: standardstreams.hh:307
StandardInStream(std::istream &stream)
constructor
Definition: standardstreams.hh:212
void readString(std::string &s)
read a string from the stream
Definition: standardstreams.hh:253
void readUnsignedInt64(uint64_t &value)
read an uint64_t from the stream
Definition: standardstreams.hh:273
void readBool(bool &value)
read a bool from the stream
Definition: standardstreams.hh:247
abstract interface for an output stream
Definition: streams.hh:46
void writeError() const
Definition: streams.hh:147
abstract interface for an input stream
Definition: streams.hh:179
void readError() const
Definition: streams.hh:348