diff options
Diffstat (limited to 'include/cru/base/Bitmask.h')
| -rw-r--r-- | include/cru/base/Bitmask.h | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/include/cru/base/Bitmask.h b/include/cru/base/Bitmask.h index 7606f784..ba5b37f2 100644 --- a/include/cru/base/Bitmask.h +++ b/include/cru/base/Bitmask.h @@ -8,50 +8,63 @@ struct Bitmask final { using Underlying = TUnderlying; constexpr Bitmask() : value(0) {} - constexpr explicit Bitmask(TUnderlying value) : value(value) {} + constexpr explicit Bitmask(Underlying value) : value(value) {} // Start from 1. static constexpr Bitmask FromOffset(int offset) { if (offset == 0) return {}; - return Bitmask(static_cast<TUnderlying>(1u << (offset - 1))); + return Bitmask(static_cast<Underlying>(Underlying(1) << (offset - 1))); } constexpr bool Has(Bitmask rhs) const { return (value & rhs.value) != 0; } - Bitmask operator|(Bitmask rhs) const { return Bitmask(value | rhs.value); } - Bitmask operator&(Bitmask rhs) const { return Bitmask(value & rhs.value); } - Bitmask operator^(Bitmask rhs) const { return Bitmask(value ^ rhs.value); } - Bitmask operator~() const { return Bitmask(~value); } - Bitmask& operator|=(Bitmask rhs) { + constexpr Bitmask operator|(Bitmask rhs) const { + return Bitmask(value | rhs.value); + } + constexpr Bitmask operator&(Bitmask rhs) const { + return Bitmask(value & rhs.value); + } + constexpr Bitmask operator^(Bitmask rhs) const { + return Bitmask(value ^ rhs.value); + } + constexpr Bitmask operator~() const { return Bitmask(~value); } + constexpr Bitmask& operator|=(Bitmask rhs) { value |= rhs.value; return *this; } - Bitmask& operator&=(Bitmask rhs) { + constexpr Bitmask& operator&=(Bitmask rhs) { value &= rhs.value; return *this; } - Bitmask& operator^=(Bitmask rhs) { + constexpr Bitmask& operator^=(Bitmask rhs) { value ^= rhs.value; return *this; } - bool operator==(Bitmask rhs) const { return this->value == rhs.value; } - bool operator!=(Bitmask rhs) const { return this->value != rhs.value; } + constexpr bool operator==(Bitmask rhs) const { + return this->value == rhs.value; + } - explicit operator TUnderlying() const { return value; } - operator bool() const { return value != 0; } + constexpr explicit operator Underlying() const { return value; } + constexpr explicit operator bool() const { return value != 0; } - TUnderlying value; + Underlying value; }; } // namespace cru namespace std { template <typename Tag, typename TUnderlying> struct hash<cru::Bitmask<Tag, TUnderlying>> { - using Bitmask = cru::Bitmask<Tag, TUnderlying>; - - std::size_t operator()(Bitmask bitmask) const { + constexpr std::size_t operator()( + cru::Bitmask<Tag, TUnderlying> bitmask) const { return std::hash<TUnderlying>{}(bitmask.value); } }; } // namespace std + +#define CRU_DEFINE_BITMASK(name) \ + namespace details { \ + struct name##Tag {}; \ + } \ + using name = ::cru::Bitmask<details::name##Tag>; \ + struct name##s |
