PIDUINO
Loading...
Searching...
No Matches
Piduino::Flags< EnumType, Underlying >

A type-safe flags class for bitwise operations on enum values. More...

#include <flags.h>

Inheritance diagram for Piduino::Flags< EnumType, Underlying >:
Collaboration diagram for Piduino::Flags< EnumType, Underlying >:

Public Member Functions

 Flags (Underlying original=0)
 Default constructor that initializes flags with a given value.
 
 Flags (const Flags &original)
 Copy constructor that creates a new Flags object from an existing one.
 
Flagsoperator|= (const Flags &f)
 Bitwise OR assignment operator. Combines this flags object with another using bitwise OR operation.
 
Flagsoperator^= (const Flags &f)
 Bitwise XOR assignment operator. Combines this flags object with another using bitwise XOR operation.
 
Flagsoperator&= (const Flags &f)
 Bitwise AND assignment operator. Combines this flags object with another using bitwise AND operation.
 
Flags operator~ () const
 Bitwise NOT operator that inverts all bits in the flags.
 
 operator RestrictedBool () const
 Safe boolean conversion operator. Allows the Flags object to be used in boolean contexts without allowing implicit conversion to other types.
 
bool operator== (const Flags &other)
 Equality comparison operator.
 
bool operator!= (const Flags &other)
 Inequality comparison operator.
 
void setFlags (Underlying flags)
 Sets multiple flags using bitwise OR operation.
 
void setFlag (EnumType singleFlag)
 Sets a single flag using bitwise OR operation.
 
void clearFlags (Underlying flags)
 Clears multiple flags using bitwise AND with complement.
 
void clearFlag (EnumType singleFlag)
 Clears a single flag using bitwise AND with complement.
 
Underlying value () const
 Gets the raw underlying value of the flags.
 

Protected Attributes

Underlying m_flags
 The underlying storage for the flag bits.
 

Private Types

typedef Underlying Flags::* RestrictedBool
 Restricted bool type for safe boolean conversion.
 

Detailed Description

template<typename EnumType, typename Underlying = int>
class Piduino::Flags< EnumType, Underlying >

A type-safe flags class for bitwise operations on enum values.

This template class provides a type-safe way to handle bitwise operations on enumeration values. It allows combining multiple enum flags using bitwise operators while maintaining type safety and preventing accidental operations between incompatible types.

Template Parameters
EnumTypeThe enumeration type that defines the individual flags
UnderlyingThe underlying integral type used to store the flags (default: int)

Definition at line 34 of file flags.h.

Member Typedef Documentation

◆ RestrictedBool

template<typename EnumType , typename Underlying = int>
typedef Underlying Flags::* Piduino::Flags< EnumType, Underlying >::RestrictedBool
private

Restricted bool type for safe boolean conversion.

Definition at line 36 of file flags.h.

Constructor & Destructor Documentation

◆ Flags() [1/2]

template<typename EnumType , typename Underlying = int>
Piduino::Flags< EnumType, Underlying >::Flags ( Underlying  original = 0)
inline

Default constructor that initializes flags with a given value.

Parameters
originalThe initial value for the flags (default: 0)

Definition at line 43 of file flags.h.

◆ Flags() [2/2]

template<typename EnumType , typename Underlying = int>
Piduino::Flags< EnumType, Underlying >::Flags ( const Flags< EnumType, Underlying > &  original)
inline

Copy constructor that creates a new Flags object from an existing one.

Parameters
originalThe Flags object to copy from

Definition at line 49 of file flags.h.

Member Function Documentation

◆ clearFlag()

template<typename EnumType , typename Underlying = int>
void Piduino::Flags< EnumType, Underlying >::clearFlag ( EnumType  singleFlag)
inline

Clears a single flag using bitwise AND with complement.

Parameters
singleFlagThe individual flag to clear (as enum value)

Definition at line 182 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::clearFlags().

Here is the call graph for this function:

◆ clearFlags()

template<typename EnumType , typename Underlying = int>
void Piduino::Flags< EnumType, Underlying >::clearFlags ( Underlying  flags)
inline

Clears multiple flags using bitwise AND with complement.

Parameters
flagsThe flags to clear (as underlying type value)

Definition at line 174 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::m_flags.

Referenced by Piduino::Flags< EnumType, Underlying >::clearFlag().

◆ operator RestrictedBool()

template<typename EnumType , typename Underlying = int>
Piduino::Flags< EnumType, Underlying >::operator RestrictedBool ( ) const
inline

Safe boolean conversion operator. Allows the Flags object to be used in boolean contexts without allowing implicit conversion to other types.

Returns
A pointer-to-member if flags are non-zero, nullptr otherwise

Definition at line 132 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::m_flags.

◆ operator!=()

template<typename EnumType , typename Underlying = int>
bool Piduino::Flags< EnumType, Underlying >::operator!= ( const Flags< EnumType, Underlying > &  other)
inline

Inequality comparison operator.

Parameters
otherThe Flags object to compare with
Returns
true if the objects have different flag values, false otherwise

Definition at line 150 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::m_flags.

◆ operator&=()

template<typename EnumType , typename Underlying = int>
Flags & Piduino::Flags< EnumType, Underlying >::operator&= ( const Flags< EnumType, Underlying > &  f)
inline

Bitwise AND assignment operator. Combines this flags object with another using bitwise AND operation.

Parameters
fThe flags object to combine with
Returns
Reference to this object after the operation

Definition at line 81 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::m_flags.

◆ operator==()

template<typename EnumType , typename Underlying = int>
bool Piduino::Flags< EnumType, Underlying >::operator== ( const Flags< EnumType, Underlying > &  other)
inline

Equality comparison operator.

Parameters
otherThe Flags object to compare with
Returns
true if both objects have the same flag values, false otherwise

Definition at line 141 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::m_flags.

◆ operator^=()

template<typename EnumType , typename Underlying = int>
Flags & Piduino::Flags< EnumType, Underlying >::operator^= ( const Flags< EnumType, Underlying > &  f)
inline

Bitwise XOR assignment operator. Combines this flags object with another using bitwise XOR operation.

Parameters
fThe flags object to combine with
Returns
Reference to this object after the operation

Definition at line 70 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::m_flags.

◆ operator|=()

template<typename EnumType , typename Underlying = int>
Flags & Piduino::Flags< EnumType, Underlying >::operator|= ( const Flags< EnumType, Underlying > &  f)
inline

Bitwise OR assignment operator. Combines this flags object with another using bitwise OR operation.

Parameters
fThe flags object to combine with
Returns
Reference to this object after the operation

Definition at line 59 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::m_flags.

◆ operator~()

template<typename EnumType , typename Underlying = int>
Flags Piduino::Flags< EnumType, Underlying >::operator~ ( ) const
inline

Bitwise NOT operator that inverts all bits in the flags.

Returns
A new Flags object with all bits inverted

Definition at line 120 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::m_flags.

◆ setFlag()

template<typename EnumType , typename Underlying = int>
void Piduino::Flags< EnumType, Underlying >::setFlag ( EnumType  singleFlag)
inline

Sets a single flag using bitwise OR operation.

Parameters
singleFlagThe individual flag to set (as enum value)

Definition at line 166 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::setFlags().

Here is the call graph for this function:

◆ setFlags()

template<typename EnumType , typename Underlying = int>
void Piduino::Flags< EnumType, Underlying >::setFlags ( Underlying  flags)
inline

Sets multiple flags using bitwise OR operation.

Parameters
flagsThe flags to set (as underlying type value)

Definition at line 158 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::m_flags.

Referenced by Piduino::Flags< EnumType, Underlying >::setFlag().

◆ value()

template<typename EnumType , typename Underlying = int>
Underlying Piduino::Flags< EnumType, Underlying >::value ( ) const
inline

Gets the raw underlying value of the flags.

Returns
The underlying integral value representing all set flags

Definition at line 190 of file flags.h.

References Piduino::Flags< EnumType, Underlying >::m_flags.

Member Data Documentation

◆ m_flags