mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 08:36:52 +08:00
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:
@@ -11,10 +11,14 @@
|
||||
#ifndef BLACKMISC_INVOKE_H
|
||||
#define BLACKMISC_INVOKE_H
|
||||
|
||||
#include <tuple>
|
||||
#include "blackmisc/typetraits.h"
|
||||
#include "blackmisc/integersequence.h"
|
||||
#include "blackmisc/promise.h"
|
||||
#include <QMetaObject>
|
||||
#include <QtGlobal>
|
||||
#include <QThread>
|
||||
#include <QTimer>
|
||||
#include <tuple>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -59,6 +63,17 @@ namespace BlackMisc
|
||||
return invokeSlotImpl(std::forward<F>(func), object, std::forward_as_tuple(std::forward<Ts>(args)...), seq(), std::is_member_pointer<std::decay_t<F>>());
|
||||
}
|
||||
|
||||
// Like QMetaObject::invokeMethod but the return value is accessed through a QFuture, and extra arguments can be provided.
|
||||
template <typename T, typename F, typename... Ts>
|
||||
auto invokeMethod(T *object, F &&func, Ts &&... args)
|
||||
{
|
||||
const auto invoker = [](auto &&... x) { return Private::invokeSlot(std::forward<decltype(x)>(x)...); };
|
||||
auto method = std::bind(invoker, std::forward<F>(func), object, std::forward<Ts>(args)...);
|
||||
CPromise<decltype(std::move(method)())> promise;
|
||||
QMetaObject::invokeMethod(object, [promise, method = std::move(method)]() mutable { promise.setResultFrom(std::move(method)); });
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
}
|
||||
//! \endcond
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user