QDirListing: simplify const_iterator

There's no need to carry the same QDirListingPrivate* around
twice. Remove const_iterator's direct QDirListingPrivate* member and
re-use the one from its DirEntry member.

Requires adding a private const_iterator::atEnd() helper function,
because while const_iterator and its member functions are implicit
friends of DirEntry, friend functions of const_iterator are
not. Improves readability, too.

Found in API-review.

Change-Id: I82b9273950e174d7598ee531fbd5dfb472911b71
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
(cherry picked from commit 0ede7f541dfc6cba163e1452aee7239818a42c1b)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-09-02 15:33:58 +02:00 committed by Qt Cherry-pick Bot
parent 089263fd4f
commit 5cb51e4afe
2 changed files with 6 additions and 8 deletions

View File

@ -725,8 +725,8 @@ QDirListing::const_iterator QDirListing::begin() const
*/
QDirListing::const_iterator &QDirListing::const_iterator::operator++()
{
dirListPtr->advance();
if (!dirListPtr->hasIterators())
dirEntry.dirListPtr->advance();
if (!dirEntry.dirListPtr->hasIterators())
*this = {}; // All done, make `this` equal to the end() iterator
return *this;
}

View File

@ -103,8 +103,7 @@ public:
class const_iterator
{
friend class QDirListing;
const_iterator(QDirListingPrivate *dp) : dirListPtr(dp) { dirEntry.dirListPtr = dp; }
QDirListingPrivate *dirListPtr = nullptr;
explicit const_iterator(QDirListingPrivate *dp) { dirEntry.dirListPtr = dp; }
DirEntry dirEntry;
public:
using iterator_category = std::input_iterator_tag;
@ -118,10 +117,9 @@ public:
pointer operator->() const { return &dirEntry; }
Q_CORE_EXPORT const_iterator &operator++();
const_iterator operator++(int) { auto tmp = *this; operator++(); return tmp; };
friend bool operator==(const const_iterator &lhs, sentinel) noexcept
{
return lhs.dirListPtr == nullptr;
}
private:
bool atEnd() const noexcept { return dirEntry.dirListPtr == nullptr; }
friend bool operator==(const const_iterator &lhs, sentinel) noexcept { return lhs.atEnd(); }
#ifndef __cpp_impl_three_way_comparison
friend bool operator!=(const const_iterator &lhs, sentinel) noexcept
{ return !operator==(lhs, sentinel{}); }