diff options
author | Ivan Čukić <ivan.cukic@kdab.com> | 2020-01-10 18:13:41 +0100 |
---|---|---|
committer | Ivan Čukić <ivan.cukic@kde.org> | 2020-03-03 20:25:19 +0100 |
commit | 14420b359b2d14d202df8df841af5e88f16352a7 (patch) | |
tree | ba956bb8c7c822980e54bbbbc66ece2e2d07cf76 /src | |
parent | 065ace2bca656467c3bc2ef760bfff1fd198dbdc (diff) |
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>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qiterator.h | 26 | ||||
-rw-r--r-- | src/corelib/tools/qiterator.qdoc | 13 |
2 files changed, 36 insertions, 3 deletions
diff --git a/src/corelib/tools/qiterator.h b/src/corelib/tools/qiterator.h index 84a0e116ca..c4665c7c87 100644 --- a/src/corelib/tools/qiterator.h +++ b/src/corelib/tools/qiterator.h @@ -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; } diff --git a/src/corelib/tools/qiterator.qdoc b/src/corelib/tools/qiterator.qdoc index 3531fb202c..ed39fe28c3 100644 --- a/src/corelib/tools/qiterator.qdoc +++ b/src/corelib/tools/qiterator.qdoc @@ -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 |