home/src/flags.hh

58 lines
1.5 KiB
C++
Raw Normal View History

#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>;
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);
}
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;
return lhs;
}
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; }
constexpr operator Flags() const { return value; }
};
template<typename Flags, typename = EnableIfWithBitOps<Flags>>
constexpr TestableFlags<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) };
}
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;
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);
}
}
#endif // flags_hh_INCLUDED