DUNE-FEM (unstable)

standardstreams.hh
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
12#include <dune/fem/io/streams/streams.hh>
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
43 struct StandardOutStreamTraits
44 {
45 typedef StandardOutStream OutStreamType;
46 };
47
60 : public OutStreamInterface< StandardOutStreamTraits >
61 {
64
65 public:
67 typedef StandardOutStreamTraits Traits;
68
69 protected:
70 using BaseType::writeError;
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 writeSignedInt64 ( int64_t value )
113 {
114 writePrimitive( value );
115 }
116
118 void writeChar ( const char value )
119 {
120 writePrimitive( value );
121 }
122
124 void writeBool ( const bool value )
125 {
126 writePrimitive( value );
127 }
128
130 void writeString ( const std::string &s )
131 {
132 const unsigned int length = s.length();
133 writePrimitive( length );
134 for( unsigned int i = 0; i < length; ++i )
135 writePrimitive( s[ i ] );
136 }
137
139 void writeUnsignedInt ( unsigned int value )
140 {
141 writePrimitive( value );
142 }
143
145 void writeUnsignedInt64 ( uint64_t value )
146 {
147 writePrimitive( value );
148 }
149
150 protected:
151 bool valid () const
152 {
153 return bool( stream_ );
154 }
155
156 template< class T >
157 void writePrimitive ( const T &value )
158 {
159 const size_t tsize = sizeof( T );
160 union { T value; char bytes[ tsize ]; } convert;
161
162 // copy value
163 convert.value = value;
164
165 // make sure that char is only one byte
166 assert( sizeof(char) == 1 ) ;
167
168 // write with byte order little endian
169 for( size_t i=0; i<tsize; ++i )
170 {
171 stream_.put( convert.bytes[ ByteOrder :: map( i, tsize ) ] );
172 }
173
174 if( !valid () )
175 writeError();
176 }
177
178 protected:
179 std::ostream& stream_;
180 };
181
182
183 struct StandardInStreamTraits
184 {
185 typedef StandardInStream InStreamType;
186 };
187
188
201 : public InStreamInterface< StandardInStreamTraits >
202 {
205
206 public:
208 typedef StandardInStreamTraits Traits;
209
210 protected:
211 using BaseType::readError;
212
213 public:
218 explicit StandardInStream ( std::istream& stream )
219 : stream_( stream )
220 {
221 if( ! valid() )
222 DUNE_THROW( Dune::IOError, "Stream not valid!" );
223 }
224
226 std::istream& stream() { return stream_; }
227
229 void readDouble ( double &value )
230 {
231 readPrimitive( value );
232 }
233
235 void readFloat ( float &value )
236 {
237 readPrimitive( value );
238 }
239
241 void readInt ( int &value )
242 {
243 readPrimitive( value );
244 }
245
247 void readSignedInt64 ( int64_t &value )
248 {
249 readPrimitive( value );
250 }
251
253 void readChar ( char &value )
254 {
255 readPrimitive( value );
256 }
257
259 void readBool ( bool &value )
260 {
261 readPrimitive( value );
262 }
263
265 void readString ( std::string &s )
266 {
267 unsigned int length;
268 readPrimitive( length );
269
270 // resize string
271 s.resize( length );
272 for( unsigned int i = 0; i < length; ++i )
273 {
274 readPrimitive( s[ i ] );
275 }
276 }
277
279 void readUnsignedInt ( unsigned int &value )
280 {
281 readPrimitive( value );
282 }
283
285 void readUnsignedInt64 ( uint64_t &value )
286 {
287 readPrimitive( value );
288 }
289
290 protected:
291 bool valid () const
292 {
293 return stream_.good() || stream_.eof();
294 }
295
296 template< class T >
297 void readPrimitive ( T &value )
298 {
299 const size_t tsize = sizeof( T ) ;
300 union { T value; char bytes[ tsize ]; } convert;
301
302 // char should be only 1 byte
303 assert( sizeof(char) == 1 ) ;
304
305 // read from stream with byte order little endian
306 for( size_t i=0; i<tsize; ++i )
307 {
308 convert.bytes[ ByteOrder :: map( i, tsize ) ] = stream_.get();
309 }
310
311 // store result to value
312 value = convert.value;
313
314 if( !valid() )
315 readError();
316 }
317
318 protected:
319 std::istream& stream_;
320 };
321
322 } // namespace Fem
323
324} // namespace Dune
325
326#endif // #ifndef DUNE_FEM_BINARYSTREAMS_HH
abstract interface for an input stream
Definition: streams.hh:190
abstract interface for an output stream
Definition: streams.hh:48
input stream reading from a given std::istream
Definition: standardstreams.hh:202
void readUnsignedInt(unsigned int &value)
read an unsigned int from the stream
Definition: standardstreams.hh:279
void readDouble(double &value)
read a double from the stream
Definition: standardstreams.hh:229
std::istream & stream()
Definition: standardstreams.hh:226
void readInt(int &value)
read an int from the stream
Definition: standardstreams.hh:241
void readFloat(float &value)
read a float from the stream
Definition: standardstreams.hh:235
void readChar(char &value)
read a char from the stream
Definition: standardstreams.hh:253
StandardInStreamTraits Traits
type of the traits
Definition: standardstreams.hh:208
StandardInStream(std::istream &stream)
constructor
Definition: standardstreams.hh:218
void readString(std::string &s)
read a string from the stream
Definition: standardstreams.hh:265
void readUnsignedInt64(uint64_t &value)
read an uint64_t from the stream
Definition: standardstreams.hh:285
void readSignedInt64(int64_t &value)
read an int64_t from the stream
Definition: standardstreams.hh:247
void readBool(bool &value)
read a bool from the stream
Definition: standardstreams.hh:259
output stream writing into a given std::ostream
Definition: standardstreams.hh:61
void writeSignedInt64(int64_t value)
write an int64_t to the stream
Definition: standardstreams.hh:112
void flush()
flush the stream
Definition: standardstreams.hh:88
void writeUnsignedInt(unsigned int value)
write an unsigned int to the stream
Definition: standardstreams.hh:139
void writeString(const std::string &s)
write a string to the stream
Definition: standardstreams.hh:130
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:118
void writeDouble(const double value)
write a double to the stream
Definition: standardstreams.hh:94
void writeBool(const bool value)
write a bool to the stream
Definition: standardstreams.hh:124
void writeUnsignedInt64(uint64_t value)
write an uint64_t to the stream
Definition: standardstreams.hh:145
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
Default exception class for I/O errors.
Definition: exceptions.hh:231
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)