PSCF v1.1
pack.h
1#ifndef UTIL_PACK_H
2#define UTIL_PACK_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
11namespace Util
12{
13
14 /*
15 * Pack an object of type T into send buffer.
16 */
17 template <typename T>
18 char* pack(char* current, char* end, T* data, int count)
19 {
20 if (current + sizeof(data) > end) {
21 UTIL_THROW("Attempted write past end of send buffer");
22 }
23 T* ptr = (T*)current;
24 for (int i = 0; i < count; ++i) {
25 *ptr = data[i];
26 ++ptr;
27 }
28 current = (char *)ptr;
29 return current;
30 }
31
32 /*
33 * Unpack an object of type T from recvBuffer.
34 */
35 template <typename T>
36 char* unpack(char* current, char* end, T* data, int count)
37 {
38 if (current + sizeof(data) > end) {
39 UTIL_THROW("Attempted read past end of recv buffer");
40 }
41 T* ptr = (T *)current;
42 for (int i = 0; i < count; ++i) {
43 data[i] = *ptr;
44 ++ptr;
45 }
46 current = (char *)ptr;
47 return current;
48 }
49
50}
51#endif
52
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1