dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
dfx::Utils::Flags< Enum > Class Template Reference

Typesafe bitmask wrapper for enum flags. More...

#include <dfx-utilities/Flags.hpp>

Public Types

using enum_type = Enum
 The wrapped enum type.
using value_type = std::underlying_type_t<Enum>
 The underlying integer storage type.

Public Member Functions

constexpr Flags () noexcept=default
 Construct an empty flag set (value = 0).
constexpr Flags (enum_type e) noexcept
 Construct from a single enum flag.
constexpr Flags (std::initializer_list< enum_type > f) noexcept
 Construct from an initializer-list of flags. All provided flags are OR-ed into the resulting mask.
constexpr Flags< enum_type > & setFlag (enum_type flag, bool on=true) noexcept
 Set or clear a single flag.
constexpr bool testFlag (enum_type flag) const noexcept
 Test whether a given flag is set.
constexpr bool testAnyFlag (Flags< enum_type > const &flags) const noexcept
 Test whether any bit from flags is set.
constexpr bool testAllFlags (Flags< enum_type > const &flags) const noexcept
 Test whether all bits from flags are set.
constexpr bool operator== (Flags< enum_type > const &other) const noexcept
 Equality comparison between two flag sets.
constexpr bool operator!= (Flags< enum_type > const &other) const noexcept
 Inequality comparison between two flag sets.
constexpr bool operator== (enum_type flag) const noexcept
 Compare the flag set against a single enum mask.
constexpr bool operator!= (enum_type flag) const noexcept
 Negated form of operator==(enum_type).
constexpr operator enum_type () const noexcept
 Implicit conversion to the enum type.
constexpr operator value_type () const noexcept
 Implicit conversion to the underlying integer storage type.
constexpr Flags< enum_typeoperator& (enum_type mask) const noexcept
 Bitwise AND with an enum mask.
constexpr Flags< enum_typeoperator& (Flags< enum_type > mask) const noexcept
 Bitwise AND with another flag set.
constexpr Flags< enum_type > & operator&= (enum_type mask) noexcept
 In-place bitwise AND with an enum mask.
constexpr Flags< enum_type > & operator&= (Flags< enum_type > mask) noexcept
 In-place bitwise AND with another flag set.
constexpr Flags< enum_typeoperator| (enum_type mask) const noexcept
 Bitwise OR with an enum mask.
constexpr Flags< enum_typeoperator| (Flags< enum_type > mask) const noexcept
 Bitwise OR with another flag set.
constexpr Flags< enum_type > & operator|= (enum_type mask) noexcept
 In-place bitwise OR with an enum mask.
constexpr Flags< enum_type > & operator|= (Flags< enum_type > mask) noexcept
 In-place bitwise OR with another flag set.
constexpr Flags< enum_typeoperator^ (enum_type mask) const noexcept
 Bitwise XOR with an enum mask.
constexpr Flags< enum_typeoperator^ (Flags< enum_type > mask) const noexcept
 Bitwise XOR with another flag set.
constexpr Flags< enum_type > & operator^= (enum_type mask) noexcept
 In-place bitwise XOR with an enum mask.
constexpr Flags< enum_type > & operator^= (Flags< enum_type > mask) noexcept
 In-place bitwise XOR with another flag set.
constexpr Flags< enum_typeoperator~ () const noexcept
 Bitwise NOT.
constexpr bool operator! () const noexcept
 Logical negation.

Detailed Description

template<IsEnum Enum>
class dfx::Utils::Flags< Enum >

Typesafe bitmask wrapper for enum flags.

Flags<Enum> wraps an enum whose enumerators represent bit flags (typically powers of two), and provides common bitmask operations (|, &, ^, ~), as well as helpers to set/test flags.

The internal storage is the enum underlying type (std::underlying_type_t<Enum>).

Template Parameters
EnumAn enum type used as a bitmask (must satisfy IsEnum).
Typical usage
enum class Mode : uint32_t
{
Read = 0x01,
Write = 0x02,
Exec = 0x04,
};
DECLARE_FLAGS(Modes, Mode);
Modes m{ Mode::Read };
m.setFlag(Mode::Write);
if (m.testAllFlags(Mode::Read | Mode::Write)) { ... }
#define DECLARE_OPERATOR_FOR_FLAGS(FlagsName)
Declare a free operator| to combine two enum values into a Flags.
Definition Flags.hpp:291
#define DECLARE_FLAGS(FlagsName, Enum)
Declare a convenient alias for dfx::Utils::Flags<Enum>.
Definition Flags.hpp:275
Notes / caveats
  • No validation is performed: if your enum values are not proper bit flags, semantics are on you.
  • operator enum_type() converts the underlying bitmask back to the enum type; this may produce a value that is not one of the declared enumerators. This is intentional for flag enums.

Member Typedef Documentation

◆ enum_type

template<IsEnum Enum>
using dfx::Utils::Flags< Enum >::enum_type = Enum

The wrapped enum type.

◆ value_type

template<IsEnum Enum>
using dfx::Utils::Flags< Enum >::value_type = std::underlying_type_t<Enum>

The underlying integer storage type.

Constructor & Destructor Documentation

◆ Flags() [1/3]

template<IsEnum Enum>
dfx::Utils::Flags< Enum >::Flags ( )
constexprdefaultnoexcept

Construct an empty flag set (value = 0).

◆ Flags() [2/3]

template<IsEnum Enum>
dfx::Utils::Flags< Enum >::Flags ( enum_type e)
inlineconstexprnoexcept

Construct from a single enum flag.

Parameters
eFlag to set.

◆ Flags() [3/3]

template<IsEnum Enum>
dfx::Utils::Flags< Enum >::Flags ( std::initializer_list< enum_type > f)
inlineconstexprnoexcept

Construct from an initializer-list of flags. All provided flags are OR-ed into the resulting mask.

Parameters
fList of flags to set.

Member Function Documentation

◆ operator enum_type()

template<IsEnum Enum>
dfx::Utils::Flags< Enum >::operator enum_type ( ) const
inlinenodiscardconstexprnoexcept

Implicit conversion to the enum type.

Returns
The current bitmask reinterpreted as the enum type.
Warning
The returned enum value may not correspond to a named enumerator.

◆ operator value_type()

template<IsEnum Enum>
dfx::Utils::Flags< Enum >::operator value_type ( ) const
inlinenodiscardconstexprnoexcept

Implicit conversion to the underlying integer storage type.

Returns
The underlying bitmask value.

◆ operator!()

template<IsEnum Enum>
bool dfx::Utils::Flags< Enum >::operator! ( ) const
inlinenodiscardconstexprnoexcept

Logical negation.

◆ operator!=() [1/2]

template<IsEnum Enum>
bool dfx::Utils::Flags< Enum >::operator!= ( enum_type flag) const
inlinenodiscardconstexprnoexcept

Negated form of operator==(enum_type).

◆ operator!=() [2/2]

template<IsEnum Enum>
bool dfx::Utils::Flags< Enum >::operator!= ( Flags< enum_type > const & other) const
inlinenodiscardconstexprnoexcept

Inequality comparison between two flag sets.

◆ operator&() [1/2]

template<IsEnum Enum>
Flags< enum_type > dfx::Utils::Flags< Enum >::operator& ( enum_type mask) const
inlinenodiscardconstexprnoexcept

Bitwise AND with an enum mask.

◆ operator&() [2/2]

template<IsEnum Enum>
Flags< enum_type > dfx::Utils::Flags< Enum >::operator& ( Flags< enum_type > mask) const
inlinenodiscardconstexprnoexcept

Bitwise AND with another flag set.

◆ operator&=() [1/2]

template<IsEnum Enum>
Flags< enum_type > & dfx::Utils::Flags< Enum >::operator&= ( enum_type mask)
inlineconstexprnoexcept

In-place bitwise AND with an enum mask.

◆ operator&=() [2/2]

template<IsEnum Enum>
Flags< enum_type > & dfx::Utils::Flags< Enum >::operator&= ( Flags< enum_type > mask)
inlineconstexprnoexcept

In-place bitwise AND with another flag set.

◆ operator==() [1/2]

template<IsEnum Enum>
bool dfx::Utils::Flags< Enum >::operator== ( enum_type flag) const
inlinenodiscardconstexprnoexcept

Compare the flag set against a single enum mask.

◆ operator==() [2/2]

template<IsEnum Enum>
bool dfx::Utils::Flags< Enum >::operator== ( Flags< enum_type > const & other) const
inlinenodiscardconstexprnoexcept

Equality comparison between two flag sets.

◆ operator^() [1/2]

template<IsEnum Enum>
Flags< enum_type > dfx::Utils::Flags< Enum >::operator^ ( enum_type mask) const
inlinenodiscardconstexprnoexcept

Bitwise XOR with an enum mask.

◆ operator^() [2/2]

template<IsEnum Enum>
Flags< enum_type > dfx::Utils::Flags< Enum >::operator^ ( Flags< enum_type > mask) const
inlinenodiscardconstexprnoexcept

Bitwise XOR with another flag set.

◆ operator^=() [1/2]

template<IsEnum Enum>
Flags< enum_type > & dfx::Utils::Flags< Enum >::operator^= ( enum_type mask)
inlineconstexprnoexcept

In-place bitwise XOR with an enum mask.

◆ operator^=() [2/2]

template<IsEnum Enum>
Flags< enum_type > & dfx::Utils::Flags< Enum >::operator^= ( Flags< enum_type > mask)
inlineconstexprnoexcept

In-place bitwise XOR with another flag set.

◆ operator|() [1/2]

template<IsEnum Enum>
Flags< enum_type > dfx::Utils::Flags< Enum >::operator| ( enum_type mask) const
inlinenodiscardconstexprnoexcept

Bitwise OR with an enum mask.

◆ operator|() [2/2]

template<IsEnum Enum>
Flags< enum_type > dfx::Utils::Flags< Enum >::operator| ( Flags< enum_type > mask) const
inlinenodiscardconstexprnoexcept

Bitwise OR with another flag set.

◆ operator|=() [1/2]

template<IsEnum Enum>
Flags< enum_type > & dfx::Utils::Flags< Enum >::operator|= ( enum_type mask)
inlineconstexprnoexcept

In-place bitwise OR with an enum mask.

◆ operator|=() [2/2]

template<IsEnum Enum>
Flags< enum_type > & dfx::Utils::Flags< Enum >::operator|= ( Flags< enum_type > mask)
inlineconstexprnoexcept

In-place bitwise OR with another flag set.

◆ operator~()

template<IsEnum Enum>
Flags< enum_type > dfx::Utils::Flags< Enum >::operator~ ( ) const
inlinenodiscardconstexprnoexcept

Bitwise NOT.

◆ setFlag()

template<IsEnum Enum>
Flags< enum_type > & dfx::Utils::Flags< Enum >::setFlag ( enum_type flag,
bool on = true )
inlineconstexprnoexcept

Set or clear a single flag.

Parameters
flagFlag to modify.
onIf true, sets the flag; if false, clears it.
Returns
*this.

◆ testAllFlags()

template<IsEnum Enum>
bool dfx::Utils::Flags< Enum >::testAllFlags ( Flags< enum_type > const & flags) const
inlinenodiscardconstexprnoexcept

Test whether all bits from flags are set.

Parameters
flagsMask of bits to test.
Returns
True if all those bits are set.

◆ testAnyFlag()

template<IsEnum Enum>
bool dfx::Utils::Flags< Enum >::testAnyFlag ( Flags< enum_type > const & flags) const
inlinenodiscardconstexprnoexcept

Test whether any bit from flags is set.

Parameters
flagsMask of bits to test.
Returns
True if at least one of those bits is set.

◆ testFlag()

template<IsEnum Enum>
bool dfx::Utils::Flags< Enum >::testFlag ( enum_type flag) const
inlinenodiscardconstexprnoexcept

Test whether a given flag is set.

This checks that all bits in flag are present in this mask.

Parameters
flagFlag (or bitmask) to test.
Returns
True if all bits from flag are set.

The documentation for this class was generated from the following file: