Issue #15 Created CPromise, a way to set the result of QFuture objects

This enables several new features:
* singleShot can return a QFuture.
* a version of invokeMethod that returns a QFuture.
* CGenericDBusInterface::callDBusFuture, which returns a QFuture.
This commit is contained in:
Mat Sutcliffe
2019-02-26 01:51:14 +00:00
parent 07f6c8b73c
commit 6aa9f0cc25
6 changed files with 264 additions and 7 deletions

View File

@@ -12,12 +12,14 @@
#define BLACKMISC_GENERICDBUSINTERFACE_H
#include "logmessage.h"
#include "blackmisc/promise.h"
#include <QDBusAbstractInterface>
#include <QDBusPendingCall>
#include <QDBusPendingReply>
#include <QDBusError>
#include <QObject>
#include <QMetaMethod>
#include <QSharedPointer>
#ifndef Q_MOC_RUN
/*!
@@ -102,6 +104,15 @@ namespace BlackMisc
return pcw;
}
//! Call DBus with asynchronous return as a future
template <typename Ret, typename... Args>
QFuture<Ret> callDBusFuture(QLatin1String method, Args&&... args)
{
auto sharedPromise = QSharedPointer<CPromise<Ret>>::create();
this->callDBusAsync(method, [ = ](auto pcw) { sharedPromise->setResult(QDBusPendingReply<Ret>(*pcw)); }, std::forward<Args>(args)...);
return sharedPromise->future();
}
//! Cancel all asynchronous DBus calls which are currently pending
//! \warning Don't call this from inside an async callback!
void cancelAllPendingAsyncCalls()