Add operator-> to the key-value iterator for QHash and QMap

This patch adds the arrow operator to the stl-like key-value
iterator (QKeyValueIterator) for QMap and QHash.

This allows using normal member access syntax it->first and it->second
instead of having to use (*it).first and (*it).second.

[ChangeLog][QtCore][Containers] Added operator-> to the key-value
iterator for QHash/QMap.

Change-Id: I9cfa6480784ebce147fcfbf37fec5ad0080e2899
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
This commit is contained in:
Ivan Čukić 2020-01-10 18:13:41 +01:00 committed by Ivan Čukić
parent 065ace2bca
commit 14420b359b
4 changed files with 48 additions and 3 deletions

View File

@ -195,7 +195,6 @@ public:
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::difference_type difference_type;
typedef std::pair<Key, T> value_type;
typedef const value_type *pointer;
typedef const value_type &reference;
QKeyValueIterator() = default;
@ -206,6 +205,31 @@ public:
return std::pair<Key, T>(i.key(), i.value());
}
struct pointer {
pointer(value_type&& r_)
: r(std::move(r_))
{}
pointer() = default;
pointer(const pointer &other) = default;
pointer(pointer &&other) = default;
pointer& operator=(const pointer &other) = default;
pointer& operator=(pointer &&other) = default;
value_type& operator*() const {
return r;
}
value_type r;
const value_type *operator->() const {
return &r;
}
};
pointer operator->() const {
return pointer(std::pair<Key, T>(i.key(), i.value()));
}
friend bool operator==(QKeyValueIterator lhs, QKeyValueIterator rhs) noexcept { return lhs.i == rhs.i; }
friend bool operator!=(QKeyValueIterator lhs, QKeyValueIterator rhs) noexcept { return lhs.i != rhs.i; }

View File

@ -57,7 +57,7 @@
\internal
*/
/*! \typedef QKeyValueIterator::pointer
/*! \struct QKeyValueIterator::pointer
\internal
*/
@ -75,11 +75,20 @@
Constructs a QKeyValueIterator on top of \a o.
*/
/*! \fn template<typename Key, typename T, class Iterator> const T &QKeyValueIterator<Key, T, Iterator>::operator*() const
/*! \fn template<typename Key, typename T, class Iterator> std::pair<Key, T> QKeyValueIterator<Key, T, Iterator>::operator*() const
Returns the current entry as a pair.
*/
/*! \fn template<typename Key, typename T, class Iterator> pointer QKeyValueIterator<Key, T, Iterator>::operator->() const
Returns the current entry as a pointer-like object to the pair.
\since 5.15
\sa operator*()
*/
/*! \fn template<typename Key, typename T, class Iterator> bool operator==(QKeyValueIterator<Key, T, Iterator> lhs, QKeyValueIterator<Key, T, Iterator> rhs)
\relates QKeyValueIterator

View File

@ -1144,6 +1144,12 @@ void tst_QHash::keyValueIterator()
entry_type pair(it.key(), it.value());
QCOMPARE(*key_value_it, pair);
QCOMPARE(key_value_it->first, pair.first);
QCOMPARE(key_value_it->second, pair.second);
QCOMPARE(&(*key_value_it).first, &it.key());
QCOMPARE(&key_value_it->first, &it.key());
QCOMPARE(&(*key_value_it).second, &it.value());
QCOMPARE(&key_value_it->second, &it.value());
++key_value_it;
++it;
}

View File

@ -882,6 +882,12 @@ void tst_QMap::keyValueIterator()
entry_type pair(it.key(), it.value());
QCOMPARE(*key_value_it, pair);
QCOMPARE(key_value_it->first, pair.first);
QCOMPARE(key_value_it->second, pair.second);
QCOMPARE(&(*key_value_it).first, &it.key());
QCOMPARE(&key_value_it->first, &it.key());
QCOMPARE(&(*key_value_it).second, &it.value());
QCOMPARE(&key_value_it->second, &it.value());
++key_value_it;
++it;
}