kakoune/src/coord.hh

100 lines
2.6 KiB
C++
Raw Normal View History

#ifndef coord_hh_INCLUDED
#define coord_hh_INCLUDED
#include "units.hh"
#include "hash.hh"
namespace Kakoune
{
template<typename EffectiveType, typename LineType, typename ColumnType>
struct LineAndColumn
{
LineType line = 0;
ColumnType column = 0;
[[gnu::always_inline]]
2013-07-26 00:44:00 +02:00
constexpr EffectiveType operator+(EffectiveType other) const
{
2015-03-05 14:52:54 +01:00
return {line + other.line, column + other.column};
}
[[gnu::always_inline]]
2013-07-26 00:44:00 +02:00
EffectiveType& operator+=(EffectiveType other)
{
line += other.line;
column += other.column;
2011-09-19 23:55:58 +02:00
return *static_cast<EffectiveType*>(this);
}
[[gnu::always_inline]]
2013-07-26 00:44:00 +02:00
constexpr EffectiveType operator-(EffectiveType other) const
{
2015-03-05 14:52:54 +01:00
return {line - other.line, column - other.column};
}
[[gnu::always_inline]]
2013-07-26 00:44:00 +02:00
EffectiveType& operator-=(EffectiveType other)
{
line -= other.line;
column -= other.column;
2011-09-19 23:55:58 +02:00
return *static_cast<EffectiveType*>(this);
}
[[gnu::always_inline]]
2023-05-21 04:40:27 +02:00
constexpr friend auto operator<=>(const EffectiveType& lhs, const EffectiveType& rhs)
{
2023-05-21 04:40:27 +02:00
return (lhs.line != rhs.line) ? lhs.line <=> rhs.line
: lhs.column <=> rhs.column;
}
[[gnu::always_inline]]
2023-05-21 04:40:27 +02:00
constexpr friend bool operator==(const EffectiveType& lhs, const EffectiveType& rhs)
{
2023-05-21 04:40:27 +02:00
return lhs.line == rhs.line and lhs.column == rhs.column;
}
2017-10-17 16:45:17 +02:00
friend constexpr size_t hash_value(const EffectiveType& val)
2015-03-11 14:59:25 +01:00
{
return hash_values(val.line, val.column);
}
};
struct BufferCoord : LineAndColumn<BufferCoord, LineCount, ByteCount>
{
[[gnu::always_inline]]
constexpr BufferCoord(LineCount line = 0, ByteCount column = 0)
: LineAndColumn{line, column} {}
};
struct DisplayCoord : LineAndColumn<DisplayCoord, LineCount, ColumnCount>
{
[[gnu::always_inline]]
constexpr DisplayCoord(LineCount line = 0, ColumnCount column = 0)
: LineAndColumn{line, column} {}
2016-08-06 10:05:50 +02:00
static constexpr const char* option_type_name = "coord";
};
struct BufferCoordAndTarget : BufferCoord
{
[[gnu::always_inline]]
constexpr BufferCoordAndTarget(LineCount line = 0, ByteCount column = 0, ColumnCount target = -1)
: BufferCoord{line, column}, target{target} {}
[[gnu::always_inline]]
constexpr BufferCoordAndTarget(BufferCoord coord, ColumnCount target = -1)
: BufferCoord{coord}, target{target} {}
ColumnCount target;
};
2017-10-17 16:45:17 +02:00
constexpr size_t hash_value(const BufferCoordAndTarget& val)
{
return hash_values(val.line, val.column, val.target);
}
}
#endif // coord_hh_INCLUDED