8 #ifndef ABLASTR_MSG_LOGGER_SERIALIZATION_H_
9 #define ABLASTR_MSG_LOGGER_SERIALIZATION_H_
15 #include <type_traits>
30 void put_in(
const T &val, std::vector<char> &vec)
32 if constexpr (std::is_same<T, std::string>())
34 const char *c_str = val.c_str();
35 const auto length =
static_cast<int>(val.size());
38 std::copy(c_str, c_str + length, std::back_inserter(vec));
42 static_assert(std::is_trivially_copyable<T>(),
43 "Cannot serialize non-trivally copyable types, except std::string.");
45 const auto *ptr_val =
reinterpret_cast<const char *
>(&val);
46 std::copy(ptr_val, ptr_val +
sizeof(T), std::back_inserter(vec));
61 void put_in_vec(
const std::vector<T> &val, std::vector<char> &vec)
63 if constexpr (std::is_same<T, char>())
65 put_in(
static_cast<int>(val.size()), vec);
66 vec.insert(vec.end(), val.begin(), val.end());
70 static_assert(std::is_trivially_copyable<T>() || std::is_same<T, std::string>(),
71 "Cannot serialize vectors of non-trivally copyable types"
72 ", except vectors of std::string.");
74 put_in(
static_cast<int>(val.size()), vec);
75 for (
const auto &el : val)
91 T
get_out(std::vector<char>::const_iterator &it)
93 if constexpr (std::is_same<T, std::string>())
95 const auto length = get_out<int>(it);
96 const auto str = std::string{it, it + length};
103 static_assert(std::is_trivially_copyable<T>(),
104 "Cannot extract non-trivally copyable types from char vectors,"
105 " with the exception of std::string.");
107 auto temp = std::array<char,
sizeof(T)>{};
108 std::copy(it, it +
sizeof(T), temp.begin());
111 std::memcpy(&res, temp.data(),
sizeof(T));
127 template <
typename T>
130 if constexpr (std::is_same<T, std::string>())
132 const auto length = get_out<int>(it);
133 std::vector<char> res(length);
134 std::copy(it, it + length, res.begin());
141 static_assert(std::is_trivially_copyable<T>() || std::is_same<T, std::string>(),
142 "Cannot extract non-trivally copyable types from char vectors,"
143 " with the exception of std::string.");
145 const auto length = get_out<int>(it);
146 std::vector<T> res(length);
147 for (
int i = 0;
i < length; ++
i)
148 res[
i] = get_out<T>(it);
Definition: Serialization.H:19
void put_in_vec(const std::vector< T > &val, std::vector< char > &vec)
Definition: Serialization.H:61
std::vector< T > get_out_vec(std::vector< char >::const_iterator &it)
Definition: Serialization.H:128
void put_in(const T &val, std::vector< char > &vec)
Definition: Serialization.H:30
T get_out(std::vector< char >::const_iterator &it)
Definition: Serialization.H:91
i
Definition: check_interp_points_and_weights.py:174
str
Definition: run_alltests_1node.py:72