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

bitfield.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_BITFIELD_HPP
10 #define EAGINE_BITFIELD_HPP
11 
12 #include "all_are_same.hpp"
13 
14 namespace eagine {
15 
18 template <typename Bit>
19 class bitfield {
20 public:
22  using bit_type = Bit;
23 
25  using value_type = std::make_unsigned_t<std::underlying_type_t<bit_type>>;
26 
28  constexpr bitfield() noexcept = default;
29 
31  constexpr explicit bitfield(value_type bits) noexcept
32  : _bits{bits} {}
33 
35  constexpr bitfield(bit_type _bit) noexcept
36  : _bits{value_type(_bit)} {}
37 
38  constexpr bitfield(bit_type _bit_a, bit_type _bit_b) noexcept
39  : _bits(value_type(_bit_a) | value_type(_bit_b)) {}
40 
42  constexpr auto is_empty() const noexcept {
43  return _bits == value_type(0);
44  }
45 
48  explicit constexpr operator bool() const noexcept {
49  return _bits != value_type(0);
50  }
51 
54  explicit constexpr operator value_type() const noexcept {
55  return _bits;
56  }
57 
59  constexpr auto bits() const noexcept -> value_type {
60  return _bits;
61  }
62 
70  constexpr auto has(bit_type bit) const noexcept {
71  return (_bits & value_type(bit)) == value_type(bit);
72  }
73 
81  constexpr auto has_not(bit_type bit) const noexcept {
82  return (_bits & value_type(bit)) != value_type(bit);
83  }
84 
92  template <typename... B>
93  constexpr auto has_all(bit_type bit, B... bits) const noexcept
94  -> std::enable_if_t<all_are_same_v<bit_type, B...>, bool> {
95  return (has(bit) && ... && has(bits));
96  }
97 
105  template <typename... B>
106  constexpr auto has_any(bit_type bit, B... bits) const noexcept
107  -> std::enable_if_t<all_are_same_v<bit_type, B...>, bool> {
108  return (has(bit) || ... || has(bits));
109  }
110 
118  template <typename... B>
119  constexpr auto has_none(bit_type bit, B... bits) const noexcept
120  -> std::enable_if_t<all_are_same_v<bit_type, B...>, bool> {
121  return (has_not(bit) && ... && has_not(bits));
122  }
123 
131  constexpr auto has_only(bit_type bit) const noexcept {
132  return _bits == value_type(bit);
133  }
134 
142  constexpr auto has_at_most(bit_type bit) const noexcept {
143  return is_empty() || has_only(bit);
144  }
145 
147  friend constexpr auto operator==(bitfield a, bitfield b) noexcept {
148  return a._bits == b._bits;
149  }
150 
152  friend constexpr auto operator!=(bitfield a, bitfield b) noexcept {
153  return a._bits != b._bits;
154  }
155 
157  friend constexpr auto operator|(bitfield a, bitfield b) noexcept
158  -> bitfield {
159  return bitfield(a._bits | b._bits);
160  }
161 
163  auto operator|=(bitfield b) noexcept -> bitfield& {
164  _bits |= b._bits;
165  return *this;
166  }
167 
169  friend constexpr auto operator&(bitfield a, bitfield b) noexcept
170  -> bitfield {
171  return bitfield(a._bits & b._bits);
172  }
173 
175  auto operator&=(bitfield b) noexcept -> bitfield& {
176  _bits &= b._bits;
177  return *this;
178  }
179 
181  friend constexpr auto operator~(bitfield b) noexcept -> bitfield {
182  return bitfield{value_type(~b._bits)};
183  }
184 
186  auto clear(bit_type b) noexcept -> bitfield& {
187  _bits &= ~value_type(b); // NOLINT(hicpp-signed-bitwise)
188  return *this;
189  }
190 
193  auto clear() noexcept -> bitfield& {
194  _bits = value_type(0);
195  return *this;
196  }
197 
198 private:
199  value_type _bits{0U};
200 };
201 
202 } // namespace eagine
203 
204 #endif // EAGINE_BITFIELD_HPP
auto clear(bit_type b) noexcept -> bitfield &
Clears the specified bit.
Definition: bitfield.hpp:186
auto operator|=(bitfield b) noexcept -> bitfield &
Bitwise-or operator.
Definition: bitfield.hpp:163
Common code is placed in this namespace.
Definition: eagine.hpp:21
std::make_unsigned_t< std::underlying_type_t< bit_type > > value_type
The Integral type used to store the bits.
Definition: bitfield.hpp:25
constexpr auto bits() const noexcept -> value_type
Returns the bits in the underlying integer value type.
Definition: bitfield.hpp:59
Class for manipulating and testing a group of enumeration-based bits.
Definition: bitfield.hpp:19
constexpr auto has(bit_type bit) const noexcept
Tests if the specified bit is set.
Definition: bitfield.hpp:70
constexpr auto has_none(bit_type bit, B... bits) const noexcept -> std::enable_if_t< all_are_same_v< bit_type, B... >, bool >
Tests if none of the specified bits are set.
Definition: bitfield.hpp:119
constexpr auto is_empty() const noexcept
Indicates that none of the bits are set.
Definition: bitfield.hpp:42
constexpr bitfield(bit_type _bit) noexcept
Construction from the bit enumeration type value.
Definition: bitfield.hpp:35
constexpr friend auto operator&(bitfield a, bitfield b) noexcept -> bitfield
Bitwise-and operator.
Definition: bitfield.hpp:169
constexpr auto has_any(bit_type bit, B... bits) const noexcept -> std::enable_if_t< all_are_same_v< bit_type, B... >, bool >
Tests if any of the specified bits are set.
Definition: bitfield.hpp:106
constexpr auto has_not(bit_type bit) const noexcept
Tests if the specified bit is not set.
Definition: bitfield.hpp:81
constexpr friend auto operator~(bitfield b) noexcept -> bitfield
Bit inversion operator.
Definition: bitfield.hpp:181
generator_capability bit_type
The enumeration type specifying individual bit values.
Definition: bitfield.hpp:22
constexpr bitfield() noexcept=default
Default constructor.
constexpr auto has_only(bit_type bit) const noexcept
Tests if only the specified bit is set.
Definition: bitfield.hpp:131
constexpr friend auto operator|(bitfield a, bitfield b) noexcept -> bitfield
Bitwise-or operator.
Definition: bitfield.hpp:157
constexpr auto has_at_most(bit_type bit) const noexcept
Tests if at most the specified bit is set (or is empty).
Definition: bitfield.hpp:142
constexpr auto has_all(bit_type bit, B... bits) const noexcept -> std::enable_if_t< all_are_same_v< bit_type, B... >, bool >
Tests if all of the specified bits are set.
Definition: bitfield.hpp:93
auto clear() noexcept -> bitfield &
Clears all bits.
Definition: bitfield.hpp:193
constexpr friend auto operator!=(bitfield a, bitfield b) noexcept
Nonequality comparison.
Definition: bitfield.hpp:152
auto operator&=(bitfield b) noexcept -> bitfield &
Bitwise-and operator.
Definition: bitfield.hpp:175
constexpr friend auto operator==(bitfield a, bitfield b) noexcept
Equality comparison.
Definition: bitfield.hpp:147

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