Gamedev Framework (gf)  0.3.0
A C++11 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
 

Public Member Functions

 Flags ()=default
 Default constructor. More...
 
constexpr Flags (NoneType)
 
constexpr Flags (AllType)
 
constexpr Flags (E e)
 Constructor with an enum value. More...
 
constexpr Flags< Eoperator~ () const
 Binary NOT operator. More...
 
Flags< E > & operator|= (Flags< E > flags)
 Binary OR and assignment. More...
 
Flags< E > & operator&= (Flags< E > flags)
 Binary AND and assignment. More...
 
 operator bool () const
 Test if any flag is set. More...
 
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 >
Flags< Eoperator| (Flags< E > lhs, Flags< E > rhs)
 Binary OR between two bitfields. More...
 
template<typename E >
Flags< Eoperator| (Flags< E > lhs, E rhs)
 Binary OR between a bitfield and a flag. More...
 
template<typename E >
Flags< Eoperator| (E lhs, Flags< E > rhs)
 Binary OR between a flag and a bitfield. More...
 
template<typename E >
Flags< Eoperator& (Flags< E > lhs, Flags< E > rhs)
 Binary AND between two bitfields. More...
 
template<typename E >
Flags< Eoperator& (Flags< E > lhs, E rhs)
 Binary AND between a bitfield and a flag. More...
 
template<typename E >
Flags< Eoperator& (E lhs, Flags< E > rhs)
 Binary AND between a flag and a bitfield. 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

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

Constructor & Destructor Documentation

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

Default constructor.

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

Constructor with an enum value.

Parameters
eAn enum value

Member Function Documentation

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
template<typename E>
gf::Flags< E >::operator bool ( ) const
inline

Test if any flag is set.

Returns
True if any flag is set
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
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
template<typename E>
constexpr Flags<E> gf::Flags< E >::operator~ ( ) const
inline

Binary NOT operator.

Returns
A bitfield where all flags have been inverted
template<typename E>
void gf::Flags< E >::reset ( E  flag)
inline

Reset a flag.

Parameters
flagThe flag to reset
template<typename E>
void gf::Flags< E >::set ( E  flag)
inline

Set a flag.

Parameters
flagThe flag to set
template<typename E>
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

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

Binary AND between two bitfields.

Parameters
lhsThe first bitfield
rhsThe second bitfield
Returns
The bitfield with a binary AND of the two bitfields
template<typename E >
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
template<typename E >
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
template<typename E >
Flags< E > operator| ( Flags< E lhs,
Flags< E rhs 
)
related

Binary OR between two bitfields.

Parameters
lhsThe first bitfield
rhsThe second bitfield
Returns
The bitfield with a binary OR of the two bitfields
template<typename E >
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
template<typename E >
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