dune-fem  2.4.1-rc
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 
14 namespace 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:
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 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  {
197  typedef StandardInStream ThisType;
199 
200  public:
203 
204  protected:
205  using BaseType::readError;
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
static size_t map(const size_t pos, const size_t size)
Definition: standardstreams.hh:32
static const char defaultEndian
Definition: standardstreams.hh:23
void readInt(int &value)
read an int from the stream
Definition: standardstreams.hh:235
bool valid() const
Definition: standardstreams.hh:145
input stream reading from a given std::istream
Definition: standardstreams.hh:194
void readUnsignedInt64(uint64_t &value)
read an uint64_t from the stream
Definition: standardstreams.hh:273
void writeBool(const bool value)
write a char to the stream
Definition: standardstreams.hh:118
std::istream & stream_
Definition: standardstreams.hh:307
StandardOutStream(std::ostream &stream)
constructor
Definition: standardstreams.hh:77
Definition: standardstreams.hh:20
void readString(std::string &s)
read a string from the stream
Definition: standardstreams.hh:253
void writeChar(const char value)
write a char to the stream
Definition: standardstreams.hh:112
std::ostream & stream()
Definition: standardstreams.hh:85
void readPrimitive(T &value)
Definition: standardstreams.hh:285
Definition: standardstreams.hh:43
void writeUnsignedInt(unsigned int value)
write an unsigned int to the stream
Definition: standardstreams.hh:133
void flush()
flush the stream
Definition: standardstreams.hh:88
static const char order
Definition: standardstreams.hh:24
std::ostream & stream_
Definition: standardstreams.hh:173
StandardInStream(std::istream &stream)
constructor
Definition: standardstreams.hh:212
void readUnsignedInt(unsigned int &value)
read an unsigned int from the stream
Definition: standardstreams.hh:267
output stream writing into a given std::ostream
Definition: standardstreams.hh:59
void writeUnsignedInt64(uint64_t value)
write an uint64_t to the stream
Definition: standardstreams.hh:139
Definition: coordinate.hh:4
abstract interface for an input stream
Definition: streams.hh:177
void readChar(char &value)
read a char from the stream
Definition: standardstreams.hh:241
std::istream & stream()
Definition: standardstreams.hh:220
void writeString(const std::string &s)
write a string to the stream
Definition: standardstreams.hh:124
void readDouble(double &value)
read a double from the stream
Definition: standardstreams.hh:223
bool valid() const
Definition: standardstreams.hh:279
StandardOutStream OutStreamType
Definition: standardstreams.hh:45
void writeDouble(const double value)
write a double to the stream
Definition: standardstreams.hh:94
StandardOutStreamTraits Traits
type of the traits
Definition: standardstreams.hh:67
void writePrimitive(const T &value)
Definition: standardstreams.hh:151
StandardInStream InStreamType
Definition: standardstreams.hh:179
Definition: standardstreams.hh:177
void readBool(bool &value)
read a bool from the stream
Definition: standardstreams.hh:247
void writeFloat(const float value)
write a float to the stream
Definition: standardstreams.hh:100
void readFloat(float &value)
read a float from the stream
Definition: standardstreams.hh:229
StandardInStreamTraits Traits
type of the traits
Definition: standardstreams.hh:202
void writeInt(const int value)
write an int to the stream
Definition: standardstreams.hh:106
abstract interface for an output stream
Definition: streams.hh:44