PSCF v1.2
BinaryFileOArchive.h
1#ifndef UTIL_BINARY_FILE_O_ARCHIVE_H
2#define UTIL_BINARY_FILE_O_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 "Byte.h"
12#include "serialize.h"
13
14#include <util/space/Vector.h>
15#include <util/space/IntVector.h>
16
17#include <complex>
18#include <vector>
19#include <string>
20#include <iostream>
21
22namespace Util
23{
24
31 {
32
33 public:
34
36 static bool is_saving();
37
39 static bool is_loading();
40
45
51 BinaryFileOArchive(std::string filename);
52
58 BinaryFileOArchive(std::ofstream& file);
59
63 virtual ~BinaryFileOArchive();
64
68 std::ofstream& file();
69
70 // Overloaded << (insertion) and & operators
71
79 template <typename T>
81
89 template <typename T>
91
101 template <typename T, size_t N>
102 BinaryFileOArchive& operator << (T (& data)[N]);
103
111 template <typename T, size_t N>
112 BinaryFileOArchive& operator & (T (& data)[N]);
113
114 // Pack functions and function templates
115
121 template <typename T>
122 void pack(const T& data);
123
130 template <typename T>
131 void pack(const T* array, int n);
132
144 template <typename T>
145 void pack(const T* array, int m, int n, int np);
146
147 private:
148
150 std::ofstream* filePtr_;
151
153 unsigned int version_;
154
156 bool createdFile_;
157
158 };
159
160 // Inline static member functions
161
163 { return true; }
164
166 { return false; }
167
168 // Overloaded << and & operators
169
170 /*
171 * Save (write) one object to this archive via the << operator.
172 */
173 template <typename T>
175 {
176 serialize(*this, data, version_);
177 return *this;
178 }
179
180 /*
181 * Write one object.
182 */
183 template <typename T>
185 {
186 serialize(*this, data, version_);
187 return *this;
188 }
189
190 /*
191 * Save a fixed size array of objects via operator <<.
192 */
193 template <typename T, size_t N>
195 {
196 for (size_t i=0; i < N; ++i) {
197 serialize(*this, data[i], version_);
198 }
199 return *this;
200 }
201
202 /*
203 * Save a fixed size array of objects via operator &.
204 */
205 template <typename T, size_t N>
207 {
208 for (size_t i=0; i < N; ++i) {
209 serialize(*this, data[i], version_);
210 }
211 return *this;
212 }
213
214 // Method templates
215
216 /*
217 * Bitwise pack a single object of type T.
218 */
219 template <typename T>
220 inline void BinaryFileOArchive::pack(const T& data)
221 { filePtr_->write( (char*)(&data), sizeof(T)); }
222
223 /*
224 * Bitwise pack a C-array of objects of type T.
225 */
226 template <typename T>
227 inline void BinaryFileOArchive::pack(const T* array, int n)
228 {
229 for (int i=0; i < n; ++i) {
230 filePtr_->write( (char*)(&array[i]), sizeof(T));
231 }
232 }
233
234 /*
235 * Bitwise pack a 2D C-array of objects of type T.
236 */
237 template <typename T>
238 inline void BinaryFileOArchive::pack(const T* array, int m, int n, int np)
239 {
240 int i, j;
241 for (i=0; i < m; ++i) {
242 for (j=0; j < n; ++j) {
243 filePtr_->write( (char*)(&array[i*np + j]), sizeof(T));
244 }
245 }
246 }
247
248 // Explicit serialize functions for primitive types
249
250 /*
251 * Save a bool to a BinaryFileOArchive.
252 */
253 template <>
254 inline void serialize(BinaryFileOArchive& ar, bool& data,
255 const unsigned int version)
256 { ar.pack(data); }
257
258 /*
259 * Save a char to a BinaryFileOArchive.
260 */
261 template <>
262 inline void serialize(BinaryFileOArchive& ar, char& data,
263 const unsigned int version)
264 { ar.pack(data); }
265
266 /*
267 * Save an unsigned int to a BinaryFileOArchive.
268 */
269 template <>
270 inline void serialize(BinaryFileOArchive& ar, unsigned int& data,
271 const unsigned int version)
272 { ar.pack(data); }
273
274 /*
275 * Save an int to a BinaryFileOArchive.
276 */
277 template <>
278 inline void serialize(BinaryFileOArchive& ar, int& data,
279 const unsigned int version)
280 { ar.pack(data); }
281
282 /*
283 * Save an unsigned long int to a BinaryFileOArchive.
284 */
285 template <>
286 inline void serialize(BinaryFileOArchive& ar, unsigned long& data,
287 const unsigned int version)
288 { ar.pack(data); }
289
290 /*
291 * Save a long int to a BinaryFileOArchive.
292 */
293 template <>
294 inline void serialize(BinaryFileOArchive& ar, long& data,
295 const unsigned int version)
296 { ar.pack(data); }
297
298 /*
299 * Save a float to a BinaryFileOArchive.
300 */
301 template <>
302 inline void serialize(BinaryFileOArchive& ar, float& data,
303 const unsigned int version)
304 { ar.pack(data); }
305
306 /*
307 * Save an double to a BinaryFileOArchive.
308 */
309 template <>
310 inline void serialize(BinaryFileOArchive& ar, double& data,
311 const unsigned int version)
312 { ar.pack(data); }
313
314 /*
315 * Save a std::complex<double> to a BinaryFileOArchive.
316 */
317 template <typename T>
318 void serialize(BinaryFileOArchive& ar, std::vector<T>& data,
319 const unsigned int version)
320 {
321 size_t size = data.size();
322 ar.pack(size);
323 for (size_t i = 0; i < size; ++i) {
324 ar & data[i];
325 }
326 }
327
328 // Explicit serialize functions for std library types
329
330 /*
331 * Save a std::complex<float> to a BinaryFileOArchive.
332 */
333 template <>
334 inline
335 void serialize(BinaryFileOArchive& ar, std::complex<float>& data,
336 const unsigned int version)
337 { ar.pack(data); }
338
339 /*
340 * Save a std::complex<double> to a BinaryFileOArchive.
341 */
342 template <>
343 inline
344 void serialize(BinaryFileOArchive& ar, std::complex<double>& data,
345 const unsigned int version)
346 { ar.pack(data); }
347
348 /*
349 * Save a std::string to a BinaryFileOArchive.
350 */
351 template <>
352 inline void serialize(BinaryFileOArchive& ar, std::string& data,
353 const unsigned int version)
354 {
355 size_t size = data.size() + 1; // the +1 is for the NULL
356 ar.pack(size);
357 const char* temp = data.c_str();
358 ar.pack(temp, size);
359 }
360
361 // Explicit serialize functions for namespace Util
362
363 /*
364 * Save a Util::Vector to a BinaryFileOArchive.
365 */
366 template <>
367 inline void serialize(BinaryFileOArchive& ar, Vector& data,
368 const unsigned int version)
369 { ar.pack(data); }
370
371 /*
372 * Save a Util::IntVector to a BinaryFileOArchive.
373 */
374 template <>
375 inline void serialize(BinaryFileOArchive& ar, IntVector& data,
376 const unsigned int version)
377 { ar.pack(data); }
378
379}
380#endif
Saving / output archive for binary ostream.
static bool is_loading()
Returns false;.
BinaryFileOArchive & operator<<(T &data)
Save (write) one object of type T via the << operator.
BinaryFileOArchive & operator&(T &data)
Save (write) one object of type T via the & operator.
std::ofstream & file()
Get the underlying ifstream by reference.
static bool is_saving()
Returns true;.
virtual ~BinaryFileOArchive()
Destructor.
void pack(const T &data)
Pack one object of type T.
Utility classes for scientific computation.