From ee6b101adf18eaf6f2dea571787a04161d7426ed Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 28 Aug 2024 14:33:58 +0200 Subject: [PATCH] QTimer: port defaultTypeFor() helper from ms to ns resolution This doesn't change a thing for QTimer, but is a building block for making its singleShot() have nanosecond resolution, and allows QChronoTimer to reuse the function. Task-number: QTBUG-128426 Change-Id: I3d7e79972f236ca63b216a1a5a871edf5025e1b8 Reviewed-by: Ahmad Samir Reviewed-by: Thiago Macieira (cherry picked from commit 25b8d3743455bd2a4a327e1068fc83acb2f1c481) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qchronotimer.h | 13 ++++--------- src/corelib/kernel/qtimer.h | 3 ++- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/corelib/kernel/qchronotimer.h b/src/corelib/kernel/qchronotimer.h index 79c475e93ce..136edb06a2e 100644 --- a/src/corelib/kernel/qchronotimer.h +++ b/src/corelib/kernel/qchronotimer.h @@ -10,6 +10,7 @@ #include #include #include +#include #include @@ -68,7 +69,7 @@ public: static void singleShot(std::chrono::nanoseconds interval, const FunctorContext *receiver, Functor &&slot) { - singleShot(interval, defaultTimerTypeFor(interval), receiver, std::forward(slot)); + singleShot(interval, QTimer::defaultTypeFor(interval), receiver, std::forward(slot)); } template static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType, @@ -88,14 +89,14 @@ public: template static void singleShot(std::chrono::nanoseconds interval, Functor &&slot) { - singleShot(interval, defaultTimerTypeFor(interval), nullptr, std::forward(slot)); + singleShot(interval, QTimer::defaultTypeFor(interval), nullptr, std::forward(slot)); } static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType, const QObject *receiver, const char *member); static void singleShot(std::chrono::nanoseconds interval, const QObject *receiver, const char *member) - { singleShot(interval, defaultTimerTypeFor(interval), receiver, member); } + { singleShot(interval, QTimer::defaultTypeFor(interval), receiver, member); } #ifdef Q_QDOC template @@ -132,12 +133,6 @@ private: int startTimer(std::chrono::nanoseconds) = delete; void killTimer(int) = delete; - static constexpr Qt::TimerType defaultTimerTypeFor(std::chrono::nanoseconds interval) noexcept - { - using namespace std::chrono_literals; - return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer; - } - static void singleShotImpl(std::chrono::nanoseconds interval, Qt::TimerType timerType, const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj); }; diff --git a/src/corelib/kernel/qtimer.h b/src/corelib/kernel/qtimer.h index 854d9072f20..06e90ac52ef 100644 --- a/src/corelib/kernel/qtimer.h +++ b/src/corelib/kernel/qtimer.h @@ -142,6 +142,7 @@ protected: private: Q_DISABLE_COPY(QTimer) Q_DECLARE_PRIVATE(QTimer) + friend class QChronoTimer; inline int startTimer(int){ return -1;} inline void killTimer(int){} @@ -149,7 +150,7 @@ private: static constexpr Qt::TimerType defaultTypeFor(int msecs) noexcept { return defaultTypeFor(std::chrono::milliseconds{msecs}); } - static constexpr Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval) noexcept + static constexpr Qt::TimerType defaultTypeFor(std::chrono::nanoseconds interval) noexcept { // coarse timers are worst in their first firing // so we prefer a high precision timer for something that happens only once