Kagome
Polkadot Runtime Engine in C++17
buffer_view.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_COMMON_BUFFERVIEW
7 #define KAGOME_COMMON_BUFFERVIEW
8 
9 #include <gsl/span>
10 
11 #include "common/hexutil.hpp"
13 
14 namespace kagome::common {
15  template <size_t MaxSize>
16  class SLBuffer;
17 }
18 
19 namespace kagome::common {
20 
21  class BufferView : public gsl::span<const uint8_t> {
22  using Span = gsl::span<const uint8_t>;
23 
24  public:
25  using Span::Span;
26  using Span::operator=;
27 
28  BufferView(const Span &other) noexcept : Span(other) {}
29 
30  std::string toHex() const {
31  return hex_lower(*this);
32  }
33 
34  bool operator==(const Span &other) const noexcept {
35  return std::equal(
36  Span::cbegin(), Span::cend(), other.cbegin(), other.cend());
37  }
38 
39  template <size_t N>
40  bool operator==(
41  const std::array<typename Span::value_type, N> &other) const noexcept {
42  return std::equal(
43  Span::cbegin(), Span::cend(), other.cbegin(), other.cend());
44  }
45 
46  bool operator<(const BufferView &other) const noexcept {
47  return std::lexicographical_compare(
48  cbegin(), cend(), other.cbegin(), other.cend());
49  }
50 
51  template <size_t N>
52  bool operator<(
53  const std::array<typename Span::value_type, N> &other) const noexcept {
54  return std::lexicographical_compare(
55  Span::cbegin(), Span::cend(), other.cbegin(), other.cend());
56  }
57  };
58 
59  inline std::ostream &operator<<(std::ostream &os, BufferView view) {
60  return os << view.toHex();
61  }
62 
63 } // namespace kagome::common
64 
65 template <>
66 struct fmt::formatter<kagome::common::BufferView> {
67  // Presentation format: 's' - short, 'l' - long.
68  char presentation = 's';
69 
70  // Parses format specifications of the form ['s' | 'l'].
71  constexpr auto parse(format_parse_context &ctx) -> decltype(ctx.begin()) {
72  // Parse the presentation format and store it in the formatter:
73  auto it = ctx.begin(), end = ctx.end();
74  if (it != end && (*it == 's' || *it == 'l')) {
75  presentation = *it++;
76  }
77 
78  // Check if reached the end of the range:
79  if (it != end && *it != '}') {
80  throw format_error("invalid format");
81  }
82 
83  // Return an iterator past the end of the parsed range:
84  return it;
85  }
86 
87  // Formats the Blob using the parsed format specification (presentation)
88  // stored in this formatter.
89  template <typename FormatContext>
90  auto format(const kagome::common::BufferView &view, FormatContext &ctx)
91  -> decltype(ctx.out()) {
92  // ctx.out() is an output iterator to write to.
93 
94  if (view.empty()) {
95  return format_to(ctx.out(), "<empty>");
96  }
97 
98  if (presentation == 's' && view.size() > 5) {
99  return format_to(
100  ctx.out(),
101  "0x{:04x}…{:04x}",
102  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
103  htobe16(*reinterpret_cast<const uint16_t *>(view.data())),
104  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
105  htobe16(*reinterpret_cast<const uint16_t *>(view.data() + view.size()
106  - sizeof(uint16_t))));
107  }
108 
109  return format_to(ctx.out(), "0x{}", view.toHex());
110  }
111 };
112 
113 #endif // KAGOME_COMMON_BUFFERVIEW
std::ostream & operator<<(std::ostream &os, const Blob< N > &blob)
Definition: blob.hpp:234
std::string hex_lower(const gsl::span< const uint8_t > bytes) noexcept
Converts bytes to hex representation.
Definition: hexutil.cpp:52
bool operator<(const std::array< typename Span::value_type, N > &other) const noexcept
Definition: buffer_view.hpp:52
common::BufferView BufferView
bool operator<(const BufferView &other) const noexcept
Definition: buffer_view.hpp:46
BufferView(const Span &other) noexcept
Definition: buffer_view.hpp:28
constexpr auto parse(format_parse_context &ctx) -> decltype(ctx.begin())
Definition: buffer_view.hpp:71
gsl::span< const uint8_t > Span
Definition: buffer_view.hpp:22
auto format(const kagome::common::BufferView &view, FormatContext &ctx) -> decltype(ctx.out())
Definition: buffer_view.hpp:90
bool operator==(const Span &other) const noexcept
Definition: buffer_view.hpp:34
bool operator==(const std::array< typename Span::value_type, N > &other) const noexcept
Definition: buffer_view.hpp:40
std::string toHex() const
Definition: buffer_view.hpp:30