Tweak flags implementation

This commit is contained in:
Maxime Coste 2014-11-04 22:18:37 +00:00
parent 4d89a297c5
commit 71197c4641

View File

@ -10,7 +10,7 @@ template<typename Flags>
struct WithBitOps : std::false_type {}; struct WithBitOps : std::false_type {};
template<typename Flags> template<typename Flags>
using EnumStorageType = typename std::underlying_type<Flags>::type; using UnderlyingType = typename std::underlying_type<Flags>::type;
template<typename Flags> template<typename Flags>
using EnableIfWithBitOps = typename std::enable_if<WithBitOps<Flags>::value>::type; using EnableIfWithBitOps = typename std::enable_if<WithBitOps<Flags>::value>::type;
@ -18,33 +18,33 @@ using EnableIfWithBitOps = typename std::enable_if<WithBitOps<Flags>::value>::ty
template<typename Flags, typename = EnableIfWithBitOps<Flags>> template<typename Flags, typename = EnableIfWithBitOps<Flags>>
constexpr Flags operator|(Flags lhs, Flags rhs) constexpr Flags operator|(Flags lhs, Flags rhs)
{ {
return (Flags)((EnumStorageType<Flags>) lhs | (EnumStorageType<Flags>) rhs); return (Flags)((UnderlyingType<Flags>) lhs | (UnderlyingType<Flags>) rhs);
} }
template<typename Flags, typename = EnableIfWithBitOps<Flags>> template<typename Flags, typename = EnableIfWithBitOps<Flags>>
Flags& operator|=(Flags& lhs, Flags rhs) Flags& operator|=(Flags& lhs, Flags rhs)
{ {
(EnumStorageType<Flags>&) lhs |= (EnumStorageType<Flags>) rhs; (UnderlyingType<Flags>&) lhs |= (UnderlyingType<Flags>) rhs;
return lhs; return lhs;
} }
template<typename Flags, typename = EnableIfWithBitOps<Flags>> template<typename Flags, typename = EnableIfWithBitOps<Flags>>
constexpr bool operator&(Flags lhs, Flags rhs) constexpr bool operator&(Flags lhs, Flags rhs)
{ {
return ((EnumStorageType<Flags>) lhs & (EnumStorageType<Flags>) rhs) != 0; return ((UnderlyingType<Flags>) lhs & (UnderlyingType<Flags>) rhs) == (UnderlyingType<Flags>)rhs;
} }
template<typename Flags, typename = EnableIfWithBitOps<Flags>> template<typename Flags, typename = EnableIfWithBitOps<Flags>>
Flags& operator&=(Flags& lhs, Flags rhs) Flags& operator&=(Flags& lhs, Flags rhs)
{ {
(EnumStorageType<Flags>&) lhs &= (EnumStorageType<Flags>) rhs; (UnderlyingType<Flags>&) lhs &= (UnderlyingType<Flags>) rhs;
return lhs; return lhs;
} }
template<typename Flags, typename = EnableIfWithBitOps<Flags>> template<typename Flags, typename = EnableIfWithBitOps<Flags>>
constexpr Flags operator~(Flags lhs) constexpr Flags operator~(Flags lhs)
{ {
return (Flags)(~(EnumStorageType<Flags>)lhs); return (Flags)(~(UnderlyingType<Flags>)lhs);
} }
} }