|
| 1 | +// SPDX-FileCopyrightText: 2025 Mathis Logemann <[email protected]> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#pragma once |
| 6 | +#include <initializer_list> |
| 7 | +#include <type_traits> |
| 8 | +namespace quite |
| 9 | +{ |
| 10 | +template <typename T> |
| 11 | +class BitFlags |
| 12 | +{ |
| 13 | + using underlying_t = std::underlying_type_t<T>; |
| 14 | + |
| 15 | + public: |
| 16 | + constexpr BitFlags() |
| 17 | + : flags_(static_cast<underlying_t>(0)) |
| 18 | + {} |
| 19 | + |
| 20 | + constexpr explicit BitFlags(T v) |
| 21 | + : flags_(to_underlying(v)) |
| 22 | + {} |
| 23 | + |
| 24 | + constexpr BitFlags(std::initializer_list<T> vs) |
| 25 | + : BitFlags() |
| 26 | + { |
| 27 | + for (T v : vs) |
| 28 | + { |
| 29 | + flags_ |= to_underlying(v); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + constexpr bool is_set(T v) const |
| 34 | + { |
| 35 | + return (flags_ & to_underlying(v)) == to_underlying(v); |
| 36 | + } |
| 37 | + |
| 38 | + constexpr void set(T v) |
| 39 | + { |
| 40 | + flags_ |= to_underlying(v); |
| 41 | + } |
| 42 | + |
| 43 | + constexpr void unset(T v) |
| 44 | + { |
| 45 | + flags_ &= ~to_underlying(v); |
| 46 | + } |
| 47 | + |
| 48 | + constexpr void clear() |
| 49 | + { |
| 50 | + flags_ = static_cast<underlying_t>(0); |
| 51 | + } |
| 52 | + |
| 53 | + constexpr operator bool() const |
| 54 | + { |
| 55 | + return flags_ != static_cast<underlying_t>(0); |
| 56 | + } |
| 57 | + |
| 58 | + friend constexpr BitFlags operator|(BitFlags lhs, T rhs) |
| 59 | + { |
| 60 | + return BitFlags(lhs.flags_ | to_underlying(rhs)); |
| 61 | + } |
| 62 | + |
| 63 | + friend constexpr BitFlags operator|(BitFlags lhs, BitFlags rhs) |
| 64 | + { |
| 65 | + return BitFlags(lhs.flags_ | rhs.flags_); |
| 66 | + } |
| 67 | + |
| 68 | + friend constexpr BitFlags operator&(BitFlags lhs, T rhs) |
| 69 | + { |
| 70 | + return BitFlags(lhs.flags_ & to_underlying(rhs)); |
| 71 | + } |
| 72 | + |
| 73 | + friend constexpr BitFlags operator&(BitFlags lhs, BitFlags rhs) |
| 74 | + { |
| 75 | + return BitFlags(lhs.flags_ & rhs.flags_); |
| 76 | + } |
| 77 | + |
| 78 | + friend constexpr BitFlags operator^(BitFlags lhs, T rhs) |
| 79 | + { |
| 80 | + return BitFlags(lhs.flags_ ^ to_underlying(rhs)); |
| 81 | + } |
| 82 | + |
| 83 | + friend constexpr BitFlags operator^(BitFlags lhs, BitFlags rhs) |
| 84 | + { |
| 85 | + return BitFlags(lhs.flags_ ^ rhs.flags_); |
| 86 | + } |
| 87 | + |
| 88 | + friend constexpr BitFlags &operator|=(BitFlags &lhs, T rhs) |
| 89 | + { |
| 90 | + lhs.flags_ |= to_underlying(rhs); |
| 91 | + return lhs; |
| 92 | + } |
| 93 | + friend constexpr BitFlags &operator|=(BitFlags &lhs, BitFlags rhs) |
| 94 | + { |
| 95 | + lhs.flags_ |= rhs.flags_; |
| 96 | + return lhs; |
| 97 | + } |
| 98 | + friend constexpr BitFlags &operator&=(BitFlags &lhs, T rhs) |
| 99 | + { |
| 100 | + lhs.flags_ &= to_underlying(rhs); |
| 101 | + return lhs; |
| 102 | + } |
| 103 | + friend constexpr BitFlags &operator&=(BitFlags &lhs, BitFlags rhs) |
| 104 | + { |
| 105 | + lhs.flags_ &= rhs.flags_; |
| 106 | + return lhs; |
| 107 | + } |
| 108 | + friend constexpr BitFlags &operator^=(BitFlags &lhs, T rhs) |
| 109 | + { |
| 110 | + lhs.flags_ ^= to_underlying(rhs); |
| 111 | + return lhs; |
| 112 | + } |
| 113 | + friend constexpr BitFlags &operator^=(BitFlags &lhs, BitFlags rhs) |
| 114 | + { |
| 115 | + lhs.flags_ ^= rhs.flags_; |
| 116 | + return lhs; |
| 117 | + } |
| 118 | + |
| 119 | + friend constexpr BitFlags operator~(const BitFlags &bf) |
| 120 | + { |
| 121 | + return BitFlags(~bf.flags_); |
| 122 | + } |
| 123 | + |
| 124 | + friend constexpr bool operator==(const BitFlags &lhs, const BitFlags &rhs) |
| 125 | + { |
| 126 | + return lhs.flags_ == rhs.flags_; |
| 127 | + } |
| 128 | + |
| 129 | + friend constexpr bool operator!=(const BitFlags &lhs, const BitFlags &rhs) |
| 130 | + { |
| 131 | + return lhs.flags_ != rhs.flags_; |
| 132 | + } |
| 133 | + |
| 134 | + // Construct BitFlags from raw values. |
| 135 | + static constexpr BitFlags from_raw(underlying_t flags) |
| 136 | + { |
| 137 | + return BitFlags(flags); |
| 138 | + } |
| 139 | + |
| 140 | + // Retrieve the raw underlying flags. |
| 141 | + constexpr underlying_t to_raw() const |
| 142 | + { |
| 143 | + return flags_; |
| 144 | + } |
| 145 | + |
| 146 | + private: |
| 147 | + constexpr explicit BitFlags(underlying_t flags) |
| 148 | + : flags_(flags) |
| 149 | + {} |
| 150 | + static constexpr underlying_t to_underlying(T v) |
| 151 | + { |
| 152 | + return static_cast<underlying_t>(v); |
| 153 | + } |
| 154 | + underlying_t flags_; |
| 155 | +}; |
| 156 | +} // namespace quite |
0 commit comments