From 652bd1efca34b7e114836f79c33b5e4a248faaee Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 3 Sep 2020 15:04:16 +0200 Subject: Make QStringList an alias to QList Fix our API, so that QStringList and QList are the same thing. This required a bit of refactoring in QList and moving the indexOf(), lastIndexOf() and contains() method into QListSpecialMethods. In addition, we need to ensure that the QStringList(const QString&) constructor is still available for compatibility with Qt 5. Once those two are done, all methods in QStringList can be moved into QListSpecialMethods. Change-Id: Ib8afbf5b6d9df4d0d47051252233506f62335fa3 Reviewed-by: Andrei Golubev Reviewed-by: Thiago Macieira --- src/corelib/tools/qcontainerfwd.h | 2 +- src/corelib/tools/qlist.h | 43 ++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 17 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qcontainerfwd.h b/src/corelib/tools/qcontainerfwd.h index 069be4ef10..46c2801b00 100644 --- a/src/corelib/tools/qcontainerfwd.h +++ b/src/corelib/tools/qcontainerfwd.h @@ -61,7 +61,7 @@ template class QStack; template class QVarLengthArray; template class QList; template using QVector = QList; -class QStringList; +using QStringList = QList; using QByteArrayList = QList; class QMetaType; class QVariant; diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index ebb0fd9531..5bdc993b67 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -54,14 +54,22 @@ QT_BEGIN_NAMESPACE namespace QtPrivate { - template qsizetype indexOf(const QList &list, const U &u, qsizetype from); - template qsizetype lastIndexOf(const QList &list, const U &u, qsizetype from); + template qsizetype indexOf(const QList &list, const U &u, qsizetype from) noexcept; + template qsizetype lastIndexOf(const QList &list, const U &u, qsizetype from) noexcept; } template struct QListSpecialMethods { protected: ~QListSpecialMethods() = default; +public: + qsizetype indexOf(const T &t, qsizetype from = 0) const noexcept; + qsizetype lastIndexOf(const T &t, qsizetype from = -1) const noexcept; + + bool contains(const T &t) const noexcept + { + return indexOf(t) != -1; + } }; template <> struct QListSpecialMethods; template <> struct QListSpecialMethods; @@ -79,8 +87,8 @@ class QList DataPointer d; - template friend qsizetype QtPrivate::indexOf(const QList &list, const U &u, qsizetype from); - template friend qsizetype QtPrivate::lastIndexOf(const QList &list, const U &u, qsizetype from); + template friend qsizetype QtPrivate::indexOf(const QList &list, const U &u, qsizetype from) noexcept; + template friend qsizetype QtPrivate::lastIndexOf(const QList &list, const U &u, qsizetype from) noexcept; public: typedef T Type; @@ -158,6 +166,11 @@ public: std::copy(i1, i2, std::back_inserter(*this)); } + // This constructor is here for compatibility with QStringList in Qt 5, that has a QStringList(const QString &) constructor + template && std::is_convertible_v>> + inline explicit QList(const String &str) + { append(str); } + // compiler-generated special member functions are fine! void swap(QList &other) noexcept { qSwap(d, other.d); } @@ -289,12 +302,10 @@ public: QList &fill(parameter_type t, qsizetype size = -1); - qsizetype indexOf(const T &t, qsizetype from = 0) const noexcept; - qsizetype lastIndexOf(const T &t, qsizetype from = -1) const noexcept; - bool contains(const T &t) const noexcept - { - return indexOf(t) != -1; - } + using QListSpecialMethods::contains; + using QListSpecialMethods::indexOf; + using QListSpecialMethods::lastIndexOf; + qsizetype count(const T &t) const noexcept { return qsizetype(std::count(&*cbegin(), &*cend(), t)); @@ -705,7 +716,7 @@ inline QList &QList::fill(parameter_type t, qsizetype newSize) namespace QtPrivate { template -qsizetype indexOf(const QList &vector, const U &u, qsizetype from) +qsizetype indexOf(const QList &vector, const U &u, qsizetype from) noexcept { if (from < 0) from = qMax(from + vector.size(), qsizetype(0)); @@ -720,7 +731,7 @@ qsizetype indexOf(const QList &vector, const U &u, qsizetype from) } template -qsizetype lastIndexOf(const QList &vector, const U &u, qsizetype from) +qsizetype lastIndexOf(const QList &vector, const U &u, qsizetype from) noexcept { if (from < 0) from += vector.d->size; @@ -739,15 +750,15 @@ qsizetype lastIndexOf(const QList &vector, const U &u, qsizetype from) } template -qsizetype QList::indexOf(const T &t, qsizetype from) const noexcept +qsizetype QListSpecialMethods::indexOf(const T &t, qsizetype from) const noexcept { - return QtPrivate::indexOf(*this, t, from); + return QtPrivate::indexOf(*static_cast *>(this), t, from); } template -qsizetype QList::lastIndexOf(const T &t, qsizetype from) const noexcept +qsizetype QListSpecialMethods::lastIndexOf(const T &t, qsizetype from) const noexcept { - return QtPrivate::lastIndexOf(*this, t, from); + return QtPrivate::lastIndexOf(*static_cast *>(this), t, from); } template -- cgit v1.2.3