/* * Copyright 2014 Daniel Vrátil * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #ifndef ASYNC_H #define ASYNC_H #include #include #include #include #include #include #include "future.h" #include "async_impl.h" namespace Async { class JobBase; template class Job; template using ThenTask = typename detail::identity&)>>::type; template using EachTask = typename detail::identity&)>>::type; template using ReduceTask = typename detail::identity&)>>::type; template Job start(ThenTask func); class Executor { public: Executor(Executor *parent) : mPrev(parent) , mResult(0) { } virtual ~Executor() { delete mResult; } virtual void exec() = 0; FutureBase* result() const { return mResult; } Executor *mPrev; FutureBase *mResult; }; template class ThenExecutor: public Executor { public: ThenExecutor(ThenTask then, Executor *parent = nullptr) : Executor(parent) , mFunc(then) { } void exec() { typedef typename std::tuple_element<0, std::tuple>::type PrevOut; Async::Future *in = 0; if (mPrev) { mPrev->exec(); in = static_cast*>(mPrev->result()); assert(in->isFinished()); } auto out = new Async::Future(); mFunc(in ? in->value() : In() ..., *out); out->waitForFinished(); mResult = out; } private: std::function&)> mFunc; }; template class EachExecutor : public Executor { public: EachExecutor(EachTask each, Executor *parent = nullptr) : Executor(parent) , mFunc(each) { } void exec() { assert(mPrev); mPrev->exec(); Async::Future *in = static_cast*>(mPrev->result()); auto *out = new Async::Future(); for (auto arg : in->value()) { Async::Future future; mFunc(arg, future); future.waitForFinished(); out->setValue(out->value() + future.value()); } out->setFinished(); mResult = out; } private: std::function&)> mFunc; }; template class ReduceExecutor : public Executor { public: ReduceExecutor(ReduceTask reduce, Executor *parent = nullptr) : Executor(parent) , mFunc(reduce) { } void exec() { assert(mPrev); mPrev->exec(); Async::Future *in = static_cast*>(mPrev->result()); auto out = new Async::Future(); mFunc(in->value(), *out); out->waitForFinished(); mResult = out; } private: std::function &)> mFunc; }; class JobBase { template friend class Job; public: JobBase(Executor *executor); ~JobBase(); void exec(); protected: Executor *mExecutor; }; template class Job : public JobBase { template friend class Job; template friend Job start(Async::ThenTask func); public: template Job then(ThenTask func) { Executor *exec = new ThenExecutor(func, mExecutor); return Job(exec); } template Job each(EachTask func) { static_assert(detail::isIterable::value, "The 'Each' task can only be connected to a job that returns a list or array."); static_assert(detail::isIterable::value, "The result type of 'Each' task must be a list or an array."); return Job(new EachExecutor(func, mExecutor)); } template Job reduce(ReduceTask func) { static_assert(Async::detail::isIterable::value, "The 'Result' task can only be connected to a job that returns a list or array"); return Job(new ReduceExecutor(func, mExecutor)); } Async::Future result() const { return *static_cast*>(mExecutor->result()); } private: Job(Executor *executor) : JobBase(executor) { } }; } // namespace Async // ********** Out of line definitions **************** template Async::Job Async::start(ThenTask func) { return Job(new ThenExecutor(func)); } #endif // ASYNC_H