kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

flags.hh (1886B)


      1 #ifndef flags_hh_INCLUDED
      2 #define flags_hh_INCLUDED
      3 
      4 #include <type_traits>
      5 
      6 #include "meta.hh"
      7 
      8 namespace Kakoune
      9 {
     10 
     11 template<typename Flags>
     12 constexpr bool with_bit_ops(Meta::Type<Flags>) { return false; }
     13 
     14 template<typename Flags>
     15 concept WithBitOps = with_bit_ops(Meta::Type<Flags>{});
     16 
     17 template<typename Flags>
     18 using UnderlyingType = std::underlying_type_t<Flags>;
     19 
     20 template<WithBitOps Flags>
     21 constexpr Flags operator|(Flags lhs, Flags rhs)
     22 {
     23     return (Flags)((UnderlyingType<Flags>) lhs | (UnderlyingType<Flags>) rhs);
     24 }
     25 
     26 template<WithBitOps Flags>
     27 constexpr Flags& operator|=(Flags& lhs, Flags rhs)
     28 {
     29     (UnderlyingType<Flags>&) lhs |= (UnderlyingType<Flags>) rhs;
     30     return lhs;
     31 }
     32 
     33 template<typename Flags>
     34 struct TestableFlags
     35 {
     36     Flags value;
     37     constexpr operator bool() const { return (UnderlyingType<Flags>)value; }
     38     constexpr operator Flags() const { return value; }
     39     constexpr operator UnderlyingType<Flags>() const { return (UnderlyingType<Flags>)value; }
     40 
     41     constexpr bool operator==(const TestableFlags<Flags>& other) const = default;
     42 };
     43 
     44 template<WithBitOps Flags>
     45 constexpr TestableFlags<Flags> operator&(Flags lhs, Flags rhs)
     46 {
     47     return { (Flags)((UnderlyingType<Flags>) lhs & (UnderlyingType<Flags>) rhs) };
     48 }
     49 
     50 template<WithBitOps Flags>
     51 constexpr Flags& operator&=(Flags& lhs, Flags rhs)
     52 {
     53     (UnderlyingType<Flags>&) lhs &= (UnderlyingType<Flags>) rhs;
     54     return lhs;
     55 }
     56 
     57 template<WithBitOps Flags>
     58 constexpr Flags operator~(Flags lhs)
     59 {
     60     return (Flags)(~(UnderlyingType<Flags>)lhs);
     61 }
     62 
     63 template<WithBitOps Flags>
     64 constexpr Flags operator^(Flags lhs, Flags rhs)
     65 {
     66     return (Flags)((UnderlyingType<Flags>) lhs ^ (UnderlyingType<Flags>) rhs);
     67 }
     68 
     69 template<WithBitOps Flags>
     70 constexpr Flags& operator^=(Flags& lhs, Flags rhs)
     71 {
     72     (UnderlyingType<Flags>&) lhs ^= (UnderlyingType<Flags>) rhs;
     73     return lhs;
     74 }
     75 
     76 }
     77 
     78 #endif // flags_hh_INCLUDED