Skip to content

Commit

Permalink
QTimer: make both defaultTypeFor() methods constexpr and noexcept
Browse files Browse the repository at this point in the history
Also de-duplicate the code by using defaultTypeFor() in singleShot(),
this way the comment and the calculation are done in one central place.

Pick-to: 6.7
Change-Id: Ib822cae0e9228e546b664fbac728a60d65c4bbc3
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
  • Loading branch information
ahmadsamir committed Feb 17, 2024
1 parent 1461fbb commit adc0920
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/corelib/kernel/qtimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions src/corelib/kernel/qtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,20 @@ public Q_SLOTS:
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)
{
Expand Down

0 comments on commit adc0920

Please sign in to comment.