QDirListing: make const_iterator move-only

The std::input_iterator concept does not require copyable, only
movable. This makes handling input_iterators safer, because copies of
input iterators since advanced zap into partially-formed state without
changing their bit pattern. If the iterator is, however, only movable,
no such zapping copies can exist.

Take advantage of this and make QDirListing::const_iterator
move-only. Its iterators are anyway no longer compatible with
classical STL algorithms, so we're not restricting their use further.

Found in API review.

Task-number: QTBUG-125512
Change-Id: Ic1dee22881893fdbf159bbfd335c10505eaffa9f
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
(cherry picked from commit 29dfcc240b3e29d5d6149c2996cfdb9d71b776bb)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-09-02 18:31:30 +02:00 committed by Qt Cherry-pick Bot
parent a1d4c64715
commit 46ec49894d
2 changed files with 8 additions and 2 deletions

View File

@ -35,7 +35,7 @@
Iterators constructed by QDirListing (QDirListing::const_iterator)
model C++20
\l{https://en.cppreference.com/w/cpp/iterator/input_iterator}{std::input_iterator},
that is, they are
that is, they are move-only,
forward-only, single-pass iterators, that don't allow random access. They
can be used in ranged-for loops (or with C++20 range algorithms that don't
require random access iterators). Dereferencing a valid iterator returns
@ -673,6 +673,7 @@ QStringList QDirListing::nameFilters() const
\list
\li This is a forward-only, single-pass iterator (you cannot iterate
directory entries in reverse order)
\li Can't be copied, only \c{std::move()}d.
\li Doesn't allow random access
\li Can be used in ranged-for loops; or with STL algorithms that don't
require random access iterators
@ -703,7 +704,8 @@ QDirListing::const_iterator QDirListing::begin() const
{
d->beginIterating();
const_iterator it{d};
return ++it;
++it;
return it;
}
/*!

View File

@ -102,6 +102,7 @@ public:
class const_iterator
{
Q_DISABLE_COPY(const_iterator)
friend class QDirListing;
explicit const_iterator(QDirListingPrivate *dp) { dirEntry.dirListPtr = dp; }
DirEntry dirEntry;
@ -113,6 +114,9 @@ public:
using reference = const value_type &;
const_iterator() = default;
const_iterator(const_iterator &&) noexcept = default;
const_iterator &operator=(const_iterator &&) noexcept = default;
reference operator*() const { return dirEntry; }
pointer operator->() const { return &dirEntry; }
Q_CORE_EXPORT const_iterator &operator++();