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 <a.samirh78@gmail.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 25b8d3743455bd2a4a327e1068fc83acb2f1c481)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-08-28 14:33:58 +02:00 committed by Qt Cherry-pick Bot
parent 3b7f21996e
commit ee6b101adf
2 changed files with 6 additions and 10 deletions

View File

@ -10,6 +10,7 @@
#include <QtCore/qnamespace.h> #include <QtCore/qnamespace.h>
#include <QtCore/qobject.h> #include <QtCore/qobject.h>
#include <QtCore/qproperty.h> #include <QtCore/qproperty.h>
#include <QtCore/qtimer.h>
#include <chrono> #include <chrono>
@ -68,7 +69,7 @@ public:
static void singleShot(std::chrono::nanoseconds interval, static void singleShot(std::chrono::nanoseconds interval,
const FunctorContext<Functor> *receiver, Functor &&slot) const FunctorContext<Functor> *receiver, Functor &&slot)
{ {
singleShot(interval, defaultTimerTypeFor(interval), receiver, std::forward<Functor>(slot)); singleShot(interval, QTimer::defaultTypeFor(interval), receiver, std::forward<Functor>(slot));
} }
template <typename Functor> template <typename Functor>
static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType, static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType,
@ -88,14 +89,14 @@ public:
template <typename Functor> template <typename Functor>
static void singleShot(std::chrono::nanoseconds interval, Functor &&slot) static void singleShot(std::chrono::nanoseconds interval, Functor &&slot)
{ {
singleShot(interval, defaultTimerTypeFor(interval), nullptr, std::forward<Functor>(slot)); singleShot(interval, QTimer::defaultTypeFor(interval), nullptr, std::forward<Functor>(slot));
} }
static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType, static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType,
const QObject *receiver, const char *member); const QObject *receiver, const char *member);
static void singleShot(std::chrono::nanoseconds interval, const QObject *receiver, static void singleShot(std::chrono::nanoseconds interval, const QObject *receiver,
const char *member) const char *member)
{ singleShot(interval, defaultTimerTypeFor(interval), receiver, member); } { singleShot(interval, QTimer::defaultTypeFor(interval), receiver, member); }
#ifdef Q_QDOC #ifdef Q_QDOC
template <typename Functor> template <typename Functor>
@ -132,12 +133,6 @@ private:
int startTimer(std::chrono::nanoseconds) = delete; int startTimer(std::chrono::nanoseconds) = delete;
void killTimer(int) = 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, static void singleShotImpl(std::chrono::nanoseconds interval, Qt::TimerType timerType,
const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj); const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
}; };

View File

@ -142,6 +142,7 @@ protected:
private: private:
Q_DISABLE_COPY(QTimer) Q_DISABLE_COPY(QTimer)
Q_DECLARE_PRIVATE(QTimer) Q_DECLARE_PRIVATE(QTimer)
friend class QChronoTimer;
inline int startTimer(int){ return -1;} inline int startTimer(int){ return -1;}
inline void killTimer(int){} inline void killTimer(int){}
@ -149,7 +150,7 @@ private:
static constexpr Qt::TimerType defaultTypeFor(int msecs) noexcept static constexpr Qt::TimerType defaultTypeFor(int msecs) noexcept
{ return defaultTypeFor(std::chrono::milliseconds{msecs}); } { 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 // coarse timers are worst in their first firing
// so we prefer a high precision timer for something that happens only once // so we prefer a high precision timer for something that happens only once