GModelIndex.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include <AK/LogStream.h>
  3. #include <AK/String.h>
  4. class GModel;
  5. class GModelIndex {
  6. friend class GModel;
  7. public:
  8. GModelIndex() {}
  9. bool is_valid() const { return m_row != -1 && m_column != -1; }
  10. int row() const { return m_row; }
  11. int column() const { return m_column; }
  12. void* internal_data() const { return m_internal_data; }
  13. GModelIndex parent() const;
  14. bool operator==(const GModelIndex& other) const
  15. {
  16. return m_model == other.m_model && m_row == other.m_row && m_column == other.m_column && m_internal_data == other.m_internal_data;
  17. }
  18. bool operator!=(const GModelIndex& other) const
  19. {
  20. return !(*this == other);
  21. }
  22. private:
  23. GModelIndex(const GModel& model, int row, int column, void* internal_data)
  24. : m_model(&model)
  25. , m_row(row)
  26. , m_column(column)
  27. , m_internal_data(internal_data)
  28. {
  29. }
  30. const GModel* m_model { nullptr };
  31. int m_row { -1 };
  32. int m_column { -1 };
  33. void* m_internal_data { nullptr };
  34. };
  35. inline const LogStream& operator<<(const LogStream& stream, const GModelIndex& value)
  36. {
  37. return stream << String::format("GModelIndex(%d,%d)", value.row(), value.column());
  38. }
  39. namespace AK {
  40. template<>
  41. struct Traits<GModelIndex> : public GenericTraits<GModelIndex> {
  42. static unsigned hash(const GModelIndex& index) { return pair_int_hash(index.row(), index.column()); }
  43. };
  44. }