libMesh
xdr_cxx.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 
28 // C++ includes
29 #include <memory>
30 #include <cstdio> // FILE
31 #ifdef LIBMESH_HAVE_XDR
32 # include <rpc/rpc.h>
33 # include <rpc/xdr.h>
34 #endif
35 
36 #include <iosfwd>
37 #include <vector>
38 #include <string>
39 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
40 # include <complex>
41 #endif
42 
43 const unsigned int xdr_MAX_STRING_LENGTH=256;
44 
45 #ifndef LIBMESH_DEFAULT_SINGLE_PRECISION
46 #define xdr_REAL xdr_double
47 #else
48 #define xdr_REAL xdr_float
49 #endif
50 
51 namespace libMesh
52 {
53 
65 class Xdr
66 {
67 
68 public:
69 
74  Xdr (const std::string & name="", const XdrMODE m=UNKNOWN);
75 
79  ~Xdr ();
80 
84  void open (const std::string & name);
85 
89  void close();
90 
95  bool is_open() const;
96 
104  bool is_eof();
105 
110  bool reading() const { return ((mode == DECODE) || (mode == READ)); }
111 
116  bool writing() const { return ((mode == ENCODE) || (mode == WRITE)); }
117 
122  XdrMODE access_mode () const { return mode; }
123 
124  // Data access methods
125 
129  template <typename T>
130  void data(T & a, const char * comment="");
131 
135  template <typename T>
136  Xdr & operator << (T & a) { libmesh_assert (writing()); data(a); return *this; }
137 
141  template <typename T>
142  Xdr & operator >> (T & a) { libmesh_assert (reading()); data(a); return *this; }
143 
147  template <typename T>
148  void data_stream (T * val, const unsigned int len, const unsigned int line_break=libMesh::invalid_uint);
149 
153  void comment (std::string &);
154 
158  void set_version(int ver) { version_number = ver; }
159 
163  int version() const { return version_number; }
164 
165 private:
166 
170  template <typename T>
171  void do_read(T & a);
172 
173  template <typename T>
174  void do_read(std::complex<T> & a);
175 
176  template <typename T>
177  void do_read(std::vector<T> & a);
178 
179  template <typename T>
180  void do_read(std::vector<std::complex<T>> & a);
181 
185  template <typename T>
186  void do_write(T & a);
187 
188  template <typename T>
189  void do_write(std::complex<T> & a);
190 
191  template <typename T>
192  void do_write(std::vector<T> & a);
193 
194  template <typename T>
195  void do_write(std::vector<std::complex<T>> & a);
196 
200  const XdrMODE mode;
201 
205  std::string file_name;
206 
207 #ifdef LIBMESH_HAVE_XDR
208 
213  std::unique_ptr<XDR> xdrs;
214 
218  FILE * fp;
219 
220 #endif
221 
225  std::unique_ptr<std::istream> in;
226 
230  std::unique_ptr<std::ostream> out;
231 
235  const int comm_len;
237 
242 
247 };
248 
249 
250 } // namespace libMesh
251 
252 
253 #endif // LIBMESH_XDR_CXX_H
libMesh::invalid_uint
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value.
Definition: libmesh.h:249
libMesh::Xdr::gzipped_file
bool gzipped_file
Are we reading/writing zipped files?
Definition: xdr_cxx.h:241
libMesh::Xdr::access_mode
XdrMODE access_mode() const
Definition: xdr_cxx.h:122
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::Xdr::is_eof
bool is_eof()
Definition: xdr_cxx.C:391
libMesh::Xdr
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:65
libMesh::Xdr::file_name
std::string file_name
The file name.
Definition: xdr_cxx.h:205
libMesh::Xdr::version
int version() const
Gets the version of the file that is being read.
Definition: xdr_cxx.h:163
libMesh::Xdr::comm_len
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:235
libMesh::WRITE
Definition: enum_xdr_mode.h:40
libMesh::Xdr::Xdr
Xdr(const std::string &name="", const XdrMODE m=UNKNOWN)
Constructor.
Definition: xdr_cxx.C:136
libMesh::Xdr::version_number
int version_number
Version of the file being read.
Definition: xdr_cxx.h:246
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::Xdr::do_write
void do_write(T &a)
Helper method for writing different data types.
Definition: xdr_cxx.C:725
libMesh::Xdr::writing
bool writing() const
Definition: xdr_cxx.h:116
libMesh::Xdr::reading
bool reading() const
Definition: xdr_cxx.h:110
libMesh::Xdr::data
void data(T &a, const char *comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:766
libMesh::XdrMODE
XdrMODE
Defines an enum for read/write mode in Xdr format.
Definition: enum_xdr_mode.h:35
libMesh::DECODE
Definition: enum_xdr_mode.h:39
libMesh::Xdr::mode
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:200
libMesh::Xdr::do_read
void do_read(T &a)
Helper method for reading different data types.
Definition: xdr_cxx.C:660
libMesh::READ
Definition: enum_xdr_mode.h:41
libMesh::Xdr::open
void open(const std::string &name)
Opens the file.
Definition: xdr_cxx.C:162
libMesh::Xdr::data_stream
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:831
xdr_MAX_STRING_LENGTH
const unsigned int xdr_MAX_STRING_LENGTH
Definition: xdr_cxx.h:43
libMesh::UNKNOWN
Definition: enum_xdr_mode.h:37
libMesh::ENCODE
Definition: enum_xdr_mode.h:38
libMesh::Xdr::close
void close()
Closes the file if it is open.
Definition: xdr_cxx.C:273
libMesh::Xdr::xzipped_file
bool xzipped_file
Definition: xdr_cxx.h:241
libMesh::Xdr::is_open
bool is_open() const
Definition: xdr_cxx.C:341
libMesh::Xdr::out
std::unique_ptr< std::ostream > out
The output file stream.
Definition: xdr_cxx.h:230
libMesh::Xdr::fp
FILE * fp
File pointer.
Definition: xdr_cxx.h:218
libMesh::Xdr::bzipped_file
bool bzipped_file
Definition: xdr_cxx.h:241
libMesh::Xdr::set_version
void set_version(int ver)
Sets the version of the file that is being read.
Definition: xdr_cxx.h:158
libMesh::Xdr::operator<<
Xdr & operator<<(T &a)
Same, but provides an ostream like interface.
Definition: xdr_cxx.h:136
libMesh::Xdr::in
std::unique_ptr< std::istream > in
The input file stream.
Definition: xdr_cxx.h:225
libMesh::Xdr::xdrs
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
Definition: xdr_cxx.h:213
libMesh::Xdr::comment
void comment(std::string &)
Writes or reads (ignores) a comment line.
Definition: xdr_cxx.C:1661
libMesh::Xdr::operator>>
Xdr & operator>>(T &a)
Same, but provides an istream like interface.
Definition: xdr_cxx.h:142
libMesh::Xdr::~Xdr
~Xdr()
Destructor.
Definition: xdr_cxx.C:155
libMesh::Quality::name
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
libMesh::Xdr::comm
char comm[xdr_MAX_STRING_LENGTH]
Definition: xdr_cxx.h:236