diff options
| author | Tatiana Borisova <tatiana.borisova@qt.io> | 2024-05-08 18:20:36 +0200 |
|---|---|---|
| committer | Tatiana Borisova <tatiana.borisova@qt.io> | 2024-05-14 00:32:34 +0200 |
| commit | 199e1a10918732f1928e2a654a33c50200e7fcdb (patch) | |
| tree | e4ef2a949b31e6c3870f9dbb2406365871a7830e /src | |
| parent | 4585cacaa9f9329a10aaf13a449151f5e9bc7a2c (diff) | |
QElapsedTimer: use new comparison helper macros
Replace public friend operators operator==(), operator!=(),
of QElapsedTimer to the friend method comparesEqual().
Add compareThreeWay() for the <=> operator.
Save friend bool Q_CORE_EXPORT operator<() method and
add defined(__cpp_lib_three_way_comparison) condition for the C++20
spaceship operator.
Task-number: QTBUG-120304
Change-Id: I575865403f4e333578ff174e8e6879e8925d4b09
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'src')
| -rw-r--r-- | src/corelib/kernel/qelapsedtimer.cpp | 5 | ||||
| -rw-r--r-- | src/corelib/kernel/qelapsedtimer.h | 39 |
2 files changed, 36 insertions, 8 deletions
diff --git a/src/corelib/kernel/qelapsedtimer.cpp b/src/corelib/kernel/qelapsedtimer.cpp index 511b81a04e2..c4308a0b8f7 100644 --- a/src/corelib/kernel/qelapsedtimer.cpp +++ b/src/corelib/kernel/qelapsedtimer.cpp @@ -14,6 +14,8 @@ QT_BEGIN_NAMESPACE \reentrant \ingroup tools + \compares strong + The QElapsedTimer class is usually used to quickly calculate how much time has elapsed between two events. Its API is similar to that of QTime, so code that was using that can be ported quickly to the new class. @@ -155,8 +157,7 @@ QT_BEGIN_NAMESPACE Returns \c true if \a lhs and \a rhs contain different times, false otherwise. */ /*! - \fn bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept - \relates QElapsedTimer + \fn bool QElapsedTimer::operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept Returns \c true if \a lhs was started before \a rhs, false otherwise. diff --git a/src/corelib/kernel/qelapsedtimer.h b/src/corelib/kernel/qelapsedtimer.h index 7d8b889f613..e71573456d4 100644 --- a/src/corelib/kernel/qelapsedtimer.h +++ b/src/corelib/kernel/qelapsedtimer.h @@ -4,6 +4,7 @@ #ifndef QELAPSEDTIMER_H #define QELAPSEDTIMER_H +#include <QtCore/qcompare.h> #include <QtCore/qglobal.h> #include <chrono> @@ -45,15 +46,41 @@ public: Duration durationTo(const QElapsedTimer &other) const noexcept; qint64 msecsTo(const QElapsedTimer &other) const noexcept; qint64 secsTo(const QElapsedTimer &other) const noexcept; - - friend bool operator==(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept - { return lhs.t1 == rhs.t1 && lhs.t2 == rhs.t2; } - friend bool operator!=(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept - { return !(lhs == rhs); } - friend bool Q_CORE_EXPORT operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept; private: + friend bool comparesEqual(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept + { + return lhs.t1 == rhs.t1 && lhs.t2 == rhs.t2; + } + Q_DECLARE_EQUALITY_COMPARABLE(QElapsedTimer) + + friend Qt::strong_ordering compareThreeWay(const QElapsedTimer &lhs, + const QElapsedTimer &rhs) noexcept + { + return Qt::compareThreeWay(lhs.t1, rhs.t1); + } + +#if defined(__cpp_lib_three_way_comparison) + friend std::strong_ordering + operator<=>(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept + { + return compareThreeWay(lhs, rhs); + } +#else + friend bool operator>(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept + { + return is_gt(compareThreeWay(lhs, rhs)); + } + friend bool operator<=(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept + { + return is_lteq(compareThreeWay(lhs, rhs)); + } + friend bool operator>=(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept + { + return is_gteq(compareThreeWay(lhs, rhs)); + } +#endif // defined(__cpp_lib_three_way_comparison) qint64 t1 = Q_INT64_C(0x8000000000000000); qint64 t2 = Q_INT64_C(0x8000000000000000); }; |
