$extrastylesheet
xdr_cxx.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_XDR_CXX_H
00021 #define LIBMESH_XDR_CXX_H
00022 
00023 // Local includes
00024 #include "libmesh/libmesh_common.h"
00025 #include "libmesh/libmesh.h"
00026 #include "libmesh/enum_xdr_mode.h"
00027 #include "libmesh/auto_ptr.h"
00028 
00029 // C++ includes
00030 #ifdef LIBMESH_HAVE_XDR
00031 #  ifdef LIBMESH_HAVE_RPC_RPC_H
00032 #    include <rpc/rpc.h>
00033 #  elif  LIBMESH_HAVE_RPC_XDR_H
00034 #    include <rpc/xdr.h>
00035 #  endif
00036 #endif
00037 
00038 #include <cstdio> // FILE
00039 #include <iosfwd>
00040 #include <vector>
00041 #include <string>
00042 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
00043 # include <complex>
00044 #endif
00045 
00046 const unsigned int xdr_MAX_STRING_LENGTH=256;
00047 
00048 #ifndef LIBMESH_DEFAULT_SINGLE_PRECISION
00049 #define xdr_REAL xdr_double
00050 #else
00051 #define xdr_REAL xdr_float
00052 #endif
00053 
00054 namespace libMesh
00055 {
00056 
00057 
00058 
00059 //--------------------------------------------------------------
00060 // Xdr class definition
00061 
00070 class Xdr
00071 {
00072 
00073 public:
00074 
00079   Xdr (const std::string& name="", const XdrMODE m=UNKNOWN);
00080 
00084   ~Xdr ();
00085 
00089   void open (const std::string& name);
00090 
00094   void close();
00095 
00100   bool is_open() const;
00101 
00106   bool reading() const { return ((mode == DECODE) || (mode == READ)); }
00107 
00112   bool writing() const { return ((mode == ENCODE) || (mode == WRITE)); }
00113 
00118   XdrMODE access_mode () const { return mode; }
00119 
00120   // Data access methods
00121 
00125   template <typename T>
00126   void data(T& a, const char* comment="");
00127 
00131   template <typename T>
00132   Xdr& operator << (T& a) { libmesh_assert (writing()); data(a); return *this; }
00133 
00137   template <typename T>
00138   Xdr& operator >> (T& a) { libmesh_assert (reading()); data(a); return *this; }
00139 
00143   template <typename T>
00144   void data_stream (T *val, const unsigned int len, const unsigned int line_break=libMesh::invalid_uint);
00145 
00149   void comment (std::string &);
00150 
00154   void set_version(int ver) { version_number = ver; }
00155 
00159   int version() const { return version_number; }
00160 
00161 private:
00162 
00166   template <typename T>
00167   void do_read(T& a);
00168 
00169   template <typename T>
00170   void do_read(std::complex<T>& a);
00171 
00172   template <typename T>
00173   void do_read(std::vector<T>& a);
00174 
00175   template <typename T>
00176   void do_read(std::vector<std::complex<T> >& a);
00177 
00181   template <typename T>
00182   void do_write(T& a);
00183 
00184   template <typename T>
00185   void do_write(std::complex<T>& a);
00186 
00187   template <typename T>
00188   void do_write(std::vector<T>& a);
00189 
00190   template <typename T>
00191   void do_write(std::vector<std::complex<T> >& a);
00192 
00196   const XdrMODE mode;
00197 
00201   std::string file_name;
00202 
00203 #ifdef LIBMESH_HAVE_XDR
00204 
00211   XDR* xdrs;
00212 
00216   FILE* fp;
00217 
00218 #endif
00219 
00223   UniquePtr<std::istream> in;
00224 
00228   UniquePtr<std::ostream> out;
00229 
00233   const int comm_len;
00234   char comm[xdr_MAX_STRING_LENGTH];
00235 
00239   bool gzipped_file, bzipped_file, xzipped_file;
00240 
00244   int version_number;
00245 };
00246 
00247 
00248 } // namespace libMesh
00249 
00250 
00251 #endif // LIBMESH_XDR_CXX_H