QDeadlineTimer: use new comparison helper macros

Replace public friend operators operator==() and operator!=() of
QDeadlineTimer to friend method comparesEqual().
Replace public friends operator<(),<=(),>(), etc of QDeadlineTimer to
friend method compareThreeWay().

Task-number: QTBUG-120304
Change-Id: Ib855ccac9b31b54fe28b822f2985154608fefa27
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Tatiana Borisova 2024-03-28 14:57:21 +01:00
parent 2cd0bc729e
commit 7689127d83
5 changed files with 68 additions and 35 deletions

View File

@ -63,25 +63,25 @@
//! [7]
//! [8]
return d1.deadlineNSecs() == d2.deadlineNSecs();
return lhs.deadlineNSecs() == rhs.deadlineNSecs();
//! [8]
//! [9]
return d1.deadlineNSecs() != d2.deadlineNSecs();
return lhs.deadlineNSecs() != rhs.deadlineNSecs();
//! [9]
//! [10]
return d1.deadlineNSecs() < d2.deadlineNSecs();
return lhs.deadlineNSecs() < rhs.deadlineNSecs();
//! [10]
//! [11]
return d1.deadlineNSecs() <= d2.deadlineNSecs();
return lhs.deadlineNSecs() <= rhs.deadlineNSecs();
//! [11]
//! [12]
return d1.deadlineNSecs() > d2.deadlineNSecs();
return lhs.deadlineNSecs() > rhs.deadlineNSecs();
//! [12]
//! [13]
return d1.deadlineNSecs() >= d2.deadlineNSecs();
return lhs.deadlineNSecs() >= rhs.deadlineNSecs();
//! [13]

View File

@ -51,6 +51,8 @@ static qint64 add_saturate(qint64 t1, Duration1 dur, Durations... extra)
\reentrant
\ingroup tools
\compares strong
The QDeadlineTimer class is usually used to calculate future deadlines and
verify whether the deadline has expired. QDeadlineTimer can also be used
for deadlines without expiration ("forever"). It forms a counterpart to
@ -591,9 +593,9 @@ QDeadlineTimer QDeadlineTimer::current(Qt::TimerType timerType) noexcept
}
/*!
\fn bool QDeadlineTimer::operator==(QDeadlineTimer d1, QDeadlineTimer d2)
\fn bool QDeadlineTimer::operator==(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs)
Returns true if the deadline on \a d1 and the deadline in \a d2 are the
Returns true if the deadline on \a lhs and the deadline in \a rhs are the
same, false otherwise. The timer type used to create the two deadlines is
ignored. This function is equivalent to:
@ -604,9 +606,9 @@ QDeadlineTimer QDeadlineTimer::current(Qt::TimerType timerType) noexcept
*/
/*!
\fn bool QDeadlineTimer::operator!=(QDeadlineTimer d1, QDeadlineTimer d2)
\fn bool QDeadlineTimer::operator!=(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs)
Returns true if the deadline on \a d1 and the deadline in \a d2 are
Returns true if the deadline on \a lhs and the deadline in \a rhs are
different, false otherwise. The timer type used to create the two deadlines
is ignored. This function is equivalent to:
@ -617,10 +619,10 @@ QDeadlineTimer QDeadlineTimer::current(Qt::TimerType timerType) noexcept
*/
/*!
\fn bool QDeadlineTimer::operator<(QDeadlineTimer d1, QDeadlineTimer d2)
\fn bool QDeadlineTimer::operator<(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs)
Returns true if the deadline on \a d1 is earlier than the deadline in \a
d2, false otherwise. The timer type used to create the two deadlines is
Returns true if the deadline on \a lhs is earlier than the deadline in \a
rhs, false otherwise. The timer type used to create the two deadlines is
ignored. This function is equivalent to:
\snippet code/src_corelib_kernel_qdeadlinetimer.cpp 10
@ -630,10 +632,10 @@ QDeadlineTimer QDeadlineTimer::current(Qt::TimerType timerType) noexcept
*/
/*!
\fn bool QDeadlineTimer::operator<=(QDeadlineTimer d1, QDeadlineTimer d2)
\fn bool QDeadlineTimer::operator<=(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs)
Returns true if the deadline on \a d1 is earlier than or the same as the
deadline in \a d2, false otherwise. The timer type used to create the two
Returns true if the deadline on \a lhs is earlier than or the same as the
deadline in \a rhs, false otherwise. The timer type used to create the two
deadlines is ignored. This function is equivalent to:
\snippet code/src_corelib_kernel_qdeadlinetimer.cpp 11
@ -643,10 +645,10 @@ QDeadlineTimer QDeadlineTimer::current(Qt::TimerType timerType) noexcept
*/
/*!
\fn bool QDeadlineTimer::operator>(QDeadlineTimer d1, QDeadlineTimer d2)
\fn bool QDeadlineTimer::operator>(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs)
Returns true if the deadline on \a d1 is later than the deadline in \a
d2, false otherwise. The timer type used to create the two deadlines is
Returns true if the deadline on \a lhs is later than the deadline in \a
rhs, false otherwise. The timer type used to create the two deadlines is
ignored. This function is equivalent to:
\snippet code/src_corelib_kernel_qdeadlinetimer.cpp 12
@ -656,10 +658,10 @@ QDeadlineTimer QDeadlineTimer::current(Qt::TimerType timerType) noexcept
*/
/*!
\fn bool QDeadlineTimer::operator>=(QDeadlineTimer d1, QDeadlineTimer d2)
\fn bool QDeadlineTimer::operator>=(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs)
Returns true if the deadline on \a d1 is later than or the same as the
deadline in \a d2, false otherwise. The timer type used to create the two
Returns true if the deadline on \a lhs is later than or the same as the
deadline in \a rhs, false otherwise. The timer type used to create the two
deadlines is ignored. This function is equivalent to:
\snippet code/src_corelib_kernel_qdeadlinetimer.cpp 13

View File

@ -58,19 +58,6 @@ public:
static QDeadlineTimer addNSecs(QDeadlineTimer dt, qint64 nsecs) noexcept Q_DECL_PURE_FUNCTION;
static QDeadlineTimer current(Qt::TimerType timerType = Qt::CoarseTimer) noexcept;
friend bool operator==(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
{ return d1.t1 == d2.t1; }
friend bool operator!=(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
{ return !(d1 == d2); }
friend bool operator<(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
{ return d1.t1 < d2.t1; }
friend bool operator<=(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
{ return d1 == d2 || d1 < d2; }
friend bool operator>(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
{ return d2 < d1; }
friend bool operator>=(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
{ return !(d1 < d2); }
friend Q_CORE_EXPORT QDeadlineTimer operator+(QDeadlineTimer dt, qint64 msecs);
friend QDeadlineTimer operator+(qint64 msecs, QDeadlineTimer dt)
{ return dt + msecs; }
@ -138,6 +125,18 @@ public:
{ return dt = dt + value; }
private:
friend bool comparesEqual(const QDeadlineTimer &lhs,
const QDeadlineTimer &rhs) noexcept
{
return lhs.t1 == rhs.t1;
}
friend Qt::strong_ordering compareThreeWay(const QDeadlineTimer &lhs,
const QDeadlineTimer &rhs) noexcept
{
return Qt::compareThreeWay(lhs.t1, rhs.t1);
}
Q_DECLARE_STRONGLY_ORDERED(QDeadlineTimer)
qint64 t1 = 0;
#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
unsigned t2 = 0;

View File

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

View File

@ -6,6 +6,7 @@
#include <QtCore/QDeadlineTimer>
#include <QtCore/QElapsedTimer>
#include <QTest>
#include <QtTest/private/qcomparisontesthelper_p.h>
#include <QTimer>
#include <chrono>
@ -35,6 +36,7 @@ class tst_QDeadlineTimer : public QObject
Q_OBJECT
private Q_SLOTS:
void compareCompiles();
void basics();
void foreverness();
void current();
@ -47,6 +49,11 @@ private Q_SLOTS:
static constexpr auto timerType = Qt::PreciseTimer;
void tst_QDeadlineTimer::compareCompiles()
{
QTestPrivate::testAllComparisonOperatorsCompile<QDeadlineTimer>();
}
void tst_QDeadlineTimer::basics()
{
QDeadlineTimer deadline;
@ -65,6 +72,9 @@ void tst_QDeadlineTimer::basics()
QCOMPARE_LE(deadline, deadline);
QCOMPARE_GE(deadline, deadline);
QVERIFY(!(deadline > deadline));
QT_TEST_ALL_COMPARISON_OPS(deadline, QDeadlineTimer(timerType), Qt::strong_ordering::equal);
QT_TEST_ALL_COMPARISON_OPS(deadline, QDeadlineTimer(), Qt::strong_ordering::equal);
QT_TEST_ALL_COMPARISON_OPS(QDeadlineTimer(), QDeadlineTimer(), Qt::strong_ordering::equal);
// should have expired, but we may be running too early after boot
QTRY_VERIFY_WITH_TIMEOUT(deadline.hasExpired(), 100);
@ -167,6 +177,7 @@ void tst_QDeadlineTimer::foreverness()
QCOMPARE_LE(deadline, deadline);
QCOMPARE_GE(deadline, deadline);
QVERIFY(!(deadline > deadline));
QT_TEST_ALL_COMPARISON_OPS(deadline, deadline, Qt::strong_ordering::equal);
// adding to forever must still be forever
QDeadlineTimer deadline2 = deadline + 1;
@ -184,6 +195,7 @@ void tst_QDeadlineTimer::foreverness()
QCOMPARE_LE(deadline2, deadline);
QCOMPARE_GE(deadline2, deadline);
QVERIFY(!(deadline2 > deadline));
QT_TEST_ALL_COMPARISON_OPS(deadline2, deadline, Qt::strong_ordering::equal);
// subtracting from forever is *also* forever
deadline2 = deadline - 1;
@ -201,6 +213,7 @@ void tst_QDeadlineTimer::foreverness()
QCOMPARE_LE(deadline2, deadline);
QCOMPARE_GE(deadline2, deadline);
QVERIFY(!(deadline2 > deadline));
QT_TEST_ALL_COMPARISON_OPS(deadline2, deadline, Qt::strong_ordering::equal);
// compare and order against a default-constructed object
QDeadlineTimer expired;
@ -210,6 +223,7 @@ void tst_QDeadlineTimer::foreverness()
QVERIFY(!(deadline <= expired));
QCOMPARE_GE(deadline, expired);
QCOMPARE_GT(deadline, expired);
QT_TEST_EQUALITY_OPS(deadline, expired, false);
}
void tst_QDeadlineTimer::current()
@ -245,6 +259,7 @@ void tst_QDeadlineTimer::current()
QCOMPARE_LE(earlierDeadline, deadline);
QVERIFY(!(earlierDeadline >= deadline));
QVERIFY(!(earlierDeadline > deadline));
QT_TEST_ALL_COMPARISON_OPS(earlierDeadline, deadline, Qt::strong_ordering::less);
}
void tst_QDeadlineTimer::deadlines()
@ -323,6 +338,7 @@ void tst_QDeadlineTimer::deadlines()
QVERIFY(!(laterDeadline <= deadline));
QCOMPARE_GE(laterDeadline, deadline);
QCOMPARE_GT(laterDeadline, deadline);
QT_TEST_ALL_COMPARISON_OPS(laterDeadline, deadline, Qt::strong_ordering::greater);
// compare and order against a default-constructed object
QDeadlineTimer expired;
@ -332,9 +348,11 @@ void tst_QDeadlineTimer::deadlines()
QVERIFY(!(deadline <= expired));
QCOMPARE_GE(deadline, expired);
QCOMPARE_GT(deadline, expired);
QT_TEST_EQUALITY_OPS(deadline, expired, false);
// compare and order against a forever deadline
QDeadlineTimer forever_(QDeadlineTimer::Forever);
QT_TEST_EQUALITY_OPS(deadline, forever_, false);
QVERIFY(!(deadline == forever_));
QCOMPARE_NE(deadline, forever_);
QCOMPARE_LT(deadline, forever_);
@ -601,12 +619,14 @@ void tst_QDeadlineTimer::stdchrono()
QCOMPARE_LT(diff.count(), 3 * minResolution / 2);
QDeadlineTimer dt_after(steady_after, timerType);
QCOMPARE_LT(now, dt_after);
QT_TEST_ALL_COMPARISON_OPS(now, dt_after, Qt::strong_ordering::less);
diff = duration_cast<milliseconds>(steady_deadline - steady_before);
QCOMPARE_GT(diff.count(), minResolution / 2);
QCOMPARE_LT(diff.count(), 3 * minResolution / 2);
QDeadlineTimer dt_before(steady_before, timerType);
QCOMPARE_GT(now, dt_before);
QT_TEST_ALL_COMPARISON_OPS(now, dt_before, Qt::strong_ordering::greater);
}
{
auto diff = duration_cast<milliseconds>(system_after - system_deadline);
@ -614,12 +634,14 @@ void tst_QDeadlineTimer::stdchrono()
QCOMPARE_LT(diff.count(), 3 * minResolution / 2);
QDeadlineTimer dt_after(system_after, timerType);
QCOMPARE_LT(now, dt_after);
QT_TEST_ALL_COMPARISON_OPS(now, dt_after, Qt::strong_ordering::less);
diff = duration_cast<milliseconds>(system_deadline - system_before);
QCOMPARE_GT(diff.count(), minResolution / 2);
QCOMPARE_LT(diff.count(), 3 * minResolution / 2);
QDeadlineTimer dt_before(system_before, timerType);
QCOMPARE_GT(now, dt_before);
QT_TEST_ALL_COMPARISON_OPS(now, dt_before, Qt::strong_ordering::greater);
}
// make it regular
@ -654,6 +676,14 @@ void tst_QDeadlineTimer::stdchrono()
QCOMPARE_LT(deadline, 5000000ns * minResolution);
QCOMPARE_GE(deadline, steady_clock::now());
QCOMPARE_GE(deadline, system_clock::now());
QT_TEST_ALL_COMPARISON_OPS(deadline, now + 3ms * minResolution, Qt::strong_ordering::greater);
QT_TEST_ALL_COMPARISON_OPS(deadline, now + 5ms * minResolution, Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(deadline, now + 3000000ns * minResolution, Qt::strong_ordering::greater);
QT_TEST_ALL_COMPARISON_OPS(deadline, now + 5000000ns * minResolution, Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(deadline, 3ms * minResolution, Qt::strong_ordering::greater);
QT_TEST_ALL_COMPARISON_OPS(deadline, 5ms * minResolution, Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(deadline, steady_clock::now(), Qt::strong_ordering::greater);
QT_TEST_ALL_COMPARISON_OPS(deadline, system_clock::now(), Qt::strong_ordering::greater);
now = QDeadlineTimer::current(timerType);
deadline = QDeadlineTimer(1s, timerType);