kakoune/src/flags.hh
Maxime Coste 993e842fdf Retreat ! go back to C++11 only code
This reverts commit b42de85031.
2015-05-26 18:42:09 +01:00

61 lines
1.5 KiB
C++

#ifndef flags_hh_INCLUDED
#define flags_hh_INCLUDED
#include <type_traits>
namespace Kakoune
{
template<typename Flags>
struct WithBitOps : std::false_type {};
template<typename Flags>
using UnderlyingType = typename std::underlying_type<Flags>::type;
template<typename Flags>
using EnableIfWithBitOps = typename std::enable_if<WithBitOps<Flags>::value>::type;
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
constexpr Flags operator|(Flags lhs, Flags rhs)
{
return (Flags)((UnderlyingType<Flags>) lhs | (UnderlyingType<Flags>) rhs);
}
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
Flags& operator|=(Flags& lhs, Flags rhs)
{
(UnderlyingType<Flags>&) lhs |= (UnderlyingType<Flags>) rhs;
return lhs;
}
template<typename Flags>
struct TestableFlags
{
Flags value;
constexpr operator bool() const { return (UnderlyingType<Flags>)value; }
constexpr operator Flags() const { return value; }
};
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
constexpr TestableFlags<Flags> operator&(Flags lhs, Flags rhs)
{
return { (Flags)((UnderlyingType<Flags>) lhs & (UnderlyingType<Flags>) rhs) };
}
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
Flags& operator&=(Flags& lhs, Flags rhs)
{
(UnderlyingType<Flags>&) lhs &= (UnderlyingType<Flags>) rhs;
return lhs;
}
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
constexpr Flags operator~(Flags lhs)
{
return (Flags)(~(UnderlyingType<Flags>)lhs);
}
}
#endif // flags_hh_INCLUDED