dune-fem  2.4.1-rc
streams.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_STREAMS_HH
2 #define DUNE_FEM_STREAMS_HH
3 
4 #include <string>
5 #include <fstream>
6 // we would use cstdint,
7 // if it would be available for all compilers, e.g. clang
8 #include <stdint.h>
9 
10 #include <dune/common/exceptions.hh>
11 
14 
15 namespace Dune
16 {
17 
18  namespace Fem
19  {
20 
21  class StreamError : public Exception {};
22 
23 
43  template< class TraitsImp >
45  : public BartonNackmanInterface< OutStreamInterface< TraitsImp >, typename TraitsImp::OutStreamType >
46  {
49 
50  public:
52  typedef TraitsImp Traits;
53 
56 
58  typedef ThisType OutStreamInterfaceType;
59 
60  protected:
61  using BaseType::asImp;
62 
63  public:
69  void flush ()
70  {
71  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().flush() );
72  }
73 
78  void writeDouble ( const double value )
79  {
80  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().writeDouble( value ) );
81  }
82 
87  void writeFloat ( const float value )
88  {
89  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().writeFloat( value ) );
90  }
91 
96  void writeInt ( const int value )
97  {
98  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().writeInt( value ) );
99  }
100 
105  void writeChar ( const char value )
106  {
107  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().writeChar( value ) );
108  }
109 
114  void writeBool ( const bool value )
115  {
116  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().writeBool( value ) );
117  }
118 
123  void writeString ( const std::string &s )
124  {
125  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().writeString( s ) );
126  }
127 
132  void writeUnsignedInt ( unsigned int value )
133  {
134  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().writeUnsignedInt( value ) );
135  }
136 
141  void writeUnsignedInt64 ( uint64_t value )
142  {
143  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().writeUnsignedInt64( value ) );
144  }
145 
146  protected:
147  void writeError () const
148  {
149  DUNE_THROW( StreamError, "Unable to write to stream." );
150  }
151  };
152 
153 
154 
176  template< class TraitsImp >
178  : public BartonNackmanInterface< InStreamInterface< TraitsImp >, typename TraitsImp::InStreamType >
179  {
182 
183  public:
185  typedef TraitsImp Traits;
186 
189 
191  typedef ThisType InStreamInterfaceType;
192 
193  protected:
194  using BaseType::asImp;
195 
196  public:
201  void readDouble ( double &value )
202  {
203  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().readDouble( value ) );
204  }
205 
210  double readDouble ()
211  {
212  double value;
213  readDouble( value );
214  return value;
215  }
216 
221  void readFloat ( float &value )
222  {
223  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().readFloat( value ) );
224  }
225 
230  float readFloat ()
231  {
232  float value;
233  readFloat( value );
234  return value;
235  }
236 
241  void readInt ( int &value )
242  {
243  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().readInt( value ) );
244  }
245 
250  int readInt ()
251  {
252  int value;
253  readInt( value );
254  return value;
255  }
256 
261  void readChar ( char &value )
262  {
263  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().readChar( value ) );
264  }
265 
270  int readChar ()
271  {
272  char value;
273  readChar( value );
274  return value;
275  }
276 
282  void readBool ( bool &value )
283  {
284  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().readBool( value ) );
285  }
286 
291  bool readBool ()
292  {
293  bool value;
294  readBool( value );
295  return value;
296  }
297 
302  void readString ( std::string &s )
303  {
304  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().readString( s ) );
305  }
306 
311  void readUnsignedInt ( unsigned int &value )
312  {
313  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().readUnsignedInt( value ) );
314  }
315 
320  unsigned int readUnsignedInt ()
321  {
322  unsigned int value;
323  readUnsignedInt( value );
324  return value;
325  }
326 
331  void readUnsignedInt64 ( uint64_t &value )
332  {
333  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().readUnsignedInt64( value ) );
334  }
335 
340  uint64_t readUnsignedInt64 ()
341  {
342  uint64_t value;
343  readUnsignedInt64( value );
344  return value;
345  }
346 
347  protected:
348  void readError () const
349  {
350  DUNE_THROW( StreamError, "Unable to read from stream." );
351  }
352  };
353 
357  template <class StreamImpl>
359  {
361  typedef typename MPIHelper :: MPICommunicator MPICommunicatorType;
362 
369  static StreamImpl* create( const std::string& filename,
370  const int rank = MPIManager::rank(),
371  const MPICommunicatorType& mpiComm = MPIHelper :: getCommunicator() )
372  {
373  return new StreamImpl( filename );
374  }
375  };
376 
377  } // namespace Fem
378 
379 } // end namespace Dune
380 
381 #include "streams_inline.hh"
382 
383 #endif // #ifndef DUNE_FEM_STREAMS_HH
int readInt()
read an int from the stream
Definition: streams.hh:250
void readBool(bool &value)
read a bool from the stream
Definition: streams.hh:282
void writeDouble(const double value)
write a double to the stream
Definition: streams.hh:78
input stream reading from an STL input stream using ASCII decoding
Definition: asciistreams.hh:183
MPIHelper::MPICommunicator MPICommunicatorType
type of MPI communicator
Definition: streams.hh:361
bool readBool()
read a bool from the stream
Definition: streams.hh:291
static int rank()
Definition: mpimanager.hh:116
ThisType OutStreamInterfaceType
type of the interface
Definition: streams.hh:58
void readError() const
Definition: streams.hh:348
void writeError() const
Definition: streams.hh:147
unsigned int readUnsignedInt()
read an unsigned int from the stream
Definition: streams.hh:320
Traits::OutStreamType OutStreamType
type of the implementation (Barton-Nackman)
Definition: streams.hh:55
TraitsImp Traits
type of the traits
Definition: streams.hh:185
void writeUnsignedInt64(uint64_t value)
write an uint64_t to the stream
Definition: streams.hh:141
static StreamImpl * create(const std::string &filename, const int rank=MPIManager::rank(), const MPICommunicatorType &mpiComm=MPIHelper::getCommunicator())
return pointer to stream object created by new.
Definition: streams.hh:369
void writeBool(const bool value)
write a bool to the stream
Definition: streams.hh:114
void flush()
flush the stream
Definition: streams.hh:69
void writeChar(const char value)
write a char to the stream
Definition: streams.hh:105
void readUnsignedInt64(uint64_t &value)
read an uint64_t from the stream
Definition: streams.hh:331
void writeInt(const int value)
write an int to the stream
Definition: streams.hh:96
Factory class for Fem Streams to deal with different constructor parameters.
Definition: streams.hh:358
Definition: coordinate.hh:4
abstract interface for an input stream
Definition: streams.hh:177
void readString(std::string &s)
read a string from the stream
Definition: streams.hh:302
ThisType InStreamInterfaceType
type of the interface
Definition: streams.hh:191
void readUnsignedInt(unsigned int &value)
read an unsigned int from the stream
Definition: streams.hh:311
float readFloat()
read a double from the stream
Definition: streams.hh:230
TraitsImp Traits
type of the traits
Definition: streams.hh:52
double readDouble()
read a double from the stream
Definition: streams.hh:210
void readChar(char &value)
read a char from the stream
Definition: streams.hh:261
Traits::InStreamType InStreamType
type of the implementation (Barton-Nackman)
Definition: streams.hh:188
void readFloat(float &value)
read a float from the stream
Definition: streams.hh:221
Definition: virtualstreams.hh:70
uint64_t readUnsignedInt64()
read an uint64_t from the stream
Definition: streams.hh:340
void readDouble(double &value)
read a double from the stream
Definition: streams.hh:201
int readChar()
read a char from the stream
Definition: streams.hh:270
void writeString(const std::string &s)
write a string to the stream
Definition: streams.hh:123
Definition: streams.hh:21
void writeUnsignedInt(unsigned int value)
write an unsigned int to the stream
Definition: streams.hh:132
void readInt(int &value)
read an int from the stream
Definition: streams.hh:241
abstract interface for an output stream
Definition: streams.hh:44
void writeFloat(const float value)
write a float to the stream
Definition: streams.hh:87
Definition: bartonnackmaninterface.hh:15