Move QListSpecialMethods over to QVector

Extend QVector with special methods for QByteArray and QString,
just as QList had them in Qt 5.

This also means that QStringList and QByteArrayList
are now implemented through a QVector, not a QList anymore.

QListIterator<QString> is now slightly source incompatible as QStringList
is a QVector, but that will be fixed in a follow-up change when
QList<QString> will start mapping to a QVector.

Change-Id: I7cfb8a72d4d95b347bbd386892f244b7203b41c2
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
Lars Knoll 2019-05-22 15:57:45 +02:00
parent 5df1cf38e3
commit 1299a2330a
6 changed files with 108 additions and 81 deletions

View File

@ -1999,7 +1999,7 @@ typedef QHash<QString, QVariant> QVariantHash;
#ifdef Q_CLANG_QDOC
class QByteArrayList;
#else
typedef QList<QByteArray> QByteArrayList;
typedef QVector<QByteArray> QByteArrayList;
#endif
#define Q_DECLARE_METATYPE_TEMPLATE_1ARG(SINGLE_ARG_TEMPLATE) \

View File

@ -39,7 +39,7 @@
**
****************************************************************************/
#include <QtCore/qlist.h>
#include <QtCore/qvector.h>
#ifndef QBYTEARRAYLIST_H
#define QBYTEARRAYLIST_H
@ -49,12 +49,12 @@
QT_BEGIN_NAMESPACE
#if !defined(QT_NO_JAVA_STYLE_ITERATORS)
typedef QListIterator<QByteArray> QByteArrayListIterator;
typedef QMutableListIterator<QByteArray> QMutableByteArrayListIterator;
typedef QVectorIterator<QByteArray> QByteArrayListIterator;
typedef QMutableVectorIterator<QByteArray> QMutableByteArrayListIterator;
#endif
#ifndef Q_CLANG_QDOC
typedef QList<QByteArray> QByteArrayList;
typedef QVector<QByteArray> QByteArrayList;
namespace QtPrivate {
QByteArray Q_CORE_EXPORT QByteArrayList_join(const QByteArrayList *that, const char *separator, int separatorLength);
@ -63,14 +63,14 @@ namespace QtPrivate {
#endif
#ifdef Q_CLANG_QDOC
class QByteArrayList : public QList<QByteArray>
class QByteArrayList : public QVector<QByteArray>
#else
template <> struct QListSpecialMethods<QByteArray>
template <> struct QVectorSpecialMethods<QByteArray>
#endif
{
#ifndef Q_CLANG_QDOC
protected:
~QListSpecialMethods() = default;
~QVectorSpecialMethods() = default;
#endif
public:
inline QByteArray join() const
@ -84,7 +84,7 @@ public:
{ return QtPrivate::QByteArrayList_indexOf(self(), needle, from); }
private:
typedef QList<QByteArray> Self;
typedef QVector<QByteArray> Self;
Self *self() { return static_cast<Self *>(this); }
const Self *self() const { return static_cast<const Self *>(this); }
};

View File

@ -38,7 +38,7 @@
**
****************************************************************************/
#include <QtCore/qlist.h>
#include <QtCore/qvector.h>
#ifndef QSTRINGLIST_H
#define QSTRINGLIST_H
@ -55,21 +55,21 @@ class QRegExp;
class QRegularExpression;
#if !defined(QT_NO_JAVA_STYLE_ITERATORS)
typedef QListIterator<QString> QStringListIterator;
typedef QMutableListIterator<QString> QMutableStringListIterator;
typedef QVectorIterator<QString> QStringListIterator;
typedef QMutableVectorIterator<QString> QMutableStringListIterator;
#endif
class QStringList;
#ifdef Q_QDOC
class QStringList : public QList<QString>
class QStringList : public QVector<QString>
#else
template <> struct QListSpecialMethods<QString>
template <> struct QVectorSpecialMethods<QString>
#endif
{
#ifndef Q_QDOC
protected:
~QListSpecialMethods() = default;
~QVectorSpecialMethods() = default;
#endif
public:
inline void sort(Qt::CaseSensitivity cs = Qt::CaseSensitive);
@ -108,23 +108,23 @@ private:
};
// ### Qt6: check if there's a better way
class QStringList : public QList<QString>
class QStringList : public QVector<QString>
{
#endif
public:
inline QStringList() noexcept { }
inline explicit QStringList(const QString &i) { append(i); }
inline QStringList(const QList<QString> &l) : QList<QString>(l) { }
inline QStringList(QList<QString> &&l) noexcept : QList<QString>(std::move(l)) { }
inline QStringList(std::initializer_list<QString> args) : QList<QString>(args) { }
inline QStringList(const QVector<QString> &l) : QVector<QString>(l) { }
inline QStringList(QVector<QString> &&l) noexcept : QVector<QString>(std::move(l)) { }
inline QStringList(std::initializer_list<QString> args) : QVector<QString>(args) { }
template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true>
inline QStringList(InputIterator first, InputIterator last)
: QList<QString>(first, last) { }
: QVector<QString>(first, last) { }
QStringList &operator=(const QList<QString> &other)
{ QList<QString>::operator=(other); return *this; }
QStringList &operator=(QList<QString> &&other) noexcept
{ QList<QString>::operator=(std::move(other)); return *this; }
QStringList &operator=(const QVector<QString> &other)
{ QVector<QString>::operator=(other); return *this; }
QStringList &operator=(QVector<QString> &&other) noexcept
{ QVector<QString>::operator=(std::move(other)); return *this; }
#if QT_STRINGVIEW_LEVEL < 2
inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
@ -138,7 +138,7 @@ public:
{ append(str); return *this; }
inline QStringList &operator<<(const QStringList &l)
{ *this += l; return *this; }
inline QStringList &operator<<(const QList<QString> &l)
inline QStringList &operator<<(const QVector<QString> &l)
{ *this += l; return *this; }
inline int indexOf(QStringView str, int from = 0) const;
@ -159,16 +159,16 @@ public:
inline int lastIndexOf(const QRegularExpression &re, int from = -1) const;
#endif // QT_CONFIG(regularexpression)
using QList<QString>::indexOf;
using QList<QString>::lastIndexOf;
using QVector<QString>::indexOf;
using QVector<QString>::lastIndexOf;
};
Q_DECLARE_TYPEINFO(QStringList, Q_MOVABLE_TYPE);
#ifndef Q_QDOC
inline QStringList *QListSpecialMethods<QString>::self()
inline QStringList *QVectorSpecialMethods<QString>::self()
{ return static_cast<QStringList *>(this); }
inline const QStringList *QListSpecialMethods<QString>::self() const
inline const QStringList *QVectorSpecialMethods<QString>::self() const
{ return static_cast<const QStringList *>(this); }
namespace QtPrivate {
@ -213,45 +213,45 @@ namespace QtPrivate {
#endif // QT_CONFIG(regularexpression)
}
inline void QListSpecialMethods<QString>::sort(Qt::CaseSensitivity cs)
inline void QVectorSpecialMethods<QString>::sort(Qt::CaseSensitivity cs)
{
QtPrivate::QStringList_sort(self(), cs);
}
inline int QListSpecialMethods<QString>::removeDuplicates()
inline int QVectorSpecialMethods<QString>::removeDuplicates()
{
return QtPrivate::QStringList_removeDuplicates(self());
}
#if QT_STRINGVIEW_LEVEL < 2
inline QString QListSpecialMethods<QString>::join(const QString &sep) const
inline QString QVectorSpecialMethods<QString>::join(const QString &sep) const
{
return QtPrivate::QStringList_join(self(), sep.constData(), sep.length());
}
#endif
inline QString QListSpecialMethods<QString>::join(QStringView sep) const
inline QString QVectorSpecialMethods<QString>::join(QStringView sep) const
{
return QtPrivate::QStringList_join(self(), sep);
}
QString QListSpecialMethods<QString>::join(QLatin1String sep) const
QString QVectorSpecialMethods<QString>::join(QLatin1String sep) const
{
return QtPrivate::QStringList_join(*self(), sep);
}
inline QString QListSpecialMethods<QString>::join(QChar sep) const
inline QString QVectorSpecialMethods<QString>::join(QChar sep) const
{
return QtPrivate::QStringList_join(self(), &sep, 1);
}
inline QStringList QListSpecialMethods<QString>::filter(QStringView str, Qt::CaseSensitivity cs) const
inline QStringList QVectorSpecialMethods<QString>::filter(QStringView str, Qt::CaseSensitivity cs) const
{
return QtPrivate::QStringList_filter(self(), str, cs);
}
#if QT_STRINGVIEW_LEVEL < 2
inline QStringList QListSpecialMethods<QString>::filter(const QString &str, Qt::CaseSensitivity cs) const
inline QStringList QVectorSpecialMethods<QString>::filter(const QString &str, Qt::CaseSensitivity cs) const
{
return QtPrivate::QStringList_filter(self(), str, cs);
}
@ -274,33 +274,33 @@ inline bool QStringList::contains(QStringView str, Qt::CaseSensitivity cs) const
return QtPrivate::QStringList_contains(this, str, cs);
}
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs)
inline QStringList &QVectorSpecialMethods<QString>::replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs)
{
QtPrivate::QStringList_replaceInStrings(self(), before, after, cs);
return *self();
}
#if QT_STRINGVIEW_LEVEL < 2
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
inline QStringList &QVectorSpecialMethods<QString>::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
{
QtPrivate::QStringList_replaceInStrings(self(), before, after, cs);
return *self();
}
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs)
inline QStringList &QVectorSpecialMethods<QString>::replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs)
{
QtPrivate::QStringList_replaceInStrings(self(), before, qToStringViewIgnoringNull(after), cs);
return *self();
}
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs)
inline QStringList &QVectorSpecialMethods<QString>::replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs)
{
QtPrivate::QStringList_replaceInStrings(self(), QStringView(before), after, cs);
return *self();
}
#endif
inline QStringList operator+(const QList<QString> &one, const QStringList &other)
inline QStringList operator+(const QVector<QString> &one, const QStringList &other)
{
QStringList n = one;
n += other;
@ -328,13 +328,13 @@ inline int QStringList::lastIndexOf(QLatin1String string, int from) const
}
#ifndef QT_NO_REGEXP
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QRegExp &rx, const QString &after)
inline QStringList &QVectorSpecialMethods<QString>::replaceInStrings(const QRegExp &rx, const QString &after)
{
QtPrivate::QStringList_replaceInStrings(self(), rx, after);
return *self();
}
inline QStringList QListSpecialMethods<QString>::filter(const QRegExp &rx) const
inline QStringList QVectorSpecialMethods<QString>::filter(const QRegExp &rx) const
{
return QtPrivate::QStringList_filter(self(), rx);
}
@ -361,13 +361,13 @@ inline int QStringList::lastIndexOf(QRegExp &rx, int from) const
#endif
#if QT_CONFIG(regularexpression)
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QRegularExpression &rx, const QString &after)
inline QStringList &QVectorSpecialMethods<QString>::replaceInStrings(const QRegularExpression &rx, const QString &after)
{
QtPrivate::QStringList_replaceInStrings(self(), rx, after);
return *self();
}
inline QStringList QListSpecialMethods<QString>::filter(const QRegularExpression &rx) const
inline QStringList QVectorSpecialMethods<QString>::filter(const QRegularExpression &rx) const
{
return QtPrivate::QStringList_filter(self(), rx);
}

View File

@ -71,14 +71,6 @@ QT_BEGIN_NAMESPACE
template <typename T> class QVector;
template <typename T> class QSet;
template <typename T> struct QListSpecialMethods
{
protected:
~QListSpecialMethods() = default;
};
template <> struct QListSpecialMethods<QByteArray>;
template <> struct QListSpecialMethods<QString>;
struct Q_CORE_EXPORT QListData {
// tags for tag-dispatching of QList implementations,
// based on QList's three different memory layouts:
@ -126,9 +118,6 @@ namespace QtPrivate {
template <typename T>
class QList
#ifndef Q_QDOC
: public QListSpecialMethods<T>
#endif
{
public:
struct MemoryLayout
@ -848,7 +837,7 @@ Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper()
template <typename T>
Q_OUTOFLINE_TEMPLATE QList<T>::QList(const QList<T> &l)
: QListSpecialMethods<T>(l), d(l.d)
: d(l.d)
{
if (!d->ref.ref()) {
p.detach(d->alloc);

View File

@ -59,12 +59,31 @@
QT_BEGIN_NAMESPACE
namespace QtPrivate {
template <typename V, typename U> int indexOf(const QVector<V> &list, const U &u, int from);
template <typename V, typename U> int lastIndexOf(const QVector<V> &list, const U &u, int from);
}
template <typename T> struct QVectorSpecialMethods
{
protected:
~QVectorSpecialMethods() = default;
};
template <> struct QVectorSpecialMethods<QByteArray>;
template <> struct QVectorSpecialMethods<QString>;
template <typename T>
class QVector
#ifndef Q_QDOC
: public QVectorSpecialMethods<T>
#endif
{
typedef QTypedArrayData<T> Data;
Data *d;
template <typename V, typename U> friend int QtPrivate::indexOf(const QVector<V> &list, const U &u, int from);
template <typename V, typename U> friend int QtPrivate::lastIndexOf(const QVector<V> &list, const U &u, int from);
public:
inline QVector() noexcept : d(Data::sharedNull()) { }
explicit QVector(int size);
@ -1004,37 +1023,51 @@ QVector<T> &QVector<T>::operator+=(const QVector &l)
return *this;
}
namespace QtPrivate {
template <typename T, typename U>
int indexOf(const QVector<T> &vector, const U &u, int from)
{
if (from < 0)
from = qMax(from + vector.size(), 0);
if (from < vector.size()) {
auto n = vector.begin() + from - 1;
auto e = vector.end();
while (++n != e)
if (*n == u)
return n - vector.begin();
}
return -1;
}
template <typename T, typename U>
int lastIndexOf(const QVector<T> &vector, const U &u, int from)
{
if (from < 0)
from += vector.d->size;
else if (from >= vector.size())
from = vector.size() - 1;
if (from >= 0) {
auto b = vector.begin();
auto n = vector.begin() + from + 1;
while (n != b) {
if (*--n == u)
return n - b;
}
}
return -1;
}
}
template <typename T>
int QVector<T>::indexOf(const T &t, int from) const
{
if (from < 0)
from = qMax(from + d->size, 0);
if (from < d->size) {
T* n = d->begin() + from - 1;
T* e = d->end();
while (++n != e)
if (*n == t)
return n - d->begin();
}
return -1;
return QtPrivate::indexOf<T, T>(*this, t, from);
}
template <typename T>
int QVector<T>::lastIndexOf(const T &t, int from) const
{
if (from < 0)
from += d->size;
else if (from >= d->size)
from = d->size-1;
if (from >= 0) {
T* b = d->begin();
T* n = d->begin() + from + 1;
while (n != b) {
if (*--n == t)
return n - b;
}
}
return -1;
return QtPrivate::lastIndexOf(*this, t, from);
}
template <typename T>
@ -1153,4 +1186,7 @@ QVector<QStringRef> QStringRef::split(QChar sep, Qt::SplitBehavior behavior, Qt:
QT_END_NAMESPACE
#include <QtCore/qbytearraylist.h>
#include <QtCore/qstringlist.h>
#endif // QVECTOR_H

View File

@ -46,6 +46,8 @@
#include <QtCore/qstringlist.h>
#include <QtGui/qrgba64.h>
#include <limits.h>
QT_BEGIN_NAMESPACE