dune-fem  2.4.1-rc
xdrstreams.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_XDRSTREAMS_HH
2 #define DUNE_FEM_XDRSTREAMS_HH
3 
4 #include <cassert>
5 #include <rpc/types.h>
6 #include <rpc/xdr.h>
7 
8 #include <dune/common/exceptions.hh>
10 
11 namespace Dune
12 {
13 
14  namespace Fem
15  {
16 
17  template< class OutStreamImp >
19  {
20  typedef OutStreamImp OutStreamType;
21  };
22 
23 
24 
35  template< class OutStreamImp >
37  : public OutStreamInterface< XDROutStreamTraits< OutStreamImp > >
38  {
41 
42  public:
44  typedef OutStreamImp OutStreamType;
45 
48 
49  enum { maxStringSize = 2<<18 };
50 
51  protected:
52  XDR xdrs_;
53 
54  protected:
55  using BaseType::writeError;
56 
57  protected:
59  {}
60 
61  public:
63  void writeDouble ( double value )
64  {
65  if( xdr_double( xdrs(), &value ) == 0 )
66  writeError();
67  }
68 
70  void writeFloat ( float value )
71  {
72  if( xdr_float( xdrs(), &value ) == 0 )
73  writeError();
74  }
75 
77  void writeInt ( int value )
78  {
79  if( xdr_int( xdrs(), &value ) == 0 )
80  writeError();
81  }
82 
84  void writeChar ( char value )
85  {
86  if( xdr_char( xdrs(), &value ) == 0 )
87  writeError();
88  }
89 
91  void writeBool ( const bool value )
92  {
93  // convert to character and write
94  char val = ( value == true ) ? 1 : 0;
95  writeChar( val );
96  }
97 
99  void writeString ( const std :: string &s )
100  {
101  assert( s.size() < maxStringSize );
102  const char *cs = s.c_str();
103  if( xdr_string( xdrs(), (char **)&cs, maxStringSize ) == 0 )
104  writeError();
105  }
106 
108  void writeUnsignedInt ( unsigned int value )
109  {
110  if( xdr_u_int( xdrs(), &value ) == 0 )
111  writeError();
112  }
113 
115  void writeUnsignedInt64 ( uint64_t value )
116  {
117 #ifdef XDR_UINT64_FUNC
118  // use u_int64_t since xdr_u_long is buggy
119  u_int64_t val = value ;
120  // XDR_UINT64_FUNC is defined in config.h
121  if( XDR_UINT64_FUNC( xdrs(), &val ) == 0 )
122  writeError();
123 #else
124  DUNE_THROW(NotImplemented,"xdr_uint64_t is missing");
125 #endif
126  }
127 
128  protected:
129  XDR *xdrs ()
130  {
131  return &xdrs_;
132  }
133  };
134 
135 
136 
137  template< class InStreamImp >
139  {
140  typedef InStreamImp InStreamType;
141  };
142 
143 
154  template< class InStreamImp >
156  : public InStreamInterface< XDRInStreamTraits< InStreamImp > >
157  {
160 
161  public:
163  typedef InStreamImp InStreamType;
164 
167 
168  private:
169  enum { maxStringSize = 2<<18 };
170 
171  protected:
172  XDR xdrs_;
173 
174  protected:
175  using BaseType::readError;
176 
177  protected:
179  {}
180 
181  public:
183  void readDouble ( double &value )
184  {
185  if( xdr_double( xdrs(), &value ) == 0 )
186  readError();
187  }
188 
190  void readFloat ( float &value )
191  {
192  if( xdr_float( xdrs(), &value ) == 0 )
193  readError();
194  }
195 
197  void readInt ( int &value )
198  {
199  if( xdr_int( xdrs(), &value ) == 0 )
200  readError();
201  }
202 
204  void readChar ( char &value )
205  {
206  if( xdr_char( xdrs(), &value ) == 0 )
207  readError();
208  }
209 
211  void readBool ( bool &value )
212  {
213  char val;
214  readChar( val );
215  // convert to boolean
216  value = ( val == 1 ) ? true : false;
217  }
218 
220  void readString ( std::string &s )
221  {
222  char data[ maxStringSize ];
223  char *cs = &(data[ 0 ]);
224  if( xdr_string( xdrs(), &cs, maxStringSize ) == 0 )
225  readError();
226  s = data;
227  }
228 
230  void readUnsignedInt ( unsigned int &value )
231  {
232  if( xdr_u_int( xdrs(), &value ) == 0 )
233  readError();
234  }
235 
237  void readUnsignedInt64 ( uint64_t &value )
238  {
239 #ifdef XDR_UINT64_FUNC
240  // use u_int64_t since xdr_u_long is buggy
241  u_int64_t val ;
242  // XDR_UINT64_FUNC is defined in config.h
243  if( XDR_UINT64_FUNC( xdrs(), &val ) == 0 )
244  readError();
245  else
246  value = val;
247 #else
248  DUNE_THROW(NotImplemented,"xdr_uint64_t is missing");
249 #endif
250  }
251 
252  protected:
253  XDR *xdrs ()
254  {
255  return &xdrs_;
256  }
257  };
258 
259 
260 
268  : public XDRBasicOutStream< XDRFileOutStream >
269  {
270  private:
271  typedef XDRFileOutStream ThisType;
273 
274  protected:
275  using BaseType :: xdrs;
276 
277  protected:
278  FILE *file_;
279 
280  public:
287  explicit XDRFileOutStream ( const std::string &filename,
288  const bool append = false )
289  {
290  const char * flags = ( append ) ? "ab" : "wb";
291  file_ = fopen( filename.c_str(), flags );
292  if( file_ == 0 )
293  DUNE_THROW( IOError, "XDRFileOutStream: Unable to open file '" << filename << "'." );
294  xdrstdio_create( xdrs(), file_, XDR_ENCODE );
295  }
296 
299  {
300  xdr_destroy( xdrs() );
301  fclose( file_ );
302  }
303 
305  inline void flush ()
306  {
307  fflush( file_ );
308  }
309  };
310 
311 
312 
320  : public XDRBasicInStream< XDRFileInStream >
321  {
322  private:
323  typedef XDRFileInStream ThisType;
325 
326  protected:
327  using BaseType :: xdrs;
328 
329  protected:
330  FILE *file_;
331 
332  public:
339  explicit XDRFileInStream ( const std::string &filename,
340  const size_t pos = 0 )
341  {
342  file_ = fopen( filename.c_str(), "rb" );
343  if( file_ == 0 )
344  DUNE_THROW( IOError, "XDRFileInStream: Unable to open file '" << filename << "'." );
345 
346  // if pos = 0 this will do nothing
347  fseek( file_, pos , SEEK_SET );
348 
349  xdrstdio_create( xdrs(), file_, XDR_DECODE );
350  }
351 
355  {
356  xdr_destroy( xdrs() );
357  fclose( file_ );
358  }
359  };
360 
361  } // namespace Fem
362 
363 } // namespace Dune
364 
365 #endif // #ifndef DUNE_FEM_XDRSTREAMS_HH
void flush()
flush the stream
Definition: xdrstreams.hh:305
void writeInt(int value)
write an int to the stream
Definition: xdrstreams.hh:77
XDROutStreamTraits< OutStreamType > Traits
type of the traits
Definition: xdrstreams.hh:47
FILE * file_
Definition: xdrstreams.hh:330
XDRFileOutStream(const std::string &filename, const bool append=false)
constructor
Definition: xdrstreams.hh:287
void writeUnsignedInt64(uint64_t value)
write an uint64_t to the stream
Definition: xdrstreams.hh:115
~XDRFileOutStream()
destructor
Definition: xdrstreams.hh:298
InStreamImp InStreamType
Definition: xdrstreams.hh:140
FILE * file_
Definition: xdrstreams.hh:278
XDRBasicInStream()
Definition: xdrstreams.hh:178
void writeUnsignedInt(unsigned int value)
write an unsigned int to the stream
Definition: xdrstreams.hh:108
XDR xdrs_
Definition: xdrstreams.hh:52
XDRBasicOutStream()
Definition: xdrstreams.hh:58
XDR output stream reading from a file.
Definition: xdrstreams.hh:319
void writeString(const std::string &s)
write a string to the stream
Definition: xdrstreams.hh:99
void readInt(int &value)
read an int from the stream
Definition: xdrstreams.hh:197
Definition: xdrstreams.hh:18
void readFloat(float &value)
read a float from the stream
Definition: xdrstreams.hh:190
OutStreamImp OutStreamType
Definition: xdrstreams.hh:20
void readDouble(double &value)
read a double from the stream
Definition: xdrstreams.hh:183
void readUnsignedInt64(uint64_t &value)
read an uint64_t from the stream
Definition: xdrstreams.hh:237
Definition: coordinate.hh:4
base implementation for XDR output streams
Definition: xdrstreams.hh:36
void readBool(bool &value)
read a bool from the stream
Definition: xdrstreams.hh:211
void writeChar(char value)
write a char to the stream
Definition: xdrstreams.hh:84
InStreamImp InStreamType
type of the implementation (Barton-Nackman)
Definition: xdrstreams.hh:163
abstract interface for an input stream
Definition: streams.hh:177
XDRInStreamTraits< InStreamType > Traits
type of the traits
Definition: xdrstreams.hh:166
void writeFloat(float value)
write a float to the stream
Definition: xdrstreams.hh:70
void writeBool(const bool value)
write a bool to the stream
Definition: xdrstreams.hh:91
OutStreamImp OutStreamType
type of the implementaton (Barton-Nackman)
Definition: xdrstreams.hh:44
XDR xdrs_
Definition: xdrstreams.hh:172
Definition: xdrstreams.hh:138
void readString(std::string &s)
read a string from the stream
Definition: xdrstreams.hh:220
void writeDouble(double value)
write a double to the stream
Definition: xdrstreams.hh:63
XDR * xdrs()
Definition: xdrstreams.hh:129
XDR * xdrs()
Definition: xdrstreams.hh:253
base implementation for XDR input streams
Definition: xdrstreams.hh:155
void readChar(char &value)
read a char from the stream
Definition: xdrstreams.hh:204
XDR output stream writing into a file.
Definition: xdrstreams.hh:267
~XDRFileInStream()
destructor
Definition: xdrstreams.hh:354
abstract interface for an output stream
Definition: streams.hh:44
XDRFileInStream(const std::string &filename, const size_t pos=0)
constructor
Definition: xdrstreams.hh:339
void readUnsignedInt(unsigned int &value)
read an unsigned int from the stream
Definition: xdrstreams.hh:230