Sparkle 0.0.1
Loading...
Searching...
No Matches
spk::Flags< TFlagType, StorageT > Struct Template Reference

Type-safe bitmask wrapper for enum flags. More...

#include <spk_flags.hpp>

Collaboration diagram for spk::Flags< TFlagType, StorageT >:
Collaboration graph

Public Types

using MaskType = StorageT
 Unsigned storage type used to hold flag bits.

Public Member Functions

constexpr Flags ()=default
 Builds an empty flag set.
constexpr Flags (TFlagType p_v)
 Initializes with a single flag.
constexpr Flags (MaskType p_raw)
 Initializes with raw bits.
constexpr Flags (std::initializer_list< TFlagType > p_values)
 Initializes with multiple flags.
constexpr void clear ()
 Clears all flags.
constexpr void reset (TFlagType p_v)
 Removes specific flags.
constexpr Flagsoperator|= (TFlagType p_v)
 In-place OR with a flag.
constexpr Flagsoperator&= (TFlagType p_v)
 In-place AND with a mask.
constexpr Flagsoperator^= (TFlagType p_v)
 In-place XOR with a flag.
constexpr bool has (TFlagType p_v) const
 Tests whether all bits in a mask are set.
constexpr bool testAny (TFlagType p_v) const
 Tests whether any bit in a mask is set.
constexpr bool any () const
 Checks if any flag is set.
constexpr bool none () const
 Checks if no flags are set.
constexpr operator bool () const
 Tests whether any bit is set.
constexpr MaskType raw () const
 Returns the underlying bits.

Public Attributes

MaskType bits {}
 Raw underlying bit storage.

Detailed Description

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
struct spk::Flags< TFlagType, StorageT >

Type-safe bitmask wrapper for enum flags.

Template Parameters
TFlagTypeEnum type satisfying bitmask_enum.
StorageTUnsigned storage type (8/16/32-bit).
See also
bitmask_enum
enum class Option : uint32_t { A = 1u << 0, B = 1u << 1 };
spk::Flags<Option> mask{Option::A};
mask |= Option::B;
if (mask.has(Option::A)) { use(); }
Type-safe bitmask wrapper for enum flags.
Definition spk_flags.hpp:111
constexpr bool has(TFlagType p_v) const
Tests whether all bits in a mask are set.
Definition spk_flags.hpp:210

Constructor & Destructor Documentation

◆ Flags() [1/3]

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
spk::Flags< TFlagType, StorageT >::Flags ( TFlagType p_v)
inlineconstexpr

Initializes with a single flag.

Parameters
p_vFlag to set.

◆ Flags() [2/3]

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
spk::Flags< TFlagType, StorageT >::Flags ( MaskType p_raw)
inlineexplicitconstexpr

Initializes with raw bits.

Parameters
p_rawInitial mask.

◆ Flags() [3/3]

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
spk::Flags< TFlagType, StorageT >::Flags ( std::initializer_list< TFlagType > p_values)
inlineconstexpr

Initializes with multiple flags.

Parameters
p_valuesFlags to OR together.

Member Function Documentation

◆ any()

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
bool spk::Flags< TFlagType, StorageT >::any ( ) const
inlineconstexpr

Checks if any flag is set.

Returns
true when bits is non-zero.

◆ has()

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
bool spk::Flags< TFlagType, StorageT >::has ( TFlagType p_v) const
inlineconstexpr

Tests whether all bits in a mask are set.

Parameters
p_vFlags to check.
Returns
true when every requested flag is present.

◆ none()

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
bool spk::Flags< TFlagType, StorageT >::none ( ) const
inlineconstexpr

Checks if no flags are set.

Returns
true when bits is zero.

◆ operator bool()

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
spk::Flags< TFlagType, StorageT >::operator bool ( ) const
inlineexplicitconstexpr

Tests whether any bit is set.

Returns
true when any flag is present.

◆ operator&=()

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
Flags & spk::Flags< TFlagType, StorageT >::operator&= ( TFlagType p_v)
inlineconstexpr

In-place AND with a mask.

Parameters
p_vFlags to keep.
Returns
Reference to this mask.

◆ operator^=()

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
Flags & spk::Flags< TFlagType, StorageT >::operator^= ( TFlagType p_v)
inlineconstexpr

In-place XOR with a flag.

Parameters
p_vFlag to toggle.
Returns
Reference to this mask.

◆ operator|=()

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
Flags & spk::Flags< TFlagType, StorageT >::operator|= ( TFlagType p_v)
inlineconstexpr

In-place OR with a flag.

Parameters
p_vFlag to set.
Returns
Reference to this mask.

◆ raw()

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
MaskType spk::Flags< TFlagType, StorageT >::raw ( ) const
inlineconstexpr

Returns the underlying bits.

Returns
Raw mask.

◆ reset()

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
void spk::Flags< TFlagType, StorageT >::reset ( TFlagType p_v)
inlineconstexpr

Removes specific flags.

Parameters
p_vFlags to unset.

◆ testAny()

template<bitmask_enum TFlagType, unsigned_storage StorageT = std::uint32_t>
bool spk::Flags< TFlagType, StorageT >::testAny ( TFlagType p_v) const
inlineconstexpr

Tests whether any bit in a mask is set.

Parameters
p_vFlags to check.
Returns
true when at least one requested flag is present.

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