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  }
81 
92  template <typename T>
93  T get_out(std::vector<char>::const_iterator &it)
94  {
95  if constexpr (std::is_same<T, std::string>())
96  {
97  const auto length = get_out<int>(it);
98  auto str = std::string{it, it + length};
99  it += length;
100 
101  return str;
102  }
103  else
104  {
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.");
108 
109  auto temp = std::array<char, sizeof(T)>{};
110  std::copy(it, it + sizeof(T), temp.begin());
111  it += sizeof(T);
112  T res;
113  std::memcpy(&res, temp.data(), sizeof(T));
114 
115  return res;
116  }
117  }
118 
129  template <typename T>
130  std::vector<T> get_out_vec(std::vector<char>::const_iterator &it)
131  {
132  if constexpr (std::is_same<T, std::string>())
133  {
134  const auto length = get_out<int>(it);
135  std::vector<char> res(length);
136  std::copy(it, it + length, res.begin());
137  it += length;
138 
139  return res;
140  }
141  else
142  {
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.");
146 
147  const auto length = get_out<int>(it);
148  std::vector<T> res(length);
149  for (int i = 0; i < length; ++i) {
150  res[i] = get_out<T>(it);
151  }
152 
153  return res;
154  }
155  }
156 }
157 
158 #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: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