OGLplus  (0.59.0) a C++ wrapper for rendering APIs

byteset.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_BYTESET_HPP
10 #define EAGINE_BYTESET_HPP
11 
12 #include "memory/block.hpp"
13 #include "types.hpp"
14 #include <climits>
15 #include <type_traits>
16 #include <utility>
17 
18 namespace eagine {
19 
24 template <std::size_t N>
25 class byteset {
26 public:
27  static_assert(N > 0, "byteset size must be greater than zero");
28 
31 
33  using value_type = byte;
34 
37 
39  using const_reference = const value_type&;
40 
42  using pointer = value_type*;
43 
45  using const_pointer = const value_type*;
46 
48  using iterator = value_type*;
49 
51  using const_iterator = const value_type*;
52 
54  constexpr byteset() noexcept = default;
55 
57  template <
58  typename... B,
59  typename = std::enable_if_t<
60  (sizeof...(B) == N) && (sizeof...(B) != 0) &&
61  std::conjunction_v<std::true_type, std::is_convertible<B, value_type>...>>>
62  explicit constexpr byteset(B... b) noexcept
63  : _bytes{value_type{b}...} {}
64 
65  template <
66  std::size_t... I,
67  typename UInt,
68  typename =
69  std::enable_if_t<(sizeof(UInt) >= N) && std::is_integral_v<UInt>>>
70  constexpr byteset(std::index_sequence<I...>, UInt init) noexcept
71  : _bytes{value_type((init >> (8 * (N - I - 1))) & 0xFFU)...} {}
72 
74  template <
75  typename UInt,
76  typename = std::enable_if_t<
77  (sizeof(UInt) >= N) && std::is_integral_v<UInt> &&
78  std::is_unsigned_v<UInt>>>
79  explicit constexpr byteset(UInt init) noexcept
80  : byteset(std::make_index_sequence<N>(), init) {}
81 
84  auto data() noexcept -> pointer {
85  return _bytes;
86  }
87 
90  constexpr auto data() const noexcept -> const_pointer {
91  return _bytes;
92  }
93 
95  constexpr auto size() const noexcept -> size_type {
96  return N;
97  }
98 
100  constexpr auto block() const noexcept -> memory::const_block {
101  return {data(), size()};
102  }
103 
105  constexpr auto operator[](size_type i) noexcept -> reference {
106  return _bytes[i];
107  }
108 
110  constexpr auto operator[](size_type i) const noexcept -> const_reference {
111  return _bytes[i];
112  }
113 
116  constexpr auto front() noexcept -> reference {
117  return _bytes[0];
118  }
119 
122  constexpr auto front() const noexcept -> const_reference {
123  return _bytes[0];
124  }
125 
128  constexpr auto back() noexcept -> reference {
129  return _bytes[N - 1];
130  }
131 
134  constexpr auto back() const noexcept -> const_reference {
135  return _bytes[N - 1];
136  }
137 
139  auto begin() noexcept -> iterator {
140  return _bytes + 0;
141  }
142 
144  auto end() noexcept -> iterator {
145  return _bytes + N;
146  }
147 
149  constexpr auto begin() const noexcept -> const_iterator {
150  return _bytes + 0;
151  }
152 
154  constexpr auto end() const noexcept -> const_iterator {
155  return _bytes + N;
156  }
157 
158  friend constexpr auto compare(const byteset& a, const byteset& b) noexcept {
159  return _do_cmp(a, b, std::make_index_sequence<N>{});
160  }
161 
163  friend constexpr auto
164  operator==(const byteset& a, const byteset& b) noexcept {
165  return compare(a, b) == 0;
166  }
167 
169  friend constexpr auto
170  operator!=(const byteset& a, const byteset& b) noexcept {
171  return compare(a, b) != 0;
172  }
173 
175  friend constexpr auto
176  operator<(const byteset& a, const byteset& b) noexcept {
177  return compare(a, b) < 0;
178  }
179 
181  friend constexpr auto
182  operator<=(const byteset& a, const byteset& b) noexcept {
183  return compare(a, b) <= 0;
184  }
185 
187  friend constexpr auto
188  operator>(const byteset& a, const byteset& b) noexcept {
189  return compare(a, b) > 0;
190  }
191 
193  friend constexpr auto
194  operator>=(const byteset& a, const byteset& b) noexcept {
195  return compare(a, b) >= 0;
196  }
197 
199  template <
200  typename UInt,
201  typename = std::enable_if_t<
202  (sizeof(UInt) >= N) && (
203 #if __SIZEOF_INT128__
204  std::is_same_v<UInt, __uint128_t> ||
205  std::is_same_v<UInt, __int128_t> ||
206 #endif
207  std::is_integral_v<UInt>)>>
208  constexpr auto as(UInt i = 0) const noexcept {
209  return _push_back_to(i, 0);
210  }
211 
212 private:
213  value_type _bytes[N]{};
214 
215  template <typename UInt>
216  constexpr auto _push_back_to(UInt state, std::size_t i) const noexcept
217  -> UInt {
218  // NOLINTNEXTLINE(hicpp-signed-bitwise)
219  return (i < N) ? _push_back_to((state << CHAR_BIT) | _bytes[i], i + 1)
220  : state;
221  }
222 
223  static constexpr auto _cmp_byte(value_type a, value_type b) noexcept
224  -> int {
225  return (a == b) ? 0 : (a < b) ? -1 : 1;
226  }
227 
228  static constexpr auto
229  _do_cmp(const byteset&, const byteset&, std::index_sequence<>) noexcept
230  -> int {
231  return 0;
232  }
233 
234  template <std::size_t I, std::size_t... In>
235  static constexpr auto _do_cmp(
236  const byteset& a,
237  const byteset& b,
238  std::index_sequence<I, In...>) noexcept -> int {
239  return (a._bytes[I] == b._bytes[I])
240  ? _do_cmp(a, b, std::index_sequence<In...>{})
241  : _cmp_byte(a._bytes[I], b._bytes[I]);
242  }
243 };
244 
245 } // namespace eagine
246 
247 #endif // EAGINE_BYTESET_HPP
value_type & reference
Alias for element reference type.
Definition: byteset.hpp:36
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
Common code is placed in this namespace.
Definition: eagine.hpp:21
constexpr auto end() const noexcept -> const_iterator
Returns a const iterator past the end of the byte sequence.
Definition: byteset.hpp:154
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32
constexpr friend auto operator>=(const byteset &a, const byteset &b) noexcept
Greater-equal comparison.
Definition: byteset.hpp:194
auto end() noexcept -> iterator
Returns an iterator past the end of the byte sequence.
Definition: byteset.hpp:144
constexpr friend auto operator==(const byteset &a, const byteset &b) noexcept
Equality comparison.
Definition: byteset.hpp:164
const value_type * const_iterator
Alias for const iterator type.
Definition: byteset.hpp:51
constexpr byteset() noexcept=default
Default constructor.
constexpr auto data() const noexcept -> const_pointer
Returns a const pointer to the byte sequence start.
Definition: byteset.hpp:90
value_type * pointer
Alias for pointer to element type.
Definition: byteset.hpp:42
unsigned char byte
Byte type alias.
Definition: types.hpp:24
auto data() noexcept -> pointer
Returns a pointer to the byte sequence start.
Definition: byteset.hpp:84
constexpr friend auto operator<(const byteset &a, const byteset &b) noexcept
Less-than comparison.
Definition: byteset.hpp:176
span_size_t size_type
Alias for size type.
Definition: byteset.hpp:30
constexpr auto size() const noexcept -> size_type
Returns the count of bytes in the stored sequence.
Definition: byteset.hpp:95
constexpr friend auto operator>(const byteset &a, const byteset &b) noexcept
Greater-than comparison.
Definition: byteset.hpp:188
constexpr friend auto operator<=(const byteset &a, const byteset &b) noexcept
Less-equal comparison.
Definition: byteset.hpp:182
const value_type * const_pointer
Alias for pointer to const element type.
Definition: byteset.hpp:45
Class storing a sequence of bytes converting them to and from unsigned integer.
Definition: byteset.hpp:25
constexpr auto begin() const noexcept -> const_iterator
Returns a const iterator to the start of the byte sequence.
Definition: byteset.hpp:149
constexpr auto operator[](size_type i) const noexcept -> const_reference
Subscript operator.
Definition: byteset.hpp:110
constexpr auto back() noexcept -> reference
Returns the last byte in the sequence.
Definition: byteset.hpp:128
constexpr auto as(UInt i=0) const noexcept
Converts the byte sequence into an unsigned integer value.
Definition: byteset.hpp:208
constexpr auto block() const noexcept -> memory::const_block
Creates a const view over the stored sequence of bytes.
Definition: byteset.hpp:100
constexpr auto front() noexcept -> reference
Returns the first byte in the sequence.
Definition: byteset.hpp:116
byte value_type
Alias for element value type.
Definition: byteset.hpp:33
constexpr auto operator[](size_type i) noexcept -> reference
Subscript operator.
Definition: byteset.hpp:105
const value_type & const_reference
Alias for const reference type.
Definition: byteset.hpp:39
constexpr byteset(UInt init) noexcept
Construiction from unsigned integer that is then split into bytes.
Definition: byteset.hpp:79
constexpr auto back() const noexcept -> const_reference
Returns the last byte in the sequence.
Definition: byteset.hpp:134
constexpr friend auto operator!=(const byteset &a, const byteset &b) noexcept
Non-equality comparison.
Definition: byteset.hpp:170
constexpr auto front() const noexcept -> const_reference
Returns the first byte in the sequence.
Definition: byteset.hpp:122
value_type * iterator
Alias for iterator type.
Definition: byteset.hpp:48
auto begin() noexcept -> iterator
Returns an iterator to the start of the byte sequence.
Definition: byteset.hpp:139

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).