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 explicit (false) Flags(enum_type e) noexcept
 Construct from a single enum flag.
constexpr Flags (value_type val) noexcept
 Construct from an underlying value.

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,
};
DFX_DECLARE_FLAGS(Modes, Mode);
Modes m{ Mode::Read };
m.setFlag(Mode::Write);
if (m.testAllFlags(Mode::Read | Mode::Write)) { ... }
#define DFX_DECLARE_OPERATOR_FOR_FLAGS(FlagsName)
Declare a free operator| to combine two enum values into a Flags.
Definition Flags.hpp:325
#define DFX_DECLARE_FLAGS(FlagsName, Enum)
Declare a convenient alias for dfx::Utils::Flags<Enum>.
Definition Flags.hpp:309
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/2]

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

Construct an empty flag set (value = 0).

◆ Flags() [2/2]

template<IsEnum Enum>
dfx::Utils::Flags< Enum >::Flags ( value_type val)
inlineexplicitconstexprnoexcept

Construct from an underlying value.

This constructor allows creating a flag set directly from an integral value. It is primarily intended for cases where flags have been serialized or manipulated as integers and need to be re-wrapped.

Warning
This constructor is unsafe as it performs no validation. It is the caller's responsibility to ensure that val represents a valid combination of flags for the enum_type. Passing arbitrary integers that do not map to valid bitmasks may result in undefined logical behavior during later flag testing.
Parameters
valThe raw integral value to be interpreted as a bitmask.

Member Function Documentation

◆ explicit()

template<IsEnum Enum>
dfx::Utils::Flags< Enum >::explicit ( false )
inlineconstexprnoexcept

Construct from a single enum flag.

Parameters
eFlag to set.

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