4#include <initializer_list>
18template <
typename ENUM>
24 static constexpr All all{};
26 using EnumBaseType =
typename std::underlying_type<ENUM>::type;
39 constexpr BitMask(
const std::initializer_list<ENUM> &init_list) {
40 for (
auto val: init_list) {
41 m_flags |=
static_cast<EnumBaseType
>(val);
39 constexpr BitMask(
const std::initializer_list<ENUM> &init_list) {
…}
46 explicit constexpr BitMask(
const ENUM val) :
47 m_flags{static_cast<EnumBaseType>(val)}
46 explicit constexpr BitMask(
const ENUM val) : {
…}
51 explicit constexpr BitMask(
const EnumBaseType value) :
51 explicit constexpr BitMask(
const EnumBaseType value) : {
…}
56 EnumBaseType
raw()
const {
return m_flags; }
59 explicit operator std::string()
const {
return toString(); }
64 std::string toString()
const {
68 for (
int bit =
size() - 1; bit >= 0; bit--) {
69 const auto val = 1 << bit;
70 ret.push_back(this->
test(
static_cast<ENUM
>(val)) ?
'1' :
'0');
78 m_flags = ~EnumBaseType(0);
84 const auto bitval =
static_cast<EnumBaseType
>(val);
85 m_flags = (on_off ? (m_flags|bitval) : (m_flags & ~bitval));
89 BitMask&
set(
const std::initializer_list<ENUM> &flags) {
90 for (
auto flag: flags) {
98 m_flags |= other.m_flags;
104 m_flags = EnumBaseType{0};
116 for (
auto val: flags) {
117 m_flags &= ~static_cast<EnumBaseType>(val);
123 m_flags &= ~(other.
raw());
129 return ret.reset(other);
134 EnumBaseType mask = 0;
135 for (
auto val: flags) {
136 mask |=
static_cast<EnumBaseType
>(val);
145 return limit({flag});
150 m_flags &= other.
raw();
156 return ret.limit(other);
167 m_flags ^=
static_cast<EnumBaseType
>(val);
175 for (
size_t bit = 0; bit <
size(); bit++) {
177 if (this->
test(
static_cast<ENUM
>(val)))
185 constexpr size_t size()
const {
186 return sizeof(EnumBaseType) * 8;
194 bool test(
const ENUM val)
const {
195 const auto raw_val =
static_cast<EnumBaseType
>(val);
196 return (m_flags & raw_val) == raw_val;
208 return ((m_flags &
static_cast<EnumBaseType
>(val)) != 0);
213 bool only(
const ENUM val)
const {
214 return m_flags ==
static_cast<EnumBaseType
>(val);
223 bool allOf(
const std::initializer_list<ENUM> &flags)
const {
224 for (
auto val: flags) {
223 bool allOf(
const std::initializer_list<ENUM> &flags)
const {
…}
233 return (m_flags & other.m_flags) == other.m_flags;
237 bool anyOf(
const std::initializer_list<ENUM> &flags)
const {
238 for (
auto val: flags) {
237 bool anyOf(
const std::initializer_list<ENUM> &flags)
const {
…}
247 return (m_flags & other.m_flags) != 0;
255 bool operator==(
const BitMask &other)
const {
256 return m_flags == other.m_flags;
259 bool operator!=(
const BitMask &other)
const {
260 return !(*
this == other);
270 return ENUM{other.
raw() & this->
raw()};
280 return ret.reset(second);
286 return ret.reset(val);
292 return ret.set(second);
303 EnumBaseType m_flags = 0;
A typesafe bit mask representation using class enums.
BitMask & limit(const BitMask other)
Sets all bits to zero except the bits in the given mask.
friend BitMask operator-(const BitMask &first, const ENUM val)
Returns an object containing all the bits found in first without val.
BitMask & set(const BitMask other)
Sets all the bits that are also set in other.
BitMask & flip(const ENUM val)
Flips the given value.
friend BitMask operator+(const BitMask &first, const BitMask &second)
Returns an object containing all the bits found in first and second.
bool anyOf(const std::initializer_list< ENUM > &flags) const
Returns whether any of the given values is set.
constexpr size_t size() const
Returns the maximum number of bits that can be stored in the bit mask.
BitMask & reset()
Sets all bits to zero.
bool operator&(const ENUM val) const
Checks whether any bit of the given value is set,.
constexpr BitMask(const EnumBaseType value)
Sets exactly the given primitive type bitmask.
BitMask & limit(const std::initializer_list< ENUM > &flags)
Sets all bits to zero except the given flags.
size_t count() const
Returns the number of set bits.
friend BitMask operator+(const BitMask &first, const ENUM val)
Returns an object containing all the bits found in first and /also val.
bool only(const ENUM val) const
Returns whether this is the only value set.
BitMask & set(const All)
Sets all bits it the set.
friend BitMask operator-(const BitMask &first, const BitMask &second)
Returns an object containing all the bits found in first without the bits found inc second.
bool testAny(const ENUM val) const
Returns whether any of the bits of val are set.
BitMask & limit(const ENUM flag)
Sets all bits to zero except the given flag.
constexpr BitMask()
Sets all bits to zero.
bool test(const ENUM val) const
Returns whether the given value is set.
BitMask & flip()
Flip every bit in the bit mask.
constexpr BitMask(const std::initializer_list< ENUM > &init_list)
Sets only the flags found in the given initializer list.
ENUM operator&(const BitMask &other) const
returns an ENUM value containing only the values found in both masks.
BitMask & set(const ENUM val, bool on_off=true)
Set or unset the given value.
bool any() const
Returns whether any bit in the bitset is set.
bool operator[](const ENUM flag) const
Returns a boolean value for the given value,.
BitMask(const All a)
Sets all bits to one.
bool allOf(const std::initializer_list< ENUM > &flags) const
Tests whether all of the given values are set.
constexpr BitMask(const ENUM val)
Sets exactly the given value to one.
bool none() const
Returns whether no bit in the bitset is set.
BitMask & reset(const ENUM val)
Zeroes the given value.
EnumBaseType raw() const
Returns the raw bitfield integer.
BitMask & reset(const std::initializer_list< ENUM > &flags)
Zeroes all of the given values.
Helper type for setting all bits during construction time of BitMask.