Gamedev Framework (gf)  0.12.0
A C++14 framework for 2D games
Public Types | Public Member Functions | Related Functions | List of all members
gf::Flags< E > Class Template Reference

Bitfield relying on an enumeration. More...

#include <gf/Flags.h>

Public Types

using Type = typename std::underlying_type< E >::type
 The underlying type of the enum. More...
 

Public Member Functions

 Flags ()=default
 Default constructor. More...
 
constexpr Flags (NoneType)
 Constructor with no flag set. More...
 
constexpr Flags (AllType)
 Constructor with all flags set. More...
 
constexpr Flags (E e)
 Constructor with an enum value. More...
 
constexpr Flags< Eoperator~ () const
 Binary NOT operator. More...
 
constexpr Flags operator| (Flags flags) const
 Binary OR between two bitfields. More...
 
Flags< E > & operator|= (Flags< E > flags)
 Binary OR and assignment. More...
 
constexpr Flags operator & (Flags flags) const
 Binary AND between two bitfields. More...
 
Flags< E > & operator &= (Flags< E > flags)
 Binary AND and assignment. More...
 
constexpr operator bool () const
 Test if any flag is set. More...
 
constexpr bool test (E flag) const
 Test if a specified flag is set. More...
 
void set (E flag)
 Set a flag. More...
 
void reset (E flag)
 Reset a flag. More...
 
Type getValue () const
 Get the underlying value of the flags. More...
 

Related Functions

(Note that these are not member functions.)

template<typename E >
constexpr Flags< Eoperator| (Flags< E > lhs, E rhs)
 Binary OR between a bitfield and a flag. More...
 
template<typename E >
constexpr Flags< Eoperator| (E lhs, Flags< E > rhs)
 Binary OR between a flag and a bitfield. More...
 
template<typename E >
constexpr Flags< Eoperator& (Flags< E > lhs, E rhs)
 Binary AND between a bitfield and a flag. More...
 
template<typename E >
constexpr Flags< Eoperator& (E lhs, Flags< E > rhs)
 Binary AND between a flag and a bitfield. More...
 
template<typename E >
constexpr Flags< EcombineFlags (E flag)
 Combine a single enum value into a flag value. More...
 
template<typename E , typename ... F>
constexpr Flags< EcombineFlags (E flag, F ... others)
 Combine several enum values into a flag value. More...
 

Detailed Description

template<typename E>
class gf::Flags< E >

Bitfield relying on an enumeration.

enum class AnimalProperties {
HasClaws = 0x01,
CanFly = 0x02,
EatsFish = 0x04,
IsEndangered = 0x08,
};
namespace gf {
template<>
struct EnableBitmaskOperators<AnimalProperties> {
static constexpr bool value = true;
};
}
gf::Flags<AnimalProperties> seahawk = AnimalProperties::CanFly | AnimalProperties::EatsFish;
seahawk |= AnimalProperties::IsEndangered;
bool b = seahawk.test(AnimalProperties::HasClaws); // false

If you do not initialize flags, then the state is undefined. You can use semantic constants gf::All and gf::None to set all the flags or none.

Member Typedef Documentation

◆ Type

template<typename E>
using gf::Flags< E >::Type = typename std::underlying_type<E>::type

The underlying type of the enum.

Constructor & Destructor Documentation

◆ Flags() [1/4]

template<typename E>
gf::Flags< E >::Flags ( )
default

Default constructor.

◆ Flags() [2/4]

template<typename E>
constexpr gf::Flags< E >::Flags ( NoneType  )
inline

Constructor with no flag set.

◆ Flags() [3/4]

template<typename E>
constexpr gf::Flags< E >::Flags ( AllType  )
inline

Constructor with all flags set.

◆ Flags() [4/4]

template<typename E>
constexpr gf::Flags< E >::Flags ( E  e)
inline

Constructor with an enum value.

Parameters
eAn enum value

Member Function Documentation

◆ getValue()

template<typename E>
Type gf::Flags< E >::getValue ( ) const
inline

Get the underlying value of the flags.

This function should not be used in normal cases.

Returns
The value of the flags

◆ operator &()

template<typename E>
constexpr Flags gf::Flags< E >::operator& ( Flags< E flags) const
inline

Binary AND between two bitfields.

Parameters
flagsAnother bitfield
Returns
The bitfield with a binary AND of the two bitfields

◆ operator &=()

template<typename E>
Flags<E>& gf::Flags< E >::operator&= ( Flags< E flags)
inline

Binary AND and assignment.

Parameters
flagsAnother bitfield
Returns
The bitfield with a binary AND of the flags and the other's flags

◆ operator bool()

template<typename E>
constexpr gf::Flags< E >::operator bool ( ) const
inline

Test if any flag is set.

Returns
True if any flag is set

◆ operator|()

template<typename E>
constexpr Flags gf::Flags< E >::operator| ( Flags< E flags) const
inline

Binary OR between two bitfields.

Parameters
flagsAnother bitfield
Returns
The bitfield with a binary OR of the two bitfields

◆ operator|=()

template<typename E>
Flags<E>& gf::Flags< E >::operator|= ( Flags< E flags)
inline

Binary OR and assignment.

Parameters
flagsAnother bitfield
Returns
The bitfield with a binary OR of the flags and the other's flags

◆ operator~()

template<typename E>
constexpr Flags<E> gf::Flags< E >::operator~ ( ) const
inline

Binary NOT operator.

Returns
A bitfield where all flags have been inverted

◆ reset()

template<typename E>
void gf::Flags< E >::reset ( E  flag)
inline

Reset a flag.

Parameters
flagThe flag to reset

◆ set()

template<typename E>
void gf::Flags< E >::set ( E  flag)
inline

Set a flag.

Parameters
flagThe flag to set

◆ test()

template<typename E>
constexpr bool gf::Flags< E >::test ( E  flag) const
inline

Test if a specified flag is set.

Parameters
flagThe flag to test
Returns
True if the flag is set

Friends And Related Function Documentation

◆ combineFlags() [1/2]

template<typename E >
constexpr Flags< E > combineFlags ( E  flag)
related

Combine a single enum value into a flag value.

Parameters
flagAn enum value
Returns
The enum value turned into a flag value

◆ combineFlags() [2/2]

template<typename E , typename ... F>
constexpr Flags< E > combineFlags ( E  flag,
F ...  others 
)
related

Combine several enum values into a flag value.

Parameters
flagAn enum value
othersThe other enum values
Returns
The enum values turned into a flag value

◆ operator&() [1/2]

template<typename E >
constexpr Flags< E > operator & ( Flags< E lhs,
E  rhs 
)
related

Binary AND between a bitfield and a flag.

Parameters
lhsThe bitfield
rhsThe flag
Returns
The bitfield with a binary AND of the bitfield and the flag

◆ operator&() [2/2]

template<typename E >
constexpr Flags< E > operator & ( E  lhs,
Flags< E rhs 
)
related

Binary AND between a flag and a bitfield.

Parameters
lhsThe flag
rhsThe bitfield
Returns
The bitfield with a binary AND of the flag and the bitfield

◆ operator|() [1/2]

template<typename E >
constexpr Flags< E > operator| ( Flags< E lhs,
E  rhs 
)
related

Binary OR between a bitfield and a flag.

Parameters
lhsThe bitfield
rhsThe flag
Returns
The bitfield with a binary OR of the bitfield and the flag

◆ operator|() [2/2]

template<typename E >
constexpr Flags< E > operator| ( E  lhs,
Flags< E rhs 
)
related

Binary OR between a flag and a bitfield.

Parameters
lhsThe flag
rhsThe bitfield
Returns
The bitfield with a binary OR of the flag and the bitfield