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
29namespace Opm {
30namespace Serialization {
31namespace detail {
32
34template <bool pod, class T>
35struct Packing
36{
37 static std::size_t packSize(const T&);
38 static void pack(const T&, std::vector<char>&, int&);
39 static void unpack(T&, std::vector<char>&, int&);
40};
41
43template<class T>
44struct Packing<true,T>
45{
48 static std::size_t packSize(const T& data)
49 {
50 return packSize(&data, 1);
51 }
52
56 static std::size_t packSize(const T*, std::size_t n)
57 {
58 return n*sizeof(T);
59 }
60
65 static void pack(const T& data,
66 std::vector<char>& buffer,
67 int& position)
68 {
69 pack(&data, 1, buffer, position);
70 }
71
77 static void pack(const T* data,
78 std::size_t n,
79 std::vector<char>& buffer,
80 int& position)
81 {
82 std::memcpy(buffer.data() + position, data, n*sizeof(T));
83 position += n*sizeof(T);
84 }
85
90 static void unpack(T& data,
91 std::vector<char>& buffer,
92 int& position)
93 {
94 unpack(&data, 1, buffer, position);
95 }
96
102 static void unpack(T* data,
103 std::size_t n,
104 std::vector<char>& buffer,
105 int& position)
106 {
107 std::memcpy(data, buffer.data() + position, n*sizeof(T));
108 position += n*sizeof(T);
109 }
110};
111
113template<class T>
114struct Packing<false,T>
115{
116 static std::size_t packSize(const T&)
117 {
118 static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
119 return 0;
120 }
121
122 static void pack(const T&, std::vector<char>&, int&)
123 {
124 static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
125 }
126
127 static void unpack(T&, std::vector<char>&, int&)
128 {
129 static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
130 }
131};
132
134template <std::size_t Size>
135struct Packing<false,std::bitset<Size>>
136{
137 static std::size_t packSize(const std::bitset<Size>& data);
138
139 static void pack(const std::bitset<Size>& data,
140 std::vector<char>& buffer, int& position);
141
142 static void unpack(std::bitset<Size>& data,
143 std::vector<char>& buffer, int& position);
144};
145
146template<>
147struct Packing<false,std::string>
148{
149 static std::size_t packSize(const std::string& data);
150
151 static void pack(const std::string& data,
152 std::vector<char>& buffer, int& position);
153
154 static void unpack(std::string& data, std::vector<char>& buffer, int& position);
155};
156
157template<>
158struct Packing<false,time_point>
159{
160 static std::size_t packSize(const time_point&);
161
162 static void pack(const time_point& data,
163 std::vector<char>& buffer, int& position);
164
165 static void unpack(time_point& data, std::vector<char>& buffer, int& position);
166};
167
168}
169
171struct MemPacker {
175 template<class T>
176 std::size_t packSize(const T& data) const
177 {
179 }
180
185 template<class T>
186 std::size_t packSize(const T* data, std::size_t n) const
187 {
188 static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
189 return detail::Packing<true,T>::packSize(data, n);
190 }
191
197 template<class T>
198 void pack(const T& data,
199 std::vector<char>& buffer,
200 int& position) const
201 {
202 detail::Packing<std::is_pod_v<T>,T>::pack(data, buffer, position);
203 }
204
211 template<class T>
212 void pack(const T* data,
213 std::size_t n,
214 std::vector<char>& buffer,
215 int& position) const
216 {
217 static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
218 detail::Packing<true,T>::pack(data, n, buffer, position);
219 }
220
226 template<class T>
227 void unpack(T& data,
228 std::vector<char>& buffer,
229 int& position) const
230 {
231 detail::Packing<std::is_pod_v<T>,T>::unpack(data, buffer, position);
232 }
233
240 template<class T>
241 void unpack(T* data,
242 std::size_t n,
243 std::vector<char>& buffer,
244 int& position) const
245 {
246 static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
247 detail::Packing<true,T>::unpack(data, n, buffer, position);
248 }
249};
250
251} // end namespace Serialization
252} // end namespace Opm
253
254#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:171
void pack(const T &data, std::vector< char > &buffer, int &position) const
Pack a variable.
Definition MemPacker.hpp:198
std::size_t packSize(const T *data, std::size_t n) const
Calculates the pack size for an array.
Definition MemPacker.hpp:186
void unpack(T &data, std::vector< char > &buffer, int &position) const
Unpack a variable.
Definition MemPacker.hpp:227
void unpack(T *data, std::size_t n, std::vector< char > &buffer, int &position) const
Unpack an array.
Definition MemPacker.hpp:241
void pack(const T *data, std::size_t n, std::vector< char > &buffer, int &position) const
Pack an array.
Definition MemPacker.hpp:212
std::size_t packSize(const T &data) const
Calculates the pack size for a variable.
Definition MemPacker.hpp:176
static std::size_t packSize(const T &data)
Calculates the pack size for a POD.
Definition MemPacker.hpp:48
static std::size_t packSize(const T *, std::size_t n)
Calculates the pack size for an array of POD.
Definition MemPacker.hpp:56
static void unpack(T &data, std::vector< char > &buffer, int &position)
Unpack a POD.
Definition MemPacker.hpp:90
static void unpack(T *data, std::size_t n, std::vector< char > &buffer, int &position)
Unpack an array of POD.
Definition MemPacker.hpp:102
static void pack(const T &data, std::vector< char > &buffer, int &position)
Pack a POD.
Definition MemPacker.hpp:65
static void pack(const T *data, std::size_t n, std::vector< char > &buffer, int &position)
Pack an array of POD.
Definition MemPacker.hpp:77
Abstract struct for packing which is (partially) specialized for specific types.
Definition MemPacker.hpp:36