Position.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. namespace VT {
  8. class Position {
  9. public:
  10. Position() { }
  11. Position(int row, int column)
  12. : m_row(row)
  13. , m_column(column)
  14. {
  15. }
  16. bool is_valid() const { return m_row >= 0 && m_column >= 0; }
  17. int row() const { return m_row; }
  18. int column() const { return m_column; }
  19. bool operator<(const Position& other) const
  20. {
  21. return m_row < other.m_row || (m_row == other.m_row && m_column < other.m_column);
  22. }
  23. bool operator<=(const Position& other) const
  24. {
  25. return *this < other || *this == other;
  26. }
  27. bool operator>=(const Position& other) const
  28. {
  29. return !(*this < other);
  30. }
  31. bool operator==(const Position& other) const
  32. {
  33. return m_row == other.m_row && m_column == other.m_column;
  34. }
  35. bool operator!=(const Position& other) const
  36. {
  37. return !(*this == other);
  38. }
  39. private:
  40. int m_row { -1 };
  41. int m_column { -1 };
  42. };
  43. struct CursorPosition {
  44. size_t row { 0 };
  45. size_t column { 0 };
  46. void clamp(u16 max_row, u16 max_column)
  47. {
  48. row = min(row, max_row);
  49. column = min(column, max_column);
  50. }
  51. };
  52. }