OpenEnroth 73e68f7
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Private Types | Static Private Member Functions | Private Attributes | Friends | List of all members
Flags< Enum > Class Template Reference

#include <Flags.h>

Classes

struct  Dummy
 

Public Types

using enumeration_type = Enum
 
using underlying_type = std::underlying_type_t< Enum >
 

Public Member Functions

constexpr Flags (std::nullptr_t=0)
 
constexpr Flags (enumeration_type value)
 
constexpr Flags (underlying_type value)
 
constexpr operator underlying_type () const
 
constexpr Flagsoperator&= (Flags mask)
 
constexpr Flagsoperator&= (enumeration_type mask)
 
constexpr Flagsoperator|= (Flags flags)
 
constexpr Flagsoperator|= (enumeration_type flags)
 
constexpr Flags operator~ () const
 
constexpr bool operator! () const
 
constexpr operator fake_bool () const
 

Private Types

using fake_bool = void(*)(Dummy *)
 

Static Private Member Functions

static void fake_true (Dummy *)
 

Private Attributes

underlying_type _value
 

Friends

constexpr friend Flags operator| (Flags l, Flags r)
 
constexpr friend Flags operator| (enumeration_type l, Flags r)
 
constexpr friend Flags operator| (Flags l, enumeration_type r)
 
constexpr friend Flags operator& (Flags l, Flags r)
 
constexpr friend Flags operator& (enumeration_type l, Flags r)
 
constexpr friend Flags operator& (Flags l, enumeration_type r)
 
constexpr friend bool operator== (Flags l, Flags r)
 
constexpr friend bool operator== (enumeration_type l, Flags r)
 

Detailed Description

template<class Enum>
class Flags< Enum >

Flags is basically an analog of QFlags from Qt, a type-safe wrapper for flags defined in enumerations.

Example usage:

class MonsterType {
public:
enum class Ability {
ABILITY_FLYING = 0x1,
ABILITY_RUNNING = 0x2,
ABILITY_SHOOTING = 0x4
};
using enum Ability;
MM_DECLARE_FLAGS(Abilities, Ability)
// ...
};
MM_DECLARE_OPERATORS_FOR_FLAGS(MonsterType::Abilities)
#define MM_DECLARE_OPERATORS_FOR_FLAGS(FLAGS)
Definition: Flags.h:142
#define MM_DECLARE_FLAGS(FLAGS, ENUM)
Definition: Flags.h:138
MonsterType
Definition: MonsterEnums.h:306

Member Typedef Documentation

◆ enumeration_type

template<class Enum >
using Flags< Enum >::enumeration_type = Enum

◆ fake_bool

template<class Enum >
using Flags< Enum >::fake_bool = void (*)(Dummy *)
private

◆ underlying_type

template<class Enum >
using Flags< Enum >::underlying_type = std::underlying_type_t<Enum>

Constructor & Destructor Documentation

◆ Flags() [1/3]

template<class Enum >
constexpr Flags< Enum >::Flags ( std::nullptr_t  = 0)
inlineconstexpr

◆ Flags() [2/3]

template<class Enum >
constexpr Flags< Enum >::Flags ( enumeration_type  value)
inlineconstexpr

◆ Flags() [3/3]

template<class Enum >
constexpr Flags< Enum >::Flags ( underlying_type  value)
inlineexplicitconstexpr

Member Function Documentation

◆ fake_true()

template<class Enum >
static void Flags< Enum >::fake_true ( Dummy )
inlinestaticprivate

◆ operator fake_bool()

template<class Enum >
constexpr Flags< Enum >::operator fake_bool ( ) const
inlineconstexpr

Implicit operator bool with some dark magic on top.

Explicit operator bool doesn't work here because then we can't just write return flags & SOME_FLAG in a function returning bool.

Implicit operator bool allows to do arithmetic on top of the returned result, so something like return flags == 0x16 actually compiles, albeit in a totally unexpected way (comparing bool to 0x16 always returns false).

Thus the solution with a function pointer.

Returns
Whether there are no flags set.

◆ operator underlying_type()

template<class Enum >
constexpr Flags< Enum >::operator underlying_type ( ) const
inlineexplicitconstexpr

◆ operator!()

template<class Enum >
constexpr bool Flags< Enum >::operator! ( ) const
inlineconstexpr

◆ operator&=() [1/2]

template<class Enum >
constexpr Flags & Flags< Enum >::operator&= ( enumeration_type  mask)
inlineconstexpr

◆ operator&=() [2/2]

template<class Enum >
constexpr Flags & Flags< Enum >::operator&= ( Flags< Enum mask)
inlineconstexpr

◆ operator|=() [1/2]

template<class Enum >
constexpr Flags & Flags< Enum >::operator|= ( enumeration_type  flags)
inlineconstexpr

◆ operator|=() [2/2]

template<class Enum >
constexpr Flags & Flags< Enum >::operator|= ( Flags< Enum flags)
inlineconstexpr

◆ operator~()

template<class Enum >
constexpr Flags Flags< Enum >::operator~ ( ) const
inlineconstexpr

Friends And Related Function Documentation

◆ operator& [1/3]

template<class Enum >
constexpr friend Flags operator& ( enumeration_type  l,
Flags< Enum r 
)
friend

◆ operator& [2/3]

template<class Enum >
constexpr friend Flags operator& ( Flags< Enum l,
enumeration_type  r 
)
friend

◆ operator& [3/3]

template<class Enum >
constexpr friend Flags operator& ( Flags< Enum l,
Flags< Enum r 
)
friend

◆ operator== [1/2]

template<class Enum >
constexpr friend bool operator== ( enumeration_type  l,
Flags< Enum r 
)
friend

◆ operator== [2/2]

template<class Enum >
constexpr friend bool operator== ( Flags< Enum l,
Flags< Enum r 
)
friend

◆ operator| [1/3]

template<class Enum >
constexpr friend Flags operator| ( enumeration_type  l,
Flags< Enum r 
)
friend

◆ operator| [2/3]

template<class Enum >
constexpr friend Flags operator| ( Flags< Enum l,
enumeration_type  r 
)
friend

◆ operator| [3/3]

template<class Enum >
constexpr friend Flags operator| ( Flags< Enum l,
Flags< Enum r 
)
friend

Member Data Documentation

◆ _value

template<class Enum >
underlying_type Flags< Enum >::_value
private

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