WarpX
Serialization.H
Go to the documentation of this file.
1 /* Copyright 2021 Luca Fedeli
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 
8 #ifndef ABLASTR_MSG_LOGGER_SERIALIZATION_H_
9 #define ABLASTR_MSG_LOGGER_SERIALIZATION_H_
10 
11 #include <algorithm>
12 #include <array>
13 #include <cstring>
14 #include <iterator>
15 #include <string>
16 #include <type_traits>
17 #include <vector>
18 
20 {
30  template <typename T>
31  void put_in(const T &val, std::vector<char> &vec)
32  {
33  if constexpr (std::is_same<T, std::string>())
34  {
35  const char *c_str = val.c_str();
36  const auto length = static_cast<int>(val.size());
37 
38  put_in(length, vec);
39  std::copy(c_str, c_str + length, std::back_inserter(vec));
40  }
41  else
42  {
43  static_assert(std::is_trivially_copyable<T>(),
44  "Cannot serialize non-trivally copyable types, except std::string.");
45 
46  const auto *ptr_val = reinterpret_cast<const char *>(&val);
47  std::copy(ptr_val, ptr_val + sizeof(T), std::back_inserter(vec));
48  }
49  }
50 
61  template <typename T>
62  void put_in_vec(const std::vector<T> &val, std::vector<char> &vec)
63  {
64  if constexpr (std::is_same<T, char>())
65  {
66  put_in(static_cast<int>(val.size()), vec);
67  vec.insert(vec.end(), val.begin(), val.end());
68  }
69  else
70  {
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.");
74 
75  put_in(static_cast<int>(val.size()), vec);
76  for (const auto &el : val)
77  put_in(el, vec);
78  }
79  }
80 
91  template <typename T>
92  T get_out(std::vector<char>::const_iterator &it)
93  {
94  if constexpr (std::is_same<T, std::string>())
95  {
96  const auto length = get_out<int>(it);
97  auto str = std::string{it, it + length};
98  it += length;
99 
100  return str;
101  }
102  else
103  {
104  static_assert(std::is_trivially_copyable<T>(),
105  "Cannot extract non-trivally copyable types from char vectors,"
106  " with the exception of std::string.");
107 
108  auto temp = std::array<char, sizeof(T)>{};
109  std::copy(it, it + sizeof(T), temp.begin());
110  it += sizeof(T);
111  T res;
112  std::memcpy(&res, temp.data(), sizeof(T));
113 
114  return res;
115  }
116  }
117 
128  template <typename T>
129  std::vector<T> get_out_vec(std::vector<char>::const_iterator &it)
130  {
131  if constexpr (std::is_same<T, std::string>())
132  {
133  const auto length = get_out<int>(it);
134  std::vector<char> res(length);
135  std::copy(it, it + length, res.begin());
136  it += length;
137 
138  return res;
139  }
140  else
141  {
142  static_assert(std::is_trivially_copyable<T>() || std::is_same<T, std::string>(),
143  "Cannot extract non-trivally copyable types from char vectors,"
144  " with the exception of std::string.");
145 
146  const auto length = get_out<int>(it);
147  std::vector<T> res(length);
148  for (int i = 0; i < length; ++i)
149  res[i] = get_out<T>(it);
150 
151  return res;
152  }
153  }
154 }
155 
156 #endif //ABLASTR_MSG_LOGGER_SERIALIZATION_H_
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:129
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:92
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