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>
|
2015-05-25 14:51:17 +02:00
|
|
|
using EnableIfWithBitOps = std::enable_if_t<WithBitOps<Flags>::value>;
|
2014-10-23 19:55:45 +02:00
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
|
|
|
constexpr Flags operator|(Flags lhs, Flags rhs)
|
|
|
|
{
|
2015-05-25 14:51:17 +02:00
|
|
|
return (Flags)((std::underlying_type_t<Flags>) lhs | (std::underlying_type_t<Flags>) rhs);
|
2014-10-23 19:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
|
|
|
Flags& operator|=(Flags& lhs, Flags rhs)
|
|
|
|
{
|
2015-05-25 14:51:17 +02:00
|
|
|
(std::underlying_type_t<Flags>&) lhs |= (std::underlying_type_t<Flags>) rhs;
|
2014-10-23 19:55:45 +02:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
2015-03-16 19:57:18 +01:00
|
|
|
template<typename Flags>
|
|
|
|
struct TestableFlags
|
|
|
|
{
|
|
|
|
Flags value;
|
2015-05-25 14:51:17 +02:00
|
|
|
constexpr operator bool() const { return (std::underlying_type_t<Flags>)value; }
|
2015-03-16 19:57:18 +01:00
|
|
|
constexpr operator Flags() const { return value; }
|
|
|
|
};
|
|
|
|
|
2014-10-23 19:55:45 +02:00
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
2015-03-16 19:57:18 +01:00
|
|
|
constexpr TestableFlags<Flags> operator&(Flags lhs, Flags rhs)
|
2014-10-23 19:55:45 +02:00
|
|
|
{
|
2015-05-25 14:51:17 +02:00
|
|
|
return { (Flags)((std::underlying_type_t<Flags>) lhs & (std::underlying_type_t<Flags>) rhs) };
|
2014-10-23 19:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
|
|
|
Flags& operator&=(Flags& lhs, Flags rhs)
|
|
|
|
{
|
2015-05-25 14:51:17 +02:00
|
|
|
(std::underlying_type_t<Flags>&) lhs &= (std::underlying_type_t<Flags>) rhs;
|
2014-10-23 19:55:45 +02:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
|
|
|
constexpr Flags operator~(Flags lhs)
|
|
|
|
{
|
2015-05-25 14:51:17 +02:00
|
|
|
return (Flags)(~(std::underlying_type_t<Flags>)lhs);
|
2014-10-23 19:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // flags_hh_INCLUDED
|