units.hh (4634B)
1 #ifndef units_hh_INCLUDED 2 #define units_hh_INCLUDED 3 4 #include "assert.hh" 5 #include "hash.hh" 6 7 #include <type_traits> 8 9 namespace Kakoune 10 { 11 12 template<typename RealType, typename ValueType = int> 13 class StronglyTypedNumber 14 { 15 public: 16 StronglyTypedNumber() = default; 17 18 [[gnu::always_inline]] 19 constexpr StronglyTypedNumber(ValueType value) 20 : m_value(value) 21 { 22 static_assert(std::is_base_of<StronglyTypedNumber, RealType>::value, 23 "RealType is not derived from StronglyTypedNumber"); 24 } 25 26 [[gnu::always_inline]] 27 constexpr friend RealType operator+(RealType lhs, RealType rhs) 28 { return RealType(lhs.m_value + rhs.m_value); } 29 30 [[gnu::always_inline]] 31 constexpr friend RealType operator-(RealType lhs, RealType rhs) 32 { return RealType(lhs.m_value - rhs.m_value); } 33 34 [[gnu::always_inline]] 35 constexpr friend RealType operator*(RealType lhs, RealType rhs) 36 { return RealType(lhs.m_value * rhs.m_value); } 37 38 [[gnu::always_inline]] 39 constexpr friend RealType operator/(RealType lhs, RealType rhs) 40 { return RealType(lhs.m_value / rhs.m_value); } 41 42 [[gnu::always_inline]] 43 RealType& operator+=(RealType other) 44 { m_value += other.m_value; return static_cast<RealType&>(*this); } 45 46 [[gnu::always_inline]] 47 RealType& operator-=(RealType other) 48 { m_value -= other.m_value; return static_cast<RealType&>(*this); } 49 50 [[gnu::always_inline]] 51 RealType& operator*=(RealType other) 52 { m_value *= other.m_value; return static_cast<RealType&>(*this); } 53 54 [[gnu::always_inline]] 55 RealType& operator/=(RealType other) 56 { m_value /= other.m_value; return static_cast<RealType&>(*this); } 57 58 [[gnu::always_inline]] 59 RealType& operator++() 60 { ++m_value; return static_cast<RealType&>(*this); } 61 62 [[gnu::always_inline]] 63 RealType& operator--() 64 { --m_value; return static_cast<RealType&>(*this); } 65 66 [[gnu::always_inline]] 67 RealType operator++(int) 68 { RealType backup(static_cast<RealType&>(*this)); ++m_value; return backup; } 69 70 [[gnu::always_inline]] 71 RealType operator--(int) 72 { RealType backup(static_cast<RealType&>(*this)); --m_value; return backup; } 73 74 [[gnu::always_inline]] 75 constexpr RealType operator-() const { return RealType(-m_value); } 76 77 [[gnu::always_inline]] 78 constexpr friend RealType operator%(RealType lhs, RealType rhs) 79 { return RealType(lhs.m_value % rhs.m_value); } 80 81 [[gnu::always_inline]] 82 RealType& operator%=(RealType other) 83 { m_value %= other.m_value; return static_cast<RealType&>(*this); } 84 85 constexpr friend bool operator==(StronglyTypedNumber lhs, StronglyTypedNumber rhs) = default; 86 constexpr friend auto operator<=>(StronglyTypedNumber lhs, StronglyTypedNumber rhs) = default; 87 88 [[gnu::always_inline]] 89 constexpr bool operator!() const 90 { return not m_value; } 91 92 [[gnu::always_inline]] 93 explicit constexpr operator ValueType() const { return m_value; } 94 [[gnu::always_inline]] 95 explicit constexpr operator bool() const { return m_value; } 96 97 friend constexpr size_t hash_value(RealType val) { return hash_value(val.m_value); } 98 friend constexpr size_t abs(RealType val) { return val.m_value < ValueType(0) ? -val.m_value : val.m_value; } 99 100 explicit operator size_t() { kak_assert(m_value >= 0); return (size_t)m_value; } 101 102 protected: 103 ValueType m_value; 104 }; 105 106 struct LineCount : public StronglyTypedNumber<LineCount, int> 107 { 108 using StronglyTypedNumber::StronglyTypedNumber; 109 }; 110 111 [[gnu::always_inline]] 112 inline constexpr LineCount operator""_line(unsigned long long int value) 113 { 114 return LineCount(value); 115 } 116 117 struct ByteCount : public StronglyTypedNumber<ByteCount, int> 118 { 119 using StronglyTypedNumber::StronglyTypedNumber; 120 }; 121 122 [[gnu::always_inline]] 123 inline constexpr ByteCount operator""_byte(unsigned long long int value) 124 { 125 return ByteCount(value); 126 } 127 128 template<typename Byte> 129 requires (std::is_same_v<std::remove_cv_t<Byte>, char> or std::is_same_v<std::remove_cv_t<Byte>, void>) 130 Byte* operator+(Byte* ptr, ByteCount count) { return ptr + (int)count; } 131 132 struct CharCount : public StronglyTypedNumber<CharCount, int> 133 { 134 using StronglyTypedNumber::StronglyTypedNumber; 135 }; 136 137 [[gnu::always_inline]] 138 inline constexpr CharCount operator""_char(unsigned long long int value) 139 { 140 return CharCount(value); 141 } 142 143 struct ColumnCount : public StronglyTypedNumber<ColumnCount, int> 144 { 145 using StronglyTypedNumber::StronglyTypedNumber; 146 }; 147 148 [[gnu::always_inline]] 149 inline constexpr ColumnCount operator""_col(unsigned long long int value) 150 { 151 return ColumnCount(value); 152 } 153 154 } 155 156 #endif // units_hh_INCLUDED