diff options
author | Dan Vrátil <dvratil@redhat.com> | 2015-02-20 13:13:56 +0100 |
---|---|---|
committer | Dan Vrátil <dvratil@redhat.com> | 2015-02-20 13:52:10 +0100 |
commit | 1da08ebfe267313015c201fd1106f891af554e14 (patch) | |
tree | 0c535e0b9a356f3ca6d0a6309dd9153c1882b088 /async/src | |
parent | 5c8714d3b29fbed600b367bdad1e695674a0e551 (diff) | |
download | sink-1da08ebfe267313015c201fd1106f891af554e14.tar.gz sink-1da08ebfe267313015c201fd1106f891af554e14.zip |
Async: allow Async::start() to have input value
The initial value can be passed in as argument to Job::exec(), or by another
job in case of job chaining.
Diffstat (limited to 'async/src')
-rw-r--r-- | async/src/async.h | 58 |
1 files changed, 44 insertions, 14 deletions
diff --git a/async/src/async.h b/async/src/async.h index 1f44f10..386722a 100644 --- a/async/src/async.h +++ b/async/src/async.h | |||
@@ -31,6 +31,7 @@ | |||
31 | #include <QObject> | 31 | #include <QObject> |
32 | #include <QSharedPointer> | 32 | #include <QSharedPointer> |
33 | 33 | ||
34 | #include <QDebug> | ||
34 | 35 | ||
35 | /* | 36 | /* |
36 | * TODO: instead of passing the future objects callbacks could be provided for result reporting (we can still use the future object internally | 37 | * TODO: instead of passing the future objects callbacks could be provided for result reporting (we can still use the future object internally |
@@ -44,6 +45,7 @@ class JobBase; | |||
44 | 45 | ||
45 | template<typename Out, typename ... In> | 46 | template<typename Out, typename ... In> |
46 | class Job; | 47 | class Job; |
48 | |||
47 | template<typename Out, typename ... In> | 49 | template<typename Out, typename ... In> |
48 | using ThenTask = typename detail::identity<std::function<void(In ..., Async::Future<Out>&)>>::type; | 50 | using ThenTask = typename detail::identity<std::function<void(In ..., Async::Future<Out>&)>>::type; |
49 | template<typename Out, typename ... In> | 51 | template<typename Out, typename ... In> |
@@ -71,6 +73,9 @@ class ExecutorBase | |||
71 | template<typename PrevOut, typename Out, typename ... In> | 73 | template<typename PrevOut, typename Out, typename ... In> |
72 | friend class Executor; | 74 | friend class Executor; |
73 | 75 | ||
76 | template<typename Out, typename ... In> | ||
77 | friend class Async::Job; | ||
78 | |||
74 | public: | 79 | public: |
75 | virtual ~ExecutorBase(); | 80 | virtual ~ExecutorBase(); |
76 | virtual void exec() = 0; | 81 | virtual void exec() = 0; |
@@ -178,8 +183,11 @@ private: | |||
178 | * void return type, and accept exactly one argument of type @p Async::Future<In>, | 183 | * void return type, and accept exactly one argument of type @p Async::Future<In>, |
179 | * where @p In is type of the result. | 184 | * where @p In is type of the result. |
180 | */ | 185 | */ |
181 | template<typename Out> | 186 | template<typename Out, typename ... In> |
182 | Job<Out> start(ThenTask<Out> func); | 187 | Job<Out, In ...> start(ThenTask<Out, In ...> func); |
188 | |||
189 | template<typename Out, typename ... In> | ||
190 | Job<Out, In ...> start(SyncThenTask<Out, In ...> func); | ||
183 | 191 | ||
184 | 192 | ||
185 | /** | 193 | /** |
@@ -262,11 +270,11 @@ class Job : public JobBase | |||
262 | template<typename OutOther, typename ... InOther> | 270 | template<typename OutOther, typename ... InOther> |
263 | friend class Job; | 271 | friend class Job; |
264 | 272 | ||
265 | template<typename OutOther> | 273 | template<typename OutOther, typename ... InOther> |
266 | friend Job<OutOther> start(Async::ThenTask<OutOther> func); | 274 | friend Job<OutOther, InOther ...> start(Async::ThenTask<OutOther, InOther ...> func); |
267 | 275 | ||
268 | template<typename OutOther> | 276 | template<typename OutOther, typename ... InOther> |
269 | friend Job<OutOther> start(Async::SyncThenTask<OutOther> func); | 277 | friend Job<OutOther, InOther ...> start(Async::SyncThenTask<OutOther, InOther ...> func); |
270 | 278 | ||
271 | public: | 279 | public: |
272 | template<typename OutOther, typename ... InOther> | 280 | template<typename OutOther, typename ... InOther> |
@@ -315,6 +323,27 @@ public: | |||
315 | new Private::SyncReduceExecutor<OutOther, InOther>(func, errorFunc, mExecutor))); | 323 | new Private::SyncReduceExecutor<OutOther, InOther>(func, errorFunc, mExecutor))); |
316 | } | 324 | } |
317 | 325 | ||
326 | template<typename FirstIn> | ||
327 | Async::Future<Out> exec(FirstIn in) | ||
328 | { | ||
329 | // Inject a fake sync executor that will return the initial value | ||
330 | Private::ExecutorBasePtr first = mExecutor; | ||
331 | while (first->mPrev) { | ||
332 | first = first->mPrev; | ||
333 | } | ||
334 | auto init = new Private::SyncThenExecutor<FirstIn>( | ||
335 | [in]() -> FirstIn { | ||
336 | return in; | ||
337 | }, | ||
338 | ErrorHandler(), Private::ExecutorBasePtr()); | ||
339 | first->mPrev = Private::ExecutorBasePtr(init); | ||
340 | |||
341 | auto result = exec(); | ||
342 | // Remove the injected executor | ||
343 | first->mPrev.reset(); | ||
344 | return result; | ||
345 | } | ||
346 | |||
318 | Async::Future<Out> exec() | 347 | Async::Future<Out> exec() |
319 | { | 348 | { |
320 | mExecutor->exec(); | 349 | mExecutor->exec(); |
@@ -357,18 +386,18 @@ private: | |||
357 | 386 | ||
358 | namespace Async { | 387 | namespace Async { |
359 | 388 | ||
360 | template<typename Out> | 389 | template<typename Out, typename ... In> |
361 | Job<Out> start(ThenTask<Out> func) | 390 | Job<Out, In ...> start(ThenTask<Out, In ...> func) |
362 | { | 391 | { |
363 | return Job<Out>(Private::ExecutorBasePtr( | 392 | return Job<Out, In...>(Private::ExecutorBasePtr( |
364 | new Private::ThenExecutor<Out>(func, ErrorHandler(), Private::ExecutorBasePtr()))); | 393 | new Private::ThenExecutor<Out, In ...>(func, ErrorHandler(), Private::ExecutorBasePtr()))); |
365 | } | 394 | } |
366 | 395 | ||
367 | template<typename Out> | 396 | template<typename Out, typename ... In> |
368 | Job<Out> start(SyncThenTask<Out> func) | 397 | Job<Out, In ...> start(SyncThenTask<Out, In ...> func) |
369 | { | 398 | { |
370 | return Job<Out>(Private::ExecutorBasePtr( | 399 | return Job<Out, In...>(Private::ExecutorBasePtr( |
371 | new Private::SyncThenExecutor<Out>(func, ErrorHandler(), Private::ExecutorBasePtr()))); | 400 | new Private::SyncThenExecutor<Out, In ...>(func, ErrorHandler(), Private::ExecutorBasePtr()))); |
372 | } | 401 | } |
373 | 402 | ||
374 | template<typename Out> | 403 | template<typename Out> |
@@ -443,6 +472,7 @@ void Executor<PrevOut, Out, In ...>::exec() | |||
443 | } | 472 | } |
444 | } | 473 | } |
445 | 474 | ||
475 | |||
446 | template<typename Out, typename ... In> | 476 | template<typename Out, typename ... In> |
447 | ThenExecutor<Out, In ...>::ThenExecutor(ThenTask<Out, In ...> then, ErrorHandler error, const ExecutorBasePtr &parent) | 477 | ThenExecutor<Out, In ...>::ThenExecutor(ThenTask<Out, In ...> then, ErrorHandler error, const ExecutorBasePtr &parent) |
448 | : Executor<typename detail::prevOut<In ...>::type, Out, In ...>(error, parent) | 478 | : Executor<typename detail::prevOut<In ...>::type, Out, In ...>(error, parent) |