QList: make (last)IndexOf and contains function templates
There's no reason why they shouldn't be; one might want to do a lookup passing an object which is comparable with the list's value type (e.g. search with a QByteArrayView needle into a QByteArrayList haystack). Insofar we've had to add overloads to QListSpecialMethods for all these cases, which clearly doesn't scale and creates top-tier and low-tier lists. There is one downside, namely, calling QList<A>::indexOf(B) for a B for which there isn't an operator==(A, B) but only a conversion towards A. Before, B was converted only once (at call site), now it's converted at every call of operator==. In general: such types are broken and should be fixed on their own. However let's avoid a possible regression in client code, so I've left the QString overloads in QStringList in. To get there: centralize the implementation of those methods in a empty base class, which gets then inherited by QListSpecialMethods, which is inherited by QList. This is there are still special methods that may want to overload contains, e.g. QStringList which also passes a case sensitivity). The only breakages comes from code that was already broken, for instance mixing signed and unsigned types, and the beauty of this is that now we *detect* that instead of silently ignoring. QVLA and other QList methods will be tackled in future commits. [ChangeLog][QtCore][QList] The indexOf, lastIndexOf and contains methods now take an object of any datatype -- and not just the list's own value type. This allows for heterogenous lookup in QLists. Change-Id: Ib34812217eb2b0f926fad1fc195b33758196941c Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
d53bbecf4c
commit
45cf0cf513
@ -63,7 +63,7 @@ namespace QtPrivate {
|
||||
#ifdef Q_CLANG_QDOC
|
||||
class QByteArrayList : public QList<QByteArray>
|
||||
#else
|
||||
template <> struct QListSpecialMethods<QByteArray>
|
||||
template <> struct QListSpecialMethods<QByteArray> : QListSpecialMethodsBase<QByteArray>
|
||||
#endif
|
||||
{
|
||||
#ifndef Q_CLANG_QDOC
|
||||
@ -71,12 +71,9 @@ protected:
|
||||
~QListSpecialMethods() = default;
|
||||
#endif
|
||||
public:
|
||||
qsizetype indexOf(const QByteArray &ba, qsizetype from = 0) const noexcept
|
||||
{ return QtPrivate::indexOf(*self(), ba, from); }
|
||||
qsizetype lastIndexOf(const QByteArray &ba, qsizetype from = -1) const noexcept
|
||||
{ return QtPrivate::lastIndexOf(*self(), ba, from); }
|
||||
bool contains(const QByteArray &ba) const noexcept
|
||||
{ return indexOf(ba) != -1; }
|
||||
using QListSpecialMethodsBase<QByteArray>::indexOf;
|
||||
using QListSpecialMethodsBase<QByteArray>::lastIndexOf;
|
||||
using QListSpecialMethodsBase<QByteArray>::contains;
|
||||
|
||||
inline QByteArray join() const
|
||||
{ return QtPrivate::QByteArrayList_join(self(), nullptr, 0); }
|
||||
@ -84,11 +81,6 @@ public:
|
||||
{ return QtPrivate::QByteArrayList_join(self(), sep.constData(), sep.size()); }
|
||||
inline QByteArray join(char sep) const
|
||||
{ return QtPrivate::QByteArrayList_join(self(), &sep, 1); }
|
||||
|
||||
private:
|
||||
typedef QList<QByteArray> Self;
|
||||
Self *self() { return static_cast<Self *>(this); }
|
||||
const Self *self() const { return static_cast<const Self *>(this); }
|
||||
};
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
@ -369,55 +369,6 @@ bool QtPrivate::QStringList_contains(const QStringList *that, QLatin1String str,
|
||||
return stringList_contains(*that, str, cs);
|
||||
}
|
||||
|
||||
/*!
|
||||
\fn bool QStringList::indexOf(QStringView str, qsizetype from) const
|
||||
\overload
|
||||
\since 5.13
|
||||
|
||||
Returns the index position of the first occurrence of \a str in
|
||||
the list, searching forward from index position \a from. Returns
|
||||
-1 if no item matched.
|
||||
|
||||
\sa lastIndexOf(), contains()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn bool QStringList::indexOf(QLatin1String str, qsizetype from) const
|
||||
\overload
|
||||
\since 5.13
|
||||
|
||||
Returns the index position of the first occurrence of \a str in
|
||||
the list, searching forward from index position \a from. Returns
|
||||
-1 if no item matched.
|
||||
|
||||
\sa lastIndexOf(), contains()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn bool QStringList::lastIndexOf(QStringView str, qsizetype from) const
|
||||
\overload
|
||||
\since 5.13
|
||||
|
||||
Returns the index position of the last occurrence of \a str in
|
||||
the list, searching backward from index position \a from. If \a
|
||||
from is -1 (the default), the search starts at the last item.
|
||||
Returns -1 if no item matched.
|
||||
|
||||
\sa indexOf(), contains()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn bool QStringList::lastIndexOf(QLatin1String str, qsizetype from) const
|
||||
\overload
|
||||
\since 5.13
|
||||
|
||||
Returns the index position of the last occurrence of \a str in
|
||||
the list, searching backward from index position \a from. If \a
|
||||
from is -1 (the default), the search starts at the last item.
|
||||
Returns -1 if no item matched.
|
||||
|
||||
\sa indexOf(), contains()
|
||||
*/
|
||||
|
||||
#if QT_CONFIG(regularexpression)
|
||||
/*!
|
||||
|
@ -82,7 +82,7 @@ namespace QtPrivate {
|
||||
#ifdef Q_QDOC
|
||||
class QStringList : public QList<QString>
|
||||
#else
|
||||
template <> struct QListSpecialMethods<QString>
|
||||
template <> struct QListSpecialMethods<QString> : QListSpecialMethodsBase<QString>
|
||||
#endif
|
||||
{
|
||||
#ifdef Q_QDOC
|
||||
@ -100,10 +100,6 @@ public:
|
||||
QStringList &operator<<(const QList<QString> &other);
|
||||
private:
|
||||
#endif
|
||||
inline QStringList *self()
|
||||
{ return static_cast<QStringList *>(this); }
|
||||
inline const QStringList *self() const
|
||||
{ return static_cast<const QStringList *>(this); }
|
||||
|
||||
public:
|
||||
inline void sort(Qt::CaseSensitivity cs = Qt::CaseSensitive)
|
||||
@ -147,22 +143,15 @@ public:
|
||||
return *self();
|
||||
}
|
||||
#endif
|
||||
using QListSpecialMethodsBase<QString>::contains;
|
||||
using QListSpecialMethodsBase<QString>::indexOf;
|
||||
using QListSpecialMethodsBase<QString>::lastIndexOf;
|
||||
|
||||
inline bool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
|
||||
{ return QtPrivate::QStringList_contains(self(), str, cs); }
|
||||
inline bool contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
|
||||
{ return QtPrivate::QStringList_contains(self(), str, cs); }
|
||||
|
||||
inline qsizetype indexOf(QStringView str, qsizetype from = 0) const noexcept
|
||||
{ return QtPrivate::indexOf<QString, QStringView>(*self(), str, from); }
|
||||
inline qsizetype indexOf(QLatin1String str, qsizetype from = 0) const noexcept
|
||||
{ return QtPrivate::indexOf<QString, QLatin1String>(*self(), str, from); }
|
||||
|
||||
inline qsizetype lastIndexOf(QStringView str, qsizetype from = -1) const noexcept
|
||||
{ return QtPrivate::lastIndexOf<QString, QStringView>(*self(), str, from); }
|
||||
inline qsizetype lastIndexOf(QLatin1String str, qsizetype from = -1) const noexcept
|
||||
{ return QtPrivate::lastIndexOf<QString, QLatin1String>(*self(), str, from); }
|
||||
|
||||
#if QT_STRINGVIEW_LEVEL < 2
|
||||
inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
|
||||
{ return QtPrivate::QStringList_contains(self(), str, cs); }
|
||||
|
@ -58,18 +58,35 @@ namespace QtPrivate {
|
||||
template <typename V, typename U> qsizetype lastIndexOf(const QList<V> &list, const U &u, qsizetype from) noexcept;
|
||||
}
|
||||
|
||||
template <typename T> struct QListSpecialMethods
|
||||
template <typename T> struct QListSpecialMethodsBase
|
||||
{
|
||||
protected:
|
||||
~QListSpecialMethodsBase() = default;
|
||||
|
||||
using Self = QList<T>;
|
||||
Self *self() { return static_cast<Self *>(this); }
|
||||
const Self *self() const { return static_cast<const Self *>(this); }
|
||||
|
||||
public:
|
||||
template <typename AT>
|
||||
qsizetype indexOf(const AT &t, qsizetype from = 0) const noexcept;
|
||||
template <typename AT>
|
||||
qsizetype lastIndexOf(const AT &t, qsizetype from = -1) const noexcept;
|
||||
|
||||
template <typename AT>
|
||||
bool contains(const AT &t) const noexcept
|
||||
{
|
||||
return self()->indexOf(t) != -1;
|
||||
}
|
||||
};
|
||||
template <typename T> struct QListSpecialMethods : QListSpecialMethodsBase<T>
|
||||
{
|
||||
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;
|
||||
}
|
||||
using QListSpecialMethodsBase<T>::indexOf;
|
||||
using QListSpecialMethodsBase<T>::lastIndexOf;
|
||||
using QListSpecialMethodsBase<T>::contains;
|
||||
};
|
||||
template <> struct QListSpecialMethods<QByteArray>;
|
||||
template <> struct QListSpecialMethods<QString>;
|
||||
@ -327,12 +344,16 @@ public:
|
||||
using QListSpecialMethods<T>::indexOf;
|
||||
using QListSpecialMethods<T>::lastIndexOf;
|
||||
#else
|
||||
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;
|
||||
template <typename AT>
|
||||
qsizetype indexOf(const AT &t, qsizetype from = 0) const noexcept;
|
||||
template <typename AT>
|
||||
qsizetype lastIndexOf(const AT &t, qsizetype from = -1) const noexcept;
|
||||
template <typename AT>
|
||||
bool contains(const AT &t) const noexcept;
|
||||
#endif
|
||||
|
||||
qsizetype count(parameter_type t) const noexcept
|
||||
template <typename AT>
|
||||
qsizetype count(const AT &t) const noexcept
|
||||
{
|
||||
return qsizetype(std::count(&*cbegin(), &*cend(), t));
|
||||
}
|
||||
@ -790,15 +811,17 @@ qsizetype lastIndexOf(const QList<T> &vector, const U &u, qsizetype from) noexce
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
qsizetype QListSpecialMethods<T>::indexOf(const T &t, qsizetype from) const noexcept
|
||||
template <typename AT>
|
||||
qsizetype QListSpecialMethodsBase<T>::indexOf(const AT &t, qsizetype from) const noexcept
|
||||
{
|
||||
return QtPrivate::indexOf(*static_cast<const QList<T> *>(this), t, from);
|
||||
return QtPrivate::indexOf(*self(), t, from);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
qsizetype QListSpecialMethods<T>::lastIndexOf(const T &t, qsizetype from) const noexcept
|
||||
template <typename AT>
|
||||
qsizetype QListSpecialMethodsBase<T>::lastIndexOf(const AT &t, qsizetype from) const noexcept
|
||||
{
|
||||
return QtPrivate::lastIndexOf(*static_cast<const QList<T> *>(this), t, from);
|
||||
return QtPrivate::lastIndexOf(*self(), t, from);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
@ -952,7 +952,7 @@
|
||||
\sa resize()
|
||||
*/
|
||||
|
||||
/*! \fn template <typename T> qsizetype QList<T>::indexOf(const T &value, qsizetype from = 0) const
|
||||
/*! \fn template <typename T> template <typename AT> qsizetype QList<T>::indexOf(const AT &value, qsizetype from = 0) const
|
||||
|
||||
Returns the index position of the first occurrence of \a value in
|
||||
the list, searching forward from index position \a from.
|
||||
@ -967,7 +967,7 @@
|
||||
\sa lastIndexOf(), contains()
|
||||
*/
|
||||
|
||||
/*! \fn template <typename T> qsizetype QList<T>::lastIndexOf(const T &value, qsizetype from = -1) const
|
||||
/*! \fn template <typename T> template <typename AT> qsizetype QList<T>::lastIndexOf(const AT &value, qsizetype from = -1) const
|
||||
|
||||
Returns the index position of the last occurrence of the value \a
|
||||
value in the list, searching backward from index position \a
|
||||
@ -983,7 +983,7 @@
|
||||
\sa indexOf()
|
||||
*/
|
||||
|
||||
/*! \fn template <typename T> bool QList<T>::contains(const T &value) const
|
||||
/*! \fn template <typename T> template <typename AT> bool QList<T>::contains(const AT &value) const
|
||||
|
||||
Returns \c true if the list contains an occurrence of \a value;
|
||||
otherwise returns \c false.
|
||||
|
Loading…
x
Reference in New Issue
Block a user