/* * Copyright 2014 Daniel Vrátil * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library. If not, see . */ #ifndef FUTURE_H #define FUTURE_H class QEventLoop; #include #include #include #include #include namespace Async { namespace Private { class Execution; class ExecutorBase; typedef QSharedPointer ExecutionPtr; } class FutureBase { friend class Async::Private::Execution; public: virtual ~FutureBase(); virtual void setFinished() = 0; virtual bool isFinished() const = 0; virtual void setError(int code = 1, const QString &message = QString()) = 0; protected: virtual void releaseExecution() = 0; class PrivateBase : public QSharedData { public: PrivateBase(const Async::Private::ExecutionPtr &execution); virtual ~PrivateBase(); void releaseExecution(); private: QWeakPointer mExecution; }; FutureBase(); FutureBase(const FutureBase &other); }; template class FutureWatcher; template class Future; template class FutureGeneric : public FutureBase { friend class FutureWatcher; public: void setFinished() { if (d->finished) { return; } d->finished = true; for (auto watcher : d->watchers) { if (watcher) { watcher->futureReadyCallback(); } } } bool isFinished() const { return d->finished; } void setError(int errorCode, const QString &message) { d->errorCode = errorCode; d->errorMessage = message; setFinished(); } int errorCode() const { return d->errorCode; } QString errorMessage() const { return d->errorMessage; } void waitForFinished() { if (isFinished()) { return; } FutureWatcher watcher; QEventLoop eventLoop; QObject::connect(&watcher, &Async::FutureWatcher::futureReady, &eventLoop, &QEventLoop::quit); watcher.setFuture(*static_cast*>(this)); eventLoop.exec(); } protected: FutureGeneric(const Async::Private::ExecutionPtr &execution) : FutureBase() , d(new Private(execution)) {} FutureGeneric(const FutureGeneric &other) : FutureBase(other) , d(other.d) {} class Private : public FutureBase::PrivateBase { public: Private(const Async::Private::ExecutionPtr &execution) : FutureBase::PrivateBase(execution) , finished(false) , errorCode(0) {} typename std::conditional::value, int /* dummy */, T>::type value; QVector>> watchers; bool finished; int errorCode; QString errorMessage; }; QExplicitlySharedDataPointer d; void releaseExecution() { d->releaseExecution(); } void addWatcher(FutureWatcher *watcher) { d->watchers.append(QPointer>(watcher)); } }; template class Future : public FutureGeneric { friend class Async::Private::ExecutorBase; template friend class Async::FutureWatcher; public: Future() : FutureGeneric(Async::Private::ExecutionPtr()) {} Future(const Future &other) : FutureGeneric(other) {} void setValue(const T &value) { this->d->value = value; } T value() const { return this->d->value; } protected: Future(const Async::Private::ExecutionPtr &execution) : FutureGeneric(execution) {} }; template<> class Future : public FutureGeneric { friend class Async::Private::ExecutorBase; template friend class Async::FutureWatcher; public: Future() : FutureGeneric(Async::Private::ExecutionPtr()) {} Future(const Future &other) : FutureGeneric(other) {} protected: Future(const Async::Private::ExecutionPtr &execution) : FutureGeneric(execution) {} }; class FutureWatcherBase : public QObject { Q_OBJECT protected: FutureWatcherBase(QObject *parent = nullptr); virtual ~FutureWatcherBase(); Q_SIGNALS: void futureReady(); }; template class FutureWatcher : public FutureWatcherBase { friend class Async::FutureGeneric; public: FutureWatcher(QObject *parent = nullptr) : FutureWatcherBase(parent) {} ~FutureWatcher() {} void setFuture(const Async::Future &future) { mFuture = future; mFuture.addWatcher(this); if (future.isFinished()) { futureReadyCallback(); } } Async::Future future() const { return mFuture; } private: void futureReadyCallback() { Q_EMIT futureReady(); } Async::Future mFuture; }; } // namespace Async #endif // FUTURE_H