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>
48 std::is_trivially_copyable_v<T> or
49 boost::serialization::is_bitwise_serializable<T>::value> {};
55 std::bool_constant<std::is_trivially_copyable_v<T> or
56 boost::serialization::is_bitwise_serializable<T>::value>;
60using use_serialize = std::bool_constant<not use_memcpy<T>::value and
63template <
class Derived>
class BasicMemcpyArchive {
70 explicit BasicMemcpyArchive(std::span<char> buf)
71 : buf(buf), insert(buf.data()) {}
73 auto get_library_version()
const {
return std::size_t{4}; }
75 auto bytes_processed()
const {
76 return static_cast<std::size_t
>(insert - buf.data());
79 void skip(std::size_t bytes) {
80 assert((insert + bytes) <= &*buf.end());
85 void read(
void *data, std::size_t bytes) {
87 assert((insert + bytes) <= &*buf.end());
88 std::memcpy(data, insert, bytes);
92 void write(
const void *data, std::size_t bytes) {
94 assert((insert + bytes) <= &*buf.end());
95 std::memcpy(insert, data, bytes);
100 template <
typename T>
101 auto operator>>(T &value) -> std::enable_if_t<use_memcpy<T>::value> {
102 read(&value,
sizeof(T));
105 template <
typename T>
106 auto operator<<(T
const &value) -> std::enable_if_t<use_memcpy<T>::value> {
107 write(&value,
sizeof(T));
111 template <
class T>
void process(T &value) {
112 auto const old_pos = insert;
113 boost::serialization::serialize_adl(*
static_cast<Derived *
>(
this), value,
115 auto const new_pos = insert;
116 assert(
static_cast<std::size_t
>(new_pos - old_pos) <=
sizeof(T));
118 auto const padding_size =
sizeof(T) - (new_pos - old_pos);
125 operator>>(T &value) -> std::enable_if_t<detail::use_serialize<T>::value> {
131 operator<<(T &value) -> std::enable_if_t<detail::use_serialize<T>::value> {
135 template <
class T>
void operator<<(
const boost::serialization::nvp<T> &nvp) {
136 operator<<(nvp.const_value());
139 template <
class T>
void operator>>(
const boost::serialization::nvp<T> &nvp) {
140 operator>>(nvp.value());
148 template <
class T>
static constexpr std::size_t packing_size() {
165 using base_type = detail::BasicMemcpyArchive<MemcpyIArchive>;
183 using base_type::packing_size;
184 using base_type::operator>>;
199 using base_type = detail::BasicMemcpyArchive<MemcpyOArchive>;
217 using base_type::packing_size;
218 using base_type::operator<<;
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.