src/main.h

changeset 97
d9a3b153f679
parent 96
165777a20dc7
child 101
910890292639
equal deleted inserted replaced
96:165777a20dc7 97:d9a3b153f679
151 .arg(toDouble(vec.x)) 151 .arg(toDouble(vec.x))
152 .arg(toDouble(vec.y)) 152 .arg(toDouble(vec.y))
153 .arg(toDouble(vec.z)) 153 .arg(toDouble(vec.z))
154 .arg(toDouble(vec.w)); 154 .arg(toDouble(vec.w));
155 } 155 }
156
157 template<typename K, typename V>
158 struct KeyValuePair
159 {
160 K key;
161 V value;
162 };
163
164 template<typename K, typename V, typename IteratorType>
165 struct MapItemsIterator : IteratorType
166 {
167 template<typename... Ts>
168 MapItemsIterator(Ts&&... args) : IteratorType{args...} {}
169 auto operator*() const
170 {
171 return KeyValuePair<K, V>{this->key(), this->value()};
172 }
173 };
174
175 template<typename K, typename V, typename MapType, typename IteratorType>
176 struct MapItems
177 {
178 MapType& map;
179 IteratorType begin()
180 {
181 return IteratorType(this->map.begin());
182 }
183
184 IteratorType end()
185 {
186 return IteratorType(this->map.end());
187 }
188 };
189
190 /*
191 * Python's dict.items for QMap: use in a for loop to iterate a map to
192 * get both keys and values. Iteration yields KeyValuePairs.
193 */
194 template<typename K, typename V>
195 auto items(const QMap<K, V>& map)
196 {
197 return MapItems<
198 const K&,
199 const V&,
200 const QMap<K, V>,
201 MapItemsIterator<K, const V, typename QMap<K, V>::const_iterator>
202 >{map};
203 }
204
205 template<typename K, typename V>
206 auto items(QMap<K, V>& map)
207 {
208 return MapItems<
209 const K&,
210 V&,
211 QMap<K, V>,
212 MapItemsIterator<K, const V, typename QMap<K, V>::iterator>
213 >{map};
214 }

mercurial