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 <a.samirh78@gmail.com>
bb10
Thiago Macieira 2024-02-18 20:31:33 -08:00
parent 9dc2935462
commit f8da484d57
12 changed files with 132 additions and 136 deletions

View File

@ -169,7 +169,7 @@ static const CFTimeInterval kCFTimeIntervalDistantFuture = std::numeric_limits<C
#pragma mark - Class definition
QEventDispatcherCoreFoundation::QEventDispatcherCoreFoundation(QObject *parent)
: QAbstractEventDispatcher(parent)
: QAbstractEventDispatcherV2(parent)
, m_processEvents(QEventLoop::EventLoopExec)
, m_postedEventsRunLoopSource(this, &QEventDispatcherCoreFoundation::processPostedEvents)
, m_runLoopActivityObserver(this, &QEventDispatcherCoreFoundation::handleRunLoopActivity, kCFRunLoopAllActivities)
@ -506,26 +506,28 @@ void QEventDispatcherCoreFoundation::unregisterSocketNotifier(QSocketNotifier *n
#pragma mark - Timers
void QEventDispatcherCoreFoundation::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object)
void QEventDispatcherCoreFoundation::registerTimer(Qt::TimerId timerId, Duration interval,
Qt::TimerType timerType, QObject *object)
{
qCDebug(lcEventDispatcherTimers) << "Registering timer with id =" << timerId << "interval =" << interval
qCDebug(lcEventDispatcherTimers) << "Registering timer with id =" << int(timerId) << "interval =" << interval
<< "type =" << timerType << "object =" << object;
Q_ASSERT(timerId > 0 && interval >= 0 && object);
Q_ASSERT(qToUnderlying(timerId) > 0 && interval.count() >= 0 && object);
Q_ASSERT(object->thread() == thread() && thread() == QThread::currentThread());
m_timerInfoList.registerTimer(timerId, interval, timerType, object);
updateTimers();
}
bool QEventDispatcherCoreFoundation::unregisterTimer(int timerId)
bool QEventDispatcherCoreFoundation::unregisterTimer(Qt::TimerId timerId)
{
Q_ASSERT(timerId > 0);
Q_ASSERT(qToUnderlying(timerId) > 0);
Q_ASSERT(thread() == QThread::currentThread());
bool returnValue = m_timerInfoList.unregisterTimer(timerId);
qCDebug(lcEventDispatcherTimers) << "Unegistered timer with id =" << timerId << "Timers left:" << m_timerInfoList.size();
qCDebug(lcEventDispatcherTimers) << "Unegistered timer with id =" << qToUnderlying(timerId)
<< "Timers left:" << m_timerInfoList.size();
updateTimers();
return returnValue;
@ -543,16 +545,18 @@ bool QEventDispatcherCoreFoundation::unregisterTimers(QObject *object)
return returnValue;
}
QList<QAbstractEventDispatcher::TimerInfo> QEventDispatcherCoreFoundation::registeredTimers(QObject *object) const
QList<QAbstractEventDispatcher::TimerInfoV2>
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()

View File

@ -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<QAbstractEventDispatcher::TimerInfo> 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<TimerInfoV2> timersForObject(QObject *object) const override final;
Duration remainingTime(Qt::TimerId timerId) const override final;
void wakeUp() override;
void interrupt() override;

View File

@ -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::TimerInfo> QEventDispatcherGlib::registeredTimers(QObject *object) const
QList<QEventDispatcherGlib::TimerInfoV2> QEventDispatcherGlib::timersForObject(QObject *object) const
{
#ifndef QT_NO_DEBUG
if (!object) {
qWarning("QEventDispatcherUNIX:registeredTimers: invalid argument");
return QList<TimerInfo>();
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)
{
}

View File

@ -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<TimerInfo> 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<TimerInfoV2> timersForObject(QObject *object) const override final;
Duration remainingTime(Qt::TimerId timerId) const override final;
void wakeUp() final;
void interrupt() final;

View File

@ -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::TimerInfo>
QEventDispatcherUNIX::registeredTimers(QObject *object) const
QList<QEventDispatcherUNIX::TimerInfoV2>
QEventDispatcherUNIX::timersForObject(QObject *object) const
{
if (!object) {
qWarning("QEventDispatcherUNIX:registeredTimers: invalid argument");
return QList<TimerInfo>();
return QList<TimerInfoV2>();
}
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()

View File

@ -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<TimerInfo> 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<TimerInfoV2> timersForObject(QObject *object) const override final;
Duration remainingTime(Qt::TimerId timerId) const override final;
void wakeUp() override;
void interrupt() final;

View File

@ -194,7 +194,6 @@ std::multimap<int, QSocketNotifier *> QEventDispatcherWasm::g_socketNotifiers;
std::map<int, QEventDispatcherWasm::SocketReadyState> 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<QAbstractEventDispatcher::TimerInfo>
QEventDispatcherWasm::registeredTimers(QObject *object) const
QList<QAbstractEventDispatcher::TimerInfoV2>
QEventDispatcherWasm::timersForObject(QObject *object) const
{
#ifndef QT_NO_DEBUG
if (!object) {
qWarning("QEventDispatcherWasm:registeredTimers: invalid argument");
return QList<TimerInfo>();
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()

View File

@ -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<QAbstractEventDispatcher::TimerInfo> 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<TimerInfoV2> timersForObject(QObject *object) const override final;
Duration remainingTime(Qt::TimerId timerId) const override final;
void interrupt() override;
void wakeUp() override;

View File

@ -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::Duration> 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::Duration> 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<QAbstractEventDispatcher::TimerInfo> QTimerInfoList::registeredTimers(QObject *object) const
auto QTimerInfoList::registeredTimers(QObject *object) const -> QList<TimerInfo>
{
QList<QAbstractEventDispatcher::TimerInfo> list;
QList<TimerInfo> 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 = &currentTimerInfo;
QTimerEvent e(currentTimerInfo->id);
QTimerEvent e(qToUnderlying(currentTimerInfo->id));
QCoreApplication::sendEvent(currentTimerInfo->obj, &e);
// Storing currentTimerInfo's address in its activateRef allows the

View File

@ -29,14 +29,14 @@ struct QTimerInfo
{
using Duration = QAbstractEventDispatcher::Duration;
using TimePoint = std::chrono::time_point<std::chrono::steady_clock, Duration>;
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<Duration> 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<QAbstractEventDispatcher::TimerInfo> registeredTimers(QObject *object) const;
QList<TimerInfo> 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;

View File

@ -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<TimerInfo> 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<TimerInfoV2> timersForObject(QObject *object) const final;
Duration remainingTime(Qt::TimerId timerId) const final;
void wakeUp();
void interrupt();

View File

@ -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::TimerInfo>
QCocoaEventDispatcher::registeredTimers(QObject *object) const
QList<QCocoaEventDispatcher::TimerInfoV2>
QCocoaEventDispatcher::timersForObject(QObject *object) const
{
#ifndef QT_NO_DEBUG
if (!object) {
qWarning("QCocoaEventDispatcher:registeredTimers: invalid argument");
return QList<TimerInfo>();
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);