2014-10-23 19:55:45 +02:00
|
|
|
#ifndef flags_hh_INCLUDED
|
|
|
|
#define flags_hh_INCLUDED
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename Flags>
|
|
|
|
struct WithBitOps : std::false_type {};
|
|
|
|
|
|
|
|
template<typename Flags>
|
2014-11-04 23:18:37 +01:00
|
|
|
using UnderlyingType = typename std::underlying_type<Flags>::type;
|
2014-10-23 19:55:45 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-11-04 23:18:37 +01:00
|
|
|
return (Flags)((UnderlyingType<Flags>) lhs | (UnderlyingType<Flags>) rhs);
|
2014-10-23 19:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
|
|
|
Flags& operator|=(Flags& lhs, Flags rhs)
|
|
|
|
{
|
2014-11-04 23:18:37 +01:00
|
|
|
(UnderlyingType<Flags>&) lhs |= (UnderlyingType<Flags>) rhs;
|
2014-10-23 19:55:45 +02:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
|
|
|
constexpr bool operator&(Flags lhs, Flags rhs)
|
|
|
|
{
|
2014-11-04 23:18:37 +01:00
|
|
|
return ((UnderlyingType<Flags>) lhs & (UnderlyingType<Flags>) rhs) == (UnderlyingType<Flags>)rhs;
|
2014-10-23 19:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
|
|
|
Flags& operator&=(Flags& lhs, Flags rhs)
|
|
|
|
{
|
2014-11-04 23:18:37 +01:00
|
|
|
(UnderlyingType<Flags>&) lhs &= (UnderlyingType<Flags>) rhs;
|
2014-10-23 19:55:45 +02:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
|
|
|
constexpr Flags operator~(Flags lhs)
|
|
|
|
{
|
2014-11-04 23:18:37 +01:00
|
|
|
return (Flags)(~(UnderlyingType<Flags>)lhs);
|
2014-10-23 19:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // flags_hh_INCLUDED
|