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