From 81b8158cca02defbfce5f1d59f06f7448fb5ea57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Vr=C3=A1til?= Date: Thu, 11 Dec 2014 12:24:31 +0100 Subject: Async: move some definitions out-of-line --- async/src/async.cpp | 10 ++++++- async/src/async.h | 76 +++++++++++++++++++++++++++-------------------------- 2 files changed, 48 insertions(+), 38 deletions(-) (limited to 'async') diff --git a/async/src/async.cpp b/async/src/async.cpp index 5c89e53..7e81f24 100644 --- a/async/src/async.cpp +++ b/async/src/async.cpp @@ -28,11 +28,18 @@ using namespace Async; +JobBase::JobBase(JobBase::JobType jobType, JobBase* prev) + : mPrev(prev) + , mResult(0) + , mJobType(jobType) +{ +} + + FutureBase::FutureBase() : mFinished(false) , mWaitLoop(nullptr) { - } FutureBase::FutureBase(const Async::FutureBase &other) @@ -65,3 +72,4 @@ void FutureBase::waitForFinished() delete mWaitLoop; mWaitLoop = 0; } + diff --git a/async/src/async.h b/async/src/async.h index 3fee450..239927d 100644 --- a/async/src/async.h +++ b/async/src/async.h @@ -39,8 +39,8 @@ class JobBase; template class Job; -template -Job start(const std::function(In ...)> &func); +template +Job start(F func); namespace Private { @@ -50,9 +50,6 @@ namespace Private class JobBase { - template - friend Async::Future* Private::doExec(Job *job, const In & ... args); - template friend class Job; @@ -63,23 +60,15 @@ protected: }; public: - JobBase(JobType jobType, JobBase *prev = nullptr) - : mPrev(prev) - , mResult(0) - , mJobType(jobType) - {} - + JobBase(JobType jobType, JobBase *prev = nullptr); virtual void exec() = 0; protected: JobBase *mPrev; void *mResult; - JobType mJobType; }; - - template class Job : public JobBase { @@ -115,20 +104,7 @@ public: return *reinterpret_cast*>(mResult); } - void exec() - { - Async::Future *in = nullptr; - if (mPrev) { - mPrev->exec(); - in = reinterpret_cast*>(mPrev->mResult); - assert(in->isFinished()); - } - - Job *job = dynamic_cast*>(this); - Async::Future *out = Private::doExec(this, in ? in->value() : In() ...); - out->waitForFinished(); - job->mResult = reinterpret_cast(out); - } + void exec(); private: Job(JobBase::JobType jobType, JobBase *parent = nullptr) @@ -137,31 +113,57 @@ private: } template - static Job create(F func, JobBase::JobType jobType, JobBase *parent = nullptr) - { - Job job(jobType, parent); - job.mFunc = func; - return job; - } + static Job create(F func, JobBase::JobType jobType, JobBase *parent = nullptr); public: std::function(In ...)> mFunc; }; + +} // namespace Async + + + +// ********** Out of line definitions **************** + template -Job start(F func) +Async::Job Async::start(F func) { return Job::create(func, JobBase::Then); } -} // namespace Async - template Async::Future* Async::Private::doExec(Job *job, const In & ... args) { return new Async::Future(job->mFunc(args ...)); }; +template +void Async::Job::exec() +{ + Async::Future *in = nullptr; + if (mPrev) { + mPrev->exec(); + in = reinterpret_cast*>(mPrev->mResult); + assert(in->isFinished()); + } + + Job *job = dynamic_cast*>(this); + Async::Future *out = Private::doExec(this, in ? in->value() : In() ...); + out->waitForFinished(); + job->mResult = reinterpret_cast(out); +} + +template +template +Async::Job Async::Job::create(F func, Async::JobBase::JobType jobType, Async::JobBase* parent) +{ + Job job(jobType, parent); + job.mFunc = func; + return job; +} + + #endif // ASYNC_H -- cgit v1.2.3