21 #include <type_traits> 22 #include <unordered_map> 34 mempcpy(std::byte *& d,
const void * s, std::size_t n) {
40 mempcpy(std::size_t & x,
const void *, std::size_t n) {
44 template<
class,
class =
void>
47 template<
class T,
class P>
49 serial_put(P & p,
const T & t) {
54 serial_size(
const T & t) {
61 serial_get(
const std::byte *& p) {
66 auto serial_put(
const T & t) {
67 std::vector<std::byte> ret(serial_size(t));
68 auto *
const p0 = ret.data(), *p = p0;
70 flog_assert(p == p0 + ret.size(),
"Wrong serialization size");
74 T serial_get1(
const std::byte * p) {
75 return serial_get<T>(p);
82 static void put(P & p,
const T & c) {
83 serial_put(p, c.size());
87 static T
get(
const std::byte *& p) {
89 for(
auto n = serial_get<typename T::size_type>(p); n--;)
90 ret.insert(serial_get<typename T::value_type>(p));
97 constexpr
bool memcpyable_v =
98 std::is_default_constructible_v<T> && std::is_trivially_move_assignable_v<T>;
101 struct serial<T, std::enable_if_t<memcpyable_v<T>>> {
102 static_assert(!std::is_pointer_v<T>,
"Cannot serialize pointers");
104 static void put(P & p,
const T & t) {
105 mempcpy(p, &t,
sizeof t);
107 static T
get(
const std::byte *& p) {
110 std::memcpy(static_cast<void *>(&ret), p,
sizeof ret);
117 template<
class T,
class U>
119 std::enable_if_t<!memcpyable_v<std::pair<T, U>>>> {
120 using type = std::pair<T, U>;
122 static void put(P & p,
const type & v) {
123 serial_put(p, v.first);
124 serial_put(p, v.second);
126 static type
get(
const std::byte *& p) {
127 return {serial_get<T>(p), serial_get<U>(p)};
130 template<
class... TT>
132 std::enable_if_t<!memcpyable_v<std::tuple<TT...>>>> {
133 using type = std::tuple<TT...>;
135 static void put(P & p,
const type & t) {
136 std::apply([&p](
const TT &... xx) { (serial_put(p, xx), ...); }, t);
138 static type
get(
const std::byte *& p) {
139 return type{serial_get<TT>(p)...};
144 using type = std::vector<T>;
146 static void put(P & p,
const type & v) {
147 serial_put(p, v.size());
151 static type
get(
const std::byte *& p) {
152 auto n = serial_get<typename type::size_type>(p);
156 ret.push_back(serial_get<T>(p));
162 template<
class K,
class V>
164 template<
class K,
class V>
170 static void put(P & p,
const std::string & s) {
171 const auto n = s.size();
173 mempcpy(p, s.data(), n);
175 static std::string
get(
const std::byte *& p) {
176 const auto n = serial_get<std::string::size_type>(p);
179 return {
reinterpret_cast<const char *
>(d), n};
188 voided<decltype(&T::legion_buffer_size),
189 std::enable_if_t<!memcpyable_v<T>>>> {
191 static void put(P & p,
const T & t) {
192 if constexpr(std::is_pointer_v<P>)
193 p += t.legion_serialize(p);
195 p += t.legion_buffer_size();
197 static T
get(
const std::byte *& p) {
199 p += ret.legion_deserialize(p);
207 template<
class T,
class =
void>
209 static std::size_t size(
const T & t) {
220 static void put(P & p,
const T & t) {
221 if constexpr(std::is_pointer_v<P>)
222 serial_put(p, Convert::put(t));
224 p += Convert::size(t);
226 static T
get(
const std::byte *& p) {
228 serial_get<std::decay_t<decltype(Convert::put(std::declval<T>()))>>(p));
Definition: serialize.hh:45
Definition: serialize.hh:206
#define flog_assert(test, message)
Definition: flog.hh:411
Definition: serialize.hh:80
Definition: serialize.hh:208
Definition: control.hh:31