Dune::Copasi
Loading...
Searching...
No Matches
bit_flags.hh
Go to the documentation of this file.
1#ifndef DUNE_COPASI_COMMON_BITFLAGS_HH
2#define DUNE_COPASI_COMMON_BITFLAGS_HH
3
4#include <dune-copasi-config.hh>
5
6#include <bitset>
7#include <climits>
8
9namespace Dune::Copasi {
10
18template<class T>
19struct is_bitflags : public std::is_enum<T>
20{};
21
23template<class T>
25
45template<typename Enum>
47{
48 // Ensure that Enum is a valid template
49 static_assert(
51 "Enum is not a bit flag and it should not be instantiated in BitFlag class");
52 static_assert(std::is_enum_v<Enum>, "Emum type must be a enummeration type");
53
55 using BitSet = std::bitset<CHAR_BIT * sizeof(Enum)>;
56
58 using UnderlyingType = std::underlying_type_t<Enum>;
59
61 static constexpr UnderlyingType null_value =
62 UnderlyingType{} ^ UnderlyingType{};
63
64public:
66 static constexpr BitFlags<Enum> all_flags() { return
67 BitFlags<Enum>{ static_cast<Enum>(~null_value) }; }
68
70 static constexpr BitFlags<Enum> no_flags() {return
71 BitFlags<Enum>{ static_cast<Enum>(null_value) }; }
72
74 // All flags are default initialized to false
75 constexpr inline BitFlags()
76 : _value{ null_value }
77 {}
78
80 // Initialization with an underlying enumerator
81 constexpr inline BitFlags(const Enum& value)
82 : _value(static_cast<UnderlyingType>(value))
83 {}
84
86 // Initialization with a standard library bitset
87 inline explicit BitFlags(const BitSet& bit_set)
89 {
90 static_assert(sizeof(UnderlyingType) <= sizeof(unsigned long long),
91 "Underlying type is longer than maximum supported cast");
92 }
93
95 [[nodiscard]] constexpr inline BitSet as_bitset() const
96 {
97 return BitSet{ static_cast<unsigned long long>(_value) };
98 }
100 [[nodiscard]] constexpr inline UnderlyingType as_underlying() const { return _value; }
101
103 constexpr inline UnderlyingType& as_underlying() { return _value; }
104
106 [[nodiscard]] constexpr inline Enum as_enum() const { return static_cast<const Enum>(_value); }
107
109 constexpr inline Enum& as_enum() { return static_cast<Enum&>(_value); }
110
112 constexpr inline explicit operator Enum() const { return as_enum(); }
113
115 constexpr inline BitFlags operator|(const BitFlags& rhs) const
116 {
117 return static_cast<Enum>(_value | rhs._value);
118 }
119
121 constexpr inline BitFlags operator&(const BitFlags& rhs) const
122 {
123 return static_cast<Enum>(_value & rhs._value);
124 }
125
127 constexpr inline BitFlags operator^(const BitFlags& rhs) const
128 {
129 return static_cast<Enum>(_value ^ rhs._value);
130 }
131
133 constexpr inline BitFlags operator<<(std::size_t pos) const
134 {
135 return static_cast<Enum>(_value << pos);
136 }
137
139 constexpr inline BitFlags operator>>(std::size_t pos) const
140 {
141 return static_cast<Enum>(_value >> pos);
142 }
143
146 {
147 _value |= rhs._value;
148 return *this;
149 }
150
153 {
154 _value &= rhs._value;
155 return *this;
156 }
157
160 {
161 _value ^= rhs._value;
162 return *this;
163 }
164
166 inline BitFlags& operator<<=(std::size_t pos)
167 {
168 _value <<= pos;
169 return *this;
170 }
171
173 inline BitFlags& operator>>=(std::size_t pos)
174 {
175 _value >>= pos;
176 return *this;
177 }
178
180 constexpr inline BitFlags operator~() const
181 {
182 return BitFlags{ static_cast<Enum>(~_value) };
183 }
184
186 [[nodiscard]] constexpr inline bool test(const BitFlags& flag) const
187 {
188 return (_value & flag._value) == flag._value;
189 }
190
192 inline void reset(const BitFlags& flag) { _value &= ~flag._value; }
193
195 inline void set(const BitFlags& flag, bool value = true)
196 {
197 value ? (_value |= flag._value) : reset(flag);
198 }
199
201 inline void flip(const BitFlags& flag) { _value ^= flag; }
202
204 [[nodiscard]] constexpr inline bool all() const { return as_bitset().all(); }
205
207 [[nodiscard]] constexpr inline bool any() const { return as_bitset().any(); }
208
210 [[nodiscard]] constexpr inline bool none() const { return as_bitset().none(); }
211
212private:
214 UnderlyingType _value;
215};
216
218template<class Enum>
219constexpr inline std::enable_if_t<is_bitflags_v<Enum>, BitFlags<Enum>>
221{
222 return BitFlags<Enum>{ lhs } | BitFlags<Enum>{ rhs };
223}
224
226template<class Enum>
227constexpr inline std::enable_if_t<is_bitflags_v<Enum>, BitFlags<Enum>>
229{
230 return BitFlags<Enum>{ lhs } & BitFlags<Enum>{ rhs };
231}
232
234template<class Enum>
235constexpr inline std::enable_if_t<is_bitflags_v<Enum>, BitFlags<Enum>>
237{
238 return BitFlags<Enum>{ lhs } ^ BitFlags<Enum>{ rhs };
239}
240
242template<class Enum>
243constexpr inline std::enable_if_t<is_bitflags_v<Enum>, bool>
245{
246 return BitFlags<Enum>{ lhs } == BitFlags<Enum>{ rhs };
247}
248
251template<class Enum>
252constexpr inline std::enable_if_t<is_bitflags_v<Enum>, bool>
254{
255 return BitFlags<Enum>{ lhs } != BitFlags<Enum>{ rhs };
256}
257
258} // namespace Dune::Copasi
259
260#endif // DUNE_COPASI_COMMON_BITFLAGS_HH
Bit flags for enumerators.
Definition bit_flags.hh:47
constexpr bool all() const
Checks if all flags are set to true.
Definition bit_flags.hh:204
constexpr bool test(const BitFlags &flag) const
Test if the required flag is active in the bitflag.
Definition bit_flags.hh:186
void reset(const BitFlags &flag)
Reset the required flags in the bitflag.
Definition bit_flags.hh:192
void flip(const BitFlags &flag)
Flip the required flags.
Definition bit_flags.hh:201
constexpr BitFlags operator|(const BitFlags &rhs) const
Bitwise or operator with another bitflag.
Definition bit_flags.hh:115
BitFlags & operator>>=(std::size_t pos)
Bitwise right shift operator with this bitflag.
Definition bit_flags.hh:173
constexpr BitFlags()
Default constructor.
Definition bit_flags.hh:75
BitFlags & operator&=(const BitFlags &rhs)
Bitwise and operator with this bitflag.
Definition bit_flags.hh:152
constexpr BitFlags operator&(const BitFlags &rhs) const
Bitwise and operator with another bitflag.
Definition bit_flags.hh:121
constexpr BitFlags operator<<(std::size_t pos) const
Bitwise left shift operator.
Definition bit_flags.hh:133
constexpr bool none() const
Checks if none flags are set to true.
Definition bit_flags.hh:210
BitFlags & operator<<=(std::size_t pos)
Bitwise left shift operator with this bitflag.
Definition bit_flags.hh:166
constexpr Enum & as_enum()
Return bitflag as its underlying enum.
Definition bit_flags.hh:109
BitFlags & operator^=(const BitFlags &rhs)
Bitwise xor operator with this bitflag.
Definition bit_flags.hh:159
void set(const BitFlags &flag, bool value=true)
Set the required flags to true.
Definition bit_flags.hh:195
constexpr Enum as_enum() const
Return bitflag as its underlying enum.
Definition bit_flags.hh:106
static constexpr BitFlags< Enum > no_flags()
Bitflag with all flags turned off.
Definition bit_flags.hh:70
constexpr BitFlags(const Enum &value)
Enum constructor.
Definition bit_flags.hh:81
constexpr UnderlyingType & as_underlying()
Return bitflag as its underlying type.
Definition bit_flags.hh:103
static constexpr BitFlags< Enum > all_flags()
Bitflag with all flags turned on.
Definition bit_flags.hh:66
constexpr BitFlags operator^(const BitFlags &rhs) const
Bitwise xor operator with another bitflag.
Definition bit_flags.hh:127
constexpr BitFlags operator>>(std::size_t pos) const
Bitwise right shift operator.
Definition bit_flags.hh:139
constexpr BitFlags operator~() const
Bitwise not operator of this bitflag.
Definition bit_flags.hh:180
constexpr BitSet as_bitset() const
Return bitflag as bitset.
Definition bit_flags.hh:95
constexpr UnderlyingType as_underlying() const
Return bitflag as its underlying type.
Definition bit_flags.hh:100
constexpr bool any() const
Checks if any flags are set to true.
Definition bit_flags.hh:207
BitFlags & operator|=(const BitFlags &rhs)
Bitwise or operator with this bitflag.
Definition bit_flags.hh:145
BitFlags(const BitSet &bit_set)
Bit set constructor.
Definition bit_flags.hh:87
Definition axis_names.hh:7
constexpr std::enable_if_t< is_bitflags_v< Enum >, BitFlags< Enum > > operator|(Enum lhs, Enum rhs)
Bitwise or between two enums that may be bitflags.
Definition bit_flags.hh:220
constexpr std::enable_if_t< is_bitflags_v< Enum >, BitFlags< Enum > > operator&(Enum lhs, Enum rhs)
Bitwise and between two enums that may be bitflags.
Definition bit_flags.hh:228
constexpr std::enable_if_t< is_bitflags_v< Enum >, bool > operator==(Enum lhs, Enum rhs)
Return true if all flags of both enums (allowed to be bitflags) are equal.
Definition bit_flags.hh:244
constexpr std::enable_if_t< is_bitflags_v< Enum >, BitFlags< Enum > > operator^(Enum lhs, Enum rhs)
Bitwise xor between two enums that may be bitflags.
Definition bit_flags.hh:236
constexpr bool is_bitflags_v
Alias for Bitflag indicator.
Definition bit_flags.hh:24
constexpr std::enable_if_t< is_bitflags_v< Enum >, bool > operator!=(Enum lhs, Enum rhs)
Definition bit_flags.hh:253
Bitflag indicator.
Definition bit_flags.hh:20