xdr_cxx.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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" // READ, WRITE, etc.
27 #include "libmesh/auto_ptr.h" // deprecated
28 
29 // C++ includes
30 #include <memory>
31 #include <cstdio> // FILE
32 #ifdef LIBMESH_HAVE_XDR
33 # include <rpc/rpc.h>
34 # include <rpc/xdr.h>
35 #endif
36 
37 #include <iosfwd>
38 #include <vector>
39 #include <string>
40 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
41 # include <complex>
42 #endif
43 
44 const unsigned int xdr_MAX_STRING_LENGTH=256;
45 
46 #ifndef LIBMESH_DEFAULT_SINGLE_PRECISION
47 #define xdr_REAL xdr_double
48 #else
49 #define xdr_REAL xdr_float
50 #endif
51 
52 namespace libMesh
53 {
54 
66 class Xdr
67 {
68 
69 public:
70 
75  Xdr (const std::string & name="", const XdrMODE m=UNKNOWN);
76 
80  ~Xdr ();
81 
85  void open (const std::string & name);
86 
90  void close();
91 
96  bool is_open() const;
97 
105  bool is_eof();
106 
111  bool reading() const { return ((mode == DECODE) || (mode == READ)); }
112 
117  bool writing() const { return ((mode == ENCODE) || (mode == WRITE)); }
118 
123  XdrMODE access_mode () const { return mode; }
124 
125  // Data access methods
126 
130  template <typename T>
131  void data(T & a, const char * comment="");
132 
136  template <typename T>
137  Xdr & operator << (T & a) { libmesh_assert (writing()); data(a); return *this; }
138 
142  template <typename T>
143  Xdr & operator >> (T & a) { libmesh_assert (reading()); data(a); return *this; }
144 
148  template <typename T>
149  void data_stream (T * val, const unsigned int len, const unsigned int line_break=libMesh::invalid_uint);
150 
154  void comment (std::string &);
155 
159  void set_version(int ver) { version_number = ver; }
160 
164  int version() const { return version_number; }
165 
166 private:
167 
171  template <typename T>
172  void do_read(T & a);
173 
174  template <typename T>
175  void do_read(std::complex<T> & a);
176 
177  template <typename T>
178  void do_read(std::vector<T> & a);
179 
180  template <typename T>
181  void do_read(std::vector<std::complex<T>> & a);
182 
186  template <typename T>
187  void do_write(T & a);
188 
189  template <typename T>
190  void do_write(std::complex<T> & a);
191 
192  template <typename T>
193  void do_write(std::vector<T> & a);
194 
195  template <typename T>
196  void do_write(std::vector<std::complex<T>> & a);
197 
201  const XdrMODE mode;
202 
206  std::string file_name;
207 
208 #ifdef LIBMESH_HAVE_XDR
209 
214  std::unique_ptr<XDR> xdrs;
215 
219  FILE * fp;
220 
221 #endif
222 
226  std::unique_ptr<std::istream> in;
227 
231  std::unique_ptr<std::ostream> out;
232 
236  const int comm_len;
238 
243 
248 };
249 
250 
251 } // namespace libMesh
252 
253 
254 #endif // LIBMESH_XDR_CXX_H
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
FILE * fp
Definition: xdr_cxx.h:219
const int comm_len
Definition: xdr_cxx.h:236
bool writing() const
Definition: xdr_cxx.h:117
void data(T &a, const char *comment="")
Definition: xdr_cxx.C:761
std::string file_name
Definition: xdr_cxx.h:206
const unsigned int xdr_MAX_STRING_LENGTH
Definition: xdr_cxx.h:44
void do_write(T &a)
Definition: xdr_cxx.C:720
const unsigned int invalid_uint
Definition: libmesh.h:245
char comm[xdr_MAX_STRING_LENGTH]
Definition: xdr_cxx.h:237
void comment(std::string &)
Definition: xdr_cxx.C:1519
void close()
Definition: xdr_cxx.C:273
Xdr & operator>>(T &a)
Definition: xdr_cxx.h:143
int version() const
Definition: xdr_cxx.h:164
bool xzipped_file
Definition: xdr_cxx.h:242
bool bzipped_file
Definition: xdr_cxx.h:242
const XdrMODE mode
Definition: xdr_cxx.h:201
void do_read(T &a)
Definition: xdr_cxx.C:655
bool gzipped_file
Definition: xdr_cxx.h:242
std::unique_ptr< std::istream > in
Definition: xdr_cxx.h:226
int version_number
Definition: xdr_cxx.h:247
std::unique_ptr< std::ostream > out
Definition: xdr_cxx.h:231
XdrMODE access_mode() const
Definition: xdr_cxx.h:123
Xdr & operator<<(T &a)
Definition: xdr_cxx.h:137
C++ interface for the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:66
bool is_eof()
Definition: xdr_cxx.C:391
std::unique_ptr< XDR > xdrs
Definition: xdr_cxx.h:214
void set_version(int ver)
Definition: xdr_cxx.h:159
bool is_open() const
Definition: xdr_cxx.C:341
void open(const std::string &name)
Definition: xdr_cxx.C:162
Xdr(const std::string &name="", const XdrMODE m=UNKNOWN)
Definition: xdr_cxx.C:136
bool reading() const
Definition: xdr_cxx.h:111
void data_stream(T *val, const unsigned int len, const unsigned int line_break=libMesh::invalid_uint)
Definition: xdr_cxx.C:825