ArrayIterator.h
Go to the documentation of this file.
1 
20 #pragma once
21 
22 namespace ConfigDB
23 {
27 template <class ArrayType, typename ItemType> class ArrayIterator
28 {
29 public:
31 
32  ArrayIterator() = default;
33  ArrayIterator(const ArrayIterator&) = default;
34 
35  ArrayIterator(ArrayType& array, unsigned index) : array(&array), mIndex(index), mCount(array.getItemCount())
36  {
37  }
38 
39  operator bool() const
40  {
41  return array && (mIndex < mCount);
42  }
43 
44  bool operator==(const ArrayIterator& rhs) const
45  {
46  return array == rhs.array && mIndex == rhs.mIndex;
47  }
48 
49  bool operator!=(const ArrayIterator& rhs) const
50  {
51  return !operator==(rhs);
52  }
53 
54  const ItemType operator*() const
55  {
56  if(!array) {
57  return {};
58  }
59  return (*array)[mIndex];
60  }
61 
62  ItemType operator*()
63  {
64  if(!array) {
65  abort();
66  }
67  return (*array)[mIndex];
68  }
69 
70  unsigned index() const
71  {
72  return mIndex;
73  }
74 
76  {
77  ++mIndex;
78  return *this;
79  }
80 
82  {
83  ArrayIterator tmp(*this);
84  ++mIndex;
85  return tmp;
86  }
87 
88 private:
89  ArrayType* array{};
90  unsigned mIndex{0};
91  const unsigned mCount{0};
92 };
93 
94 } // namespace ConfigDB
Array iterator support.
Definition: ArrayIterator.h:28
unsigned index() const
Definition: ArrayIterator.h:70
ArrayIterator & operator++()
Definition: ArrayIterator.h:75
ItemType operator*()
Definition: ArrayIterator.h:62
const ItemType operator*() const
Definition: ArrayIterator.h:54
ArrayIterator operator++(int)
Definition: ArrayIterator.h:81
bool operator!=(const ArrayIterator &rhs) const
Definition: ArrayIterator.h:49
ArrayIterator(const ArrayIterator &)=default
bool operator==(const ArrayIterator &rhs) const
Definition: ArrayIterator.h:44
ArrayIterator(ArrayType &array, unsigned index)
Definition: ArrayIterator.h:35
Definition: Array.h:26