AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AListModel.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#include <AUI/Common/AVector.h>
15#include <AUI/Common/ASignal.h>
16#include "AListModelIndex.h"
17#include "IMutableListModel.h"
18#include <AUI/Traits/strings.h>
19
20namespace aui::detail {
21
22}
23
24template <typename StoredType>
25class AListModel: public IRemovableListModel<StoredType>,
26 public IValueMutableListModel<StoredType> {
27private:
28 AVector<StoredType> mVector;
29
30 using self = AListModel<StoredType>;
31
32public:
33 using iterator = typename decltype(mVector)::iterator;
34 using const_iterator = typename decltype(mVector)::const_iterator;
35 using size_type = typename decltype(mVector)::size_type;
36 using value_type = typename decltype(mVector)::value_type;
37
38 AListModel() = default;
39 AListModel(const self& s): mVector(s.mVector) {}
40 AListModel(self&& s) noexcept: mVector(std::move(s.mVector)) {}
41 explicit AListModel(AVector<StoredType>&& vector) noexcept: mVector(std::move(vector)) {}
42
43 AListModel& operator=(AVector<StoredType>&& rhs) noexcept {
44 clear();
45 mVector = std::move(rhs);
46 emit this->dataInserted(this->range(AListModelIndex(0),
47 AListModelIndex(mVector.size())));
48 return *this;
49 }
50
51 void setItem(const AListModelIndex& item, const StoredType& value) override {
52 mVector[item.getRow()] = value;
53 }
54
55 [[nodiscard]]
56 bool empty() const noexcept {
57 return mVector.empty();
58 }
59 void reserve(size_t s) noexcept {
60 mVector.reserve(s);
61 }
62 void resize(size_t s) noexcept {
63 mVector.resize(s);
64 }
65
66 iterator erase(iterator begin) noexcept {
67 return this->erase(begin, begin + 1);
68 }
69 iterator erase(iterator begin, iterator end) noexcept {
70 auto range = this->range(AListModelIndex{size_t(begin - mVector.begin())},
71 AListModelIndex{size_t(end - mVector.begin())});
72 auto it = mVector.erase(begin, end);
73 emit this->dataRemoved(range);
74
75 return it;
76 }
77
78
79 void push_back(const StoredType& data) {
80 insert(end(), std::move(data));
81 }
82
83 void push_back(StoredType&& data) {
84 insert(end(), std::forward<StoredType>(data));
85 }
86
87 const_iterator insert(const_iterator at, StoredType data) {
88 at = mVector.insert(at, std::move(data));
89 emit this->dataInserted(this->range(AListModelIndex(at - begin()),
90 AListModelIndex(at - begin() + 1)));
91 return at;
92 }
93
94 void pop_back() noexcept {
95 mVector.pop_back();
96 emit this->dataRemoved(this->range(AListModelIndex(mVector.size() ),
97 AListModelIndex(mVector.size() + 1)));
98 }
99
100 AListModel& operator<<(const StoredType& data) {
101 push_back(data);
102 return *this;
103 }
104 AListModel& operator<<(StoredType&& data) {
105 push_back(std::forward<StoredType>(data));
106 return *this;
107 }
108
109 size_t listSize() override {
110 return mVector.size();
111 }
112
113 StoredType listItemAt(const AListModelIndex& index) override {
114 return mVector.at(index.getRow());
115 }
116 void invalidate(size_t index) {
117 emit this->dataChanged(this->range(AListModelIndex(index), AListModelIndex(index + 1u)));
118 }
119
120 void invalidate(iterator index) {
121 invalidate(std::distance(begin(), index));
122 }
123
124 void clear() noexcept {
125 erase(mVector.begin(), mVector.end());
126 }
127
128 void removeItems(const AListModelRange<StoredType>& items) override {
129 erase(mVector.begin() + items.begin().getIndex().getRow(), mVector.end() + items.begin().getIndex().getRow());
130 }
131
132 void removeItem(const AListModelIndex& item) override {
133 erase(mVector.begin() + item.getRow());
134 }
135
136 [[nodiscard]]
137 size_t size() const noexcept {
138 return mVector.size();
139 }
140
152 const StoredType& at(size_t index) const {
153 if (index >= size()) throw AException("index of bounds: {} (size {})"_format(index, size()));
154 return *(mVector.begin() + index);
155 }
156
175 StoredType& at(size_t index) {
176 if (index >= size()) throw AException("index of bounds: {} (size {})"_format(index, size()));
177 return *(mVector.begin() + index);
178 }
179
187 const StoredType& operator[](size_t index) const {
188 AUI_ASSERTX(size() > index, "index out of bounds");
189 return *(mVector.begin() + index);
190 }
191
200 void removeAt(size_t index) noexcept
201 {
202 aui::container::remove_at(*this, index);
203 }
204
205 const_iterator begin() const {
206 return mVector.begin();
207 }
208 const_iterator end() const {
209 return mVector.end();
210 }
211
212 iterator begin() {
213 return mVector.begin();
214 }
215 iterator end() {
216 return mVector.end();
217 }
218
226 template<typename V>
227 static _<AListModel<StoredType>> make(const std::initializer_list<V>& t) {
228 auto list = _new<AListModel<StoredType>>();
229 list->reserve(t.size());
230 for (auto& item : t) {
231 list->push_back(item);
232 }
233 return list;
234 }
235
243 template<typename V>
245 auto list = _new<AListModel<StoredType>>();
246 list->mVector = std::move(t);
247 return list;
248 }
249
257 template<typename V>
258 static _<AListModel<StoredType>> fromVector(std::vector<V> t) {
259 auto list = _new<AListModel<StoredType>>();
260 list->mVector = AVector<StoredType>(std::vector<StoredType>(std::move(t)));
261 return list;
262 }
263
264 template<typename UnaryOperation>
265 auto map(UnaryOperation&& transformer) {
266 return mVector.map(std::forward<UnaryOperation>(transformer));
267 }
268
269 [[nodiscard]]
270 const AVector<StoredType>& toVector() noexcept {
271 return mVector;
272 }
273};
Abstract AUI exception.
Definition AException.h:28
Definition AListModelIndex.h:20
Definition AListModelRange.h:23
static _< AListModel< StoredType > > fromVector(std::vector< V > t)
Definition AListModel.h:258
static _< AListModel< StoredType > > fromVector(AVector< V > t)
Definition AListModel.h:244
void removeAt(size_t index) noexcept
Definition AListModel.h:200
StoredType & at(size_t index)
Definition AListModel.h:175
const StoredType & operator[](size_t index) const
Definition AListModel.h:187
const StoredType & at(size_t index) const
Definition AListModel.h:152
static _< AListModel< StoredType > > make(const std::initializer_list< V > &t)
Definition AListModel.h:227
A std::vector with AUI extensions.
Definition AVector.h:39
emits< AListModelRange< StoredType > > dataInserted
Definition IListModel.h:88
emits< AListModelRange< StoredType > > dataChanged
Definition IListModel.h:83
emits< AListModelRange< StoredType > > dataRemoved
Definition IListModel.h:93
Definition IMutableListModel.h:20
Definition IMutableListModel.h:36
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:178
void remove_at(Container &c, size_t index) noexcept
Removes element at the specified index.
Definition containers.h:98
#define emit
emits the specified signal in context of this object.
Definition AObject.h:310
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74