11 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_BITMASK_H
12 #define INCLUDE_MOLASSEMBLER_TEMPLE_BITMASK_H
19 namespace Molassembler {
28 template<
typename EnumType>
32 using Underlying = std::underlying_type_t<EnumType>;
35 static constexpr Underlying maximum = Temple::Math::floor(
37 static_cast<double>(std::numeric_limits<Underlying>::max()),
49 explicit constexpr
Bitmask() : value {0} {}
51 explicit constexpr Bitmask(EnumType a) : value {
52 static_cast<Underlying
>(1) << static_cast<Underlying>(a)
55 std::is_unsigned<Underlying>::value
56 && std::is_integral<Underlying>::value,
57 "Underlying type for this enum type must unsigned and integral"
61 explicit constexpr Bitmask(Underlying a) : value {a} {}
70 constexpr
bool isSet(
const EnumType& a)
const {
73 static_cast<Underlying>(1) << static_cast<Underlying>(a)
86 auto v =
static_cast<Underlying
>(a);
89 throw std::domain_error(
90 "This enum has too many options to be representable as a bitmask "
91 "in the specified underlying type."
96 value | (
static_cast<Underlying
>(1) << v)
105 auto v =
static_cast<Underlying
>(a);
108 throw std::domain_error(
109 "This enum has too many options to be representable as a bitmask "
110 "in the specified underlying type."
114 value = value | (
static_cast<Underlying
>(1) << v);
129 template<
typename EnumType>
130 constexpr Bitmask<EnumType> make_bitmask(EnumType a) {
131 return Bitmask<EnumType> {a};
constexpr bool operator[](const EnumType &a) const
Check whether a particular enum value is set.
Definition: Bitmask.h:123
constexpr void operator|=(const EnumType &a)
Set a particular enum value in this bitmask.
Definition: Bitmask.h:104
constexpr math implementations
constexpr bool operator&(const EnumType &a) const
Check whether a particular enum value is set.
Definition: Bitmask.h:118
constexpr bool isSet(const EnumType &a) const
Checks whether an enum value is set in the bitmask.
Definition: Bitmask.h:70
constexpr Bitmask operator|(const EnumType &a) const
Create a new bitmask that also sets a particular enum value.
Definition: Bitmask.h:85