ACF $AcfVersion:0$
TContainer.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QList>
7
8// ACF includes
12
13namespace ibase
14{
15
16
24template <typename ItemClass, class ContainerType = QList<ItemClass> >
26{
27public:
28 typedef ContainerType Container;
29
30 virtual void Reserve(int count);
31 virtual void Resize(int count);
32 virtual void RemoveAt(int index);
33 virtual void Reset();
34
35 const ItemClass& GetAt(int index) const;
36 ItemClass& GetAt(int index);
37 ItemClass& PushBack(const ItemClass& item);
38 ItemClass& PushFront(const ItemClass& item);
39 ItemClass& InsertAt(const ItemClass& item, int index);
40 void PopBack();
41 void PopFront();
42
43 // methods with names compatible to standard containers
44
45 inline void push_back(const ItemClass& item)
46 {
47 PushBack(item);
48 }
49
50
51 inline void push_front(const ItemClass& item)
52 {
53 PushFront(item);
54 }
55
56
57 inline ItemClass& operator[](qsizetype index)
58 {
59 return m_items[index];
60 }
61
62
63 inline const ItemClass& operator[](qsizetype index) const
64 {
65 return m_items[index];
66 }
67
68
69 typename Container::iterator insert(qsizetype index, const ItemClass& item)
70 {
71 auto changes = ElementAddChanges(index);
72 istd::CChangeNotifier notifier(this, &changes);
73
74 return m_items.insert(m_items.begin() + index, item);
75 }
76
77
78 typename Container::iterator insert(qsizetype index, ItemClass&& item)
79 {
80 auto changes = ElementAddChanges(index);
81 istd::CChangeNotifier notifier(this, &changes);
82
83 return m_items.insert(m_items.begin() + index, std::move(item));
84 }
85
86 // remove of multiple elements is not implemented yet
87 void remove(qsizetype i)
88 {
89 Q_UNUSED(i);
90
91#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
92 auto changes = ElementRemoveChanges(i);
93 istd::CChangeNotifier notifier(this, &changes);
94
95 m_items.remove(i);
96#endif
97 }
98
99
100 void resize(qsizetype size)
101 {
102 Q_UNUSED(size);
103
104#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
105 istd::CChangeNotifier notifier(this, &s_resetChange);
106
107 m_items.resize(size);
108#endif
109 }
110
111
112 inline qsizetype size() const
113 {
114 return m_items.size();
115 }
116
117
118 TContainer& operator=(const TContainer& container);
119
120 // reimplemented (istd::IContainerInfo)
121 virtual int GetItemsCount() const override;
122 virtual bool IsEmpty() const override;
123 virtual bool IsIndexValid(int index) const override;
124
125 // reimplemented (istd::IChangeable)
126 virtual bool CopyFrom(const IChangeable& object, CompatibilityMode mode = CM_WITHOUT_REFS) override;
127
128 // Iterators
129 typename Container::iterator begin()
130 {
131 return m_items.begin();
132 }
133
134
135 typename Container::iterator end()
136 {
137 return m_items.end();
138 }
139
140
141 typename Container::const_iterator begin() const
142 {
143 return m_items.begin();
144 }
145
146
147 typename Container::const_iterator end() const
148 {
149 return m_items.end();
150 }
151
152
153 typename Container::const_iterator cbegin() const
154 {
155 return m_items.begin();
156 }
157
158
159 typename Container::const_iterator cend() const
160 {
161 return m_items.end();
162 }
163
164
165 // Equality
166 bool operator==(const TContainer& other) const
167 {
168 return m_items == other.m_items;
169 }
170
171 bool operator!=(const TContainer& other) const
172 {
173 return !operator==(other);
174 }
175
176protected:
178
179 typedef ContainerType Items;
180
182};
183
184
185// public methods
186
187template <typename ItemClass, typename ContainerType>
191
192
193template <typename ItemClass, typename ContainerType>
197
198
199template <typename ItemClass, typename ContainerType>
201{
202 Q_ASSERT(TContainer::IsIndexValid(index));
203
204 return m_items[index];
205}
206
207
208template <typename ItemClass, typename ContainerType>
209const ItemClass& TContainer<ItemClass, ContainerType>::GetAt(int index) const
210{
211 Q_ASSERT(TContainer::IsIndexValid(index));
212
213 return m_items[index];
214}
215
216
217template <typename ItemClass, typename ContainerType>
218ItemClass& TContainer<ItemClass, ContainerType>::PushBack(const ItemClass& item)
219{
220 auto changes = ElementAddChanges(m_items.size());
221 istd::CChangeNotifier notifier(this, &changes);
222 Q_UNUSED(notifier);
223
224 m_items.push_back(item);
225
226 return m_items.back();
227}
228
229
230template <typename ItemClass, typename ContainerType>
231ItemClass& TContainer<ItemClass, ContainerType>::PushFront(const ItemClass& item)
232{
233 auto changes = ElementAddChanges(0);
234 istd::CChangeNotifier notifier(this, &changes);
235 Q_UNUSED(notifier);
236
237 m_items.push_front(item);
238
239 return m_items.front();
240}
241
242
243template <typename ItemClass, typename ContainerType>
244ItemClass& TContainer<ItemClass, ContainerType>::InsertAt(const ItemClass& item, int index)
245{
246 if ((index < 0) || (index >= m_items.size())){
247 return PushBack(item);
248 }
249 else{
250 auto changes = ElementAddChanges(index);
251 istd::CChangeNotifier notifier(this, &changes);
252 m_items.insert(index, item);
253
254 return m_items[index];
255 }
256}
257
258
259template <typename ItemClass, typename ContainerType>
261{
262 auto changes = ElementRemoveChanges(m_items.size() - 1);
263 istd::CChangeNotifier notifier(this, &changes);
264 Q_UNUSED(notifier);
265
266 m_items.pop_back();
267}
268
269
270template <typename ItemClass, typename ContainerType>
272{
273 auto changes = ElementRemoveChanges(0);
274 istd::CChangeNotifier notifier(this, &changes);
275 Q_UNUSED(notifier);
276
277 m_items.pop_front();
278}
279
280
281template <typename ItemClass, typename ContainerType>
283{
284 Q_ASSERT(index >= 0);
285 Q_ASSERT(index < int(m_items.size()));
286
287 if (index < int(m_items.size())){
288 auto changes = ElementRemoveChanges(index);
289 istd::CChangeNotifier notifier(this, &changes);
290 Q_UNUSED(notifier);
291
292 m_items.erase(m_items.begin() + index);
293 }
294}
295
296
297template <typename ItemClass, typename ContainerType>
299{
300 istd::CChangeNotifier notifier(this, &s_resetChange);
301 Q_UNUSED(notifier);
302
303 m_items.clear();
304}
305
306
307template <typename ItemClass, typename ContainerType>
309{
310 istd::CChangeNotifier notifier(this, &s_resetChange);
311 m_items = container.m_items;
312
313 return *this;
314}
315
316
317// reimplemented (istd::IContainerInfo)
318
319template <typename ItemClass, typename ContainerType>
321{
322 return int(m_items.size());
323}
324
325
326template <typename ItemClass, typename ContainerType>
328{
329 return (m_items.size() == 0);
330}
331
332
333template <typename ItemClass, typename ContainerType>
335{
336 return (index >= 0 && index < int(m_items.size()));
337}
338
339
340// reimplemented (istd::IChangeable)
341
342template <typename ItemClass, typename ContainerType>
344{
345 const TContainer* containerPtr = dynamic_cast<const TContainer*>(&object);
346 if (containerPtr != NULL){
347 m_items = containerPtr->m_items;
348
349 return true;
350 }
351
352 return false;
353}
354
355
356// protected static members
357
358template <typename ItemClass, typename ContainerType>
360
361
362} // namespace ibase
363
364
Base implementation for creating simple observable collection.
static istd::IChangeable::ChangeSet ElementRemoveChanges(qsizetype index)
static istd::IChangeable::ChangeSet ElementAddChanges(qsizetype index)
Common implementation of an abstract container.
Definition TContainer.h:26
virtual void Resize(int count)
Definition TContainer.h:194
Container::iterator insert(qsizetype index, ItemClass &&item)
Definition TContainer.h:78
virtual void RemoveAt(int index)
Definition TContainer.h:282
TContainer & operator=(const TContainer &container)
Definition TContainer.h:308
ItemClass & PushBack(const ItemClass &item)
Definition TContainer.h:218
Container::iterator insert(qsizetype index, const ItemClass &item)
Definition TContainer.h:69
Container::const_iterator end() const
Definition TContainer.h:147
Container::const_iterator begin() const
Definition TContainer.h:141
ItemClass & PushFront(const ItemClass &item)
Definition TContainer.h:231
static const ChangeSet s_resetChange
Definition TContainer.h:177
void remove(qsizetype i)
Definition TContainer.h:87
ItemClass & InsertAt(const ItemClass &item, int index)
Definition TContainer.h:244
virtual bool CopyFrom(const IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS) override
Definition TContainer.h:343
void resize(qsizetype size)
Definition TContainer.h:100
virtual bool IsEmpty() const override
Returns a true if container is empty, otherwise a false.
Definition TContainer.h:327
ContainerType Container
Definition TContainer.h:28
ItemClass & operator[](qsizetype index)
Definition TContainer.h:57
Container::const_iterator cbegin() const
Definition TContainer.h:153
virtual void Reserve(int count)
Definition TContainer.h:188
const ItemClass & GetAt(int index) const
Definition TContainer.h:209
bool operator!=(const TContainer &other) const
Definition TContainer.h:171
void push_front(const ItemClass &item)
Definition TContainer.h:51
virtual void Reset()
Definition TContainer.h:298
virtual int GetItemsCount() const override
Returns number of elements in the container.
Definition TContainer.h:320
qsizetype size() const
Definition TContainer.h:112
ContainerType Items
Definition TContainer.h:179
void push_back(const ItemClass &item)
Definition TContainer.h:45
Container::const_iterator cend() const
Definition TContainer.h:159
virtual bool IsIndexValid(int index) const override
Definition TContainer.h:334
bool operator==(const TContainer &other) const
Definition TContainer.h:166
ItemClass & GetAt(int index)
Definition TContainer.h:200
Container::iterator end()
Definition TContainer.h:135
Container::iterator begin()
Definition TContainer.h:129
const ItemClass & operator[](qsizetype index) const
Definition TContainer.h:63
Help class which provides the automatic update mechanism of the model.
Set of change flags (its IDs).
Definition IChangeable.h:36
CompatibilityMode
Control how relationship betweeen objects are interpreted.
@ CM_WITHOUT_REFS
External references are simple ignored.
Common interface for all container implementations.
#define NULL
Definition istd.h:74
This namespace contains basic implementations of standard primitives on the component level.