libMesh
xdr_cxx.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_XDR_CXX_H
21 #define LIBMESH_XDR_CXX_H
22 
23 // Local includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/libmesh.h"
26 #include "libmesh/enum_xdr_mode.h"
27 #include "libmesh/auto_ptr.h"
28 
29 // C++ includes
30 #ifdef LIBMESH_HAVE_XDR
31 # ifdef LIBMESH_HAVE_RPC_RPC_H
32 # include <rpc/rpc.h>
33 # elif LIBMESH_HAVE_RPC_XDR_H
34 # include <rpc/xdr.h>
35 # endif
36 #endif
37 
38 #include <cstdio> // FILE
39 #include <iosfwd>
40 #include <vector>
41 #include <string>
42 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
43 # include <complex>
44 #endif
45 
46 const unsigned int xdr_MAX_STRING_LENGTH=256;
47 
48 #ifndef LIBMESH_DEFAULT_SINGLE_PRECISION
49 #define xdr_REAL xdr_double
50 #else
51 #define xdr_REAL xdr_float
52 #endif
53 
54 namespace libMesh
55 {
56 
68 class Xdr
69 {
70 
71 public:
72 
77  Xdr (const std::string & name="", const XdrMODE m=UNKNOWN);
78 
82  ~Xdr ();
83 
87  void open (const std::string & name);
88 
92  void close();
93 
98  bool is_open() const;
99 
107  bool is_eof();
108 
113  bool reading() const { return ((mode == DECODE) || (mode == READ)); }
114 
119  bool writing() const { return ((mode == ENCODE) || (mode == WRITE)); }
120 
125  XdrMODE access_mode () const { return mode; }
126 
127  // Data access methods
128 
132  template <typename T>
133  void data(T & a, const char * comment="");
134 
138  template <typename T>
139  Xdr & operator << (T & a) { libmesh_assert (writing()); data(a); return *this; }
140 
144  template <typename T>
145  Xdr & operator >> (T & a) { libmesh_assert (reading()); data(a); return *this; }
146 
150  template <typename T>
151  void data_stream (T * val, const unsigned int len, const unsigned int line_break=libMesh::invalid_uint);
152 
156  void comment (std::string &);
157 
161  void set_version(int ver) { version_number = ver; }
162 
166  int version() const { return version_number; }
167 
168 private:
169 
173  template <typename T>
174  void do_read(T & a);
175 
176  template <typename T>
177  void do_read(std::complex<T> & a);
178 
179  template <typename T>
180  void do_read(std::vector<T> & a);
181 
182  template <typename T>
183  void do_read(std::vector<std::complex<T>> & a);
184 
188  template <typename T>
189  void do_write(T & a);
190 
191  template <typename T>
192  void do_write(std::complex<T> & a);
193 
194  template <typename T>
195  void do_write(std::vector<T> & a);
196 
197  template <typename T>
198  void do_write(std::vector<std::complex<T>> & a);
199 
203  const XdrMODE mode;
204 
208  std::string file_name;
209 
210 #ifdef LIBMESH_HAVE_XDR
211 
217 
221  FILE * fp;
222 
223 #endif
224 
229 
234 
238  const int comm_len;
240 
245 
250 };
251 
252 
253 } // namespace libMesh
254 
255 
256 #endif // LIBMESH_XDR_CXX_H
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
~Xdr()
Destructor.
Definition: xdr_cxx.C:153
FILE * fp
File pointer.
Definition: xdr_cxx.h:221
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:238
void data(T &a, const char *comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:761
std::string file_name
The file name.
Definition: xdr_cxx.h:208
const unsigned int xdr_MAX_STRING_LENGTH
Definition: xdr_cxx.h:46
void do_write(T &a)
Helper method for writing different data types.
Definition: xdr_cxx.C:720
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value.
Definition: libmesh.h:184
UniquePtr< std::istream > in
The input file stream.
Definition: xdr_cxx.h:228
char comm[xdr_MAX_STRING_LENGTH]
Definition: xdr_cxx.h:239
void comment(std::string &)
Writes or reads (ignores) a comment line.
Definition: xdr_cxx.C:1519
void close()
Closes the file if it is open.
Definition: xdr_cxx.C:271
Xdr & operator>>(T &a)
Same, but provides an istream like interface.
Definition: xdr_cxx.h:145
bool xzipped_file
Definition: xdr_cxx.h:244
XdrMODE access_mode() const
Definition: xdr_cxx.h:125
bool bzipped_file
Definition: xdr_cxx.h:244
The libMesh namespace provides an interface to certain functionality in the library.
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:203
void do_read(T &a)
Helper method for reading different data types.
Definition: xdr_cxx.C:655
bool gzipped_file
Are we reading/writing zipped files?
Definition: xdr_cxx.h:244
libmesh_assert(j)
std::unique_ptr< T > UniquePtr
Definition: auto_ptr.h:46
XdrMODE
Defines an enum for read/write mode in Xdr format.
Definition: enum_xdr_mode.h:32
int version_number
Version of the file being read.
Definition: xdr_cxx.h:249
UniquePtr< XDR > xdrs
Pointer to the standard XDR struct.
Definition: xdr_cxx.h:216
bool reading() const
Definition: xdr_cxx.h:113
int version() const
Gets the version of the file that is being read.
Definition: xdr_cxx.h:166
bool writing() const
Definition: xdr_cxx.h:119
Xdr & operator<<(T &a)
Same, but provides an ostream like interface.
Definition: xdr_cxx.h:139
UniquePtr< std::ostream > out
The output file stream.
Definition: xdr_cxx.h:233
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:68
bool is_eof()
Definition: xdr_cxx.C:389
void set_version(int ver)
Sets the version of the file that is being read.
Definition: xdr_cxx.h:161
bool is_open() const
Definition: xdr_cxx.C:339
void open(const std::string &name)
Opens the file.
Definition: xdr_cxx.C:160
Xdr(const std::string &name="", const XdrMODE m=UNKNOWN)
Constructor.
Definition: xdr_cxx.C:134
void data_stream(T *val, const unsigned int len, const unsigned int line_break=libMesh::invalid_uint)
Inputs or outputs a raw data stream.
Definition: xdr_cxx.C:825