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>
This commit is contained in:
Tatiana Borisova 2024-05-08 18:20:36 +02:00
parent 4585cacaa9
commit 199e1a1091
4 changed files with 55 additions and 11 deletions

View File

@ -14,6 +14,8 @@ QT_BEGIN_NAMESPACE
\reentrant \reentrant
\ingroup tools \ingroup tools
\compares strong
The QElapsedTimer class is usually used to quickly calculate how much 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, 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. 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. Returns \c true if \a lhs and \a rhs contain different times, false otherwise.
*/ */
/*! /*!
\fn bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept \fn bool QElapsedTimer::operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
\relates QElapsedTimer
Returns \c true if \a lhs was started before \a rhs, false otherwise. Returns \c true if \a lhs was started before \a rhs, false otherwise.

View File

@ -4,6 +4,7 @@
#ifndef QELAPSEDTIMER_H #ifndef QELAPSEDTIMER_H
#define QELAPSEDTIMER_H #define QELAPSEDTIMER_H
#include <QtCore/qcompare.h>
#include <QtCore/qglobal.h> #include <QtCore/qglobal.h>
#include <chrono> #include <chrono>
@ -45,15 +46,41 @@ public:
Duration durationTo(const QElapsedTimer &other) const noexcept; Duration durationTo(const QElapsedTimer &other) const noexcept;
qint64 msecsTo(const QElapsedTimer &other) const noexcept; qint64 msecsTo(const QElapsedTimer &other) const noexcept;
qint64 secsTo(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; friend bool Q_CORE_EXPORT operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept;
private: 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 t1 = Q_INT64_C(0x8000000000000000);
qint64 t2 = Q_INT64_C(0x8000000000000000); qint64 t2 = Q_INT64_C(0x8000000000000000);
}; };

View File

@ -14,4 +14,6 @@ endif()
qt_internal_add_test(tst_qelapsedtimer qt_internal_add_test(tst_qelapsedtimer
SOURCES SOURCES
tst_qelapsedtimer.cpp tst_qelapsedtimer.cpp
LIBRARIES
Qt::TestPrivate
) )

View File

@ -5,6 +5,7 @@
#include <QtCore/QString> #include <QtCore/QString>
#include <QtCore/QElapsedTimer> #include <QtCore/QElapsedTimer>
#include <QTest> #include <QTest>
#include <QtTest/private/qcomparisontesthelper_p.h>
#include <QTimer> #include <QTimer>
static const int minResolution = 100; // the minimum resolution for the tests static const int minResolution = 100; // the minimum resolution for the tests
@ -22,6 +23,7 @@ class tst_QElapsedTimer : public QObject
Q_OBJECT Q_OBJECT
private Q_SLOTS: private Q_SLOTS:
void compareCompiles();
void statics(); void statics();
void validity(); void validity();
void basics(); void basics();
@ -29,6 +31,11 @@ private Q_SLOTS:
void msecsTo(); void msecsTo();
}; };
void tst_QElapsedTimer::compareCompiles()
{
QTestPrivate::testAllComparisonOperatorsCompile<QElapsedTimer>();
}
void tst_QElapsedTimer::statics() void tst_QElapsedTimer::statics()
{ {
// these have been required since Qt 6.6 // these have been required since Qt 6.6
@ -77,6 +84,7 @@ void tst_QElapsedTimer::basics()
QVERIFY(!(t1 < t1)); QVERIFY(!(t1 < t1));
QCOMPARE(t1.msecsTo(t1), qint64(0)); QCOMPARE(t1.msecsTo(t1), qint64(0));
QCOMPARE(t1.secsTo(t1), qint64(0)); QCOMPARE(t1.secsTo(t1), qint64(0));
QT_TEST_ALL_COMPARISON_OPS(t1, t1, Qt::strong_ordering::equal);
quint64 value1 = t1.msecsSinceReference(); quint64 value1 = t1.msecsSinceReference();
qDebug() << "value1:" << value1 << "t1:" << t1; qDebug() << "value1:" << value1 << "t1:" << t1;
@ -141,10 +149,16 @@ void tst_QElapsedTimer::msecsTo()
QTest::qSleep(minResolution); QTest::qSleep(minResolution);
QElapsedTimer t2; QElapsedTimer t2;
t2.start(); t2.start();
QTest::qSleep(minResolution);
QElapsedTimer t3;
t3.start();
QVERIFY(t1 != t2); QT_TEST_EQUALITY_OPS(t1, t2, false);
QVERIFY(!(t1 == t2)); QT_TEST_EQUALITY_OPS(QElapsedTimer(), QElapsedTimer(), true);
QVERIFY(t1 < t2); QT_TEST_EQUALITY_OPS(QElapsedTimer(), t2, false);
QT_TEST_ALL_COMPARISON_OPS(t1, t2, Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(t3, t2, Qt::strong_ordering::greater);
QT_TEST_ALL_COMPARISON_OPS(t3, QElapsedTimer(), Qt::strong_ordering::greater);
auto diff = t1.msecsTo(t2); auto diff = t1.msecsTo(t2);
QVERIFY2(diff > 0, QString("difference t1 and t2 is %1").arg(diff).toLatin1()); QVERIFY2(diff > 0, QString("difference t1 and t2 is %1").arg(diff).toLatin1());