My Project
Loading...
Searching...
No Matches
MemPacker.hpp
1/*
2 Copyright 2019 Equinor AS.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM 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 OPM 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 OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef MEM_PACKER_HPP
20#define MEM_PACKER_HPP
21
22#include <opm/common/utility/TimeService.hpp>
23
24#include <bitset>
25#include <cstddef>
26#include <cstring>
27#include <string>
28#include <vector>
29
30namespace Opm {
31namespace Serialization {
32namespace detail {
33
35template <bool pod, class T>
36struct Packing
37{
38 static std::size_t packSize(const T&);
39 static void pack(const T&, std::vector<char>&, int&);
40 static void unpack(T&, std::vector<char>&, int&);
41};
42
44template<class T>
45struct Packing<true,T>
46{
49 static std::size_t packSize(const T& data)
50 {
51 return packSize(&data, 1);
52 }
53
57 static std::size_t packSize(const T*, std::size_t n)
58 {
59 return n*sizeof(T);
60 }
61
66 static void pack(const T& data,
67 std::vector<char>& buffer,
68 int& position)
69 {
70 pack(&data, 1, buffer, position);
71 }
72
78 static void pack(const T* data,
79 std::size_t n,
80 std::vector<char>& buffer,
81 int& position)
82 {
83 std::memcpy(buffer.data() + position, data, n*sizeof(T));
84 position += n*sizeof(T);
85 }
86
91 static void unpack(T& data,
92 std::vector<char>& buffer,
93 int& position)
94 {
95 unpack(&data, 1, buffer, position);
96 }
97
103 static void unpack(T* data,
104 std::size_t n,
105 std::vector<char>& buffer,
106 int& position)
107 {
108 std::memcpy(data, buffer.data() + position, n*sizeof(T));
109 position += n*sizeof(T);
110 }
111};
112
114template<class T>
115struct Packing<false,T>
116{
117 static std::size_t packSize(const T&)
118 {
119 static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
120 return 0;
121 }
122
123 static void pack(const T&, std::vector<char>&, int&)
124 {
125 static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
126 }
127
128 static void unpack(T&, std::vector<char>&, int&)
129 {
130 static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
131 }
132};
133
135template <std::size_t Size>
136struct Packing<false,std::bitset<Size>>
137{
138 static std::size_t packSize(const std::bitset<Size>& data);
139
140 static void pack(const std::bitset<Size>& data,
141 std::vector<char>& buffer, int& position);
142
143 static void unpack(std::bitset<Size>& data,
144 std::vector<char>& buffer, int& position);
145};
146
147template<>
148struct Packing<false,std::string>
149{
150 static std::size_t packSize(const std::string& data);
151
152 static void pack(const std::string& data,
153 std::vector<char>& buffer, int& position);
154
155 static void unpack(std::string& data, std::vector<char>& buffer, int& position);
156};
157
158template<>
159struct Packing<false,time_point>
160{
161 static std::size_t packSize(const time_point&);
162
163 static void pack(const time_point& data,
164 std::vector<char>& buffer, int& position);
165
166 static void unpack(time_point& data, std::vector<char>& buffer, int& position);
167};
168
169}
170
172struct MemPacker {
176 template<class T>
177 std::size_t packSize(const T& data) const
178 {
180 }
181
186 template<class T>
187 std::size_t packSize(const T* data, std::size_t n) const
188 {
189 static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
190 return detail::Packing<true,T>::packSize(data, n);
191 }
192
198 template<class T>
199 void pack(const T& data,
200 std::vector<char>& buffer,
201 int& position) const
202 {
203 detail::Packing<std::is_pod_v<T>,T>::pack(data, buffer, position);
204 }
205
212 template<class T>
213 void pack(const T* data,
214 std::size_t n,
215 std::vector<char>& buffer,
216 int& position) const
217 {
218 static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
219 detail::Packing<true,T>::pack(data, n, buffer, position);
220 }
221
227 template<class T>
228 void unpack(T& data,
229 std::vector<char>& buffer,
230 int& position) const
231 {
232 detail::Packing<std::is_pod_v<T>,T>::unpack(data, buffer, position);
233 }
234
241 template<class T>
242 void unpack(T* data,
243 std::size_t n,
244 std::vector<char>& buffer,
245 int& position) const
246 {
247 static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
248 detail::Packing<true,T>::unpack(data, n, buffer, position);
249 }
250};
251
252} // end namespace Serialization
253} // end namespace Opm
254
255#endif // MEM_PACKER_HPP
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
Struct handling packing of serialization to a memory buffer.
Definition MemPacker.hpp:172
void pack(const T &data, std::vector< char > &buffer, int &position) const
Pack a variable.
Definition MemPacker.hpp:199
std::size_t packSize(const T *data, std::size_t n) const
Calculates the pack size for an array.
Definition MemPacker.hpp:187
void unpack(T &data, std::vector< char > &buffer, int &position) const
Unpack a variable.
Definition MemPacker.hpp:228
void unpack(T *data, std::size_t n, std::vector< char > &buffer, int &position) const
Unpack an array.
Definition MemPacker.hpp:242
void pack(const T *data, std::size_t n, std::vector< char > &buffer, int &position) const
Pack an array.
Definition MemPacker.hpp:213
std::size_t packSize(const T &data) const
Calculates the pack size for a variable.
Definition MemPacker.hpp:177
static std::size_t packSize(const T &data)
Calculates the pack size for a POD.
Definition MemPacker.hpp:49
static std::size_t packSize(const T *, std::size_t n)
Calculates the pack size for an array of POD.
Definition MemPacker.hpp:57
static void unpack(T &data, std::vector< char > &buffer, int &position)
Unpack a POD.
Definition MemPacker.hpp:91
static void unpack(T *data, std::size_t n, std::vector< char > &buffer, int &position)
Unpack an array of POD.
Definition MemPacker.hpp:103
static void pack(const T &data, std::vector< char > &buffer, int &position)
Pack a POD.
Definition MemPacker.hpp:66
static void pack(const T *data, std::size_t n, std::vector< char > &buffer, int &position)
Pack an array of POD.
Definition MemPacker.hpp:78
Abstract struct for packing which is (partially) specialized for specific types.
Definition MemPacker.hpp:37