/* * 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 { class FutureBase { public: virtual ~FutureBase(); virtual void setFinished() = 0; virtual bool isFinished() const = 0; virtual void setError(int code = 1, const QString &message = QString()) = 0; protected: 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() : FutureBase() , d(new Private) {} FutureGeneric(const FutureGeneric &other) : FutureBase(other) , d(other.d) {} class Private : public QSharedData { public: Private() : QSharedData(), finished(false), errorCode(0) {} typename std::conditional::value, int /* dummy */, T>::type value; QVector>> watchers; bool finished; int errorCode; QString errorMessage; }; QExplicitlySharedDataPointer d; void addWatcher(FutureWatcher *watcher) { d->watchers.append(QPointer>(watcher)); } }; template class Future : public FutureGeneric { public: Future() : FutureGeneric() {} Future(const Future &other) : FutureGeneric(other) {} void setValue(const T &value) { this->d->value = value; } T value() const { return this->d->value; } }; template<> class Future : public FutureGeneric { public: Future() : FutureGeneric() {} Future(const Future &other) : FutureGeneric(other) {} }; class FutureWatcherBase : public QObject { Q_OBJECT protected: FutureWatcherBase(QObject *parent = 0); virtual ~FutureWatcherBase(); Q_SIGNALS: void futureReady(); }; template class FutureWatcher : public FutureWatcherBase { friend class Async::FutureGeneric; public: FutureWatcher(QObject *parent = 0) : 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