summaryrefslogtreecommitdiff
path: root/src/line_and_column.hh
blob: aaffb2225d9970f4e79dfcd019b3cf16e4e1f3e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef line_and_column_hh_INCLUDED
#define line_and_column_hh_INCLUDED

#include "units.hh"

namespace Kakoune
{

template<typename EffectiveType, typename LineType, typename ColumnType>
struct LineAndColumn
{
    LineType   line;
    ColumnType column;

    constexpr LineAndColumn(LineType line = 0, ColumnType column = 0)
        : line(line), column(column) {}

    constexpr EffectiveType operator+(const EffectiveType& other) const
    {
        return EffectiveType(line + other.line, column + other.column);
    }

    constexpr EffectiveType& operator+=(const EffectiveType& other)
    {
        line   += other.line;
        column += other.column;
        return *static_cast<EffectiveType*>(this);
    }

    constexpr EffectiveType operator-(const EffectiveType& other) const
    {
        return EffectiveType(line - other.line, column - other.column);
    }

    EffectiveType& operator-=(const EffectiveType& other)
    {
        line   -= other.line;
        column -= other.column;
        return *static_cast<EffectiveType*>(this);
    }

    constexpr bool operator< (const EffectiveType& other) const
    {
        return (line != other.line) ? line < other.line
                                    : column < other.column;
    }

    constexpr bool operator<= (const EffectiveType& other) const
    {
        return (line != other.line) ? line < other.line
                                    : column <= other.column;
    }

    constexpr bool operator> (const EffectiveType& other) const
    {
        return (line != other.line) ? line > other.line
                                    : column > other.column;
    }

    constexpr bool operator>= (const EffectiveType& other) const
    {
        return (line != other.line) ? line > other.line
                                    : column >= other.column;
    }

    constexpr bool operator== (const EffectiveType& other) const
    {
        return line == other.line and column == other.column;
    }

    constexpr bool operator!= (const EffectiveType& other) const
    {
        return line != other.line or column != other.column;
    }
};

}

#endif // line_and_column_hh_INCLUDED