8 #ifndef HERD_BITMASK_H_ 9 #define HERD_BITMASK_H_ 13 #include <type_traits> 21 #define ENABLE_BITMASK_OPERATORS(x) \ 22 template <> struct EnableBitMaskOperators<x> { static const bool enable = true; }; 26 template <
typename Enum>
27 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
operator|(
const Enum lhs,
const Enum rhs) {
28 using underlying =
typename std::underlying_type<Enum>::type;
29 return static_cast<Enum
>(
static_cast<underlying
>(lhs) | static_cast<underlying>(rhs));
32 template <
typename Enum>
33 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
operator&(
const Enum lhs,
const Enum rhs) {
34 using underlying =
typename std::underlying_type<Enum>::type;
35 return static_cast<Enum
>(
static_cast<underlying
>(lhs) & static_cast<underlying>(rhs));
38 template <
typename Enum>
39 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
operator^(
const Enum lhs,
const Enum rhs) {
40 using underlying =
typename std::underlying_type<Enum>::type;
41 return static_cast<Enum
>(
static_cast<underlying
>(lhs) ^ static_cast<underlying>(rhs));
44 template <
typename Enum>
45 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
operator~(
const Enum rhs) {
46 using underlying =
typename std::underlying_type<Enum>::type;
47 return static_cast<Enum
>(~static_cast<underlying>(rhs));
50 template <
typename Enum>
51 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
operator!=(
const Enum lhs,
const Enum rhs) {
52 using underlying =
typename std::underlying_type<Enum>::type;
53 return static_cast<Enum
>(
static_cast<underlying
>(lhs) != static_cast<underlying>(rhs));
56 template <
typename Enum>
57 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
operator|=(Enum &lhs,
const Enum rhs) {
58 using underlying =
typename std::underlying_type<Enum>::type;
59 lhs =
static_cast<Enum
>(
static_cast<underlying
>(lhs) | static_cast<underlying>(rhs));
63 template <
typename Enum>
64 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
operator&=(Enum &lhs,
const Enum rhs) {
65 using underlying =
typename std::underlying_type<Enum>::type;
66 lhs =
static_cast<Enum
>(
static_cast<underlying
>(lhs) & static_cast<underlying>(rhs));
70 template <
typename Enum>
71 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
operator^=(Enum &lhs,
const Enum rhs) {
72 using underlying =
typename std::underlying_type<Enum>::type;
73 lhs =
static_cast<Enum
>(
static_cast<underlying
>(lhs) ^ static_cast<underlying>(rhs));
78 template <
typename Enum>
79 typename std::enable_if<EnableBitMaskOperators<Enum>::enable,
bool>::type
80 MatchAnyBit(
const Enum test,
const Enum ones,
const Enum zeroes = static_cast<Enum>(0)) {
81 return static_cast<bool>(test & ones) || static_cast<bool>((test ^ zeroes) & zeroes);
85 template <
typename Enum>
86 typename std::enable_if<EnableBitMaskOperators<Enum>::enable,
bool>::type
87 MatchAllBits(
const Enum test,
const Enum ones,
const Enum zeroes = static_cast<Enum>(0)) {
88 return (test & (ones | zeroes)) == ones;
92 template <
int N,
typename Enum>
93 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, std::string>::type
to_string_binary(
const Enum rhs) {
94 return std::bitset<N>{
static_cast<size_t>(rhs)}.to_string();
std::enable_if< EnableBitMaskOperators< Enum >::enable, std::string >::type to_string_binary(const Enum rhs)
Definition: BitMask.h:93
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator^(const Enum lhs, const Enum rhs)
Definition: BitMask.h:39
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator|(const Enum lhs, const Enum rhs)
Definition: BitMask.h:27
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator|=(Enum &lhs, const Enum rhs)
Definition: BitMask.h:57
static const bool enable
Definition: BitMask.h:24
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator &=(Enum &lhs, const Enum rhs)
Definition: BitMask.h:64
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator!=(const Enum lhs, const Enum rhs)
Definition: BitMask.h:51
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator &(const Enum lhs, const Enum rhs)
Definition: BitMask.h:33
std::enable_if< EnableBitMaskOperators< Enum >::enable, bool >::type MatchAnyBit(const Enum test, const Enum ones, const Enum zeroes=static_cast< Enum >(0))
Definition: BitMask.h:80
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator~(const Enum rhs)
Definition: BitMask.h:45
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator^=(Enum &lhs, const Enum rhs)
Definition: BitMask.h:71
std::enable_if< EnableBitMaskOperators< Enum >::enable, bool >::type MatchAllBits(const Enum test, const Enum ones, const Enum zeroes=static_cast< Enum >(0))
Definition: BitMask.h:87