ACF $AcfVersion:0$
TPointerVector.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// STL includes
6#include <vector>
7
8// ACF includes
9#include <istd/istd.h>
10
11
12namespace istd
13{
14
15
19template<typename Pointer>
21{
22public:
23 typedef Pointer* ElementType;
24
25 static Pointer* GetPtr(const ElementType& element)
26 {
27 return element;
28 }
29
30 static Pointer* PopPtr(const ElementType& element)
31 {
32 return GetPtr(element);
33 }
34
35 static void Delete(const ElementType& element)
36 {
37 delete element;
38 }
39};
40
41
46template <typename Pointer, class AccessAdapter = TDeleteAdapter<Pointer> >
48{
49public:
50 typedef typename AccessAdapter::ElementType ElementType;
51
52 enum
53 {
57 InvalidIndex = -1
58 };
59
62
64
68 bool IsEmpty() const;
69
73 int GetCount() const;
74
78 void SetCount(int count);
79
83 void Reset();
84
89 int HasElement(const Pointer* elementPtr) const;
90
94 Pointer* GetAt(int index) const;
95
100 const ElementType& GetElementAt(int index) const;
101
105 void SetElementAt(int index, const ElementType& element);
106
112 void RemoveAt(int index);
113
120 bool Remove(Pointer* elementPtr);
121
126 Pointer* PopAt(int index);
127
131 void PushBack(const ElementType& element);
132
136 void InsertElementAt(int index, const ElementType& element);
137
142 void SwapElements(int index1, int index2);
143
144private:
145 typedef std::vector<ElementType> Elements;
146
147 Elements m_elements;
148};
149
150
151// inline methods
152
153template <typename Pointer, class AccessAdapter>
157
158
159template <typename Pointer, class AccessAdapter>
161{
162 I_IF_DEBUG(Q_ASSERT(otherVector.IsEmpty()));
163}
164
165
166template <typename Pointer, class AccessAdapter>
168{
169 return m_elements.empty();
170}
171
172
173template <typename Pointer, class AccessAdapter>
175{
176 return int(m_elements.size());
177}
178
179
180// public methods
181
182template <typename Pointer, class AccessAdapter>
187
188
189template <typename Pointer, class AccessAdapter>
191{
192 while (int(m_elements.size()) > count){
193 AccessAdapter::Delete(m_elements.back());
194
195 m_elements.pop_back();
196 }
197
198 m_elements.resize(count);
199}
200
201
202template <typename Pointer, class AccessAdapter>
204{
205 for ( typename Elements::iterator iter = m_elements.begin();
206 iter != m_elements.end();
207 ++iter){
208 AccessAdapter::Delete(*iter);
209 }
210
211 m_elements.clear();
212}
213
214
215template <typename Pointer, class AccessAdapter>
216int TPointerVector<Pointer, AccessAdapter>::HasElement(const Pointer* elementPtr) const
217{
218 int elementsCount = GetCount();
219
220 for (int elementIndex = 0; elementIndex < elementsCount; elementIndex++){
221 typename Elements::const_iterator delIter = (m_elements.begin() + elementIndex);
222 if (AccessAdapter::GetPtr(*delIter) == elementPtr){
223 return elementIndex;
224 }
225 }
226
227 return InvalidIndex;
228}
229
230
231template <typename Pointer, class AccessAdapter>
233{
234 Q_ASSERT(index >= 0);
235 Q_ASSERT(index < int(m_elements.size()));
236
237 return AccessAdapter::GetPtr(m_elements[index]);
238}
239
240
241template <typename Pointer, class AccessAdapter>
243{
244 Q_ASSERT(index >= 0);
245 Q_ASSERT(index < int(m_elements.size()));
246
247 return m_elements[index];
248}
249
250
251template <typename Pointer, class AccessAdapter>
253{
254 typename Elements::iterator delIter = (m_elements.begin() + index);
255
256 AccessAdapter::Delete(*delIter);
257
258 m_elements[index] = element;
259}
260
261
262template <typename Pointer, class AccessAdapter>
264{
265 Q_ASSERT(index >= 0);
266 Q_ASSERT(index < int(m_elements.size()));
267
268 typename Elements::iterator delIter = (m_elements.begin() + index);
269
270 AccessAdapter::Delete(*delIter);
271
272 m_elements.erase(delIter);
273}
274
275
276template <typename Pointer, class AccessAdapter>
278{
279 int elementsCount = GetCount();
280
281 for (int elementIndex = 0; elementIndex < elementsCount; elementIndex++){
282 typename Elements::iterator delIter = (m_elements.begin() + elementIndex);
283 if (AccessAdapter::GetPtr(*delIter) == elementPtr){
284 RemoveAt(elementIndex);
285
286 return true;
287 }
288 }
289
290 return false;
291}
292
293
294template <typename Pointer, class AccessAdapter>
296{
297 Q_ASSERT(index >= 0);
298 Q_ASSERT(index < int(m_elements.size()));
299
300 Pointer* popPtr = AccessAdapter::PopPtr(m_elements[index]);
301
302 m_elements.erase(m_elements.begin() + index);
303
304 return popPtr;
305}
306
307
308template <typename Pointer, class AccessAdapter>
310{
311 m_elements.push_back(element);
312}
313
314
315template <typename Pointer, class AccessAdapter>
317{
318 Q_ASSERT(index >= 0);
319 Q_ASSERT(index <= GetCount());
320 Q_ASSERT(HasElement(AccessAdapter::GetPtr(element)) == InvalidIndex);
321
322 m_elements.insert(m_elements.begin() + index, element);
323}
324
325
326template <typename Pointer, class AccessAdapter>
328{
329 Q_ASSERT(index1 >= 0);
330 Q_ASSERT(index1 <= GetCount());
331 Q_ASSERT(index2 >= 0);
332 Q_ASSERT(index2 <= GetCount());
333
334 ElementType element1 = m_elements[index1];
335 m_elements[index1] = m_elements[index2];
336 m_elements[index2] = element1;
337}
338
339
340} // namespace istd
341
342
343
344
Default delete adapter.
static Pointer * PopPtr(const ElementType &element)
static Pointer * GetPtr(const ElementType &element)
static void Delete(const ElementType &element)
Implementation of a pointer container, which controls the live cycle of the pointer object.
bool Remove(Pointer *elementPtr)
Remove element elementPtr.
Pointer * PopAt(int index)
Pop element at specified index.
void SetCount(int count)
Set number of elements.
int HasElement(const Pointer *elementPtr) const
Check if some element is stored in this vector and return the index of the element,...
TPointerVector(const TPointerVector &)
AccessAdapter::ElementType ElementType
Pointer * GetAt(int index) const
Get pointer at specified index.
bool IsEmpty() const
Check if there is element stored in this set.
int GetCount() const
Get number of stored elements.
void SwapElements(int index1, int index2)
Switch two elements.
void RemoveAt(int index)
Remove element at specified index.
void SetElementAt(int index, const ElementType &element)
Set element at specified index.
void PushBack(const ElementType &element)
Add new element at the end of collection.
void Reset()
Remove all elements.
void InsertElementAt(int index, const ElementType &element)
Insert element at specified position.
@ InvalidIndex
Invalid index in the vector.
const ElementType & GetElementAt(int index) const
Get element at specified index.
#define I_IF_DEBUG(instructions)
Definition istd.h:59
Standard library.
Definition IComponent.h:17