QTimerInfo: use range-for; use STL algorithms

Change-Id: I7e1b603540a2a2b4d1b12d4dbdba7e9183ee367f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Konrad Kujawa <konrad.kujawa@qt.io>
bb10
Ahmad Samir 2023-02-21 19:08:13 +02:00
parent 2f17a1b4ef
commit 17d36d92fc
2 changed files with 46 additions and 58 deletions

View File

@ -100,10 +100,8 @@ bool QTimerInfoList::timeChanged(timespec *delta)
void QTimerInfoList::timerRepair(const timespec &diff)
{
// repair all timers
for (int i = 0; i < size(); ++i) {
QTimerInfo *t = at(i);
t->timeout = t->timeout + diff;
}
for (QTimerInfo *t : std::as_const(*this))
t->timeout += diff;
}
void QTimerInfoList::repairTimersIfNeeded()
@ -351,26 +349,17 @@ bool QTimerInfoList::timerWait(timespec &tm)
timespec now = updateCurrentTime();
repairTimersIfNeeded();
auto isWaiting = [](QTimerInfo *tinfo) { return !tinfo->activateRef; };
// Find first waiting timer not already active
QTimerInfo *t = nullptr;
for (QTimerInfoList::const_iterator it = constBegin(); it != constEnd(); ++it) {
if (!(*it)->activateRef) {
t = *it;
break;
}
}
auto it = std::find_if(cbegin(), cend(), isWaiting);
if (it == cend())
return false;
if (!t)
return false;
if (now < t->timeout) {
// time to wait
QTimerInfo *t = *it;
if (now < t->timeout) // Time to wait
tm = roundToMillisecond(t->timeout - now);
} else {
// no time to wait
tm.tv_sec = 0;
tm.tv_nsec = 0;
}
else // No time to wait
tm = {0, 0};
return true;
}
@ -391,23 +380,22 @@ milliseconds QTimerInfoList::remainingDuration(int timerId)
repairTimersIfNeeded();
timespec tm = {0, 0};
for (const auto *t : std::as_const(*this)) {
if (t->id == timerId) {
if (now < t->timeout) {
// time to wait
tm = roundToMillisecond(t->timeout - now);
return QtMiscUtils::timespecToChronoMs(&tm);
} else {
return milliseconds{0};
}
}
auto it = findTimerById(timerId);
if (it == cend()) {
#ifndef QT_NO_DEBUG
qWarning("QTimerInfoList::timerRemainingTime: timer id %i not found", timerId);
#endif
return milliseconds{-1};
}
#ifndef QT_NO_DEBUG
qWarning("QTimerInfoList::timerRemainingTime: timer id %i not found", timerId);
#endif
return milliseconds{-1};
const QTimerInfo *t = *it;
if (now < t->timeout) {
// time to wait
tm = roundToMillisecond(t->timeout - now);
return QtMiscUtils::timespecToChronoMs(&tm);
} else {
return milliseconds{0};
}
}
void QTimerInfoList::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object)
@ -477,22 +465,19 @@ void QTimerInfoList::registerTimer(int timerId, milliseconds interval,
bool QTimerInfoList::unregisterTimer(int timerId)
{
auto it = findTimerById(timerId);
if (it == cend())
return false; // id not found
// set timer inactive
for (int i = 0; i < size(); ++i) {
QTimerInfo *t = at(i);
if (t->id == timerId) {
// found it
removeAt(i);
if (t == firstTimerInfo)
firstTimerInfo = nullptr;
if (t->activateRef)
*(t->activateRef) = nullptr;
delete t;
return true;
}
}
// id not found
return false;
QTimerInfo *t = *it;
if (t == firstTimerInfo)
firstTimerInfo = nullptr;
if (t->activateRef)
*(t->activateRef) = nullptr;
delete t;
erase(it);
return true;
}
bool QTimerInfoList::unregisterTimers(QObject *object)
@ -534,21 +519,18 @@ int QTimerInfoList::activateTimers()
if (qt_disable_lowpriority_timers || isEmpty())
return 0; // nothing to do
int n_act = 0, maxCount = 0;
firstTimerInfo = nullptr;
timespec now = updateCurrentTime();
// qDebug() << "Thread" << QThread::currentThreadId() << "woken up at" << now;
repairTimersIfNeeded();
// Find out how many timer have expired
for (QTimerInfoList::const_iterator it = constBegin(); it != constEnd(); ++it) {
if (now < (*it)->timeout)
break;
maxCount++;
}
auto isExpired = [&now](const QTimerInfo *t) { return now < t->timeout; };
auto it = std::find_if(cbegin(), cend(), isExpired);
int maxCount = it - cbegin();
int n_act = 0;
//fire the timers.
while (maxCount--) {
if (isEmpty())

View File

@ -81,6 +81,12 @@ public:
QList<QAbstractEventDispatcher::TimerInfo> registeredTimers(QObject *object) const;
int activateTimers();
QList::const_iterator findTimerById(int timerId) const
{
auto matchesId = [timerId](const QTimerInfo *t) { return t->id == timerId; };
return std::find_if(cbegin(), cend(), matchesId);
}
};
QT_END_NAMESPACE