diff options
Diffstat (limited to 'async/src/async.h')
-rw-r--r-- | async/src/async.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/async/src/async.h b/async/src/async.h index 8f6457b..0d1d81e 100644 --- a/async/src/async.h +++ b/async/src/async.h | |||
@@ -85,6 +85,7 @@ template<typename Out, typename In> | |||
85 | using SyncReduceTask = typename detail::identity<std::function<Out(In)>>::type; | 85 | using SyncReduceTask = typename detail::identity<std::function<Out(In)>>::type; |
86 | 86 | ||
87 | using ErrorHandler = std::function<void(int, const QString &)>; | 87 | using ErrorHandler = std::function<void(int, const QString &)>; |
88 | using Condition = std::function<bool()>; | ||
88 | 89 | ||
89 | namespace Private | 90 | namespace Private |
90 | { | 91 | { |
@@ -226,6 +227,9 @@ template<typename ReturnType, typename KJobType, ReturnType (KJobType::*KJobResu | |||
226 | Job<ReturnType, Args ...> start(); | 227 | Job<ReturnType, Args ...> start(); |
227 | #endif | 228 | #endif |
228 | 229 | ||
230 | template<typename Out> | ||
231 | Job<Out> dowhile(Condition condition, ThenTask<void> func); | ||
232 | |||
229 | 233 | ||
230 | /** | 234 | /** |
231 | * A null job. | 235 | * A null job. |
@@ -507,6 +511,31 @@ Job<ReturnType, Args ...> start() | |||
507 | } | 511 | } |
508 | #endif | 512 | #endif |
509 | 513 | ||
514 | static void asyncWhile(const std::function<void(std::function<void(bool)>)> &body, const std::function<void()> &completionHandler) { | ||
515 | body([body, completionHandler](bool complete) { | ||
516 | if (complete) { | ||
517 | completionHandler(); | ||
518 | } else { | ||
519 | asyncWhile(body, completionHandler); | ||
520 | } | ||
521 | }); | ||
522 | } | ||
523 | } | ||
524 | template<typename Out> | ||
525 | Job<Out> dowhile(Condition condition, ThenTask<void> body) | ||
526 | { | ||
527 | return Async::start<void>([body, condition](Async::Future<void> &future) { | ||
528 | asyncWhile([condition, body](std::function<void(bool)> whileCallback) { | ||
529 | Async::start<void>(body).then<void>([whileCallback, condition]() { | ||
530 | whileCallback(!condition()); | ||
531 | }).exec(); | ||
532 | }, | ||
533 | [&future]() { //while complete | ||
534 | future.setFinished(); | ||
535 | }); | ||
536 | }); | ||
537 | } | ||
538 | |||
510 | template<typename Out> | 539 | template<typename Out> |
511 | Job<Out> null() | 540 | Job<Out> null() |
512 | { | 541 | { |