libMesh
xdr_cxx.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 // I see a redundant declaration warning here on Ubuntu 20.10
33 #include "libmesh/ignore_warnings.h"
34 # include <rpc/rpc.h>
35 # include <rpc/xdr.h>
36 #include "libmesh/restore_warnings.h"
37 #endif
38 
39 #include <complex>
40 #include <iosfwd>
41 #include <vector>
42 #include <string>
43 #include <string_view>
44 
45 const unsigned int xdr_MAX_STRING_LENGTH=256;
46 
47 #ifndef LIBMESH_DEFAULT_SINGLE_PRECISION
48 #define xdr_REAL xdr_double
49 #else
50 #define xdr_REAL xdr_float
51 #endif
52 
53 namespace libMesh
54 {
55 
67 class Xdr
68 {
69 
70 public:
71 
76  Xdr (std::string name="", const XdrMODE m=UNKNOWN);
77 
82  Xdr (std::ostream & stream);
87  Xdr (std::istream & stream);
88 
92  ~Xdr ();
93 
97  void open (std::string name);
98 
102  void close();
103 
108  bool is_open() const;
109 
117  bool is_eof();
118 
123  bool reading() const { return ((mode == DECODE) || (mode == READ)); }
124 
129  bool writing() const { return ((mode == ENCODE) || (mode == WRITE)); }
130 
135  XdrMODE access_mode () const { return mode; }
136 
137  // Data access methods
138 
142  template <typename T>
143  void data(T & a, std::string_view comment="");
144 
148  template <typename T>
149  Xdr & operator << (T & a) { libmesh_assert (writing()); data(a); return *this; }
150 
154  template <typename T>
155  Xdr & operator >> (T & a) { libmesh_assert (reading()); data(a); return *this; }
156 
160  template <typename T>
161  void data_stream (T * val, const unsigned int len, const unsigned int line_break=libMesh::invalid_uint);
162 
166  void comment (std::string &);
167 
171  void set_version(int ver) { version_number = ver; }
172 
176  int version() const { return version_number; }
177 
178 private:
179 
183  template <typename T>
184  void do_read(T & a);
185 
186  template <typename T>
187  void do_read(std::complex<T> & a);
188 
189  template <typename T>
190  void do_read(std::vector<T> & a);
191 
192  template <typename T>
193  void do_read(std::vector<std::complex<T>> & a);
194 
198  template <typename T>
199  void do_write(T & a);
200 
201  template <typename T>
202  void do_write(std::complex<T> & a);
203 
204  template <typename T>
205  void do_write(std::vector<T> & a);
206 
207  template <typename T>
208  void do_write(std::vector<std::complex<T>> & a);
209 
213  template <typename T>
214  void _complex_data_stream (std::complex<T> * val, const unsigned int len,
215  const unsigned int line_break);
216 
220  template <typename XFP>
221  void _xfp_data_stream (XFP * val, const unsigned int len,
222 #ifdef LIBMESH_HAVE_XDR
223  xdrproc_t
224 #else
225  void *
226 #endif
227  xdr_proc,
228  const unsigned int line_break,
229  const int n_digits);
230 
234  const XdrMODE mode;
235 
239  std::string file_name;
240 
241 #ifdef LIBMESH_HAVE_XDR
242 
247  std::unique_ptr<XDR> xdrs;
248 
252  FILE * fp;
253 
254 #endif
255 
259  std::unique_ptr<std::istream> in;
260 
264  std::unique_ptr<std::ostream> out;
265 
269  const int comm_len;
271 
276 
281 };
282 
283 
284 } // namespace libMesh
285 
286 
287 #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:42
~Xdr()
Destructor.
Definition: xdr_cxx.C:161
FILE * fp
File pointer.
Definition: xdr_cxx.h:252
Xdr(std::string name="", const XdrMODE m=UNKNOWN)
File-based constructor.
Definition: xdr_cxx.C:105
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:269
bool writing() const
Definition: xdr_cxx.h:129
std::string file_name
The file name.
Definition: xdr_cxx.h:239
const unsigned int xdr_MAX_STRING_LENGTH
Definition: xdr_cxx.h:45
void do_write(T &a)
Helper method for writing different data types.
Definition: xdr_cxx.C:731
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition: libmesh.h:286
char comm[xdr_MAX_STRING_LENGTH]
Definition: xdr_cxx.h:270
void comment(std::string &)
Writes or reads (ignores) a comment line.
Definition: xdr_cxx.C:1300
void close()
Closes the file if it is open.
Definition: xdr_cxx.C:279
Xdr & operator>>(T &a)
Same, but provides an istream like interface.
Definition: xdr_cxx.h:155
int version() const
Gets the version of the file that is being read.
Definition: xdr_cxx.h:176
bool xzipped_file
Definition: xdr_cxx.h:275
bool bzipped_file
Definition: xdr_cxx.h:275
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:234
void do_read(T &a)
Helper method for reading different data types.
Definition: xdr_cxx.C:666
bool gzipped_file
Are we reading/writing zipped files?
Definition: xdr_cxx.h:275
void _complex_data_stream(std::complex< T > *val, const unsigned int len, const unsigned int line_break)
Helper method for complex types.
Definition: xdr_cxx.C:1168
XdrMODE
Defines an enum for read/write mode in Xdr format.
Definition: enum_xdr_mode.h:35
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:780
std::unique_ptr< std::istream > in
The input file stream.
Definition: xdr_cxx.h:259
int version_number
Version of the file being read.
Definition: xdr_cxx.h:280
std::unique_ptr< std::ostream > out
The output file stream.
Definition: xdr_cxx.h:264
libmesh_assert(ctx)
XdrMODE access_mode() const
Definition: xdr_cxx.h:135
Xdr & operator<<(T &a)
Same, but provides an ostream like interface.
Definition: xdr_cxx.h:149
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:67
bool is_eof()
Definition: xdr_cxx.C:397
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
Definition: xdr_cxx.h:247
void set_version(int ver)
Sets the version of the file that is being read.
Definition: xdr_cxx.h:171
void _xfp_data_stream(XFP *val, const unsigned int len, #ifdef LIBMESH_HAVE_XDR xdrproc_t #else void *#endif xdr_proc, const unsigned int line_break, const int n_digits)
Helper method for extended FP types.
bool is_open() const
Definition: xdr_cxx.C:347
void open(std::string name)
Opens the file.
Definition: xdr_cxx.C:168
bool reading() const
Definition: xdr_cxx.h:123
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:845