CatapultServer  v0.5.0.1 (Elephant)
HexFormatter.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "StreamFormatGuard.h"
23 #include "traits/Traits.h"
24 #include <array>
25 #include <iostream>
26 #include <vector>
27 
28 namespace catapult { namespace utils {
29 
32  template<size_t N>
34  public:
36  explicit IntegralHexFormatterGuard(std::ostream& out)
37  : StreamFormatGuard(out, std::ios::hex | std::ios::uppercase, '0')
38  , m_out(out)
39  {}
40 
41  public:
43  template<typename T>
44  void output(T value) {
45  Output(m_out, value);
46  }
47 
48  private:
49  template<typename T>
50  static void OutputValue(std::ostream& out, T value, int size) {
51  out << std::setw(2 * size) << +value;
52  }
53 
54  template<typename T>
55  static void Output(std::ostream& out, const T& value) {
56  if constexpr (std::is_integral_v<T>) {
57  OutputValue(out, value, N);
58  } else {
59  auto pData = reinterpret_cast<const uint8_t*>(&value);
60  for (auto i = 0u; i < sizeof(T); ++i)
61  OutputValue(out, pData[sizeof(T) - 1 - i], 1);
62  }
63  }
64 
65  private:
66  std::ostream& m_out;
67  };
68 
70  template<typename T, size_t N = sizeof(T)>
72  public:
73  explicit IntegralHexFormatter(T value) : m_value(value)
74  {}
75 
76  public:
77  friend std::ostream& operator<<(std::ostream& out, const IntegralHexFormatter& formatter) {
79  guard.output(formatter.m_value);
80  return out;
81  }
82 
83  private:
85  };
86 
88  template<typename TInputIterator>
90  public:
91  ContainerHexFormatter(TInputIterator begin, TInputIterator end, char separator) :
92  m_begin(begin),
93  m_end(end),
94  m_separator(separator)
95  {}
96 
97  public:
98  friend std::ostream& operator<<(std::ostream& out, const ContainerHexFormatter<TInputIterator>& formatter) {
99  using T = decltype(*m_begin);
100  IntegralHexFormatterGuard<sizeof(T)> guard(out);
101  for (auto iter = formatter.m_begin; formatter.m_end != iter; ++iter) {
102  guard.output(*iter);
103 
104  if (0 != formatter.m_separator && iter + 1 != formatter.m_end)
105  out << formatter.m_separator;
106  }
107 
108  return out;
109  }
110 
111  private:
112  TInputIterator m_begin;
113  TInputIterator m_end;
115  };
116 
118  template<typename TInputIterator>
119  auto HexFormat(TInputIterator begin, TInputIterator end, char separator = 0) {
120  return ContainerHexFormatter<TInputIterator>(begin, end, separator);
121  }
122 
124  template<typename TContainer>
125  auto HexFormat(const TContainer& container, char separator) {
126  return HexFormat(std::cbegin(container), std::cend(container), separator);
127  }
128 
130  template<typename T>
131  auto HexFormat(const T& data) {
132  if constexpr (traits::is_scalar_v<T>) {
133  return IntegralHexFormatter<T>(data);
134  } else {
135  auto pData = reinterpret_cast<const uint8_t*>(&data);
136  return HexFormat(pData, pData + sizeof(T), 0);
137  }
138  }
139 
141  template<typename T>
142  auto HexFormat(const std::vector<T>& container) {
143  return HexFormat(container, 0);
144  }
145 
147  template<typename T, size_t N>
148  auto HexFormat(const std::array<T, N>& container) {
149  return HexFormat(container, 0);
150  }
151 }}
catapult::utils::IntegralHexFormatterGuard::IntegralHexFormatterGuard
IntegralHexFormatterGuard(std::ostream &out)
Creates a formatter guard around out.
Definition: HexFormatter.h:36
catapult::utils::HexFormat
constexpr void HexFormat(const ByteArray< N, TTag > &)
catapult::utils::IntegralHexFormatterGuard::Output
static void Output(std::ostream &out, const T &value)
Definition: HexFormatter.h:55
catapult::utils::ContainerHexFormatter::operator<<
friend std::ostream & operator<<(std::ostream &out, const ContainerHexFormatter< TInputIterator > &formatter)
Definition: HexFormatter.h:98
StreamFormatGuard.h
catapult::utils::IntegralHexFormatter
Formatter for printing an integral hex number to a stream.
Definition: HexFormatter.h:71
catapult::utils::IntegralHexFormatter::operator<<
friend std::ostream & operator<<(std::ostream &out, const IntegralHexFormatter &formatter)
Definition: HexFormatter.h:77
catapult::utils::ContainerHexFormatter::m_end
TInputIterator m_end
Definition: HexFormatter.h:113
catapult::utils::IntegralHexFormatterGuard::m_out
std::ostream & m_out
Definition: HexFormatter.h:66
catapult::utils::ContainerHexFormatter
Formatter for printing a container of integral hex numbers to a stream.
Definition: HexFormatter.h:89
size
uint64_t size
Definition: MemoryCounters.cpp:65
catapult::utils::IntegralHexFormatterGuard
Definition: HexFormatter.h:33
catapult::utils::ContainerHexFormatter::m_begin
TInputIterator m_begin
Definition: HexFormatter.h:112
catapult::utils::StreamFormatGuard
Definition: StreamFormatGuard.h:29
Traits.h
catapult::utils::IntegralHexFormatter::m_value
T m_value
Definition: HexFormatter.h:84
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::IntegralHexFormatterGuard::output
void output(T value)
Outputs value to the underlying stream.
Definition: HexFormatter.h:44
catapult::utils::ContainerHexFormatter::m_separator
char m_separator
Definition: HexFormatter.h:114
catapult::utils::ContainerHexFormatter::ContainerHexFormatter
ContainerHexFormatter(TInputIterator begin, TInputIterator end, char separator)
Definition: HexFormatter.h:91
catapult::utils::IntegralHexFormatterGuard::OutputValue
static void OutputValue(std::ostream &out, T value, int size)
Definition: HexFormatter.h:50
catapult::utils::IntegralHexFormatter::IntegralHexFormatter
IntegralHexFormatter(T value)
Definition: HexFormatter.h:73