diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-09-03 15:04:16 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-09-12 23:37:15 +0200 |
commit | 652bd1efca34b7e114836f79c33b5e4a248faaee (patch) | |
tree | 23eea549c4a282626ec5d891011e76482b8cc4be /src/corelib/tools | |
parent | ead02871cc97fa7dae956b1cd649a91de432dc73 (diff) |
Make QStringList an alias to QList<QString>
Fix our API, so that QStringList and QList<QString> 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<QString>.
Change-Id: Ib8afbf5b6d9df4d0d47051252233506f62335fa3
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qcontainerfwd.h | 2 | ||||
-rw-r--r-- | src/corelib/tools/qlist.h | 43 |
2 files changed, 28 insertions, 17 deletions
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 T> class QStack; template<class T, qsizetype Prealloc = 256> class QVarLengthArray; template <class T> class QList; template<typename T> using QVector = QList<T>; -class QStringList; +using QStringList = QList<QString>; using QByteArrayList = QList<QByteArray>; 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 <typename V, typename U> qsizetype indexOf(const QList<V> &list, const U &u, qsizetype from); - template <typename V, typename U> qsizetype lastIndexOf(const QList<V> &list, const U &u, qsizetype from); + template <typename V, typename U> qsizetype indexOf(const QList<V> &list, const U &u, qsizetype from) noexcept; + template <typename V, typename U> qsizetype lastIndexOf(const QList<V> &list, const U &u, qsizetype from) noexcept; } template <typename T> 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<QByteArray>; template <> struct QListSpecialMethods<QString>; @@ -79,8 +87,8 @@ class QList DataPointer d; - template <typename V, typename U> friend qsizetype QtPrivate::indexOf(const QList<V> &list, const U &u, qsizetype from); - template <typename V, typename U> friend qsizetype QtPrivate::lastIndexOf(const QList<V> &list, const U &u, qsizetype from); + template <typename V, typename U> friend qsizetype QtPrivate::indexOf(const QList<V> &list, const U &u, qsizetype from) noexcept; + template <typename V, typename U> friend qsizetype QtPrivate::lastIndexOf(const QList<V> &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<typename String, typename = std::enable_if_t<std::is_same_v<T, QString> && std::is_convertible_v<String, QString>>> + inline explicit QList(const String &str) + { append(str); } + // compiler-generated special member functions are fine! void swap(QList<T> &other) noexcept { qSwap(d, other.d); } @@ -289,12 +302,10 @@ public: QList<T> &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<T>::contains; + using QListSpecialMethods<T>::indexOf; + using QListSpecialMethods<T>::lastIndexOf; + qsizetype count(const T &t) const noexcept { return qsizetype(std::count(&*cbegin(), &*cend(), t)); @@ -705,7 +716,7 @@ inline QList<T> &QList<T>::fill(parameter_type t, qsizetype newSize) namespace QtPrivate { template <typename T, typename U> -qsizetype indexOf(const QList<T> &vector, const U &u, qsizetype from) +qsizetype indexOf(const QList<T> &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<T> &vector, const U &u, qsizetype from) } template <typename T, typename U> -qsizetype lastIndexOf(const QList<T> &vector, const U &u, qsizetype from) +qsizetype lastIndexOf(const QList<T> &vector, const U &u, qsizetype from) noexcept { if (from < 0) from += vector.d->size; @@ -739,15 +750,15 @@ qsizetype lastIndexOf(const QList<T> &vector, const U &u, qsizetype from) } template <typename T> -qsizetype QList<T>::indexOf(const T &t, qsizetype from) const noexcept +qsizetype QListSpecialMethods<T>::indexOf(const T &t, qsizetype from) const noexcept { - return QtPrivate::indexOf<T, T>(*this, t, from); + return QtPrivate::indexOf(*static_cast<const QList<T> *>(this), t, from); } template <typename T> -qsizetype QList<T>::lastIndexOf(const T &t, qsizetype from) const noexcept +qsizetype QListSpecialMethods<T>::lastIndexOf(const T &t, qsizetype from) const noexcept { - return QtPrivate::lastIndexOf(*this, t, from); + return QtPrivate::lastIndexOf(*static_cast<const QList<T> *>(this), t, from); } template <typename T> |