TPointerVector.h
Go to the documentation of this file.
1 #ifndef istd_TPointerVector_included
2 #define istd_TPointerVector_included
3 
4 
5 // STL includes
6 #include <vector>
7 
8 // ACF includes
9 #include <istd/istd.h>
10 
11 
12 namespace istd
13 {
14 
15 
19 template<typename Pointer>
21 {
22 public:
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 
46 template <typename Pointer, class AccessAdapter = TDeleteAdapter<Pointer> >
48 {
49 public:
50  typedef typename AccessAdapter::ElementType ElementType;
51 
52  enum
53  {
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 
144 private:
145  typedef std::vector<ElementType> Elements;
146 
147  Elements m_elements;
148 };
149 
150 
151 // inline methods
152 
153 template <typename Pointer, class AccessAdapter>
155 {
156 }
157 
158 
159 template <typename Pointer, class AccessAdapter>
161 {
162  I_IF_DEBUG(Q_ASSERT(otherVector.IsEmpty()));
163 }
164 
165 
166 template <typename Pointer, class AccessAdapter>
168 {
169  return m_elements.empty();
170 }
171 
172 
173 template <typename Pointer, class AccessAdapter>
175 {
176  return int(m_elements.size());
177 }
178 
179 
180 // public methods
181 
182 template <typename Pointer, class AccessAdapter>
184 {
185  Reset();
186 }
187 
188 
189 template <typename Pointer, class AccessAdapter>
191 {
192  m_elements.resize(count);
193 }
194 
195 
196 template <typename Pointer, class AccessAdapter>
198 {
199  for ( typename Elements::iterator iter = m_elements.begin();
200  iter != m_elements.end();
201  ++iter){
202  AccessAdapter::Delete(*iter);
203  }
204 
205  m_elements.clear();
206 }
207 
208 
209 template <typename Pointer, class AccessAdapter>
210 int TPointerVector<Pointer, AccessAdapter>::HasElement(const Pointer* elementPtr) const
211 {
212  int elementsCount = GetCount();
213 
214  for (int elementIndex = 0; elementIndex < elementsCount; elementIndex++){
215  typename Elements::const_iterator delIter = (m_elements.begin() + elementIndex);
216  if (AccessAdapter::GetPtr(*delIter) == elementPtr){
217  return elementIndex;
218  }
219  }
220 
221  return InvalidIndex;
222 }
223 
224 
225 template <typename Pointer, class AccessAdapter>
227 {
228  Q_ASSERT(index >= 0);
229  Q_ASSERT(index < int(m_elements.size()));
230 
231  return AccessAdapter::GetPtr(m_elements[index]);
232 }
233 
234 
235 template <typename Pointer, class AccessAdapter>
237 {
238  Q_ASSERT(index >= 0);
239  Q_ASSERT(index < int(m_elements.size()));
240 
241  return m_elements[index];
242 }
243 
244 
245 template <typename Pointer, class AccessAdapter>
247 {
248  typename Elements::iterator delIter = (m_elements.begin() + index);
249 
250  AccessAdapter::Delete(*delIter);
251 
252  m_elements[index] = element;
253 }
254 
255 
256 template <typename Pointer, class AccessAdapter>
258 {
259  Q_ASSERT(index >= 0);
260  Q_ASSERT(index < int(m_elements.size()));
261 
262  typename Elements::iterator delIter = (m_elements.begin() + index);
263 
264  AccessAdapter::Delete(*delIter);
265 
266  m_elements.erase(delIter);
267 }
268 
269 
270 template <typename Pointer, class AccessAdapter>
272 {
273  int elementsCount = GetCount();
274 
275  for (int elementIndex = 0; elementIndex < elementsCount; elementIndex++){
276  typename Elements::iterator delIter = (m_elements.begin() + elementIndex);
277  if (AccessAdapter::GetPtr(*delIter) == elementPtr){
278  RemoveAt(elementIndex);
279 
280  return true;
281  }
282  }
283 
284  return false;
285 }
286 
287 
288 template <typename Pointer, class AccessAdapter>
290 {
291  Q_ASSERT(index >= 0);
292  Q_ASSERT(index < int(m_elements.size()));
293 
294  Pointer* popPtr = AccessAdapter::PopPtr(m_elements[index]);
295 
296  m_elements.erase(m_elements.begin() + index);
297 
298  return popPtr;
299 }
300 
301 
302 template <typename Pointer, class AccessAdapter>
304 {
305  m_elements.push_back(element);
306 }
307 
308 
309 template <typename Pointer, class AccessAdapter>
311 {
312  Q_ASSERT(index >= 0);
313  Q_ASSERT(index <= GetCount());
314  Q_ASSERT(HasElement(AccessAdapter::GetPtr(element)) == InvalidIndex);
315 
316  m_elements.insert(m_elements.begin() + index, element);
317 }
318 
319 
320 template <typename Pointer, class AccessAdapter>
322 {
323  Q_ASSERT(index1 >= 0);
324  Q_ASSERT(index1 <= GetCount());
325  Q_ASSERT(index2 >= 0);
326  Q_ASSERT(index2 <= GetCount());
327 
328  ElementType element1 = m_elements[index1];
329  m_elements[index1] = m_elements[index2];
330  m_elements[index2] = element1;
331 }
332 
333 
334 } // namespace istd
335 
336 
337 #endif // !istd_TPointerVector_included
338 
339 
#define I_IF_DEBUG(instructions)
Definition: istd.h:49
Pointer * PopAt(int index)
Pop element at specified index.
Pointer * GetAt(int index) const
Get pointer at specified index.
const ElementType & GetElementAt(int index) const
Get element at specified index.
bool IsEmpty() const
Check if there is element stored in this set.
void SetElementAt(int index, const ElementType &element)
Set element at specified index.
void InsertElementAt(int index, const ElementType &element)
Insert element at specified position.
Default delete adapter.
void PushBack(const ElementType &element)
Add new element at the end of collection.
Invalid index in the vector.
void SwapElements(int index1, int index2)
Switch two elements.
int HasElement(const Pointer *elementPtr) const
Check if some element is stored in this vector and return the index of the element, if anyone exists, otherwise return InvalidIndex.
int GetCount() const
Get number of stored elements.
bool Remove(Pointer *elementPtr)
Remove element elementPtr.
Implementation of a pointer container, which controls the live cycle of the pointer object...
void RemoveAt(int index)
Remove element at specified index.
static Pointer * PopPtr(const ElementType &element)
static void Delete(const ElementType &element)
void SetCount(int count)
Set number of elements.
void Reset()
Remove all elements.
static Pointer * GetPtr(const ElementType &element)
AccessAdapter::ElementType ElementType

© 2007-2017 Witold Gantzke and Kirill Lepskiy