2014-10-23 19:55:45 +02:00
|
|
|
#ifndef flags_hh_INCLUDED
|
|
|
|
#define flags_hh_INCLUDED
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
2017-03-15 18:55:34 +01:00
|
|
|
#include "meta.hh"
|
|
|
|
|
2014-10-23 19:55:45 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename Flags>
|
2017-03-15 18:55:34 +01:00
|
|
|
constexpr bool with_bit_ops(Meta::Type<Flags>) { return false; }
|
2014-10-23 19:55:45 +02:00
|
|
|
|
|
|
|
template<typename Flags>
|
2016-03-12 16:27:54 +01:00
|
|
|
using UnderlyingType = std::underlying_type_t<Flags>;
|
2015-05-26 19:42:09 +02:00
|
|
|
|
2015-11-20 09:50:53 +01:00
|
|
|
template<typename Flags, typename T = void>
|
2016-03-12 16:27:54 +01:00
|
|
|
using EnableIfWithBitOps = std::enable_if_t<with_bit_ops(Meta::Type<Flags>{}), T>;
|
2015-11-20 09:50:53 +01:00
|
|
|
|
|
|
|
template<typename Flags, typename T = void>
|
2016-03-12 16:27:54 +01:00
|
|
|
using EnableIfWithoutBitOps = std::enable_if_t<not with_bit_ops(Meta::Type<Flags>{}), T>;
|
2014-10-23 19:55:45 +02:00
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
|
|
|
constexpr Flags operator|(Flags lhs, Flags rhs)
|
|
|
|
{
|
2015-05-26 19:42:09 +02:00
|
|
|
return (Flags)((UnderlyingType<Flags>) lhs | (UnderlyingType<Flags>) rhs);
|
2014-10-23 19:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
2017-10-02 16:34:15 +02:00
|
|
|
constexpr Flags& operator|=(Flags& lhs, Flags rhs)
|
2014-10-23 19:55:45 +02:00
|
|
|
{
|
2015-05-26 19:42:09 +02:00
|
|
|
(UnderlyingType<Flags>&) lhs |= (UnderlyingType<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-26 19:42:09 +02:00
|
|
|
constexpr operator bool() const { return (UnderlyingType<Flags>)value; }
|
2015-03-16 19:57:18 +01:00
|
|
|
constexpr operator Flags() const { return value; }
|
2016-11-14 20:14:09 +01:00
|
|
|
constexpr operator UnderlyingType<Flags>() const { return (UnderlyingType<Flags>)value; }
|
2016-08-30 00:56:22 +02:00
|
|
|
|
2017-10-02 16:34:15 +02:00
|
|
|
constexpr bool operator==(const TestableFlags<Flags>& other) const { return value == other.value; }
|
|
|
|
constexpr bool operator!=(const TestableFlags<Flags>& other) const { return value != other.value; }
|
2015-03-16 19:57:18 +01:00
|
|
|
};
|
|
|
|
|
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-26 19:42:09 +02:00
|
|
|
return { (Flags)((UnderlyingType<Flags>) lhs & (UnderlyingType<Flags>) rhs) };
|
2014-10-23 19:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
|
2017-10-02 16:34:15 +02:00
|
|
|
constexpr Flags& operator&=(Flags& lhs, Flags rhs)
|
2014-10-23 19:55:45 +02:00
|
|
|
{
|
2015-05-26 19:42:09 +02: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)
|
|
|
|
{
|
2015-05-26 19:42:09 +02:00
|
|
|
return (Flags)(~(UnderlyingType<Flags>)lhs);
|
2014-10-23 19:55:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 00:56:22 +02:00
|
|
|
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>>
|
2017-10-02 16:34:15 +02:00
|
|
|
constexpr Flags& operator^=(Flags& lhs, Flags rhs)
|
2016-08-30 00:56:22 +02:00
|
|
|
{
|
|
|
|
(UnderlyingType<Flags>&) lhs ^= (UnderlyingType<Flags>) rhs;
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
2014-10-23 19:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // flags_hh_INCLUDED
|