ARTEMIS
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 <string>
15 #include <type_traits>
16 #include <vector>
17 
19 {
29  template <typename T>
30  void put_in(const T &val, std::vector<char> &vec)
31  {
32  if constexpr (std::is_same<T, std::string>())
33  {
34  const char *c_str = val.c_str();
35  const auto length = static_cast<int>(val.size());
36 
37  put_in(length, vec);
38  std::copy(c_str, c_str + length, std::back_inserter(vec));
39  }
40  else
41  {
42  static_assert(std::is_trivially_copyable<T>(),
43  "Cannot serialize non-trivally copyable types, except std::string.");
44 
45  const auto *ptr_val = reinterpret_cast<const char *>(&val);
46  std::copy(ptr_val, ptr_val + sizeof(T), std::back_inserter(vec));
47  }
48  }
49 
60  template <typename T>
61  void put_in_vec(const std::vector<T> &val, std::vector<char> &vec)
62  {
63  if constexpr (std::is_same<T, char>())
64  {
65  put_in(static_cast<int>(val.size()), vec);
66  vec.insert(vec.end(), val.begin(), val.end());
67  }
68  else
69  {
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.");
73 
74  put_in(static_cast<int>(val.size()), vec);
75  for (const auto &el : val)
76  put_in(el, vec);
77  }
78  }
79 
90  template <typename T>
91  T get_out(std::vector<char>::const_iterator &it)
92  {
93  if constexpr (std::is_same<T, std::string>())
94  {
95  const auto length = get_out<int>(it);
96  const auto str = std::string{it, it + length};
97  it += length;
98 
99  return str;
100  }
101  else
102  {
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.");
106 
107  auto temp = std::array<char, sizeof(T)>{};
108  std::copy(it, it + sizeof(T), temp.begin());
109  it += sizeof(T);
110  T res;
111  std::memcpy(&res, temp.data(), sizeof(T));
112 
113  return res;
114  }
115  }
116 
127  template <typename T>
128  std::vector<T> get_out_vec(std::vector<char>::const_iterator &it)
129  {
130  if constexpr (std::is_same<T, std::string>())
131  {
132  const auto length = get_out<int>(it);
133  std::vector<char> res(length);
134  std::copy(it, it + length, res.begin());
135  it += length;
136 
137  return res;
138  }
139  else
140  {
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.");
144 
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);
149 
150  return res;
151  }
152  }
153 }
154 
155 #endif //ABLASTR_MSG_LOGGER_SERIALIZATION_H_
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