PSCF v1.1
TextFileIArchive.h
1#ifndef UTIL_TEXT_FILE_I_ARCHIVE_H
2#define UTIL_TEXT_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 "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 <string>
19#include <vector>
20#include <fstream>
21
22namespace Util
23{
24
31 {
32
33 public:
34
36 static bool is_saving();
37
39 static bool is_loading();
40
45
51 TextFileIArchive(std::string filename);
52
58 TextFileIArchive(std::ifstream& file);
59
63 virtual ~TextFileIArchive();
64
68 std::ifstream& file();
69
73 template <typename T>
75
79 template <typename T>
81
87 template <typename T>
88 void unpack(T& data);
89
96 template <typename T>
97 void unpack(T* array, int n);
98
107 template <typename T>
108 void unpack(T* array, int m, int n, int np);
109
110 private:
111
113 std::ifstream* filePtr_;
114
116 unsigned int version_;
117
119 bool createdFile_;
120
121 };
122
123 // Inline methods
124
126 { return false; }
127
129 { return true; }
130
131 // Inline non-static methods
132
133 /*
134 * Load one object.
135 */
136 template <typename T>
138 {
139 serialize(*this, data, version_);
140 return *this;
141 }
142
143 /*
144 * Load one object.
145 */
146 template <typename T>
148 {
149 serialize(*this, data, version_);
150 return *this;
151 }
152
153 // Method templates
154
155 /*
156 * Load a single object of type T.
157 */
158 template <typename T>
159 inline void TextFileIArchive::unpack(T& data)
160 { *filePtr_ >> data; }
161
162 /*
163 * Load a C array.
164 */
165 template <typename T>
166 inline void TextFileIArchive::unpack(T* array, int n)
167 {
168 for (int i=0; i < n; ++i) {
169 *filePtr_ >> array[i];
170 }
171 }
172
173 /*
174 * Bitwise pack a 2D C-array of objects of type T.
175 */
176 template <typename T>
177 void TextFileIArchive::unpack(T* array, int m, int n, int np)
178 {
179 int i, j;
180 for (i = 0; i < m; ++i) {
181 for (j = 0; j < n; ++j) {
182 *filePtr_ >> array[i*np + j];
183 }
184 }
185 }
186
187 /*
188 * Load a single char.
189 */
190 template <>
191 inline void TextFileIArchive::unpack(char& data)
192 { filePtr_->get(data); }
193
194 /*
195 * Load a C-array of characters.
196 */
197 template <>
198 inline void TextFileIArchive::unpack(char* array, int n)
199 {
200 filePtr_->get(array, n+1,'\0');
201 }
202
203 // Explicit serialize functions for primitive types
204
205 /*
206 * Load a bool from a TextFileIArchive.
207 */
208 template <>
209 inline void serialize(TextFileIArchive& ar, bool& data,
210 const unsigned int version)
211 { ar.unpack(data); }
212
213 /*
214 * Load a char from a TextFileIArchive.
215 */
216 template <>
217 inline void serialize(TextFileIArchive& ar, char& data,
218 const unsigned int version)
219 { ar.unpack(data); }
220
221 /*
222 * Load an unsigned int from a TextFileIArchive.
223 */
224 template <>
225 inline void serialize(TextFileIArchive& ar, unsigned int& data,
226 const unsigned int version)
227 { ar.unpack(data); }
228
229 /*
230 * Load an int from a TextFileIArchive.
231 */
232 template <>
233 inline void serialize(TextFileIArchive& ar, int& data,
234 const unsigned int version)
235 { ar.unpack(data); }
236
237 /*
238 * Load an unsigned long int from a TextFileIArchive.
239 */
240 template <>
241 inline void serialize(TextFileIArchive& ar, unsigned long& data,
242 const unsigned int version)
243 { ar.unpack(data); }
244
245 /*
246 * Load a long int from a TextFileIArchive.
247 */
248 template <>
249 inline void serialize(TextFileIArchive& ar, long& data,
250 const unsigned int version)
251 { ar.unpack(data); }
252
253 /*
254 * Load a float from a TextFileIArchive.
255 */
256 template <>
257 inline void serialize(TextFileIArchive& ar, float& data,
258 const unsigned int version)
259 { ar.unpack(data); }
260
261 /*
262 * Load a double from a TextFileIArchive.
263 */
264 template <>
265 inline void serialize(TextFileIArchive& ar, double& data,
266 const unsigned int version)
267 { ar.unpack(data); }
268
269 /*
270 * Load a std::vector from a TextFileIArchive.
271 */
272 template <typename T>
273 void serialize(TextFileIArchive& ar, std::vector<T>& data,
274 const unsigned int version)
275 {
276 T element;
277 std::size_t size;
278 ar.unpack(size);
279 data.reserve(size);
280 data.clear();
281 for (size_t i = 0; i < size; ++i) {
282 ar & element;
283 data.push_back(element);
284 }
285 }
286
287 // Explicit serialize functions for std library types
288
289 /*
290 * Load a std::complex<float> from a TextFileIArchive.
291 */
292 template <>
293 inline
294 void serialize(TextFileIArchive& ar, std::complex<float>& data,
295 const unsigned int version)
296 { ar.unpack(data); }
297
298 /*
299 * Load a std::complex<double> from a TextFileIArchive.
300 */
301 template <>
302 inline
303 void serialize(TextFileIArchive& ar, std::complex<double>& data,
304 const unsigned int version)
305 { ar.unpack(data); }
306
307 /*
308 * Load a std::string from a TextFileIArchive.
309 */
310 template <>
311 void serialize(TextFileIArchive& ar, std::string& data,
312 const unsigned int version);
313
314 // Explicit serialize functions for namespace Util types
315
316 /*
317 * Load a Util::Vector from a TextFileIArchive.
318 */
319 template <>
320 inline void serialize(TextFileIArchive& ar, Vector& data,
321 const unsigned int version)
322 { ar.unpack(data); }
323
324 /*
325 * Load a Util::IntVector from a TextFileIArchive.
326 */
327 template <>
328 inline void serialize(TextFileIArchive& ar, IntVector& data,
329 const unsigned int version)
330 { ar.unpack(data); }
331
332}
333#endif
Loading archive for text istream.
TextFileIArchive()
Constructor.
TextFileIArchive & operator&(T &data)
Load one object.
std::ifstream & file()
Get the underlying ifstream by reference.
static bool is_saving()
Returns true;.
TextFileIArchive & operator>>(T &data)
Load one object.
void unpack(T &data)
Load a single T object.
static bool is_loading()
Returns false;.
virtual ~TextFileIArchive()
Destructor.
Utility classes for scientific computation.
Definition: accumulators.mod:1