Simpatico  v1.10
XdrFileIArchive.h
1 #ifndef UTIL_XDR_FILE_I_ARCHIVE_H
2 #define UTIL_XDR_FILE_I_ARCHIVE_H
3 
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include "serialize.h"
12 #include <util/space/Vector.h>
13 #include <util/space/IntVector.h>
14 
15 #include <complex>
16 #include <vector>
17 #include <string>
18 
19 #include <rpc/rpc.h>
20 #include <rpc/xdr.h>
21 #include <stdio.h>
22 
23 namespace Util
24 {
25 
40  {
41 
42  public:
43 
45  static bool is_saving();
46 
48  static bool is_loading();
49 
54 
60  XdrFileIArchive(std::string filename);
61 
67  XdrFileIArchive(std::ofstream& file);
68 
72  virtual ~XdrFileIArchive();
73 
79  void init(FILE* file);
80 
84  template <typename T>
85  XdrFileIArchive& operator & (T& data);
86 
90  template <typename T>
91  XdrFileIArchive& operator >> (T& data);
92 
96  FILE* file();
97 
101  XDR* xdrPtr();
102 
103  private:
104 
106  XDR xdr_;
107 
109  FILE* filePtr_;
110 
112  unsigned int version_;
113 
115  bool createdFile_;
116 
117  };
118 
119  // Inline methods
120 
122  { return false; }
123 
125  { return true; }
126 
127  // Inline non-static methods
128 
129  /*
130  * Write one object.
131  */
132  template <typename T>
134  {
135  serialize(*this, data, version_);
136  return *this;
137  }
138 
139  /*
140  * Write one object.
141  */
142  template <typename T>
144  {
145  serialize(*this, data, version_);
146  return *this;
147  }
148 
149  /*
150  * Get the underlying file handle.
151  */
152  inline FILE* XdrFileIArchive::file()
153  { return filePtr_; }
154 
155  /*
156  * Get a pointer to XDR object.
157  */
159  { return &xdr_; }
160 
161  // Explicit serialize functions for primitive types
162 
163  /*
164  * Load a bool from a XdrFileIArchive.
165  */
166  template <>
167  inline void serialize(XdrFileIArchive& ar, bool& data,
168  const unsigned int version)
169  {
170  bool_t temp = data;
171  xdr_bool(ar.xdrPtr(), &temp);
172  }
173 
174  /*
175  * Load a char from a XdrFileIArchive.
176  */
177  template <>
178  inline void serialize(XdrFileIArchive& ar, char& data,
179  const unsigned int version)
180  { xdr_char(ar.xdrPtr(), &data); }
181 
182  /*
183  * Load an unsigned int from a XdrFileIArchive.
184  */
185  template <>
186  inline void serialize(XdrFileIArchive& ar, unsigned int& data,
187  const unsigned int version)
188  { xdr_u_int(ar.xdrPtr(), &data); }
189 
190  /*
191  * Load an int from a XdrFileIArchive.
192  */
193  template <>
194  inline void serialize(XdrFileIArchive& ar, int& data,
195  const unsigned int version)
196  { xdr_int(ar.xdrPtr(), &data); }
197 
198  #if 0
199  /*
200  * Load an unsigned long int from a XdrFileIArchive.
201  */
202  template <>
203  inline void serialize(XdrFileIArchive& ar, unsigned long& data,
204  const unsigned int version)
205  { xdr_u_long(ar.xdrPtr(), &data); }
206 
207  /*
208  * Load a long int from a XdrFileIArchive.
209  */
210  template <>
211  inline void serialize(XdrFileIArchive& ar, long& data,
212  const unsigned int version)
213  { xdr_long(ar.xdrPtr(), &data); }
214  #endif
215 
216  /*
217  * Load a float from a XdrFileIArchive.
218  */
219  template <>
220  inline void serialize(XdrFileIArchive& ar, float& data,
221  const unsigned int version)
222  { xdr_float(ar.xdrPtr(), &data); }
223 
224  /*
225  * Load a double from a XdrFileIArchive.
226  */
227  template <>
228  inline void serialize(XdrFileIArchive& ar, double& data,
229  const unsigned int version)
230  { xdr_double(ar.xdrPtr(), &data); }
231 
232  /*
233  * Load a std::vector from a XdrFileIArchive.
234  */
235  template <typename T>
236  void serialize(XdrFileIArchive& ar, std::vector<T>& data,
237  const unsigned int version)
238  {
239  unsigned int size = data.size();
240  xdr_u_int(ar.xdrPtr(), &size);
241  for (size_t i = 0; i < size; ++i) {
242  ar & data[i];
243  }
244  }
245 
246  // Explicit serialize functions for std library types
247 
248  /*
249  * Load a std::complex<float> from a XdrFileIArchive.
250  */
251  template <>
252  inline
253  void serialize(XdrFileIArchive& ar, std::complex<float>& data,
254  const unsigned int version)
255  {
256  float a;
257  float b;
258  xdr_float(ar.xdrPtr(), &a);
259  xdr_float(ar.xdrPtr(), &b);
260  data = std::complex<float>(a, b);
261  }
262 
263  /*
264  * Load a std::complex<double> from a XdrFileIArchive.
265  */
266  template <>
267  inline
268  void serialize(XdrFileIArchive& ar, std::complex<double>& data,
269  const unsigned int version)
270  {
271  double a;
272  double b;
273  xdr_double(ar.xdrPtr(), &a);
274  xdr_double(ar.xdrPtr(), &b);
275  data = std::complex<double>(a, b);
276  }
277 
278  /*
279  * Load a std::string from a XdrFileIArchive.
280  */
281  template <>
282  void serialize(XdrFileIArchive& ar, std::string& data,
283  const unsigned int version);
284 
285  // Explicit serialize functions for namespace Util
286 
287  /*
288  * Load a Util::Vector from a XdrFileIArchive.
289  */
290  template <>
291  inline void serialize(XdrFileIArchive& ar, Vector& data,
292  const unsigned int version)
293  {
294  ar & data[0];
295  ar & data[1];
296  ar & data[2];
297  }
298 
299  /*
300  * Load a Util::IntVector from a XdrFileIArchive.
301  */
302  template <>
303  inline void serialize(XdrFileIArchive& ar, IntVector& data,
304  const unsigned int version)
305  {
306  ar & data[0];
307  ar & data[1];
308  ar & data[2];
309  }
310 
311 }
312 #endif
A Vector is a Cartesian vector.
Definition: Vector.h:75
XDR * xdrPtr()
Get a pointer to the enclosed XDR object.
XdrFileIArchive & operator&(T &data)
Load one object.
FILE * file()
Get the underlying file handle.
virtual ~XdrFileIArchive()
Destructor.
static bool is_saving()
Returns false.
Utility classes for scientific computation.
Definition: accumulators.mod:1
XdrFileIArchive & operator>>(T &data)
Load one object.
Loading / input archive for binary XDR file.
static bool is_loading()
Returns true.
An IntVector is an integer Cartesian vector.
Definition: IntVector.h:73
XdrFileIArchive()
Constructor.
void init(FILE *file)
Initialize by associating with an open file.