Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/DeprecatedString.h>
10
11namespace GUI {
12
13class TextPosition {
14public:
15 TextPosition() = default;
16 TextPosition(size_t line, size_t column)
17 : m_line(line)
18 , m_column(column)
19 {
20 }
21
22 bool is_valid() const { return m_line != 0xffffffffu && m_column != 0xffffffffu; }
23
24 size_t line() const { return m_line; }
25 size_t column() const { return m_column; }
26
27 void set_line(size_t line) { m_line = line; }
28 void set_column(size_t column) { m_column = column; }
29
30 bool operator==(TextPosition const& other) const { return m_line == other.m_line && m_column == other.m_column; }
31 bool operator!=(TextPosition const& other) const { return m_line != other.m_line || m_column != other.m_column; }
32 bool operator<(TextPosition const& other) const { return m_line < other.m_line || (m_line == other.m_line && m_column < other.m_column); }
33 bool operator>(TextPosition const& other) const { return *this != other && !(*this < other); }
34 bool operator>=(TextPosition const& other) const { return *this > other || (*this == other); }
35
36private:
37 size_t m_line { 0xffffffff };
38 size_t m_column { 0xffffffff };
39};
40
41}
42
43template<>
44struct AK::Formatter<GUI::TextPosition> : AK::Formatter<FormatString> {
45 ErrorOr<void> format(FormatBuilder& builder, GUI::TextPosition const& value)
46 {
47 if (value.is_valid())
48 return Formatter<FormatString>::format(builder, "({},{})"sv, value.line(), value.column());
49
50 return builder.put_string("GUI::TextPosition(Invalid)"sv);
51 }
52};