Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Bitmask.h
Go to the documentation of this file.
1 
11 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_BITMASK_H
12 #define INCLUDE_MOLASSEMBLER_TEMPLE_BITMASK_H
13 
15 
16 #include <stdexcept>
17 
18 namespace Scine {
19 namespace Molassembler {
20 namespace Temple {
21 
28 template<typename EnumType>
29 struct Bitmask {
32  using Underlying = std::underlying_type_t<EnumType>;
34 
35  static constexpr Underlying maximum = Temple::Math::floor(
36  Temple::Math::log(
37  static_cast<double>(std::numeric_limits<Underlying>::max()),
38  2.0
39  )
40  );
41 
44  Underlying value;
46 
49  explicit constexpr Bitmask() : value {0} {}
50 
51  explicit constexpr Bitmask(EnumType a) : value {
52  static_cast<Underlying>(1) << static_cast<Underlying>(a)
53  } {
54  static_assert(
55  std::is_unsigned<Underlying>::value
56  && std::is_integral<Underlying>::value,
57  "Underlying type for this enum type must unsigned and integral"
58  );
59  }
60 
61  explicit constexpr Bitmask(Underlying a) : value {a} {}
63 
66 
70  constexpr bool isSet(const EnumType& a) const {
71  return (
72  value & (
73  static_cast<Underlying>(1) << static_cast<Underlying>(a)
74  )
75  ) > 0;
76  }
78 
81 
85  constexpr Bitmask operator | (const EnumType& a) const {
86  auto v = static_cast<Underlying>(a);
87 
88  if(v > maximum) {
89  throw std::domain_error(
90  "This enum has too many options to be representable as a bitmask "
91  "in the specified underlying type."
92  );
93  }
94 
95  return Bitmask {
96  value | (static_cast<Underlying>(1) << v)
97  };
98  }
99 
104  constexpr void operator |= (const EnumType& a) {
105  auto v = static_cast<Underlying>(a);
106 
107  if(v > maximum) {
108  throw std::domain_error(
109  "This enum has too many options to be representable as a bitmask "
110  "in the specified underlying type."
111  );
112  }
113 
114  value = value | (static_cast<Underlying>(1) << v);
115  }
116 
118  constexpr bool operator & (const EnumType& a) const {
119  return isSet(a);
120  }
121 
123  constexpr bool operator [] (const EnumType& a) const {
124  return isSet(a);
125  }
127 };
128 
129 template<typename EnumType>
130 constexpr Bitmask<EnumType> make_bitmask(EnumType a) {
131  return Bitmask<EnumType> {a};
132 }
133 
134 } // namespace Temple
135 } // namespace Molassembler
136 } // namespace Scine
137 
138 #endif
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
Definition: Bitmask.h:29
constexpr Bitmask operator|(const EnumType &a) const
Create a new bitmask that also sets a particular enum value.
Definition: Bitmask.h:85