/* SPDX-FileCopyrightText: 2013-2016 Ivan Cukic SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL */ #ifndef ACTIVITIES_DBUSFUTURE_P_H #define ACTIVITIES_DBUSFUTURE_P_H #include #include #include #include #include #include #include #include "debug_p.h" namespace DBusFuture { namespace detail { //_ template class DBusCallFutureInterface : public QObject, public QFutureInterface<_Result> { public: DBusCallFutureInterface(QDBusPendingReply<_Result> reply) : reply(reply) , replyWatcher(nullptr) { } ~DBusCallFutureInterface() override { delete replyWatcher; } void callFinished(); QFuture<_Result> start() { replyWatcher = new QDBusPendingCallWatcher(reply); QObject::connect(replyWatcher, &QDBusPendingCallWatcher::finished, [this]() { callFinished(); }); this->reportStarted(); if (reply.isFinished()) { this->callFinished(); } return this->future(); } private: QDBusPendingReply<_Result> reply; QDBusPendingCallWatcher *replyWatcher; }; template void DBusCallFutureInterface<_Result>::callFinished() { deleteLater(); if (!reply.isError()) { this->reportResult(reply.value()); } this->reportFinished(); } template<> void DBusCallFutureInterface::callFinished(); template class ValueFutureInterface : public QObject, QFutureInterface<_Result> { public: ValueFutureInterface(const _Result &value) : value(value) { } QFuture<_Result> start() { auto future = this->future(); this->reportResult(value); this->reportFinished(); deleteLater(); return future; } private: _Result value; }; template<> class ValueFutureInterface : public QObject, QFutureInterface { public: ValueFutureInterface(); QFuture start(); // { // auto future = this->future(); // this->reportFinished(); // deleteLater(); // return future; // } }; } //^ namespace detail template QFuture<_Result> asyncCall(QDBusAbstractInterface *interface, const QString &method, Args &&...args) { using namespace detail; auto callFutureInterface = new DBusCallFutureInterface<_Result>(interface->asyncCall(method, std::forward(args)...)); return callFutureInterface->start(); } template QFuture<_Result> fromValue(const _Result &value) { using namespace detail; auto valueFutureInterface = new ValueFutureInterface<_Result>(value); return valueFutureInterface->start(); } template QFuture<_Result> fromReply(const QDBusPendingReply<_Result> &reply) { using namespace detail; auto callFutureInterface = new DBusCallFutureInterface<_Result>(reply); return callFutureInterface->start(); } QFuture fromVoid(); } // namespace DBusFuture #endif /* ACTIVITIES_DBUSFUTURE_P_H */