Avoid a REQUIRES macro for SFINAE

This commit is contained in:
Chris Robinson
2020-12-05 05:57:18 -08:00
parent 8a352d25f9
commit 7485e402c1
+34 -31
View File
@@ -6,6 +6,8 @@
#include <limits>
#include <type_traits>
using uint = unsigned int;
namespace al {
/* The "canonical" way to store raw byte data. Like C++17's std::byte, it's not
@@ -14,38 +16,41 @@ namespace al {
*/
enum class byte : unsigned char { };
#define REQUIRES(...) std::enable_if_t<(__VA_ARGS__),bool> = true
template<typename T, REQUIRES(std::is_integral<T>::value)>
inline constexpr T to_integer(al::byte b) noexcept { return T(b); }
template<typename T>
constexpr std::enable_if_t<std::is_integral<T>::value,T>
to_integer(al::byte b) noexcept { return T(b); }
template<typename T, REQUIRES(std::is_integral<T>::value)>
inline constexpr al::byte operator<<(al::byte lhs, T rhs) noexcept
{ return al::byte(to_integer<unsigned int>(lhs) << rhs); }
template<typename T>
constexpr std::enable_if_t<std::is_integral<T>::value,al::byte>
operator<<(al::byte lhs, T rhs) noexcept { return al::byte(to_integer<uint>(lhs) << rhs); }
template<typename T, REQUIRES(std::is_integral<T>::value)>
inline constexpr al::byte operator>>(al::byte lhs, T rhs) noexcept
{ return al::byte(to_integer<unsigned int>(lhs) >> rhs); }
template<typename T>
constexpr std::enable_if_t<std::is_integral<T>::value,al::byte>
operator>>(al::byte lhs, T rhs) noexcept { return al::byte(to_integer<uint>(lhs) >> rhs); }
template<typename T, REQUIRES(std::is_integral<T>::value)>
inline al::byte& operator<<=(al::byte &lhs, T rhs) noexcept
{ lhs = lhs << rhs; return lhs; }
template<typename T>
constexpr std::enable_if_t<std::is_integral<T>::value,al::byte&>
operator<<=(al::byte &lhs, T rhs) noexcept { lhs = lhs << rhs; return lhs; }
template<typename T, REQUIRES(std::is_integral<T>::value)>
inline al::byte& operator>>=(al::byte &lhs, T rhs) noexcept
{ lhs = lhs >> rhs; return lhs; }
template<typename T>
constexpr std::enable_if_t<std::is_integral<T>::value,al::byte&>
operator>>=(al::byte &lhs, T rhs) noexcept { lhs = lhs >> rhs; return lhs; }
#define AL_DECL_OP(op, opeq) \
template<typename T, REQUIRES(std::is_integral<T>::value)> \
inline constexpr al::byte operator op (al::byte lhs, T rhs) noexcept \
{ return al::byte(to_integer<unsigned int>(lhs) op static_cast<unsigned int>(rhs)); } \
template<typename T, REQUIRES(std::is_integral<T>::value)> \
inline constexpr al::byte& operator opeq (al::byte &lhs, T rhs) noexcept \
{ lhs = lhs op rhs; return lhs; } \
inline constexpr al::byte operator op (al::byte lhs, al::byte rhs) noexcept \
{ return al::byte(lhs op to_integer<unsigned int>(rhs)); } \
inline constexpr al::byte& operator opeq (al::byte &lhs, al::byte rhs) noexcept \
template<typename T> \
constexpr std::enable_if_t<std::is_integral<T>::value,al::byte> \
operator op (al::byte lhs, T rhs) noexcept \
{ return al::byte(to_integer<uint>(lhs) op static_cast<uint>(rhs)); } \
\
template<typename T> \
constexpr std::enable_if_t<std::is_integral<T>::value,al::byte&> \
operator opeq (al::byte &lhs, T rhs) noexcept { lhs = lhs op rhs; return lhs; } \
\
constexpr al::byte operator op (al::byte lhs, al::byte rhs) noexcept \
{ return al::byte(lhs op to_integer<uint>(rhs)); } \
\
constexpr al::byte& operator opeq (al::byte &lhs, al::byte rhs) noexcept \
{ lhs = lhs op rhs; return lhs; }
AL_DECL_OP(|, |=)
@@ -54,8 +59,8 @@ AL_DECL_OP(^, ^=)
#undef AL_DECL_OP
inline constexpr al::byte operator~(al::byte b) noexcept
{ return al::byte(~to_integer<unsigned int>(b)); }
constexpr al::byte operator~(al::byte b) noexcept
{ return al::byte(~to_integer<uint>(b)); }
namespace detail_ {
@@ -96,16 +101,14 @@ public:
return (vals & (1 << b)) != 0;
}
template<size_t b, size_t ...args, REQUIRES(sizeof...(args) > 0)>
void set() noexcept
template<size_t b, size_t ...args>
std::enable_if_t<(sizeof...(args) > 0)> set() noexcept
{
set<b>();
set<args...>();
}
};
#undef REQUIRES
} // namespace al
#endif /* AL_BYTE_H */