Kagome
Polkadot Runtime Engine in C++17
tie.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_SCALE_TIE_HPP
7 #define KAGOME_SCALE_TIE_HPP
8 
9 #include <tuple>
10 
11 #define SCALE_TIE(N) \
12  static constexpr size_t scale_tie = N; \
13  template <typename T> \
14  bool operator==(const T &r) const { \
15  using ThisT = std::decay_t<decltype(*this)>; \
16  using ExtT = std::decay_t<T>; \
17  static_assert( \
18  std::is_same_v<ExtT, ThisT> || std::is_base_of_v<ThisT, ExtT>); \
19  return ::scale::as_tie(*this, [&](auto l) { \
20  return ::scale::as_tie(r, [&](auto r) { return l == r; }); \
21  }); \
22  } \
23  template <typename T> \
24  bool operator!=(const T &r) const { \
25  return !operator==(r); \
26  }
27 
28 namespace scale {
29  class ScaleEncoderStream;
30  class ScaleDecoderStream;
31 
32  // generated by housekeeping/scale_tie.py
33  template <typename T,
34  typename F,
35  size_t N = std::remove_reference_t<T>::scale_tie>
36  auto as_tie(T &&v, F &&f) {
37  if constexpr (N == 1) {
38  auto &[v0] = v;
39  return std::forward<F>(f)(std::tie(v0));
40  } else if constexpr (N == 2) {
41  auto &[v0, v1] = v;
42  return std::forward<F>(f)(std::tie(v0, v1));
43  } else if constexpr (N == 3) {
44  auto &[v0, v1, v2] = v;
45  return std::forward<F>(f)(std::tie(v0, v1, v2));
46  } else if constexpr (N == 4) {
47  auto &[v0, v1, v2, v3] = v;
48  return std::forward<F>(f)(std::tie(v0, v1, v2, v3));
49  } else if constexpr (N == 5) {
50  auto &[v0, v1, v2, v3, v4] = v;
51  return std::forward<F>(f)(std::tie(v0, v1, v2, v3, v4));
52  } else if constexpr (N == 6) {
53  auto &[v0, v1, v2, v3, v4, v5] = v;
54  return std::forward<F>(f)(std::tie(v0, v1, v2, v3, v4, v5));
55  } else if constexpr (N == 7) {
56  auto &[v0, v1, v2, v3, v4, v5, v6] = v;
57  return std::forward<F>(f)(std::tie(v0, v1, v2, v3, v4, v5, v6));
58  } else if constexpr (N == 8) {
59  auto &[v0, v1, v2, v3, v4, v5, v6, v7] = v;
60  return std::forward<F>(f)(std::tie(v0, v1, v2, v3, v4, v5, v6, v7));
61  } else if constexpr (N == 9) {
62  auto &[v0, v1, v2, v3, v4, v5, v6, v7, v8] = v;
63  return std::forward<F>(f)(std::tie(v0, v1, v2, v3, v4, v5, v6, v7, v8));
64  } else if constexpr (N == 10) {
65  auto &[v0, v1, v2, v3, v4, v5, v6, v7, v8, v9] = v;
66  return std::forward<F>(f)(
67  std::tie(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9));
68  } else {
69  // generate code for bigger tuples
70  static_assert(1 <= N && N <= 10);
71  }
72  }
73 
74  template <typename T, size_t = T::scale_tie>
75  ScaleEncoderStream &operator<<(ScaleEncoderStream &s, const T &v) {
76  as_tie(v, [&](auto v) {
77  std::apply([&](const auto &...v) { (..., (s << v)); }, v);
78  });
79  return s;
80  }
81 
82  template <typename T, size_t = T::scale_tie>
83  ScaleDecoderStream &operator>>(ScaleDecoderStream &s, T &v) {
84  as_tie(v, [&](auto v) {
85  std::apply([&](auto &...v) { (..., (s >> v)); }, v);
86  });
87  return s;
88  }
89 } // namespace scale
90 
91 #endif // KAGOME_SCALE_TIE_HPP
scale::ScaleEncoderStream & operator<<(scale::ScaleEncoderStream &s, const libp2p::peer::PeerInfo &peer_info)
auto as_tie(T &&v, F &&f)
Definition: tie.hpp:36
scale::ScaleDecoderStream & operator>>(scale::ScaleDecoderStream &s, libp2p::peer::PeerInfo &peer_info)