8 #ifndef NAIA_BITMASK_H_
9 #define NAIA_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, Enum >::type operator~(const Enum rhs)
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator&(const Enum lhs, const Enum rhs)
std::enable_if< EnableBitMaskOperators< Enum >::enable, std::string >::type to_string_binary(const Enum rhs)
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator^(const Enum lhs, const Enum rhs)
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator&=(Enum &lhs, const Enum rhs)
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator|(const Enum lhs, const Enum rhs)
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator|=(Enum &lhs, const Enum rhs)
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator!=(const Enum lhs, const Enum rhs)
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator^=(Enum &lhs, const Enum rhs)
std::enable_if< EnableBitMaskOperators< Enum >::enable, bool >::type MatchAllBits(const Enum test, const Enum ones, const Enum zeroes=static_cast< Enum >(0))
std::enable_if< EnableBitMaskOperators< Enum >::enable, bool >::type MatchAnyBit(const Enum test, const Enum ones, const Enum zeroes=static_cast< Enum >(0))