CatapultServer  v0.5.0.1 (Elephant)
ByteArray.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "HexFormatter.h"
23 #include <array>
24 
25 namespace catapult { namespace utils {
26 
28  template<size_t N, typename TTag>
29  class ByteArray : TTag {
30  public:
31  using const_iterator = typename std::array<uint8_t, N>::const_iterator;
32 
33  public:
35  constexpr ByteArray() : m_array()
36  {}
37 
39  constexpr ByteArray(const std::array<uint8_t, N>& array) : m_array(array)
40  {}
41 
43  constexpr ByteArray(const ByteArray& rhs) : m_array(rhs.m_array)
44  {}
45 
46  public:
48  ByteArray& operator=(const ByteArray& rhs) {
49  m_array = rhs.m_array;
50  return *this;
51  }
52 
53  public:
55  constexpr size_t size() const {
56  return m_array.size();
57  }
58 
60  constexpr const uint8_t& operator[](size_t index) const {
61  return m_array[index];
62  }
63 
65  constexpr uint8_t& operator[](size_t index) {
66  return m_array[index];
67  }
68 
70  constexpr const uint8_t* data() const noexcept {
71  return m_array.data();
72  }
73 
75  constexpr uint8_t* data() noexcept {
76  return m_array.data();
77  }
78 
79  public:
81  constexpr auto cbegin() const noexcept {
82  return m_array.cbegin();
83  }
84 
86  constexpr auto cend() const noexcept {
87  return m_array.cend();
88  }
89 
91  constexpr auto begin() const noexcept {
92  return m_array.begin();
93  }
94 
96  constexpr auto end() const noexcept {
97  return m_array.end();
98  }
99 
101  constexpr auto begin() noexcept {
102  return m_array.begin();
103  }
104 
106  constexpr auto end() noexcept {
107  return m_array.end();
108  }
109 
110  public:
112  constexpr bool operator==(const ByteArray& rhs) const {
113  return m_array == rhs.m_array;
114  }
115 
117  constexpr bool operator!=(const ByteArray& rhs) const {
118  return !(*this == rhs);
119  }
120 
122  constexpr bool operator>=(const ByteArray& rhs) const {
123  return m_array >= rhs.m_array;
124  }
125 
127  constexpr bool operator>(const ByteArray& rhs) const {
128  return m_array > rhs.m_array;
129  }
130 
132  constexpr bool operator<=(const ByteArray& rhs) const {
133  return m_array <= rhs.m_array;
134  }
135 
137  constexpr bool operator<(const ByteArray& rhs) const {
138  return m_array < rhs.m_array;
139  }
140 
142  friend std::ostream& operator<<(std::ostream& out, const ByteArray& byteArray) {
143  out << HexFormat(byteArray.m_array);
144  return out;
145  }
146 
147  private:
148  std::array<uint8_t, N> m_array;
149  };
150 
151  // force compilation error if HexFormat is used with ByteArray
152  template<size_t N, typename TTag>
153  constexpr void HexFormat(const ByteArray<N, TTag>&);
154 }}
catapult::utils::ByteArray::cbegin
constexpr auto cbegin() const noexcept
Returns a const iterator to the first byte.
Definition: ByteArray.h:81
catapult::utils::ByteArray::size
constexpr size_t size() const
Returns the array size.
Definition: ByteArray.h:55
catapult::utils::ByteArray::begin
constexpr auto begin() noexcept
Returns an iterator to the first byte.
Definition: ByteArray.h:101
catapult::utils::ByteArray::ByteArray
constexpr ByteArray(const std::array< uint8_t, N > &array)
Creates a byte array around array.
Definition: ByteArray.h:39
catapult::utils::ByteArray::ByteArray
constexpr ByteArray()
Creates a zero-initialized byte array.
Definition: ByteArray.h:35
catapult::utils::HexFormat
constexpr void HexFormat(const ByteArray< N, TTag > &)
catapult::utils::ByteArray< Hash256_Size, Hash256_tag >::const_iterator
typename std::array< uint8_t, N >::const_iterator const_iterator
Definition: ByteArray.h:31
catapult::utils::ByteArray::end
constexpr auto end() const noexcept
Returns a const iterator to one past the last byte.
Definition: ByteArray.h:96
catapult::utils::ByteArray::operator!=
constexpr bool operator!=(const ByteArray &rhs) const
Returns true if this value is not equal to rhs.
Definition: ByteArray.h:117
catapult::utils::ByteArray::operator[]
constexpr const uint8_t & operator[](size_t index) const
Returns a const reference to the byte at index.
Definition: ByteArray.h:60
catapult::utils::ByteArray::operator=
ByteArray & operator=(const ByteArray &rhs)
Assigns rhs to this.
Definition: ByteArray.h:48
catapult::utils::ByteArray::operator[]
constexpr uint8_t & operator[](size_t index)
Returns a reference to the byte at index.
Definition: ByteArray.h:65
catapult::utils::ByteArray::data
constexpr const uint8_t * data() const noexcept
Returns a const pointer to the underlying array.
Definition: ByteArray.h:70
catapult::utils::ByteArray::operator>=
constexpr bool operator>=(const ByteArray &rhs) const
Returns true if this value is greater than or equal to rhs.
Definition: ByteArray.h:122
HexFormatter.h
catapult::utils::ByteArray::data
constexpr uint8_t * data() noexcept
Returns a pointer to the underlying array.
Definition: ByteArray.h:75
catapult::utils::ByteArray::begin
constexpr auto begin() const noexcept
Returns a const iterator to the first byte.
Definition: ByteArray.h:91
catapult::utils::ByteArray::operator>
constexpr bool operator>(const ByteArray &rhs) const
Returns true if this value is greater than rhs.
Definition: ByteArray.h:127
catapult::utils::ByteArray::m_array
std::array< uint8_t, N > m_array
Definition: ByteArray.h:148
catapult::utils::ByteArray::end
constexpr auto end() noexcept
Returns an iterator to one past the last byte.
Definition: ByteArray.h:106
catapult::utils::ByteArray::operator==
constexpr bool operator==(const ByteArray &rhs) const
Returns true if this value is equal to rhs.
Definition: ByteArray.h:112
catapult::utils::ByteArray::operator<<
friend std::ostream & operator<<(std::ostream &out, const ByteArray &byteArray)
Insertion operator for outputting byteArray to out.
Definition: ByteArray.h:142
catapult::utils::ByteArray::operator<
constexpr bool operator<(const ByteArray &rhs) const
Returns true if this value is less than rhs.
Definition: ByteArray.h:137
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::ByteArray::operator<=
constexpr bool operator<=(const ByteArray &rhs) const
Returns true if this value is less than or equal to rhs.
Definition: ByteArray.h:132
catapult::utils::ByteArray
Base class for wrappers of byte array types, to provide some type-safety.
Definition: ByteArray.h:29
catapult::utils::ByteArray::ByteArray
constexpr ByteArray(const ByteArray &rhs)
Creates a copy of rhs.
Definition: ByteArray.h:43
catapult::utils::ByteArray::cend
constexpr auto cend() const noexcept
Returns a const iterator to one past the last byte.
Definition: ByteArray.h:86