coord.hh (2942B)
1 #ifndef coord_hh_INCLUDED 2 #define coord_hh_INCLUDED 3 4 #include "units.hh" 5 #include "hash.hh" 6 7 namespace Kakoune 8 { 9 10 template<typename EffectiveType, typename LineType, typename ColumnType> 11 struct LineAndColumn 12 { 13 LineType line = 0; 14 ColumnType column = 0; 15 16 [[gnu::always_inline]] 17 constexpr EffectiveType operator+(EffectiveType other) const 18 { 19 return {line + other.line, column + other.column}; 20 } 21 22 [[gnu::always_inline]] 23 EffectiveType& operator+=(EffectiveType other) 24 { 25 line += other.line; 26 column += other.column; 27 return *static_cast<EffectiveType*>(this); 28 } 29 30 [[gnu::always_inline]] 31 constexpr EffectiveType operator-(EffectiveType other) const 32 { 33 return {line - other.line, column - other.column}; 34 } 35 36 [[gnu::always_inline]] 37 EffectiveType& operator-=(EffectiveType other) 38 { 39 line -= other.line; 40 column -= other.column; 41 return *static_cast<EffectiveType*>(this); 42 } 43 44 constexpr friend auto operator<=>(const LineAndColumn& lhs, const LineAndColumn& rhs) = default; 45 constexpr friend bool operator==(const LineAndColumn& lhs, const LineAndColumn& rhs) = default; 46 47 friend constexpr size_t hash_value(const EffectiveType& val) 48 { 49 return hash_values(val.line, val.column); 50 } 51 }; 52 53 struct BufferCoord : LineAndColumn<BufferCoord, LineCount, ByteCount> 54 { 55 [[gnu::always_inline]] 56 constexpr BufferCoord(LineCount line = 0, ByteCount column = 0) 57 : LineAndColumn{line, column} {} 58 59 constexpr friend auto operator<=>(const BufferCoord& lhs, const BufferCoord& rhs) = default; 60 constexpr friend bool operator==(const BufferCoord& lhs, const BufferCoord& rhs) = default; 61 }; 62 63 struct DisplayCoord : LineAndColumn<DisplayCoord, LineCount, ColumnCount> 64 { 65 [[gnu::always_inline]] 66 constexpr DisplayCoord(LineCount line = 0, ColumnCount column = 0) 67 : LineAndColumn{line, column} {} 68 69 constexpr friend auto operator<=>(const DisplayCoord& lhs, const DisplayCoord& rhs) = default; 70 constexpr friend bool operator==(const DisplayCoord& lhs, const DisplayCoord& rhs) = default; 71 72 static constexpr const char* option_type_name = "coord"; 73 }; 74 75 struct BufferCoordAndTarget : BufferCoord 76 { 77 [[gnu::always_inline]] 78 constexpr BufferCoordAndTarget(LineCount line = 0, ByteCount column = 0, ColumnCount target = -1, ColumnCount display_target=-1) 79 : BufferCoord{line, column}, target{target}, display_target{display_target} {} 80 81 [[gnu::always_inline]] 82 constexpr BufferCoordAndTarget(BufferCoord coord, ColumnCount target = -1, ColumnCount display_target=-1) 83 : BufferCoord{coord}, target{target}, display_target{display_target} {} 84 85 ColumnCount target; 86 ColumnCount display_target; 87 }; 88 89 constexpr size_t hash_value(const BufferCoordAndTarget& val) 90 { 91 return hash_values(val.line, val.column, val.target); 92 } 93 94 } 95 96 #endif // coord_hh_INCLUDED