commonlib
vector_with_ids_iterator.h
Go to the documentation of this file.
1 
18 #pragma once
19 
20 #include <vector>
21 
22 #include "containers.h"
23 
24 BEGIN_NAMESPACE_CONTAINERS
25 
26 // Forward declaration
27 template <typename T>
28 class VectorWithIds;
29 
30 namespace VectorWithIdsIterators
31 {
32 enum class ConstTypeSwitch
33 {
34  isConst = 0,
35  isNonConst = 1
36 };
37 
38 template <typename T, ConstTypeSwitch ConstSwitch>
40 {
41  using ValueType = T;
42  using PointeeType = typename std::vector<std::pair<int32_t, T>>::iterator;
43 };
44 
45 template <typename T>
46 struct UnderlyingTypes<T, ConstTypeSwitch::isConst>
47 {
48  using ValueType = const T;
49  using PointeeType = typename std::vector<std::pair<int32_t,T>>::const_iterator;
50 };
51 
52 template <typename T>
53 struct UnderlyingTypes<T, ConstTypeSwitch::isNonConst>
54 {
55  using ValueType = T;
56  using PointeeType = typename std::vector<std::pair<int32_t, T>>::iterator;
57 };
58 
59 template <typename T, ConstTypeSwitch ConstSwitch>
61 {
62  friend class VectorWithIds<T>;
63 
64 public:
65  using ValueType = typename UnderlyingTypes<T, ConstSwitch>::ValueType;
66  using PointeeType = typename UnderlyingTypes<T, ConstSwitch>::PointeeType;
67 
68  using iterator_category = std::forward_iterator_tag;
69  using value_type = ValueType;
70  using difference_type = std::ptrdiff_t;
71  using pointer = ValueType*;
72  using reference = ValueType&;
73 
74 public:
75 
79  int32_t getId() const;
80 
84  ValueType& operator*();
85 
89  ValueType* operator->();
90 
96 
102 
109 
115  bool operator==(const VectorWithIdsIterator<T, ConstSwitch>& other) const;
116 
122  bool operator!=(const VectorWithIdsIterator<T, ConstSwitch>& other) const;
123 
124 private:
125  PointeeType pointee;
126  VectorWithIdsIterator(PointeeType pointee)
127  : pointee(pointee)
128  {
129  }
130 };
131 
132 template <typename T, ConstTypeSwitch ConstSwitch>
134 {
135  return pointee->first;
136 }
137 
138 template <typename T, ConstTypeSwitch ConstSwitch>
139 typename VectorWithIdsIterator<T, ConstSwitch>::ValueType& VectorWithIdsIterator<T, ConstSwitch>::operator*()
140 {
141  return pointee->second;
142 }
143 
144 template <typename T, ConstTypeSwitch ConstSwitch>
145 typename VectorWithIdsIterator<T, ConstSwitch>::ValueType* VectorWithIdsIterator<T, ConstSwitch>::operator->()
146 {
147  return &(pointee->second);
148 }
149 
150 template <typename T, ConstTypeSwitch ConstSwitch>
152 {
153  ++pointee;
154  return *this;
155 }
156 
157 template <typename T, ConstTypeSwitch ConstSwitch>
159 {
160  VectorWithIdsIterator<T, ConstSwitch> previousState(pointee);
161  ++pointee;
162  return previousState;
163 }
164 
165 template <typename T, ConstTypeSwitch ConstSwitch>
167 {
168  return VectorWithIdsIterator<T, ConstSwitch>(pointee + increment);
169 }
170 
171 template <typename T, ConstTypeSwitch ConstSwitch>
173 {
174  return pointee == other.pointee;
175 }
176 
177 template <typename T, ConstTypeSwitch ConstSwitch>
179 {
180  return pointee != other.pointee;
181 }
182 
183 } // namespace VectorWithIdsIterators
184 
185 END_NAMESPACE_CONTAINERS
Definition: vector_with_ids.h:38
int32_t getId() const
Definition: vector_with_ids_iterator.h:133
Definition: vector_with_ids_iterator.h:60
const VectorWithIdsIterator< T, ConstSwitch > & operator++()
Post increment iterator to the next element in container.
Definition: vector_with_ids_iterator.h:151
ValueType * operator->()
Definition: vector_with_ids_iterator.h:145
bool operator!=(const VectorWithIdsIterator< T, ConstSwitch > &other) const
Check if iterators don&#39;t point to the same element in the container.
Definition: vector_with_ids_iterator.h:178
Definition: vector_with_ids_iterator.h:30
Definition: vector_with_ids_iterator.h:39
bool operator==(const VectorWithIdsIterator< T, ConstSwitch > &other) const
Check if iterators point to the same element in the container.
Definition: vector_with_ids_iterator.h:172
ValueType & operator*()
Definition: vector_with_ids_iterator.h:139
VectorWithIdsIterator< T, ConstSwitch > operator+(int32_t increment)
Increment iterator to by the number of elements passed as an increment.
Definition: vector_with_ids_iterator.h:166