AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AListModelRange.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14
15#include "AListModelIndex.h"
16#include <AUI/Common/SharedPtr.h>
17#include <cassert>
18
19
20template<typename T> class IListModel;
21
22template <typename T>
24private:
25 AListModelIndex mBegin;
26 AListModelIndex mEnd;
27 _<IListModel<T>> mModel;
28
29public:
30 AListModelRange() = default;
31 AListModelRange(const AListModelIndex& begin, const AListModelIndex& end, _<IListModel<T>> model) : mBegin(begin),
32 mEnd(end),
33 mModel(std::move(model)) {}
34
35 bool operator==(const AListModelRange& rhs) const {
36 return std::tie(mBegin, mEnd, mModel) == std::tie(rhs.mBegin, rhs.mEnd, rhs.mModel);
37 }
38
39 class Iterator {
40 private:
41 AListModelIndex mIndex;
42 _<IListModel<T>> mModel;
43
44
45 public:
46 Iterator(const AListModelIndex& index, _<IListModel<T>> model):
47 mIndex(index), mModel(std::move(model)) {}
48
49 Iterator& operator*() {
50 return *this;
51 }
52
53 auto& operator->() {
54 return mModel->listItemAt(mIndex);
55 }
56 auto get() const {
57 return mModel->listItemAt(mIndex);
58 }
59
60 Iterator& operator++() {
61 mIndex = {mIndex.getRow() + 1, mIndex.getColumn()};
62 return *this;
63 }
64
65 bool operator!=(const Iterator& other) const {
66 AUI_ASSERT(mModel == other.mModel);
67 return mIndex.getRow() != other.mIndex.getRow();
68 }
69
70 bool operator==(const Iterator& other) const {
71 AUI_ASSERT(mModel == other.mModel);
72 return mIndex.getRow() == other.mIndex.getRow();
73 }
74
75 [[nodiscard]] const AListModelIndex& getIndex() const {
76 return mIndex;
77 }
78 };
79
80 AListModelRange<T>::Iterator begin() const {
81 return {mBegin, mModel};
82 }
84 return {mEnd, mModel};
85 }
86
87 [[nodiscard]] const AListModelIndex& getBegin() const {
88 return mBegin;
89 }
90
91 [[nodiscard]] const AListModelIndex& getEnd() const {
92 return mEnd;
93 }
94
95 const _<IListModel<T>>& getModel() const {
96 return mModel;
97 }
98};
99
100
101template<typename T>
102inline std::ostream& operator<<(std::ostream& o, const AListModelRange<T>& range) {
103 o << "[ " << range.getBegin() << "; " << range.getEnd() << " )";
104
105 return o;
106}
107
108#include "IListModel.h"
Definition: AListModelIndex.h:20
Definition: AListModelRange.h:39
Definition: AListModelRange.h:23
Definition: IListModel.h:23
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
#define AUI_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition: Assert.h:55