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
\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.

View File

@ -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);
};

View File

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

View File

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