Skip to content

Commit

Permalink
Move QListSpecialMethods over to QVector
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
laknoll committed Oct 30, 2019
1 parent 5df1cf3 commit 1299a23
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/corelib/kernel/qmetatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
16 changes: 8 additions & 8 deletions src/corelib/text/qbytearraylist.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
**
****************************************************************************/

#include <QtCore/qlist.h>
#include <QtCore/qvector.h>

#ifndef QBYTEARRAYLIST_H
#define QBYTEARRAYLIST_H
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -84,7 +84,7 @@ template <> struct QListSpecialMethods<QByteArray>
{ 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); }
};
Expand Down
74 changes: 37 additions & 37 deletions src/corelib/text/qstringlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
**
****************************************************************************/

#include <QtCore/qlist.h>
#include <QtCore/qvector.h>

#ifndef QSTRINGLIST_H
#define QSTRINGLIST_H
Expand All @@ -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);
Expand Down Expand Up @@ -108,23 +108,23 @@ template <> struct QListSpecialMethods<QString>
};

// ### 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;
Expand All @@ -138,7 +138,7 @@ class QStringList : public QList<QString>
{ 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;
Expand All @@ -159,16 +159,16 @@ class QStringList : public QList<QString>
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 {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
13 changes: 1 addition & 12 deletions src/corelib/tools/qlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -126,9 +118,6 @@ namespace QtPrivate {

template <typename T>
class QList
#ifndef Q_QDOC
: public QListSpecialMethods<T>
#endif
{
public:
struct MemoryLayout
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 1299a23

Please sign in to comment.