Skip to content

Add await functions for handling QFutures (from other threads)

Linus Jahn requested to merge lnj/kaidan:future-utils into master

This adds two await functions:

  1. Just attaches a QFutureWatcher to a given QFuture and executes a given lambda with the future result.
  2. Executes a lambda on another thread (using a run() function) and handles the returned QFuture (from the other thread) on the caller thread.

Example for the simple case:

await(discoveryManager->requestDiscoInfo("groups.kaidan.im"), this,
      [](QXmppDiscoveryManager::InfoResult &&result) {
	if (const auto info = std::get_if<QXmppDiscoveryIq>(&result)) {
		qDebug() << info->features();
	} else {
		qDebug() << std::get<QXmppStanza::Error>(result).text();
	}
});

Example usage for the advanced case:

await(m_client, [=] {
	// XMPP thread (m_client)
	return discoveryManager->requestDiscoInfo("groups.kaidan.im");
}, this, [](QXmppDiscoveryManager::InfoResult &&result) {
	// caller thread (this)
	if (const auto info = std::get_if<QXmppDiscoveryIq>(&result)) {
		qDebug() << info->features();
	} else {
		qDebug() << std::get<QXmppStanza::Error>(result).text();
	}
});

m_client needs to have a run(std::function) function, that can be added using the RUN_FUNCTION() macro.

Currently this is unused, but we're going to need it as soon as we start to use the new QFuture APIs from QXmpp.

Merge request reports

Loading