QHostInfo: add -Result and -Runnable ctors taking SlotObjUniquePtr

... and use them around the code.

Avoids the impedance mismatch between users, which .release() their
SlotObjUniquePtr's into the ctors, and the ctor, which constructs its
member SlotObjUniquePtr from that.

The old constructors are left in on purpose and removed in a follow-up
commit to facilitate cherry-picking into branches that might have
additional callers.

Change-Id: I6f8bce317d5116296973a3a2e3f97db1451f579d
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 14cf4f75848180503d3c83ff1d9aeb4536c307b7)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-07-22 06:49:05 +02:00 committed by Qt Cherry-pick Bot
parent 954f13545c
commit 6de7e0d646
2 changed files with 22 additions and 4 deletions

View File

@ -72,6 +72,13 @@ Q_APPLICATION_STATIC(QHostInfoLookupManager, theHostInfoLookupManager)
}
QHostInfoResult::QHostInfoResult(const QObject *receiver, QtPrivate::SlotObjUniquePtr slot)
: receiver{receiver}, slotObj{std::move(slot)}, withContextObject{slotObj && receiver}
{
if (receiver)
moveToThread(receiver->thread());
}
QHostInfoResult::~QHostInfoResult()
= default;
@ -758,7 +765,7 @@ int QHostInfo::lookupHostImpl(const QString &name,
hostInfo.setError(QHostInfo::HostNotFound);
hostInfo.setErrorString(QCoreApplication::translate("QHostInfo", "No host name given"));
QHostInfoResult result(receiver, slotObj.release());
QHostInfoResult result(receiver, std::move(slotObj));
if (receiver && member)
QObject::connect(&result, SIGNAL(resultsReady(QHostInfo)),
receiver, member, Qt::QueuedConnection);
@ -775,7 +782,7 @@ int QHostInfo::lookupHostImpl(const QString &name,
QHostInfo hostInfo = QHostInfoAgent::lookup(name);
hostInfo.setLookupId(id);
QHostInfoResult result(receiver, slotObj.release());
QHostInfoResult result(receiver, std::move(slotObj));
if (receiver && member)
QObject::connect(&result, SIGNAL(resultsReady(QHostInfo)),
receiver, member, Qt::QueuedConnection);
@ -791,7 +798,7 @@ int QHostInfo::lookupHostImpl(const QString &name,
QHostInfo info = manager->cache.get(name, &valid);
if (valid) {
info.setLookupId(id);
QHostInfoResult result(receiver, slotObj.release());
QHostInfoResult result(receiver, std::move(slotObj));
if (receiver && member)
QObject::connect(&result, SIGNAL(resultsReady(QHostInfo)),
receiver, member, Qt::QueuedConnection);
@ -801,7 +808,7 @@ int QHostInfo::lookupHostImpl(const QString &name,
}
// cache is not enabled or it was not in the cache, do normal lookup
QHostInfoRunnable *runnable = new QHostInfoRunnable(name, id, receiver, slotObj.release());
QHostInfoRunnable *runnable = new QHostInfoRunnable(name, id, receiver, std::move(slotObj));
if (receiver && member)
QObject::connect(&runnable->resultEmitter, SIGNAL(resultsReady(QHostInfo)),
receiver, member, Qt::QueuedConnection);
@ -818,6 +825,13 @@ QHostInfoRunnable::QHostInfoRunnable(const QString &hn, int i, const QObject *re
setAutoDelete(true);
}
QHostInfoRunnable::QHostInfoRunnable(const QString &hn, int i, const QObject *receiver,
QtPrivate::SlotObjUniquePtr slotObj)
: toBeLookedUp{hn}, id{i}, resultEmitter{receiver, std::move(slotObj)}
{
setAutoDelete(true);
}
QHostInfoRunnable::~QHostInfoRunnable()
= default;

View File

@ -51,6 +51,8 @@ public:
moveToThread(receiver->thread());
}
explicit QHostInfoResult(const QObject *receiver, QtPrivate::SlotObjUniquePtr slot);
~QHostInfoResult() override;
void postResultsReady(const QHostInfo &info);
@ -145,6 +147,8 @@ class QHostInfoRunnable : public QRunnable
public:
QHostInfoRunnable(const QString &hn, int i, const QObject *receiver,
QtPrivate::QSlotObjectBase *slotObj);
explicit QHostInfoRunnable(const QString &hn, int i, const QObject *receiver,
QtPrivate::SlotObjUniquePtr slotObj);
~QHostInfoRunnable() override;
void run() override;