idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
idfxx::flags< E > Class Template Reference

Type-safe set of flags from a scoped enum. More...

#include <idfxx_core/include/idfxx/flags.hpp>

Public Types

using enum_type = E
 The scoped enum type.
 
using underlying = std::underlying_type_t< E >
 The underlying integral type of the enum.
 

Public Member Functions

constexpr flags () noexcept=default
 Default constructor, initializes to empty flags (zero).
 
constexpr flags (E e) noexcept
 Constructs from a single enum value.
 
constexpr flags operator| (flags other) const noexcept
 Combines flags using bitwise OR.
 
constexpr flagsoperator|= (flags other) noexcept
 Combines flags in-place using bitwise OR.
 
constexpr flags operator& (flags other) const noexcept
 Intersects flags using bitwise AND.
 
constexpr flagsoperator&= (flags other) noexcept
 Intersects flags in-place using bitwise AND.
 
constexpr flags operator^ (flags other) const noexcept
 Toggles flags using bitwise XOR.
 
constexpr flagsoperator^= (flags other) noexcept
 Toggles flags in-place using bitwise XOR.
 
constexpr flags operator- (flags other) const noexcept
 Clears specific flags (set difference).
 
constexpr flagsoperator-= (flags other) noexcept
 Clears specific flags in-place (set difference).
 
constexpr flags operator~ () const noexcept
 Computes the bitwise complement.
 
constexpr bool contains (flags other) const noexcept
 Checks if all specified flags are set.
 
constexpr bool contains_any (flags other) const noexcept
 Checks if any of the specified flags are set.
 
constexpr bool empty () const noexcept
 Checks if no flags are set.
 
constexpr operator bool () const noexcept
 Explicit conversion to bool.
 
constexpr underlying value () const noexcept
 Returns the underlying integral value.
 
constexpr bool operator== (flags const &) const noexcept=default
 Equality comparison between flags objects.
 
constexpr bool operator== (E e) const noexcept
 Equality comparison with an individual enum value.
 

Static Public Member Functions

static constexpr flags from_raw (underlying v) noexcept
 Constructs flags from a raw underlying value.
 

Detailed Description

template<flag_enum E>
class idfxx::flags< E >

Type-safe set of flags from a scoped enum.

Provides type-safe bitflag operations with full operator support. Individual enum values implicitly convert to flags<E>, allowing natural syntax like: auto f = my_flag::a | my_flag::b;

All operations are constexpr and marked [[nodiscard]] to prevent accidental misuse. The class provides zero-overhead abstractions over raw bitwise operations.

Requires opt-in via:

template<> inline constexpr bool idfxx::enable_flags_operators<MyEnum> = true;
Template Parameters
EThe flag enum type (must satisfy flag_enum concept).

Definition at line 88 of file flags.hpp.

Member Typedef Documentation

◆ enum_type

template<flag_enum E>
using idfxx::flags< E >::enum_type = E

The scoped enum type.

Definition at line 91 of file flags.hpp.

◆ underlying

template<flag_enum E>
using idfxx::flags< E >::underlying = std::underlying_type_t<E>

The underlying integral type of the enum.

Definition at line 94 of file flags.hpp.

Constructor & Destructor Documentation

◆ flags() [1/2]

template<flag_enum E>
constexpr idfxx::flags< E >::flags ( )
constexprdefaultnoexcept

Default constructor, initializes to empty flags (zero).

◆ flags() [2/2]

template<flag_enum E>
constexpr idfxx::flags< E >::flags ( e)
inlineconstexprnoexcept

Constructs from a single enum value.

Allows implicit conversion from enum values to flags, enabling natural syntax when passing enum values to functions expecting flags.

Parameters
eThe enum value to initialize from.

Definition at line 118 of file flags.hpp.

Member Function Documentation

◆ contains()

template<flag_enum E>
constexpr bool idfxx::flags< E >::contains ( flags< E >  other) const
inlineconstexprnoexcept

Checks if all specified flags are set.

Returns true if all bits set in other are also set in this object. Returns false if other is empty (zero).

Parameters
otherThe flags to check for.
Returns
True if all specified flags are set, false otherwise.

Definition at line 227 of file flags.hpp.

◆ contains_any()

template<flag_enum E>
constexpr bool idfxx::flags< E >::contains_any ( flags< E >  other) const
inlineconstexprnoexcept

Checks if any of the specified flags are set.

Returns true if at least one bit set in other is also set in this object.

Parameters
otherThe flags to check for.
Returns
True if any specified flags are set, false otherwise.

Definition at line 240 of file flags.hpp.

◆ empty()

template<flag_enum E>
constexpr bool idfxx::flags< E >::empty ( ) const
inlineconstexprnoexcept

Checks if no flags are set.

Returns
True if the flags object is empty (all bits zero).

Definition at line 247 of file flags.hpp.

◆ from_raw()

template<flag_enum E>
static constexpr flags idfxx::flags< E >::from_raw ( underlying  v)
inlinestaticconstexprnoexcept

Constructs flags from a raw underlying value.

Use with care, as this bypasses type safety and may create flags with bits set that don't correspond to defined enum values.

Parameters
vThe raw underlying value.
Returns
A flags object with the specified raw value.

Definition at line 273 of file flags.hpp.

Referenced by idfxx::event_group< E >::clear(), idfxx::event_group< E >::clear_from_isr(), idfxx::event_group< E >::get(), idfxx::event_group< E >::get_from_isr(), and idfxx::event_group< E >::set().

◆ operator bool()

template<flag_enum E>
constexpr idfxx::flags< E >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Explicit conversion to bool.

Returns
True if any flags are set (non-zero), false if empty.

Definition at line 254 of file flags.hpp.

◆ operator&()

template<flag_enum E>
constexpr flags idfxx::flags< E >::operator& ( flags< E >  other) const
inlineconstexprnoexcept

Intersects flags using bitwise AND.

Parameters
otherThe flags to intersect with.
Returns
A new flags object containing only bits set in both operands.

Definition at line 149 of file flags.hpp.

◆ operator&=()

template<flag_enum E>
constexpr flags & idfxx::flags< E >::operator&= ( flags< E >  other)
inlineconstexprnoexcept

Intersects flags in-place using bitwise AND.

Parameters
otherThe flags to intersect with.
Returns
Reference to this object.

Definition at line 158 of file flags.hpp.

◆ operator-()

template<flag_enum E>
constexpr flags idfxx::flags< E >::operator- ( flags< E >  other) const
inlineconstexprnoexcept

Clears specific flags (set difference).

Removes all bits set in other from this flags object.

Parameters
otherThe flags to remove.
Returns
A new flags object with the specified flags cleared.

Definition at line 193 of file flags.hpp.

◆ operator-=()

template<flag_enum E>
constexpr flags & idfxx::flags< E >::operator-= ( flags< E >  other)
inlineconstexprnoexcept

Clears specific flags in-place (set difference).

Parameters
otherThe flags to remove.
Returns
Reference to this object.

Definition at line 202 of file flags.hpp.

◆ operator==() [1/2]

template<flag_enum E>
constexpr bool idfxx::flags< E >::operator== ( e) const
inlineconstexprnoexcept

Equality comparison with an individual enum value.

Parameters
eThe enum value to compare against.
Returns
True if this flags object equals the single enum value.

Definition at line 289 of file flags.hpp.

◆ operator==() [2/2]

template<flag_enum E>
constexpr bool idfxx::flags< E >::operator== ( flags< E > const &  ) const
constexprdefaultnoexcept

Equality comparison between flags objects.

Returns
True if both flags have the same underlying value.

◆ operator^()

template<flag_enum E>
constexpr flags idfxx::flags< E >::operator^ ( flags< E >  other) const
inlineconstexprnoexcept

Toggles flags using bitwise XOR.

Parameters
otherThe flags to toggle.
Returns
A new flags object with toggled bits.

Definition at line 170 of file flags.hpp.

◆ operator^=()

template<flag_enum E>
constexpr flags & idfxx::flags< E >::operator^= ( flags< E >  other)
inlineconstexprnoexcept

Toggles flags in-place using bitwise XOR.

Parameters
otherThe flags to toggle.
Returns
Reference to this object.

Definition at line 179 of file flags.hpp.

◆ operator|()

template<flag_enum E>
constexpr flags idfxx::flags< E >::operator| ( flags< E >  other) const
inlineconstexprnoexcept

Combines flags using bitwise OR.

Parameters
otherThe flags to combine with.
Returns
A new flags object containing all set bits from both operands.

Definition at line 128 of file flags.hpp.

◆ operator|=()

template<flag_enum E>
constexpr flags & idfxx::flags< E >::operator|= ( flags< E >  other)
inlineconstexprnoexcept

Combines flags in-place using bitwise OR.

Parameters
otherThe flags to combine with.
Returns
Reference to this object.

Definition at line 137 of file flags.hpp.

◆ operator~()

template<flag_enum E>
constexpr flags idfxx::flags< E >::operator~ ( ) const
inlineconstexprnoexcept

Computes the bitwise complement.

Inverts all bits in the underlying type. Note that this includes bits beyond those defined in the enum.

Returns
A new flags object with all bits inverted.

Definition at line 215 of file flags.hpp.

◆ value()

template<flag_enum E>
constexpr underlying idfxx::flags< E >::value ( ) const
inlineconstexprnoexcept

Returns the underlying integral value.

Returns
The underlying enum value.

Definition at line 261 of file flags.hpp.

Referenced by idfxx::to_string().


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