1#ifndef DUNE_COPASI_COMMON_BITFLAGS_HH
2#define DUNE_COPASI_COMMON_BITFLAGS_HH
4#include <dune-copasi-config.hh>
45template<
typename Enum>
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");
55 using BitSet = std::bitset<CHAR_BIT *
sizeof(Enum)>;
58 using UnderlyingType = std::underlying_type_t<Enum>;
61 static constexpr UnderlyingType null_value =
62 UnderlyingType{} ^ UnderlyingType{};
76 : _value{ null_value }
82 : _value(static_cast<UnderlyingType>(value))
87 inline explicit BitFlags(
const BitSet& bit_set)
88 :
BitFlags(static_cast<Enum>(bit_set.to_ullong()))
90 static_assert(
sizeof(UnderlyingType) <=
sizeof(
unsigned long long),
91 "Underlying type is longer than maximum supported cast");
95 [[nodiscard]]
constexpr inline BitSet
as_bitset()
const
97 return BitSet{
static_cast<unsigned long long>(_value) };
100 [[nodiscard]]
constexpr inline UnderlyingType
as_underlying()
const {
return _value; }
106 [[nodiscard]]
constexpr inline Enum
as_enum()
const {
return static_cast<const Enum
>(_value); }
109 constexpr inline Enum&
as_enum() {
return static_cast<Enum&
>(_value); }
112 constexpr inline explicit operator Enum()
const {
return as_enum(); }
117 return static_cast<Enum
>(_value | rhs._value);
123 return static_cast<Enum
>(_value & rhs._value);
129 return static_cast<Enum
>(_value ^ rhs._value);
135 return static_cast<Enum
>(_value << pos);
141 return static_cast<Enum
>(_value >> pos);
147 _value |= rhs._value;
154 _value &= rhs._value;
161 _value ^= rhs._value;
182 return BitFlags{
static_cast<Enum
>(~_value) };
188 return (_value & flag._value) == flag._value;
197 value ? (_value |= flag._value) :
reset(flag);
204 [[nodiscard]]
constexpr inline bool all()
const {
return as_bitset().all(); }
207 [[nodiscard]]
constexpr inline bool any()
const {
return as_bitset().any(); }
210 [[nodiscard]]
constexpr inline bool none()
const {
return as_bitset().none(); }
214 UnderlyingType _value;
219constexpr inline std::enable_if_t<is_bitflags_v<Enum>, BitFlags<Enum>>
227constexpr inline std::enable_if_t<is_bitflags_v<Enum>, BitFlags<Enum>>
235constexpr inline std::enable_if_t<is_bitflags_v<Enum>, BitFlags<Enum>>
243constexpr inline std::enable_if_t<is_bitflags_v<Enum>,
bool>
252constexpr inline std::enable_if_t<is_bitflags_v<Enum>,
bool>
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