diff options
-rw-r--r-- | src/corelib/tools/qlist.cpp | 48 | ||||
-rw-r--r-- | src/corelib/tools/qlist.h | 30 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qlist/tst_qlist.cpp | 33 |
3 files changed, 111 insertions, 0 deletions
diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index db00dcb458..ef47ec1c05 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -584,6 +584,54 @@ void **QListData::erase(void **xi) \sa operator==() */ +/*! \fn bool operator<(const QList<T> &lhs, const QList<T> &rhs) + \since 5.6 + \relates QList + + Returns \c true if list \a lhs is + \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} + {lexicographically less than} \a rhs; otherwise returns \c false. + + This function requires the value type to have an implementation + of \c operator<(). +*/ + +/*! \fn bool operator<=(const QList<T> &lhs, const QList<T> &rhs) + \since 5.6 + \relates QList + + Returns \c true if list \a lhs is + \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} + {lexicographically less than or equal to} \a rhs; otherwise returns \c false. + + This function requires the value type to have an implementation + of \c operator<(). +*/ + +/*! \fn bool operator>(const QList<T> &lhs, const QList<T> &rhs) + \since 5.6 + \relates QList + + Returns \c true if list \a lhs is + \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} + {lexicographically greater than} \a rhs; otherwise returns \c false. + + This function requires the value type to have an implementation + of \c operator<(). +*/ + +/*! \fn bool operator>=(const QList<T> &lhs, const QList<T> &rhs) + \since 5.6 + \relates QList + + Returns \c true if list \a lhs is + \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} + {lexicographically greater than or equal to} \a rhs; otherwise returns \c false. + + This function requires the value type to have an implementation + of \c operator<(). +*/ + /*! \fn int QList::size() const diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 57e67d52d7..7d90f72876 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -1015,6 +1015,36 @@ inline int QList<T>::count_impl(const T &t, QListData::ArrayCompatibleLayout) co Q_DECLARE_SEQUENTIAL_ITERATOR(List) Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List) +template <typename T> +bool operator<(const QList<T> &lhs, const QList<T> &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(), + rhs.begin(), rhs.end()))) +{ + return std::lexicographical_compare(lhs.begin(), lhs.end(), + rhs.begin(), rhs.end()); +} + +template <typename T> +inline bool operator>(const QList<T> &lhs, const QList<T> &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return rhs < lhs; +} + +template <typename T> +inline bool operator<=(const QList<T> &lhs, const QList<T> &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return !(lhs > rhs); +} + +template <typename T> +inline bool operator>=(const QList<T> &lhs, const QList<T> &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return !(lhs < rhs); +} + QT_END_NAMESPACE #include <QtCore/qbytearraylist.h> diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index 1207986dde..6ec3064564 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -76,6 +76,13 @@ struct Movable { return i == other.i; } + bool operator<(const Movable &other) const + { + check(state, Constructed); + check(other.state, Constructed); + return i < other.i; + } + Movable &operator=(const Movable &other) { check(state, Constructed); @@ -144,6 +151,13 @@ struct Optimal return i == other.i; } + bool operator<(const Optimal &other) const + { + check(state, Constructed); + check(other.state, Constructed); + return i < other.i; + } + Optimal &operator=(const Optimal &other) { check(state, Constructed); @@ -220,6 +234,12 @@ struct Complex return value == other.value; } + bool operator<(Complex const &other) const + { + check(); other.check(); + return value < other.value; + } + void check() const { QVERIFY(this == checkSum); @@ -1576,6 +1596,19 @@ void tst_QList::testOperators() const // [] QCOMPARE(list[0], T_FOO); QCOMPARE(list[list.size() - 1], T_CAT); + + // <, >, <=, >= + QVERIFY(!(list < listtwo)); + QVERIFY(!(list > listtwo)); + QVERIFY( list <= listtwo); + QVERIFY( list >= listtwo); + listtwo.push_back(T_CAT); + QVERIFY( list < listtwo); + QVERIFY(!(list > listtwo)); + QVERIFY( list <= listtwo); + QVERIFY(!(list >= listtwo)); + QVERIFY(listtwo > list); + QVERIFY(listtwo >= list); } void tst_QList::testOperatorsOptimal() const |