ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
memcpy_archive.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo 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 * ESPResSo 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
20#pragma once
21
22#include <boost/mpl/bool.hpp>
23#include <boost/serialization/is_bitwise_serializable.hpp>
24#include <boost/serialization/nvp.hpp>
25#include <boost/serialization/serialization.hpp>
26
27#include <cassert>
28#include <cstddef>
29#include <cstring>
30#include <memory>
31#include <span>
32#include <type_traits>
33
34namespace Utils {
35/** @brief Type trait to indicate that a type is
36 * serializable with a static size, e.g. is
37 * suitable for memcpy serialization. Only
38 * specialize this to @c std::true_type if it is
39 * guaranteed that serializing this type always
40 * returns the same number of bytes, independent
41 * of object state.
42 *
43 * @tparam T type under consideration.
44 */
45template <class T>
47 : std::bool_constant<
48 std::is_trivially_copyable_v<T> or
49 boost::serialization::is_bitwise_serializable<T>::value> {};
50
51namespace detail {
52/* Use memcpy for packing */
53template <class T>
54using use_memcpy =
55 std::bool_constant<std::is_trivially_copyable_v<T> or
56 boost::serialization::is_bitwise_serializable<T>::value>;
57/* Use serialize function only if the type is opt-in but not
58 * trivially copyable, in which case memcpy is more efficient. */
59template <class T>
60using use_serialize = std::bool_constant<not use_memcpy<T>::value and
62
63template <class Derived> class BasicMemcpyArchive {
64 /** Buffer to write to */
65 std::span<char> buf;
66 /** Current position in the buffer */
67 char *insert;
68
69public:
70 explicit BasicMemcpyArchive(std::span<char> buf)
71 : buf(buf), insert(buf.data()) {}
72
73 auto get_library_version() const { return std::size_t{4}; }
74
75 auto bytes_processed() const {
76 return static_cast<std::size_t>(insert - buf.data());
77 }
78
79 void skip(std::size_t bytes) {
80 assert((insert + bytes) <= &*buf.end());
81 insert += bytes;
82 }
83
84private:
85 void read(void *data, std::size_t bytes) {
86 /* check that there is enough space left in the buffer */
87 assert((insert + bytes) <= &*buf.end());
88 std::memcpy(data, insert, bytes);
89 insert += bytes;
90 }
91
92 void write(const void *data, std::size_t bytes) {
93 /* check that there is enough space left in the buffer */
94 assert((insert + bytes) <= &*buf.end());
95 std::memcpy(insert, data, bytes);
96 insert += bytes;
97 }
98
99public:
100 template <typename T>
101 auto operator>>(T &value) -> std::enable_if_t<use_memcpy<T>::value> {
102 read(&value, sizeof(T));
103 }
104
105 template <typename T>
106 auto operator<<(T const &value) -> std::enable_if_t<use_memcpy<T>::value> {
107 write(&value, sizeof(T));
108 }
109
110private:
111 template <class T> void process(T &value) {
112 auto const old_pos = insert;
113 boost::serialization::serialize_adl(*static_cast<Derived *>(this), value,
114 4);
115 auto const new_pos = insert;
116 assert(static_cast<std::size_t>(new_pos - old_pos) <= sizeof(T));
117
118 auto const padding_size = sizeof(T) - (new_pos - old_pos);
119 skip(padding_size);
120 }
121
122public:
123 template <class T>
124 auto
125 operator>>(T &value) -> std::enable_if_t<detail::use_serialize<T>::value> {
126 process(value);
127 }
128
129 template <class T>
130 auto
131 operator<<(T &value) -> std::enable_if_t<detail::use_serialize<T>::value> {
132 process(value);
133 }
134
135 template <class T> void operator<<(const boost::serialization::nvp<T> &nvp) {
136 operator<<(nvp.const_value());
137 }
138
139 template <class T> void operator>>(const boost::serialization::nvp<T> &nvp) {
140 operator>>(nvp.value());
141 }
142
143 /**
144 * @brief Determine the static packing size of a type.
145 * @tparam T Type to consider.
146 * @return Packed size in bytes.
147 */
148 template <class T> static constexpr std::size_t packing_size() {
149 return sizeof(T);
150 }
151};
152} // namespace detail
153
154/**
155 * @brief Archive that deserializes from a buffer via memcpy.
156 *
157 * Can only process types that have a static serialization size,
158 * e.g. that serialize to the same number of bytes independent of
159 * the state of the object. This can either be automatically detected
160 * for types that are trivially copyable, or by explicitly assured
161 * by specializing @ref is_statically_serializable to @c std::true_type.
162 */
163class MemcpyIArchive : public detail::BasicMemcpyArchive<MemcpyIArchive> {
164private:
165 using base_type = detail::BasicMemcpyArchive<MemcpyIArchive>;
166
167public:
168 using is_loading = boost::mpl::true_;
169 using is_saving = boost::mpl::false_;
170
171 /**
172 * @param buf Buffer to read from.
173 */
174 explicit MemcpyIArchive(std::span<char> buf) : base_type(buf) {}
175
176 /**
177 * @brief Number of bytes read from the buffer.
178 * @return Number of bytes read.
179 */
180 std::size_t bytes_read() const { return bytes_processed(); }
181
182 /** @copydoc base_type::packing_size */
183 using base_type::packing_size;
184 using base_type::operator>>;
185
186 template <class T> MemcpyIArchive &operator&(T &value) {
187 operator>>(value);
188
189 return *this;
190 }
191};
192
193/**
194 * @brief Archive that serializes to a buffer via memcpy.
195 *
196 * @copydetails MemcpyIArchive
197 */
198class MemcpyOArchive : public detail::BasicMemcpyArchive<MemcpyOArchive> {
199 using base_type = detail::BasicMemcpyArchive<MemcpyOArchive>;
200
201public:
202 using is_loading = boost::mpl::false_;
203 using is_saving = boost::mpl::true_;
204
205 /**
206 * @param buf Buffer to write to.
207 */
208 explicit MemcpyOArchive(std::span<char> buf) : base_type(buf) {}
209
210 /**
211 * @brief Number of bytes written to the buffer.
212 * @return Number of bytes written.
213 */
214 std::size_t bytes_written() const { return bytes_processed(); }
215
216 /** @copydoc base_type::packing_size */
217 using base_type::packing_size;
218 using base_type::operator<<;
219
220 template <class T> MemcpyOArchive &operator&(T &value) {
221 operator<<(value);
222
223 return *this;
224 }
225};
226} // namespace Utils
Archive that deserializes from a buffer via memcpy.
boost::mpl::true_ is_loading
MemcpyIArchive & operator&(T &value)
std::size_t bytes_read() const
Number of bytes read from the buffer.
MemcpyIArchive(std::span< char > buf)
boost::mpl::false_ is_saving
Archive that serializes to a buffer via memcpy.
boost::mpl::false_ is_loading
MemcpyOArchive(std::span< char > buf)
std::size_t bytes_written() const
Number of bytes written to the buffer.
MemcpyOArchive & operator&(T &value)
boost::mpl::true_ is_saving
static std::basic_ostream< char > & operator<<(std::basic_ostream< char > &os, const dim3 &dim)
Type trait to indicate that a type is serializable with a static size, e.g.