PSCF v1.5.0
MemoryIArchive.h
1#ifndef UTIL_MEMORY_I_ARCHIVE_H
2#define UTIL_MEMORY_I_ARCHIVE_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu)
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 <cstring>
19#include <string>
20#include <vector>
21
22namespace Util
23{
24
25 class MemoryOArchive;
26
33 {
34
35 public:
36
38 static bool is_saving();
39
41 static bool is_loading();
42
47
52
58 void allocate(size_t capacity);
59
64
68 void reset();
69
75 void clear();
76
80 void release();
81
87 template <typename T>
89
95 template <typename T>
97
107 template <typename T, size_t N>
108 MemoryIArchive& operator >> (T (& data)[N]);
109
117 template <typename T, size_t N>
118 MemoryIArchive& operator & (T (& data)[N]);
119
120 // Pack functions and function templates
121
127 template <typename T>
128 void unpack(T& data);
129
136 template <typename T>
137 void unpack(T* array, int n);
138
150 template <typename T>
151 void unpack(T* array, int m, int n, int np);
152
153 #ifdef UTIL_MPI
160 void recv(MPI::Intracomm& comm, int source);
161 #endif
162
166 Byte* begin() const;
167
171 Byte* cursor() const;
172
176 Byte* end() const;
177
181 size_t capacity() const;
182
186 bool isAllocated() const;
187
188 private:
189
191 Byte* buffer_;
192
194 Byte* begin_;
195
197 Byte* cursor_;
198
200 Byte* end_;
201
203 Byte* endAllocated_;
204
206 MemoryOArchive* oArchivePtr_;
207
209 size_t capacity_;
210
212 unsigned int version_;
213
215 bool ownsData_;
216
217 };
218
219 // Inline static methods
220
222 { return false; }
223
225 { return true; }
226
227 // Inline non-static methods
228
229 /*
230 * Return pointer to beginning of block.
231 */
233 { return begin_; }
234
235 /*
236 * Return pointer to cursor position.
237 */
239 { return cursor_; }
240
241 /*
242 * Return end of packed block (one Byte past the last).
243 */
244 inline Byte* MemoryIArchive::end() const
245 { return end_; }
246
247 /*
248 * Return capacity in Bytes.
249 */
250 inline size_t MemoryIArchive::capacity() const
251 { return capacity_; }
252
253 /*
254 * Has a memory block been allocated?
255 */
256 inline bool MemoryIArchive::isAllocated() const
257 { return (bool) begin_; }
258
259 // Template methods
260
261 /*
262 * Load one T object from this MemoryIArchive.
263 */
264 template <typename T>
266 {
267 serialize(*this, data, version_);
268 return *this;
269 }
270
271 /*
272 * Load one T object from a MemoryIArchive.
273 */
274 template <typename T>
276 {
277 serialize(*this, data, version_);
278 return *this;
279 }
280
281 /*
282 * Load a fixed size array of objects via operator >>.
283 */
284 template <typename T, size_t N>
286 {
287 for (size_t i=0; i < N; ++i) {
288 serialize(*this, data[i], version_);
289 }
290 return *this;
291 }
292
293 /*
294 * Load a fixed size array of objects via operator &.
295 */
296 template <typename T, size_t N>
298 {
299 for (size_t i=0; i < N; ++i) {
300 serialize(*this, data[i], version_);
301 }
302 return *this;
303 }
304
305 /*
306 * Load one T object from this MemoryIArchive.
307 */
308 template <typename T>
310 {
311 if (cursor_ + sizeof(data) > end_) {
312 UTIL_THROW("Attempted read past end of packed block");
313 }
314 // Copy bytes, because cursor_ need not be aligned for type T.
315 // The cast to void* avoids -Wclass-memaccess warnings for classes.
316 std::memcpy(static_cast<void*>(&data), cursor_, sizeof(T));
317 cursor_ += sizeof(T);
318 }
319
320 /*
321 * Load a C-array of objects of type T.
322 */
323 template <typename T>
324 void MemoryIArchive::unpack(T* array, int n)
325 {
326 if (cursor_ + n*sizeof(T) > end_) {
327 UTIL_THROW("Attempted read past end of data");
328 }
329 std::memcpy(static_cast<void*>(array), cursor_, n*sizeof(T));
330 cursor_ += n*sizeof(T);
331 }
332
333 /*
334 * Bitwise pack a 2D C-array of objects of type T.
335 */
336 template <typename T>
337 void MemoryIArchive::unpack(T* array, int m, int n, int np)
338 {
339 if (cursor_ + m*n*sizeof(T) > end_) {
340 UTIL_THROW("Attempted read past end of data");
341 }
342 for (int i = 0; i < m; ++i) {
343 std::memcpy(static_cast<void*>(array + i*np), cursor_, n*sizeof(T));
344 cursor_ += n*sizeof(T);
345 }
346 }
347
348 // Explicit specializations of serialize function
349
350 /*
351 * Load a char from a MemoryIArchive.
352 */
353 template <>
354 inline void serialize(MemoryIArchive& ar, char& data,
355 const unsigned int version)
356 { ar.unpack(data); }
357
358 /*
359 * Load a bool from a MemoryIArchive.
360 */
361 template <>
362 inline void serialize(MemoryIArchive& ar, bool& data,
363 const unsigned int version)
364 { ar.unpack(data); }
365
366 /*
367 * Load an unsigned int from a MemoryIArchive.
368 */
369 template <>
370 inline void serialize(MemoryIArchive& ar, unsigned int& data,
371 const unsigned int version)
372 { ar.unpack(data); }
373
374 /*
375 * Load an int from a MemoryIArchive.
376 */
377 template <>
378 inline void serialize(MemoryIArchive& ar, int& data,
379 const unsigned int version)
380 { ar.unpack(data); }
381
382 /*
383 * Load an unsigned long int from a MemoryIArchive.
384 */
385 template <>
386 inline void serialize(MemoryIArchive& ar, unsigned long& data,
387 const unsigned int version)
388 { ar.unpack(data); }
389
390 /*
391 * Load a long int from a MemoryIArchive.
392 */
393 template <>
394 inline void serialize(MemoryIArchive& ar, long& data,
395 const unsigned int version)
396 { ar.unpack(data); }
397
398 /*
399 * Load a float from a MemoryIArchive.
400 */
401 template <>
402 inline void serialize(MemoryIArchive& ar, float& data,
403 const unsigned int version)
404 { ar.unpack(data); }
405
406 /*
407 * Load a double from a MemoryIArchive.
408 */
409 template <>
410 inline void serialize(MemoryIArchive& ar, double& data,
411 const unsigned int version)
412 { ar.unpack(data); }
413
414 /*
415 * Load a std::vector from a MemoryIArchive.
416 */
417 template <typename T>
418 void serialize(MemoryIArchive& ar, std::vector<T>& data,
419 const unsigned int version)
420 {
421 T element;
422 std::size_t size;
423 ar.unpack(size);
424 data.reserve(size);
425 data.clear();
426 for (size_t i = 0; i < size; ++i) {
427 ar & element;
428 data.push_back(element);
429 }
430 }
431
432 // Explicit serialize methods for std library types.
433
434 /*
435 * Load a std::complex<float> from a MemoryIArchive.
436 */
437 template <>
438 inline void serialize(MemoryIArchive& ar, std::complex<float>& data,
439 const unsigned int version)
440 { ar.unpack(data); }
441
442 /*
443 * Load a std::complex<double> from a MemoryIArchive.
444 */
445 template <>
446 inline void serialize(MemoryIArchive& ar, std::complex<double>& data,
447 const unsigned int version)
448 { ar.unpack(data); }
449
450 /*
451 * Load a std::string from MemoryIArchive.
452 */
453 template <>
454 void serialize(MemoryIArchive& ar, std::string& data,
455 const unsigned int version);
456
457 // Explicit serialize methods for Util namespace types.
458
459 /*
460 * Load a Util::Vector from a MemoryIArchive.
461 */
462 template <>
463 inline void serialize(MemoryIArchive& ar, Vector& data,
464 const unsigned int version)
465 { ar.unpack(data); }
466
467 /*
468 * Load a Util::IntVector from a MemoryIArchive.
469 */
470 template <>
471 inline void serialize(MemoryIArchive& ar, IntVector& data,
472 const unsigned int version)
473 { ar.unpack(data); }
474
475}
476#endif
An IntVector is an integer Cartesian vector.
Definition IntVector.h:74
Input archive for packed heterogeneous binary data.
Byte * begin() const
Return pointer to beginning of block.
static bool is_saving()
Returns true;.
MemoryIArchive & operator&(T &data)
Load (read) one object of type T via the & operator.
static bool is_loading()
Returns false;.
MemoryIArchive & operator=(MemoryOArchive &other)
Assignment from MemoryOArchive.
void allocate(size_t capacity)
Allocate memory block.
void reset()
Reset the cursor to the beginning (for rereading).
size_t capacity() const
Return capacity in Bytes.
void clear()
Reset to empty state.
void unpack(T &data)
Unpack one object of type T.
Byte * cursor() const
Return pointer to current position (cursor).
MemoryIArchive & operator>>(T &data)
Load (read) one object of type T via the >> operator.
void release()
Release memory obtained by assignment.
MemoryIArchive()
Constructor.
Byte * end() const
Return pointer to end of packed block (one Byte past the last).
bool isAllocated() const
Has memory been allocated?
Save archive for packed heterogeneous binary data.
A Vector is a Cartesian vector.
Definition Vector.h:76
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
Utility classes for scientific computation.
unsigned char Byte
Define a "Byte" type.
Definition Byte.h:19