From 797edbc58c76d6070db30e2f5b0d1a4b907bf7e1 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 4 Sep 2023 21:18:13 +0200 Subject: [PATCH] QHostInfoResult: use the same semantics as the contextless connect() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 3-arg QObject::connect() overload (the "contextless" one) simply forwards to the 4-arg overload, using the sender object as both the sender and the receiver/context. QHostInfo does not use connect() directly in order to emit notifications that a lookup is finished. Instead, it uses some of QObject private plumbing. When handling a lookup request that specifies a function to call when done, but no context object, QHostInfo passes nullptr as context/receiver. Change QHostInfoResult's behavior to always have a "receiver": in case the caller didn't specify one, use `this` (= the sender). As a drive-by, this allows to streamline some code. Change-Id: Ic2e63ac18ba36269036950b6f6b7fecea51d0c99 Reviewed-by: Volker Hilsheimer Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Timur Pocheptsov --- src/network/kernel/qhostinfo.cpp | 18 ++++++++++-------- src/network/kernel/qhostinfo_p.h | 7 ++++--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/network/kernel/qhostinfo.cpp b/src/network/kernel/qhostinfo.cpp index 6662ae1eee..dab4d055ed 100644 --- a/src/network/kernel/qhostinfo.cpp +++ b/src/network/kernel/qhostinfo.cpp @@ -73,10 +73,10 @@ Q_APPLICATION_STATIC(QHostInfoLookupManager, theHostInfoLookupManager) } QHostInfoResult::QHostInfoResult(const QObject *receiver, QtPrivate::SlotObjUniquePtr slot) - : receiver{receiver}, slotObj{std::move(slot)}, withContextObject{slotObj && receiver} + : receiver{receiver ? receiver : this}, slotObj{std::move(slot)} { - if (receiver) - moveToThread(receiver->thread()); + Q_ASSERT(this->receiver); + moveToThread(this->receiver->thread()); } QHostInfoResult::~QHostInfoResult() @@ -101,7 +101,7 @@ void QHostInfoResult::postResultsReady(const QHostInfo &info) return; } // we used to have a context object, but it's already destroyed - if (withContextObject && !receiver) + if (!receiver) return; static const int signal_index = []() -> int { @@ -129,11 +129,13 @@ bool QHostInfoResult::event(QEvent *event) { if (event->type() == QEvent::MetaCall) { Q_ASSERT(slotObj); - auto metaCallEvent = static_cast(event); - auto args = metaCallEvent->args(); - // we didn't have a context object, or it's still alive - if (!withContextObject || receiver) + + // we used to have a context object, but it's already destroyed + if (receiver) { + auto metaCallEvent = static_cast(event); + auto args = metaCallEvent->args(); slotObj->call(const_cast(receiver.data()), args); + } deleteLater(); return true; diff --git a/src/network/kernel/qhostinfo_p.h b/src/network/kernel/qhostinfo_p.h index 70cacc6b3d..4c0d5010fc 100644 --- a/src/network/kernel/qhostinfo_p.h +++ b/src/network/kernel/qhostinfo_p.h @@ -56,8 +56,8 @@ protected: private: QHostInfoResult(const QHostInfoResult *other) - : receiver(other->receiver), slotObj{copy(other->slotObj)}, - withContextObject(other->withContextObject) + : receiver(other->receiver.get() != other ? other->receiver.get() : this), + slotObj{copy(other->slotObj)} { // cleanup if the application terminates before results are delivered connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, @@ -66,9 +66,10 @@ private: moveToThread(other->thread()); } + // receiver is either a QObject provided by the user, + // or it's set to `this` (to emulate the behavior of the contextless connect()) QPointer receiver = nullptr; QtPrivate::SlotObjUniquePtr slotObj; - const bool withContextObject = false; }; class QHostInfoAgent