ergo
serialization_tools.h
Go to the documentation of this file.
1 /* Ergo, version 3.8, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2019 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4  * and Anastasia Kruchinina.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Primary academic reference:
20  * Ergo: An open-source program for linear-scaling electronic structure
21  * calculations,
22  * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23  * Kruchinina,
24  * SoftwareX 7, 107 (2018),
25  * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26  *
27  * For further information about Ergo, see <http://www.ergoscf.org>.
28  */
29 
39 #ifndef SERIALIZATION_TOOLS_HEADER
40 #define SERIALIZATION_TOOLS_HEADER
41 
42 #include <vector>
43 
44 /* general std::vector serialization template code */
45 
46 template <typename VectorType>
47 size_t std_vector_getSize(const VectorType & v) {
48  // One size_t for storing the number of elements, plus the space needed for the elements themselves.
49  size_t sizeOfOneElement = sizeof(typename VectorType::value_type);
50  return sizeof(size_t) + v.size() * sizeOfOneElement;
51 }
52 
53 typedef char* CharPtrType;
54 
55 template <typename VectorType>
57  size_t nElements = v.size();
58  memcpy(p, &nElements, sizeof(size_t));
59  p += sizeof(size_t);
60  size_t sizeOfOneElement = sizeof(typename VectorType::value_type);
61  size_t sizeOfAllElements = v.size() * sizeOfOneElement;
62  memcpy(p, &v[0], sizeOfAllElements);
63  p += sizeOfAllElements;
64 }
65 
66 typedef const char* ConstCharPtrType;
67 
68 template <typename VectorType>
70  assert(bufEndPtr > p);
71  size_t nBytesInBuffer = bufEndPtr - p;
72  assert(nBytesInBuffer >= sizeof(size_t));
73  size_t nElements;
74  memcpy(&nElements, p, sizeof(size_t));
75  p += sizeof(size_t);
76  // Check that nElements has a reasonable value.
77  size_t sizeOfOneElement = sizeof(typename VectorType::value_type);
78  size_t sizeOfAllElements = nElements * sizeOfOneElement;
79  assert(sizeOfAllElements + sizeof(size_t) <= nBytesInBuffer);
80  v.resize(nElements);
81  memcpy(&v[0], p, sizeOfAllElements);
82  p += sizeOfAllElements;
83 }
84 
85 
86 #endif
std_vector_getSize
size_t std_vector_getSize(const VectorType &v)
Definition: serialization_tools.h:47
std_vector_assignFromBuffer_and_move_ptr
void std_vector_assignFromBuffer_and_move_ptr(VectorType &v, ConstCharPtrType &p, const char *bufEndPtr)
Definition: serialization_tools.h:69
ConstCharPtrType
const char * ConstCharPtrType
Definition: serialization_tools.h:66
mat::VectorGeneral
Definition: MatrixBase.h:55
std_vector_writeToBuffer_and_move_ptr
void std_vector_writeToBuffer_and_move_ptr(const VectorType &v, CharPtrType &p)
Definition: serialization_tools.h:56
CharPtrType
char * CharPtrType
Definition: serialization_tools.h:53