All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Enums module

Provides support for using enums as bitmasks. More...

Functions

template<typename Enum , typename Underlying = typename std::underlying_type<Enum>::type, detail::EnableIfEnum< Enum > = true>
constexpr Underlying gears::enums::to_underlying (Enum x) noexcept
 Casts an enum to its underlying type. More...
 
template<typename Enum , typename... Enums, detail::EnableIfEnum< Enum, Enums...> = true>
Enum & gears::enums::set_flags (Enum &flags, Enums &&...args) noexcept
 Sets the flags to an enum. More...
 
template<typename Enum , typename... Enums, detail::EnableIfEnum< Enum, Enums...> = true>
Enum & gears::enums::remove_flags (Enum &flags, Enums &&...args) noexcept
 Unsets the flags to an enum. More...
 
template<typename Enum , typename... Enums, detail::EnableIfEnum< Enum, Enums...> = true>
constexpr bool gears::enums::has_flags (const Enum &flags, Enums &&...args) noexcept
 Checks if flags are set. More...
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
constexpr Enum gears::enums::operators::operator~ (const Enum &x) noexcept
 Implements ~x on enums.
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
constexpr Enum gears::enums::operators::operator| (const Enum &lhs, const Enum &rhs) noexcept
 Implements lhs | rhs on enums.
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
constexpr Enum gears::enums::operators::operator& (const Enum &lhs, const Enum &rhs) noexcept
 Implements lhs & rhs on enums.
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
constexpr Enum gears::enums::operators::operator^ (const Enum &lhs, const Enum &rhs) noexcept
 Implements lhs ^ rhs on enums.
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
Enum & gears::enums::operators::operator|= (Enum &lhs, const Enum &rhs) noexcept
 Implements lhs |= rhs on enums.
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
Enum & gears::enums::operators::operator&= (Enum &lhs, const Enum &rhs) noexcept
 Implements lhs &= rhs on enums.
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
Enum & gears::enums::operators::operator^= (Enum &lhs, const Enum &rhs) noexcept
 Implements lhs ^= rhs on enums.
 
template<typename Enum , typename... Enums, detail::EnableIfEnum< Enum, Enums...> = true>
constexpr Enum gears::enums::activate_flags (const Enum &first, const Enum &second, Enums &&...rest) noexcept
 Activates the flags specified. More...
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
constexpr bool gears::enums::operators::operator!= (const Enum &lhs, const Enum &rhs) noexcept
 Implements lhs != rhs.
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
constexpr bool gears::enums::operators::operator!= (const Enum &lhs, const detail::Underlying< Enum > &rhs) noexcept
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
constexpr bool gears::enums::operators::operator!= (const detail::Underlying< Enum > &lhs, const Enum &rhs) noexcept
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
constexpr bool gears::enums::operators::operator== (const Enum &lhs, const Enum &rhs) noexcept
 Implements lhs == rhs.
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
constexpr bool gears::enums::operators::operator== (const Enum &lhs, const detail::Underlying< Enum > &rhs) noexcept
 
template<typename Enum , typename std::enable_if< std::is_enum< Enum >::value, int >::type = 0>
constexpr bool gears::enums::operators::operator== (const detail::Underlying< Enum > &lhs, const Enum &rhs) noexcept
 

Detailed Description

This module is meant to help with the usage of enums as bitmasks. C++11 has added support for strongly typed enums through the usage of enum class or enum struct. A big problem with using the strongly typed enums is that the usual operators are unavailable for usage. For example, the following code is not well formed:

enum class test {
one = 1 << 0,
two = 1 << 1
};
int main() {
auto x = test::one & test::two;
}

The purpose of this module is to aid in the usage of enums by making it a little bit easier than having to define the entire set of operators yourself. To fix the error above, all you have to do is the following:

#include <gears/enums.hpp>
using namespace gears::enums::operators;
enum class test {
one = 1 << 0,
two = 1 << 1
};
int main() {
auto x = test::one & test::two;
}

Function Documentation

template<typename Enum , typename... Enums, detail::EnableIfEnum< Enum, Enums...> = true>
constexpr Enum gears::enums::activate_flags ( const Enum &  first,
const Enum &  second,
Enums &&...  rest 
)
noexcept

Activates the flags specified. This function returns an enum type containing all the argument flags activated. The flags are activated

Parameters
firstFirst flag to activate.
secondSecond flag to activate.
restRest of the flags to activate.
Returns
An enum with all the argument flags activated.

Definition at line 86 of file enums/helpers.hpp.

template<typename Enum , typename... Enums, detail::EnableIfEnum< Enum, Enums...> = true>
constexpr bool gears::enums::has_flags ( const Enum &  flags,
Enums &&...  args 
)
noexcept

Checks if flags are set. This function checks if multiple bit flags are set but can also be used for the existence of a single flag. This is equivalent to calling flags & (arg1 | arg2 | args...) == (arg1 | arg2 | args...).

Parameters
flagsThe enum containing all the flags.
argsThe enum flags to test for.
Returns
true is all the flags in args are set. false otherwise.

Definition at line 134 of file enums/helpers.hpp.

template<typename Enum , typename... Enums, detail::EnableIfEnum< Enum, Enums...> = true>
Enum& gears::enums::remove_flags ( Enum &  flags,
Enums &&...  args 
)
inlinenoexcept

Unsets the flags to an enum. This is equivalent to calling flags &= ~(args1 | arg2 | args...).

Parameters
flagsThe enum to remove the flags to.
argsThe enum flags to deactivate to flags.
Returns
A reference to flags.

Definition at line 117 of file enums/helpers.hpp.

template<typename Enum , typename... Enums, detail::EnableIfEnum< Enum, Enums...> = true>
Enum& gears::enums::set_flags ( Enum &  flags,
Enums &&...  args 
)
inlinenoexcept

Sets the flags to an enum. This is equivalent to calling flags |= args1 | arg2 | args....

Parameters
flagsThe enum to set the flags to.
argsThe enum flags to activate to flags.
Returns
A reference to flags.

Definition at line 102 of file enums/helpers.hpp.

template<typename Enum , typename Underlying = typename std::underlying_type<Enum>::type, detail::EnableIfEnum< Enum > = true>
constexpr Underlying gears::enums::to_underlying ( Enum  x)
noexcept

Casts an enum to its underlying type. This is the equivalent of calling:

static_cast<typename std::underlying_type<Enum>::type>(x);
Parameters
xenum or enum class to cast
Returns
The enum's value in its underlying type.

Definition at line 58 of file enums/helpers.hpp.