From f8da484d5741d1191504eefddf5134fd7009e16a Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sun, 18 Feb 2024 20:31:33 -0800 Subject: [PATCH] QEventDispatcher*: port the Unix dispatchers to V2 They're all ported in one go because all the changes are the same and they all rely on QTimerInfoList. The changes are: - use Qt::TimerId to uniquely identify timer IDs - use Duration (nanoseconds) to specify the timer interval - rename registeredTimers() to timersForObject(), which is const Change-Id: I83dda2d36c904517b3c0fffd17b52958767d8a68 Reviewed-by: Ahmad Samir --- src/corelib/kernel/qeventdispatcher_cf.mm | 26 ++++++++------ src/corelib/kernel/qeventdispatcher_cf_p.h | 14 ++++---- src/corelib/kernel/qeventdispatcher_glib.cpp | 36 ++++++++++--------- src/corelib/kernel/qeventdispatcher_glib_p.h | 14 ++++---- src/corelib/kernel/qeventdispatcher_unix.cpp | 30 ++++++++-------- src/corelib/kernel/qeventdispatcher_unix_p.h | 14 ++++---- src/corelib/kernel/qeventdispatcher_wasm.cpp | 23 ++++++------ src/corelib/kernel/qeventdispatcher_wasm_p.h | 13 +++---- src/corelib/kernel/qtimerinfo_unix.cpp | 34 +++++++----------- src/corelib/kernel/qtimerinfo_unix_p.h | 23 ++++++------ .../platforms/cocoa/qcocoaeventdispatcher.h | 14 ++++---- .../platforms/cocoa/qcocoaeventdispatcher.mm | 27 +++++++------- 12 files changed, 132 insertions(+), 136 deletions(-) diff --git a/src/corelib/kernel/qeventdispatcher_cf.mm b/src/corelib/kernel/qeventdispatcher_cf.mm index 16c5464f10..042b0651f4 100644 --- a/src/corelib/kernel/qeventdispatcher_cf.mm +++ b/src/corelib/kernel/qeventdispatcher_cf.mm @@ -169,7 +169,7 @@ static const CFTimeInterval kCFTimeIntervalDistantFuture = std::numeric_limits QEventDispatcherCoreFoundation::registeredTimers(QObject *object) const +QList +QEventDispatcherCoreFoundation::timersForObject(QObject *object) const { Q_ASSERT(object); return m_timerInfoList.registeredTimers(object); } -int QEventDispatcherCoreFoundation::remainingTime(int timerId) +QEventDispatcherCoreFoundation::Duration +QEventDispatcherCoreFoundation::remainingTime(Qt::TimerId timerId) const { - Q_ASSERT(timerId > 0); - return m_timerInfoList.timerRemainingTime(timerId); + Q_ASSERT(qToUnderlying(timerId) > 0); + return m_timerInfoList.remainingDuration(timerId); } void QEventDispatcherCoreFoundation::updateTimers() diff --git a/src/corelib/kernel/qeventdispatcher_cf_p.h b/src/corelib/kernel/qeventdispatcher_cf_p.h index e778b4916f..3575a6fb39 100644 --- a/src/corelib/kernel/qeventdispatcher_cf_p.h +++ b/src/corelib/kernel/qeventdispatcher_cf_p.h @@ -168,7 +168,7 @@ private: CFRunLoopObserverRef m_observer; }; -class Q_CORE_EXPORT QEventDispatcherCoreFoundation : public QAbstractEventDispatcher +class Q_CORE_EXPORT QEventDispatcherCoreFoundation : public QAbstractEventDispatcherV2 { Q_OBJECT @@ -182,12 +182,12 @@ public: void registerSocketNotifier(QSocketNotifier *notifier) override; void unregisterSocketNotifier(QSocketNotifier *notifier) override; - void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) override; - bool unregisterTimer(int timerId) override; - bool unregisterTimers(QObject *object) override; - QList registeredTimers(QObject *object) const override; - - int remainingTime(int timerId) override; + void registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, + QObject *object) override final; + bool unregisterTimer(Qt::TimerId timerId) override final; + bool unregisterTimers(QObject *object) override final; + QList timersForObject(QObject *object) const override final; + Duration remainingTime(Qt::TimerId timerId) const override final; void wakeUp() override; void interrupt() override; diff --git a/src/corelib/kernel/qeventdispatcher_glib.cpp b/src/corelib/kernel/qeventdispatcher_glib.cpp index ba3a1ea988..1e906c4b27 100644 --- a/src/corelib/kernel/qeventdispatcher_glib.cpp +++ b/src/corelib/kernel/qeventdispatcher_glib.cpp @@ -327,12 +327,12 @@ void QEventDispatcherGlibPrivate::runTimersOnceWithNormalPriority() } QEventDispatcherGlib::QEventDispatcherGlib(QObject *parent) - : QAbstractEventDispatcher(*(new QEventDispatcherGlibPrivate), parent) + : QAbstractEventDispatcherV2(*(new QEventDispatcherGlibPrivate), parent) { } QEventDispatcherGlib::QEventDispatcherGlib(GMainContext *mainContext, QObject *parent) - : QAbstractEventDispatcher(*(new QEventDispatcherGlibPrivate(mainContext)), parent) + : QAbstractEventDispatcherV2(*(new QEventDispatcherGlibPrivate(mainContext)), parent) { } QEventDispatcherGlib::~QEventDispatcherGlib() @@ -477,10 +477,11 @@ void QEventDispatcherGlib::unregisterSocketNotifier(QSocketNotifier *notifier) } } -void QEventDispatcherGlib::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) +void QEventDispatcherGlib::registerTimer(Qt::TimerId timerId, Duration interval, + Qt::TimerType timerType, QObject *object) { #ifndef QT_NO_DEBUG - if (timerId < 1 || interval < 0 || !object) { + if (qToUnderlying(timerId) < 1 || interval < 0ns || !object) { qWarning("QEventDispatcherGlib::registerTimer: invalid arguments"); return; } else if (object->thread() != thread() || thread() != QThread::currentThread()) { @@ -490,14 +491,13 @@ void QEventDispatcherGlib::registerTimer(int timerId, qint64 interval, Qt::Timer #endif Q_D(QEventDispatcherGlib); - d->timerSource->timerList.registerTimer(timerId, std::chrono::milliseconds{ interval }, - timerType, object); + d->timerSource->timerList.registerTimer(timerId, interval, timerType, object); } -bool QEventDispatcherGlib::unregisterTimer(int timerId) +bool QEventDispatcherGlib::unregisterTimer(Qt::TimerId timerId) { #ifndef QT_NO_DEBUG - if (timerId < 1) { + if (qToUnderlying(timerId) < 1) { qWarning("QEventDispatcherGlib::unregisterTimer: invalid argument"); return false; } else if (thread() != QThread::currentThread()) { @@ -526,28 +526,30 @@ bool QEventDispatcherGlib::unregisterTimers(QObject *object) return d->timerSource->timerList.unregisterTimers(object); } -QList QEventDispatcherGlib::registeredTimers(QObject *object) const +QList QEventDispatcherGlib::timersForObject(QObject *object) const { +#ifndef QT_NO_DEBUG if (!object) { - qWarning("QEventDispatcherUNIX:registeredTimers: invalid argument"); - return QList(); + qWarning("QEventDispatcherGlib:timersForObject: invalid argument"); + return {}; } +#endif Q_D(const QEventDispatcherGlib); return d->timerSource->timerList.registeredTimers(object); } -int QEventDispatcherGlib::remainingTime(int timerId) +QEventDispatcherGlib::Duration QEventDispatcherGlib::remainingTime(Qt::TimerId timerId) const { #ifndef QT_NO_DEBUG - if (timerId < 1) { + if (qToUnderlying(timerId) < 1) { qWarning("QEventDispatcherGlib::remainingTimeTime: invalid argument"); - return -1; + return Duration::min(); } #endif - Q_D(QEventDispatcherGlib); - return d->timerSource->timerList.timerRemainingTime(timerId); + Q_D(const QEventDispatcherGlib); + return d->timerSource->timerList.remainingDuration(timerId); } void QEventDispatcherGlib::interrupt() @@ -572,7 +574,7 @@ bool QEventDispatcherGlib::versionSupported() } QEventDispatcherGlib::QEventDispatcherGlib(QEventDispatcherGlibPrivate &dd, QObject *parent) - : QAbstractEventDispatcher(dd, parent) + : QAbstractEventDispatcherV2(dd, parent) { } diff --git a/src/corelib/kernel/qeventdispatcher_glib_p.h b/src/corelib/kernel/qeventdispatcher_glib_p.h index 4881a8543f..30783d3858 100644 --- a/src/corelib/kernel/qeventdispatcher_glib_p.h +++ b/src/corelib/kernel/qeventdispatcher_glib_p.h @@ -24,7 +24,7 @@ QT_BEGIN_NAMESPACE class QEventDispatcherGlibPrivate; -class Q_CORE_EXPORT QEventDispatcherGlib : public QAbstractEventDispatcher +class Q_CORE_EXPORT QEventDispatcherGlib : public QAbstractEventDispatcherV2 { Q_OBJECT Q_DECLARE_PRIVATE(QEventDispatcherGlib) @@ -39,12 +39,12 @@ public: void registerSocketNotifier(QSocketNotifier *socketNotifier) final; void unregisterSocketNotifier(QSocketNotifier *socketNotifier) final; - void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) final; - bool unregisterTimer(int timerId) final; - bool unregisterTimers(QObject *object) final; - QList registeredTimers(QObject *object) const final; - - int remainingTime(int timerId) final; + void registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, + QObject *object) override final; + bool unregisterTimer(Qt::TimerId timerId) override final; + bool unregisterTimers(QObject *object) override final; + QList timersForObject(QObject *object) const override final; + Duration remainingTime(Qt::TimerId timerId) const override final; void wakeUp() final; void interrupt() final; diff --git a/src/corelib/kernel/qeventdispatcher_unix.cpp b/src/corelib/kernel/qeventdispatcher_unix.cpp index e6da258d9f..21bd224415 100644 --- a/src/corelib/kernel/qeventdispatcher_unix.cpp +++ b/src/corelib/kernel/qeventdispatcher_unix.cpp @@ -257,11 +257,11 @@ int QEventDispatcherUNIXPrivate::activateSocketNotifiers() } QEventDispatcherUNIX::QEventDispatcherUNIX(QObject *parent) - : QAbstractEventDispatcher(*new QEventDispatcherUNIXPrivate, parent) + : QAbstractEventDispatcherV2(*new QEventDispatcherUNIXPrivate, parent) { } QEventDispatcherUNIX::QEventDispatcherUNIX(QEventDispatcherUNIXPrivate &dd, QObject *parent) - : QAbstractEventDispatcher(dd, parent) + : QAbstractEventDispatcherV2(dd, parent) { } QEventDispatcherUNIX::~QEventDispatcherUNIX() @@ -270,10 +270,10 @@ QEventDispatcherUNIX::~QEventDispatcherUNIX() /*! \internal */ -void QEventDispatcherUNIX::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *obj) +void QEventDispatcherUNIX::registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, QObject *obj) { #ifndef QT_NO_DEBUG - if (timerId < 1 || interval < 0 || !obj) { + if (qToUnderlying(timerId) < 1 || interval.count() < 0 || !obj) { qWarning("QEventDispatcherUNIX::registerTimer: invalid arguments"); return; } else if (obj->thread() != thread() || thread() != QThread::currentThread()) { @@ -283,16 +283,16 @@ void QEventDispatcherUNIX::registerTimer(int timerId, qint64 interval, Qt::Timer #endif Q_D(QEventDispatcherUNIX); - d->timerList.registerTimer(timerId, std::chrono::milliseconds{ interval }, timerType, obj); + d->timerList.registerTimer(timerId, interval, timerType, obj); } /*! \internal */ -bool QEventDispatcherUNIX::unregisterTimer(int timerId) +bool QEventDispatcherUNIX::unregisterTimer(Qt::TimerId timerId) { #ifndef QT_NO_DEBUG - if (timerId < 1) { + if (qToUnderlying(timerId) < 1) { qWarning("QEventDispatcherUNIX::unregisterTimer: invalid argument"); return false; } else if (thread() != QThread::currentThread()) { @@ -324,12 +324,12 @@ bool QEventDispatcherUNIX::unregisterTimers(QObject *object) return d->timerList.unregisterTimers(object); } -QList -QEventDispatcherUNIX::registeredTimers(QObject *object) const +QList +QEventDispatcherUNIX::timersForObject(QObject *object) const { if (!object) { qWarning("QEventDispatcherUNIX:registeredTimers: invalid argument"); - return QList(); + return QList(); } Q_D(const QEventDispatcherUNIX); @@ -476,17 +476,17 @@ bool QEventDispatcherUNIX::processEvents(QEventLoop::ProcessEventsFlags flags) return (nevents > 0); } -int QEventDispatcherUNIX::remainingTime(int timerId) +auto QEventDispatcherUNIX::remainingTime(Qt::TimerId timerId) const -> Duration { #ifndef QT_NO_DEBUG - if (timerId < 1) { + if (int(timerId) < 1) { qWarning("QEventDispatcherUNIX::remainingTime: invalid argument"); - return -1; + return Duration::min(); } #endif - Q_D(QEventDispatcherUNIX); - return d->timerList.timerRemainingTime(timerId); + Q_D(const QEventDispatcherUNIX); + return d->timerList.remainingDuration(timerId); } void QEventDispatcherUNIX::wakeUp() diff --git a/src/corelib/kernel/qeventdispatcher_unix_p.h b/src/corelib/kernel/qeventdispatcher_unix_p.h index 2479cd1e63..6596e998c9 100644 --- a/src/corelib/kernel/qeventdispatcher_unix_p.h +++ b/src/corelib/kernel/qeventdispatcher_unix_p.h @@ -61,7 +61,7 @@ struct QThreadPipe #endif }; -class Q_CORE_EXPORT QEventDispatcherUNIX : public QAbstractEventDispatcher +class Q_CORE_EXPORT QEventDispatcherUNIX : public QAbstractEventDispatcherV2 { Q_OBJECT Q_DECLARE_PRIVATE(QEventDispatcherUNIX) @@ -75,12 +75,12 @@ public: void registerSocketNotifier(QSocketNotifier *notifier) final; void unregisterSocketNotifier(QSocketNotifier *notifier) final; - void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) final; - bool unregisterTimer(int timerId) final; - bool unregisterTimers(QObject *object) final; - QList registeredTimers(QObject *object) const final; - - int remainingTime(int timerId) final; + void registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, + QObject *object) override final; + bool unregisterTimer(Qt::TimerId timerId) override final; + bool unregisterTimers(QObject *object) override final; + QList timersForObject(QObject *object) const override final; + Duration remainingTime(Qt::TimerId timerId) const override final; void wakeUp() override; void interrupt() final; diff --git a/src/corelib/kernel/qeventdispatcher_wasm.cpp b/src/corelib/kernel/qeventdispatcher_wasm.cpp index 51904eaab8..f4fcdbb8b2 100644 --- a/src/corelib/kernel/qeventdispatcher_wasm.cpp +++ b/src/corelib/kernel/qeventdispatcher_wasm.cpp @@ -194,7 +194,6 @@ std::multimap QEventDispatcherWasm::g_socketNotifiers; std::map QEventDispatcherWasm::g_socketState; QEventDispatcherWasm::QEventDispatcherWasm() - : QAbstractEventDispatcher() { // QEventDispatcherWasm operates in two main modes: // - On the main thread: @@ -356,10 +355,10 @@ void QEventDispatcherWasm::unregisterSocketNotifier(QSocketNotifier *notifier) runOnMainThread([] { clearEmscriptenSocketCallbacks(); }); } -void QEventDispatcherWasm::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) +void QEventDispatcherWasm::registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, QObject *object) { #ifndef QT_NO_DEBUG - if (timerId < 1 || interval < 0 || !object) { + if (qToUnderlying(timerId) < 1 || interval < 0ns || !object) { qWarning("QEventDispatcherWasm::registerTimer: invalid arguments"); return; } else if (object->thread() != thread() || thread() != QThread::currentThread()) { @@ -368,16 +367,16 @@ void QEventDispatcherWasm::registerTimer(int timerId, qint64 interval, Qt::Timer return; } #endif - qCDebug(lcEventDispatcherTimers) << "registerTimer" << timerId << interval << timerType << object; + qCDebug(lcEventDispatcherTimers) << "registerTimer" << int(timerId) << interval << timerType << object; m_timerInfo->registerTimer(timerId, interval, timerType, object); updateNativeTimer(); } -bool QEventDispatcherWasm::unregisterTimer(int timerId) +bool QEventDispatcherWasm::unregisterTimer(Qt::TimerId timerId) { #ifndef QT_NO_DEBUG - if (timerId < 1) { + if (qToUnderlying(timerId) < 1) { qWarning("QEventDispatcherWasm::unregisterTimer: invalid argument"); return false; } else if (thread() != QThread::currentThread()) { @@ -387,7 +386,7 @@ bool QEventDispatcherWasm::unregisterTimer(int timerId) } #endif - qCDebug(lcEventDispatcherTimers) << "unregisterTimer" << timerId; + qCDebug(lcEventDispatcherTimers) << "unregisterTimer" << int(timerId); bool ans = m_timerInfo->unregisterTimer(timerId); updateNativeTimer(); @@ -414,22 +413,22 @@ bool QEventDispatcherWasm::unregisterTimers(QObject *object) return ans; } -QList -QEventDispatcherWasm::registeredTimers(QObject *object) const +QList +QEventDispatcherWasm::timersForObject(QObject *object) const { #ifndef QT_NO_DEBUG if (!object) { qWarning("QEventDispatcherWasm:registeredTimers: invalid argument"); - return QList(); + return {}; } #endif return m_timerInfo->registeredTimers(object); } -int QEventDispatcherWasm::remainingTime(int timerId) +QEventDispatcherWasm::Duration QEventDispatcherWasm::remainingTime(Qt::TimerId timerId) const { - return m_timerInfo->timerRemainingTime(timerId); + return m_timerInfo->remainingDuration(timerId); } void QEventDispatcherWasm::interrupt() diff --git a/src/corelib/kernel/qeventdispatcher_wasm_p.h b/src/corelib/kernel/qeventdispatcher_wasm_p.h index 0d94c48a25..372be88a1c 100644 --- a/src/corelib/kernel/qeventdispatcher_wasm_p.h +++ b/src/corelib/kernel/qeventdispatcher_wasm_p.h @@ -32,7 +32,7 @@ QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcher); Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcherTimers) -class Q_CORE_EXPORT QEventDispatcherWasm : public QAbstractEventDispatcher +class Q_CORE_EXPORT QEventDispatcherWasm : public QAbstractEventDispatcherV2 { Q_OBJECT public: @@ -44,11 +44,12 @@ public: void registerSocketNotifier(QSocketNotifier *notifier) override; void unregisterSocketNotifier(QSocketNotifier *notifier) override; - void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) override; - bool unregisterTimer(int timerId) override; - bool unregisterTimers(QObject *object) override; - QList registeredTimers(QObject *object) const override; - int remainingTime(int timerId) override; + void registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, + QObject *object) override final; + bool unregisterTimer(Qt::TimerId timerId) override final; + bool unregisterTimers(QObject *object) override final; + QList timersForObject(QObject *object) const override final; + Duration remainingTime(Qt::TimerId timerId) const override final; void interrupt() override; void wakeUp() override; diff --git a/src/corelib/kernel/qtimerinfo_unix.cpp b/src/corelib/kernel/qtimerinfo_unix.cpp index 4c846fa5ec..b83f0194c2 100644 --- a/src/corelib/kernel/qtimerinfo_unix.cpp +++ b/src/corelib/kernel/qtimerinfo_unix.cpp @@ -27,7 +27,7 @@ Q_CORE_EXPORT bool qt_disable_lowpriority_timers=false; QTimerInfoList::QTimerInfoList() = default; -steady_clock::time_point QTimerInfoList::updateCurrentTime() +steady_clock::time_point QTimerInfoList::updateCurrentTime() const { currentTime = steady_clock::now(); return currentTime; @@ -245,7 +245,7 @@ std::optional QTimerInfoList::timerWait() if (it == timers.cend()) return std::nullopt; - nanoseconds timeToWait = (*it)->timeout - now; + Duration timeToWait = (*it)->timeout - now; if (timeToWait > 0ns) return roundToMillisecond(timeToWait); return 0ms; @@ -253,24 +253,19 @@ std::optional QTimerInfoList::timerWait() /* Returns the timer's remaining time in milliseconds with the given timerId. - If the timer id is not found in the list, the returned value will be -1. + If the timer id is not found in the list, the returned value will be \c{Duration::min()}. If the timer is overdue, the returned value will be 0. */ -qint64 QTimerInfoList::timerRemainingTime(int timerId) -{ - return roundToMillisecond(remainingDuration(timerId)).count(); -} - -QTimerInfoList::Duration QTimerInfoList::remainingDuration(int timerId) +QTimerInfoList::Duration QTimerInfoList::remainingDuration(Qt::TimerId timerId) const { const steady_clock::time_point now = updateCurrentTime(); auto it = findTimerById(timerId); if (it == timers.cend()) { #ifndef QT_NO_DEBUG - qWarning("QTimerInfoList::timerRemainingTime: timer id %i not found", timerId); + qWarning("QTimerInfoList::timerRemainingTime: timer id %i not found", int(timerId)); #endif - return -1ms; + return Duration::min(); } const QTimerInfo *t = *it; @@ -279,12 +274,7 @@ QTimerInfoList::Duration QTimerInfoList::remainingDuration(int timerId) return 0ms; } -void QTimerInfoList::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) -{ - registerTimer(timerId, milliseconds{interval}, timerType, object); -} - -void QTimerInfoList::registerTimer(int timerId, Duration interval, +void QTimerInfoList::registerTimer(Qt::TimerId timerId, QTimerInfoList::Duration interval, Qt::TimerType timerType, QObject *object) { // correct the timer type first @@ -327,7 +317,7 @@ void QTimerInfoList::registerTimer(int timerId, Duration interval, timerInsert(t); } -bool QTimerInfoList::unregisterTimer(int timerId) +bool QTimerInfoList::unregisterTimer(Qt::TimerId timerId) { auto it = findTimerById(timerId); if (it == timers.cend()) @@ -367,12 +357,12 @@ bool QTimerInfoList::unregisterTimers(QObject *object) return count > 0; } -QList QTimerInfoList::registeredTimers(QObject *object) const +auto QTimerInfoList::registeredTimers(QObject *object) const -> QList { - QList list; + QList list; for (const auto &t : timers) { if (t->obj == object) - list.emplaceBack(t->id, t->interval.count(), t->timerType); + list.emplaceBack(TimerInfo{t->interval, t->id, t->timerType}); } return list; } @@ -432,7 +422,7 @@ int QTimerInfoList::activateTimers() if (!currentTimerInfo->activateRef) { currentTimerInfo->activateRef = ¤tTimerInfo; - QTimerEvent e(currentTimerInfo->id); + QTimerEvent e(qToUnderlying(currentTimerInfo->id)); QCoreApplication::sendEvent(currentTimerInfo->obj, &e); // Storing currentTimerInfo's address in its activateRef allows the diff --git a/src/corelib/kernel/qtimerinfo_unix_p.h b/src/corelib/kernel/qtimerinfo_unix_p.h index 5ebda6a68d..293e9c4d4e 100644 --- a/src/corelib/kernel/qtimerinfo_unix_p.h +++ b/src/corelib/kernel/qtimerinfo_unix_p.h @@ -29,14 +29,14 @@ struct QTimerInfo { using Duration = QAbstractEventDispatcher::Duration; using TimePoint = std::chrono::time_point; - QTimerInfo(int timerId, Duration interval, Qt::TimerType type, QObject *obj) + QTimerInfo(Qt::TimerId timerId, Duration interval, Qt::TimerType type, QObject *obj) : interval(interval), id(timerId), timerType(type), obj(obj) { } TimePoint timeout = {}; // - when to actually fire Duration interval = Duration{-1}; // - timer interval - int id = -1; // - timer identifier + Qt::TimerId id = Qt::TimerId::Invalid; // - timer identifier Qt::TimerType timerType; // - timer type QObject *obj = nullptr; // - object to receive event QTimerInfo **activateRef = nullptr; // - ref from activateTimers @@ -46,22 +46,21 @@ class Q_CORE_EXPORT QTimerInfoList { public: using Duration = QAbstractEventDispatcher::Duration; + using TimerInfo = QAbstractEventDispatcher::TimerInfoV2; QTimerInfoList(); - std::chrono::steady_clock::time_point currentTime; + mutable std::chrono::steady_clock::time_point currentTime; std::optional timerWait(); void timerInsert(QTimerInfo *); - qint64 timerRemainingTime(int timerId); - Duration remainingDuration(int timerId); + Duration remainingDuration(Qt::TimerId timerId) const; - void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object); - void registerTimer(int timerId, Duration interval, Qt::TimerType timerType, - QObject *object); - bool unregisterTimer(int timerId); + void registerTimer(Qt::TimerId timerId, Duration interval, + Qt::TimerType timerType, QObject *object); + bool unregisterTimer(Qt::TimerId timerId); bool unregisterTimers(QObject *object); - QList registeredTimers(QObject *object) const; + QList registeredTimers(QObject *object) const; int activateTimers(); bool hasPendingTimers(); @@ -76,14 +75,14 @@ public: qsizetype size() const { return timers.size(); } - auto findTimerById(int timerId) + auto findTimerById(Qt::TimerId timerId) const { auto matchesId = [timerId](const auto &t) { return t->id == timerId; }; return std::find_if(timers.cbegin(), timers.cend(), matchesId); } private: - std::chrono::steady_clock::time_point updateCurrentTime(); + std::chrono::steady_clock::time_point updateCurrentTime() const; // state variables used by activateTimers() QTimerInfo *firstTimerInfo = nullptr; diff --git a/src/plugins/platforms/cocoa/qcocoaeventdispatcher.h b/src/plugins/platforms/cocoa/qcocoaeventdispatcher.h index c942aa9304..96eb70dabc 100644 --- a/src/plugins/platforms/cocoa/qcocoaeventdispatcher.h +++ b/src/plugins/platforms/cocoa/qcocoaeventdispatcher.h @@ -74,7 +74,7 @@ typedef struct _QCocoaModalSessionInfo { } QCocoaModalSessionInfo; class QCocoaEventDispatcherPrivate; -class QCocoaEventDispatcher : public QAbstractEventDispatcher +class QCocoaEventDispatcher : public QAbstractEventDispatcherV2 { Q_OBJECT Q_DECLARE_PRIVATE(QCocoaEventDispatcher) @@ -89,12 +89,12 @@ public: void registerSocketNotifier(QSocketNotifier *notifier); void unregisterSocketNotifier(QSocketNotifier *notifier); - void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object); - bool unregisterTimer(int timerId); - bool unregisterTimers(QObject *object); - QList registeredTimers(QObject *object) const; - - int remainingTime(int timerId); + void registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, + QObject *object) final; + bool unregisterTimer(Qt::TimerId timerId) final; + bool unregisterTimers(QObject *object) final; + QList timersForObject(QObject *object) const final; + Duration remainingTime(Qt::TimerId timerId) const final; void wakeUp(); void interrupt(); diff --git a/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm b/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm index 77d636793b..739fbda4f5 100644 --- a/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm +++ b/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm @@ -171,10 +171,11 @@ void QCocoaEventDispatcherPrivate::maybeStopCFRunLoopTimer() runLoopTimerRef = nullptr; } -void QCocoaEventDispatcher::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *obj) +void QCocoaEventDispatcher::registerTimer(Qt::TimerId timerId, Duration interval, + Qt::TimerType timerType, QObject *obj) { #ifndef QT_NO_DEBUG - if (timerId < 1 || interval < 0 || !obj) { + if (qToUnderlying(timerId) < 1 || interval.count() < 0 || !obj) { qWarning("QCocoaEventDispatcher::registerTimer: invalid arguments"); return; } else if (obj->thread() != thread() || thread() != QThread::currentThread()) { @@ -188,10 +189,10 @@ void QCocoaEventDispatcher::registerTimer(int timerId, qint64 interval, Qt::Time d->maybeStartCFRunLoopTimer(); } -bool QCocoaEventDispatcher::unregisterTimer(int timerId) +bool QCocoaEventDispatcher::unregisterTimer(Qt::TimerId timerId) { #ifndef QT_NO_DEBUG - if (timerId < 1) { + if (qToUnderlying(timerId) < 1) { qWarning("QCocoaEventDispatcher::unregisterTimer: invalid argument"); return false; } else if (thread() != QThread::currentThread()) { @@ -230,13 +231,13 @@ bool QCocoaEventDispatcher::unregisterTimers(QObject *obj) return returnValue; } -QList -QCocoaEventDispatcher::registeredTimers(QObject *object) const +QList +QCocoaEventDispatcher::timersForObject(QObject *object) const { #ifndef QT_NO_DEBUG if (!object) { qWarning("QCocoaEventDispatcher:registeredTimers: invalid argument"); - return QList(); + return {}; } #endif @@ -540,17 +541,17 @@ bool QCocoaEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags) return retVal; } -int QCocoaEventDispatcher::remainingTime(int timerId) +auto QCocoaEventDispatcher::remainingTime(Qt::TimerId timerId) const -> Duration { #ifndef QT_NO_DEBUG - if (timerId < 1) { + if (qToUnderlying(timerId) < 1) { qWarning("QCocoaEventDispatcher::remainingTime: invalid argument"); - return -1; + return Duration::min(); } #endif - Q_D(QCocoaEventDispatcher); - return d->timerInfoList.timerRemainingTime(timerId); + Q_D(const QCocoaEventDispatcher); + return d->timerInfoList.remainingDuration(timerId); } void QCocoaEventDispatcher::wakeUp() @@ -801,7 +802,7 @@ void qt_mac_maybeCancelWaitForMoreEventsForwarder(QAbstractEventDispatcher *even } QCocoaEventDispatcher::QCocoaEventDispatcher(QObject *parent) - : QAbstractEventDispatcher(*new QCocoaEventDispatcherPrivate, parent) + : QAbstractEventDispatcherV2(*new QCocoaEventDispatcherPrivate, parent) { Q_D(QCocoaEventDispatcher);