From 27291b97b481d0b652c44ee0fae4989791477898 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Fri, 16 Feb 2024 15:34:18 +0200 Subject: [PATCH] QTimer: make both defaultTypeFor() methods constexpr and noexcept Also de-duplicate the code by using defaultTypeFor() in singleShot(), this way the comment and the calculation are done in one central place. Change-Id: Ib822cae0e9228e546b664fbac728a60d65c4bbc3 Reviewed-by: Marc Mutz Reviewed-by: Thiago Macieira (cherry picked from commit adc0920c48d812fb1b3e2b7bfc76217a7126a41e) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qtimer.cpp | 5 +---- src/corelib/kernel/qtimer.h | 15 +++++++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/corelib/kernel/qtimer.cpp b/src/corelib/kernel/qtimer.cpp index 04e32d9ec72..f93293f2832 100644 --- a/src/corelib/kernel/qtimer.cpp +++ b/src/corelib/kernel/qtimer.cpp @@ -329,10 +329,7 @@ void QTimer::singleShotImpl(int msec, Qt::TimerType timerType, void QTimer::singleShot(int msec, const QObject *receiver, const char *member) { - // coarse timers are worst in their first firing - // so we prefer a high precision timer for something that happens only once - // unless the timeout is too big, in which case we go for coarse anyway - singleShot(msec, msec >= 2000 ? Qt::CoarseTimer : Qt::PreciseTimer, receiver, member); + singleShot(msec, defaultTypeFor(msec), receiver, member); } /*! \overload diff --git a/src/corelib/kernel/qtimer.h b/src/corelib/kernel/qtimer.h index 254e4cd9938..80802a4cc55 100644 --- a/src/corelib/kernel/qtimer.h +++ b/src/corelib/kernel/qtimer.h @@ -152,13 +152,20 @@ private: inline void killTimer(int){} static constexpr Qt::TimerType defaultTypeFor(int msecs) noexcept - { return msecs >= 2000 ? Qt::CoarseTimer : Qt::PreciseTimer; } + { return defaultTypeFor(std::chrono::milliseconds{msecs}); } + + static constexpr Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval) noexcept + { + // coarse timers are worst in their first firing + // so we prefer a high precision timer for something that happens only once + // unless the timeout is too big, in which case we go for coarse anyway + using namespace std::chrono_literals; + return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer; + } + static void singleShotImpl(int msec, Qt::TimerType timerType, const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj); - static Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval) - { return defaultTypeFor(int(interval.count())); } - static void singleShotImpl(std::chrono::milliseconds interval, Qt::TimerType timerType, const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj) {