![]() |
ATLAS Offline Software
|
Helpers for treating a class enum as a bitmask. More...
#include <type_traits>
Go to the source code of this file.
Namespaces | |
CxxUtils | |
Macros | |
#define | ATH_BITMASK IS_ATH_BITMASK=1 |
Mark that a class enum should be treated as a bitmask. More... | |
Functions | |
template<class E > | |
constexpr std::enable_if_t< is_bitmask_v< E >, E > | operator~ (E lhs) |
operator~ More... | |
template<class E , class F , typename = std::enable_if_t<(is_bitmask_v<E> || is_bitmask_v<F>) && has_same_underlying_v<E,F>>> | |
constexpr auto | operator& (E lhs, F rhs) |
operator& More... | |
template<class E , class F , typename = std::enable_if_t<(is_bitmask_v<E> || is_bitmask_v<F>) && has_same_underlying_v<E,F>>> | |
constexpr auto | operator| (E lhs, F rhs) |
operator| More... | |
template<class E , class F , typename = std::enable_if_t<(is_bitmask_v<E> || is_bitmask_v<F>) && has_same_underlying_v<E,F>>> | |
constexpr auto | operator^ (E lhs, F rhs) |
operator^ More... | |
template<class E > | |
constexpr std::enable_if_t< is_bitmask_v< E >, E & > | operator&= (E &lhs, E rhs) |
operator&= More... | |
template<class E > | |
constexpr std::enable_if_t< is_bitmask_v< E >, E & > | operator|= (E &lhs, E rhs) |
operator|= More... | |
template<class E > | |
constexpr std::enable_if_t< is_bitmask_v< E >, E & > | operator^= (E &lhs, E rhs) |
operator^= More... | |
template<class E > | |
constexpr std::enable_if_t< is_bitmask_v< E >, E & > | CxxUtils::set (E &lhs, E rhs) |
Convenience function to set bits in a class enum bitmask. More... | |
template<class E > | |
constexpr std::enable_if_t< is_bitmask_v< E >, E & > | CxxUtils::reset (E &lhs, E rhs) |
Convenience function to clear bits in a class enum bitmask. More... | |
template<class E > | |
constexpr std::enable_if_t< is_bitmask_v< E >, bool > | CxxUtils::test (E lhs, E rhs) |
Convenience function to test bits in a class enum bitmask. More... | |
Helpers for treating a class enum as a bitmask.
This header defines overloads for the bitwise operators that work with a class enum. To enable these overloads, you need to add the ATH_BITMASK
macro at the end of your enum declaration:
After that, the usual bitwise operations (&, |, ^, &=, |=, ^=, ~) should work as expected. There are also a few convenience functions defined in the CxxUtils
namespace: set
, reset
, and test
.
In case of two different enum types (with common underlying type) the (non-assignment) boolean operators (&, |, ^) are also defined:
This approach was suggested by these postings:
http://blog.bitwigglers.org/using-enum-classes-as-type-safe-bitmasks https://www.justsoftwaresolutions.co.uk/cplusplus/using-enum-classes-as-bitfields.html
except that we rely on injecting a known enumerator into the type rather than using a separate traits class. This way works better if the enumeration is defined in a nested scope.
Definition in file bitmask.h.
#define ATH_BITMASK IS_ATH_BITMASK=1 |
Mark that a class enum should be treated as a bitmask.
Put this at the end of the enumeration, after a comma, like this:
|
constexpr |
operator&
One operand needs to be a bitmask and the other share at least the same underlying type. This allows bit operations with the underlying type (e.g. int) and chained operations involving more than two bitmasks.
|
constexpr |
|
constexpr |
|
constexpr |
|
constexpr |
|
constexpr |