From 284b751ee424985341812a32721227112160a905 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 27 Jul 2018 14:32:52 +0900 Subject: connectSingleShot() Time and again I need one-off slots that disconnect once they are done. The code has been inside Connection implementation for quite some time, now it's put to the interface for usage in other places (notably qmc-example). --- lib/connection.cpp | 6 +----- lib/connection.h | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/connection.cpp b/lib/connection.cpp index 2881f7f4..8007cea1 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -264,13 +264,9 @@ void Connection::checkAndConnect(const QString& userId, // Not good to go, try to fix the homeserver URL. if (userId.startsWith('@') && userId.indexOf(':') != -1) { - // The below construct makes a single-shot connection that triggers - // on the signal and then self-disconnects. + connectSingleShot(this, &Connection::homeserverChanged, this, connectFn); // NB: doResolveServer can emit resolveError, so this is a part of // checkAndConnect function contract. - QMetaObject::Connection connection; - connection = connect(this, &Connection::homeserverChanged, - this, [=] { connectFn(); disconnect(connection); }); resolveServer(userId); } else emit resolveError( diff --git a/lib/connection.h b/lib/connection.h index c7e28282..7adab883 100644 --- a/lib/connection.h +++ b/lib/connection.h @@ -48,6 +48,26 @@ namespace QMatrixClient class DownloadFileJob; class SendToDeviceJob; + /** Create a single-shot connection that triggers on the signal and + * then self-disconnects + * + * Only supports DirectConnection type. + */ + template + inline auto connectSingleShot(SenderT1* sender, SignalT signal, + ReceiverT2* receiver, SlotT slot) + { + QMetaObject::Connection connection; + connection = QObject::connect(sender, signal, receiver, slot, + Qt::DirectConnection); + Q_ASSERT(connection); + QObject::connect(sender, signal, receiver, + [connection] { QObject::disconnect(connection); }, + Qt::DirectConnection); + return connection; + } + /** Enumeration with flags defining the network job running policy * So far only background/foreground flags are available. * -- cgit v1.2.3