44 lines
1.3 KiB
C++
44 lines
1.3 KiB
C++
#pragma once
|
|
#include <type_traits>
|
|
|
|
template <typename E>
|
|
struct enable_bitmask : std::false_type {};
|
|
|
|
#define RENDERIVE_ENABLE_BITMASK(E) \
|
|
template <> \
|
|
struct enable_bitmask<E> : std::true_type {};
|
|
|
|
template <typename E>
|
|
constexpr std::enable_if_t<enable_bitmask<E>::value, E> operator|(E lhs, E rhs) {
|
|
using U = std::underlying_type_t<E>;
|
|
return static_cast<E>(static_cast<U>(lhs) | static_cast<U>(rhs));
|
|
}
|
|
|
|
template <typename E>
|
|
constexpr std::enable_if_t<enable_bitmask<E>::value, E> operator&(E lhs, E rhs) {
|
|
using U = std::underlying_type_t<E>;
|
|
return static_cast<E>(static_cast<U>(lhs) & static_cast<U>(rhs));
|
|
}
|
|
|
|
template <typename E>
|
|
constexpr std::enable_if_t<enable_bitmask<E>::value, E> operator^(E lhs, E rhs) {
|
|
using U = std::underlying_type_t<E>;
|
|
return static_cast<E>(static_cast<U>(lhs) ^ static_cast<U>(rhs));
|
|
}
|
|
|
|
template <typename E>
|
|
constexpr std::enable_if_t<enable_bitmask<E>::value, E> operator~(E e) {
|
|
using U = std::underlying_type_t<E>;
|
|
return static_cast<E>(~static_cast<U>(e));
|
|
}
|
|
|
|
template <typename E>
|
|
constexpr std::enable_if_t<enable_bitmask<E>::value, E&> operator|=(E& lhs, E rhs) {
|
|
return lhs = lhs | rhs;
|
|
}
|
|
|
|
template <typename E>
|
|
constexpr std::enable_if_t<enable_bitmask<E>::value, E&> operator&=(E& lhs, E rhs) {
|
|
return lhs = lhs & rhs;
|
|
}
|