8 #ifndef ABLASTR_MSG_LOGGER_SERIALIZATION_H_
9 #define ABLASTR_MSG_LOGGER_SERIALIZATION_H_
16 #include <type_traits>
31 void put_in(
const T &val, std::vector<char> &vec)
33 if constexpr (std::is_same<T, std::string>())
35 const char *c_str = val.c_str();
36 const auto length =
static_cast<int>(val.size());
39 std::copy(c_str, c_str +
length, std::back_inserter(vec));
43 static_assert(std::is_trivially_copyable<T>(),
44 "Cannot serialize non-trivally copyable types, except std::string.");
46 const auto *ptr_val =
reinterpret_cast<const char *
>(&val);
47 std::copy(ptr_val, ptr_val +
sizeof(T), std::back_inserter(vec));
62 void put_in_vec(
const std::vector<T> &val, std::vector<char> &vec)
64 if constexpr (std::is_same<T, char>())
66 put_in(
static_cast<int>(val.size()), vec);
67 vec.insert(vec.end(), val.begin(), val.end());
71 static_assert(std::is_trivially_copyable<T>() || std::is_same<T, std::string>(),
72 "Cannot serialize vectors of non-trivally copyable types"
73 ", except vectors of std::string.");
75 put_in(
static_cast<int>(val.size()), vec);
76 for (
const auto &el : val) {
93 T
get_out(std::vector<char>::const_iterator &it)
95 if constexpr (std::is_same<T, std::string>())
97 const auto length = get_out<int>(it);
105 static_assert(std::is_trivially_copyable<T>(),
106 "Cannot extract non-trivally copyable types from char vectors,"
107 " with the exception of std::string.");
109 auto temp = std::array<char,
sizeof(T)>{};
110 std::copy(it, it +
sizeof(T), temp.begin());
113 std::memcpy(&res, temp.data(),
sizeof(T));
129 template <
typename T>
132 if constexpr (std::is_same<T, std::string>())
134 const auto length = get_out<int>(it);
135 std::vector<char> res(
length);
136 std::copy(it, it +
length, res.begin());
143 static_assert(std::is_trivially_copyable<T>() || std::is_same<T, std::string>(),
144 "Cannot extract non-trivally copyable types from char vectors,"
145 " with the exception of std::string.");
147 const auto length = get_out<int>(it);
148 std::vector<T> res(
length);
150 res[
i] = get_out<T>(it);
Definition: Serialization.H:20
void put_in_vec(const std::vector< T > &val, std::vector< char > &vec)
Definition: Serialization.H:62
std::vector< T > get_out_vec(std::vector< char >::const_iterator &it)
Definition: Serialization.H:130
void put_in(const T &val, std::vector< char > &vec)
Definition: Serialization.H:31
T get_out(std::vector< char >::const_iterator &it)
Definition: Serialization.H:93
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 length(Array4< T > const &a) noexcept
i
Definition: check_interp_points_and_weights.py:174
str
Definition: run_alltests_1node.py:72