QObject: replace _q_reregisterTimers with a lambda

- Pass the QList by value, no heap allocation and no plain new/delete
- A lambda means that there isn't runtime string-based lookup to find
  the member method in QObjectPrivate

The code is only a couple of lines and used in a single place, so might
as well move the code from _q_reregisterTimers to the local lambda.

Modify tst_moc to account for the numer of methods in QObjectPrivate
changing. The test had hardcoded numbers.

Change-Id: I07906fc7138b8e601e4695f8d2de1b5fdd88449c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2023-07-10 18:48:17 +03:00
parent e8e9c287ec
commit b902b152af
4 changed files with 23 additions and 31 deletions

View File

@ -1451,8 +1451,13 @@ bool QObject::event(QEvent *e)
Q_ASSERT_X(res, Q_FUNC_INFO,
"QAbstractEventDispatcher::unregisterTimers() returned false,"
" but there are timers associated with this object.");
QMetaObject::invokeMethod(this, "_q_reregisterTimers", Qt::QueuedConnection,
Q_ARG(void*, (new QList<QAbstractEventDispatcher::TimerInfo>(timers))));
auto reRegisterTimers = [this, timers = std::move(timers)]() {
QAbstractEventDispatcher *eventDispatcher =
d_func()->threadData.loadRelaxed()->eventDispatcher.loadRelaxed();
for (const auto &ti : timers)
eventDispatcher->registerTimer(ti.timerId, ti.interval, ti.timerType, this);
};
QMetaObject::invokeMethod(this, std::move(reRegisterTimers), Qt::QueuedConnection);
}
}
break;
@ -1808,19 +1813,6 @@ void QObjectPrivate::setThreadData_helper(QThreadData *currentData, QThreadData
}
}
void QObjectPrivate::_q_reregisterTimers(void *pointer)
{
Q_Q(QObject);
QList<QAbstractEventDispatcher::TimerInfo> *timerList = reinterpret_cast<QList<QAbstractEventDispatcher::TimerInfo> *>(pointer);
QAbstractEventDispatcher *eventDispatcher = threadData.loadRelaxed()->eventDispatcher.loadRelaxed();
for (int i = 0; i < timerList->size(); ++i) {
const QAbstractEventDispatcher::TimerInfo &ti = timerList->at(i);
eventDispatcher->registerTimer(ti.timerId, ti.interval, ti.timerType, q);
}
delete timerList;
}
//
// The timer flag hasTimer is set when startTimer is called.
// It is not reset when killing the timer because more than

View File

@ -353,7 +353,6 @@ private:
bool doSetProperty(const char *name, const QVariant *lvalue, QVariant *rvalue);
Q_DISABLE_COPY(QObject)
Q_PRIVATE_SLOT(d_func(), void _q_reregisterTimers(void *))
private:
static QMetaObject::Connection connectImpl(const QObject *sender, void **signal,

View File

@ -140,7 +140,6 @@ public:
void setParent_helper(QObject *);
void moveToThread_helper();
void setThreadData_helper(QThreadData *currentData, QThreadData *targetData, QBindingStatus *status);
void _q_reregisterTimers(void *pointer);
bool isSender(const QObject *receiver, const char *signal) const;
QObjectList receiverList(const char *signal) const;

View File

@ -1589,41 +1589,43 @@ void tst_Moc::specifyMetaTagsFromCmdline() {
void tst_Moc::invokable()
{
const int fooIndex = 4;
{
const QMetaObject &mobj = InvokableBeforeReturnType::staticMetaObject;
QCOMPARE(mobj.methodCount(), 6);
QCOMPARE(mobj.method(5).methodSignature(), QByteArray("foo()"));
QCOMPARE(mobj.methodCount(), 5);
QCOMPARE(mobj.method(fooIndex).methodSignature(), QByteArray("foo()"));
}
{
const QMetaObject &mobj = InvokableBeforeInline::staticMetaObject;
QCOMPARE(mobj.methodCount(), 7);
QCOMPARE(mobj.method(5).methodSignature(), QByteArray("foo()"));
QCOMPARE(mobj.method(6).methodSignature(), QByteArray("bar()"));
QCOMPARE(mobj.methodCount(), 6);
QCOMPARE(mobj.method(fooIndex).methodSignature(), QByteArray("foo()"));
QCOMPARE(mobj.method(fooIndex + 1).methodSignature(), QByteArray("bar()"));
}
}
void tst_Moc::singleFunctionKeywordSignalAndSlot()
{
const int mySignalIndex = 4;
{
const QMetaObject &mobj = SingleFunctionKeywordBeforeReturnType::staticMetaObject;
QCOMPARE(mobj.methodCount(), 7);
QCOMPARE(mobj.method(5).methodSignature(), QByteArray("mySignal()"));
QCOMPARE(mobj.method(6).methodSignature(), QByteArray("mySlot()"));
QCOMPARE(mobj.methodCount(), 6);
QCOMPARE(mobj.method(mySignalIndex).methodSignature(), QByteArray("mySignal()"));
QCOMPARE(mobj.method(mySignalIndex + 1).methodSignature(), QByteArray("mySlot()"));
}
{
const QMetaObject &mobj = SingleFunctionKeywordBeforeInline::staticMetaObject;
QCOMPARE(mobj.methodCount(), 7);
QCOMPARE(mobj.method(5).methodSignature(), QByteArray("mySignal()"));
QCOMPARE(mobj.method(6).methodSignature(), QByteArray("mySlot()"));
QCOMPARE(mobj.methodCount(), 6);
QCOMPARE(mobj.method(mySignalIndex).methodSignature(), QByteArray("mySignal()"));
QCOMPARE(mobj.method(mySignalIndex + 1).methodSignature(), QByteArray("mySlot()"));
}
{
const QMetaObject &mobj = SingleFunctionKeywordAfterInline::staticMetaObject;
QCOMPARE(mobj.methodCount(), 7);
QCOMPARE(mobj.method(5).methodSignature(), QByteArray("mySignal()"));
QCOMPARE(mobj.method(6).methodSignature(), QByteArray("mySlot()"));
QCOMPARE(mobj.methodCount(), 6);
QCOMPARE(mobj.method(mySignalIndex).methodSignature(), QByteArray("mySignal()"));
QCOMPARE(mobj.method(mySignalIndex + 1).methodSignature(), QByteArray("mySlot()"));
}
}