diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2023-09-04 21:18:13 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2023-09-06 13:23:15 +0200 |
commit | 797edbc58c76d6070db30e2f5b0d1a4b907bf7e1 (patch) | |
tree | 562403cbf0418da510a265d416d3e1ea4edeb5f7 /src/network/kernel | |
parent | 8550d60acaead1f5a148f8e8e8384798dc6cd071 (diff) |
QHostInfoResult: use the same semantics as the contextless connect()
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 <volker.hilsheimer@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src/network/kernel')
-rw-r--r-- | src/network/kernel/qhostinfo.cpp | 18 | ||||
-rw-r--r-- | 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<QMetaCallEvent *>(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<QMetaCallEvent *>(event); + auto args = metaCallEvent->args(); slotObj->call(const_cast<QObject*>(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<const QObject> receiver = nullptr; QtPrivate::SlotObjUniquePtr slotObj; - const bool withContextObject = false; }; class QHostInfoAgent |