diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-01 10:59:36 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-01 10:59:36 +0200 |
commit | f407e12321dada7470e561cefd576031cd9e4168 (patch) | |
tree | c27aa2f26ae6110d9b46c015a963697c1a7b530c /async/src | |
parent | 08e35aad0b53364fb82e830f1b2e2ceb60658ea5 (diff) | |
download | sink-f407e12321dada7470e561cefd576031cd9e4168.tar.gz sink-f407e12321dada7470e561cefd576031cd9e4168.zip |
dowhile cleanup, second dowhile version without separate condition.
Diffstat (limited to 'async/src')
-rw-r--r-- | async/src/async.cpp | 40 | ||||
-rw-r--r-- | async/src/async.h | 40 |
2 files changed, 53 insertions, 27 deletions
diff --git a/async/src/async.cpp b/async/src/async.cpp index 6eefd1b..20ba4e6 100644 --- a/async/src/async.cpp +++ b/async/src/async.cpp | |||
@@ -47,3 +47,43 @@ JobBase::~JobBase() | |||
47 | { | 47 | { |
48 | } | 48 | } |
49 | 49 | ||
50 | static void asyncWhile(const std::function<void(std::function<void(bool)>)> &body, const std::function<void()> &completionHandler) { | ||
51 | body([body, completionHandler](bool complete) { | ||
52 | if (complete) { | ||
53 | completionHandler(); | ||
54 | } else { | ||
55 | asyncWhile(body, completionHandler); | ||
56 | } | ||
57 | }); | ||
58 | } | ||
59 | |||
60 | Job<void> Async::dowhile(Condition condition, ThenTask<void> body) | ||
61 | { | ||
62 | return Async::start<void>([body, condition](Async::Future<void> &future) { | ||
63 | asyncWhile([condition, body](std::function<void(bool)> whileCallback) { | ||
64 | Async::start<void>(body).then<void>([whileCallback, condition]() { | ||
65 | whileCallback(!condition()); | ||
66 | }).exec(); | ||
67 | }, | ||
68 | [&future]() { //while complete | ||
69 | future.setFinished(); | ||
70 | }); | ||
71 | }); | ||
72 | } | ||
73 | |||
74 | Job<void> Async::dowhile(ThenTask<bool> body) | ||
75 | { | ||
76 | return Async::start<void>([body](Async::Future<void> &future) { | ||
77 | asyncWhile([body](std::function<void(bool)> whileCallback) { | ||
78 | Async::start<bool>(body).then<bool, bool>([whileCallback](bool result) { | ||
79 | whileCallback(!result); | ||
80 | //FIXME this return value is only required because .then<bool, void> doesn't work | ||
81 | return true; | ||
82 | }).exec(); | ||
83 | }, | ||
84 | [&future]() { //while complete | ||
85 | future.setFinished(); | ||
86 | }); | ||
87 | }); | ||
88 | } | ||
89 | |||
diff --git a/async/src/async.h b/async/src/async.h index 0d1d81e..57879a7 100644 --- a/async/src/async.h +++ b/async/src/async.h | |||
@@ -227,8 +227,19 @@ template<typename ReturnType, typename KJobType, ReturnType (KJobType::*KJobResu | |||
227 | Job<ReturnType, Args ...> start(); | 227 | Job<ReturnType, Args ...> start(); |
228 | #endif | 228 | #endif |
229 | 229 | ||
230 | template<typename Out> | 230 | /** |
231 | Job<Out> dowhile(Condition condition, ThenTask<void> func); | 231 | * Async while loop. |
232 | * | ||
233 | * The loop continues while @param condition returns true. | ||
234 | */ | ||
235 | Job<void> dowhile(Condition condition, ThenTask<void> func); | ||
236 | |||
237 | /** | ||
238 | * Async while loop. | ||
239 | * | ||
240 | * Loop continues while body returns true. | ||
241 | */ | ||
242 | Job<void> dowhile(ThenTask<bool> body); | ||
232 | 243 | ||
233 | 244 | ||
234 | /** | 245 | /** |
@@ -249,7 +260,6 @@ Job<Out> null(); | |||
249 | template<typename Out> | 260 | template<typename Out> |
250 | Job<Out> error(int errorCode = 1, const QString &errorMessage = QString()); | 261 | Job<Out> error(int errorCode = 1, const QString &errorMessage = QString()); |
251 | 262 | ||
252 | |||
253 | class JobBase | 263 | class JobBase |
254 | { | 264 | { |
255 | template<typename Out, typename ... In> | 265 | template<typename Out, typename ... In> |
@@ -511,30 +521,6 @@ Job<ReturnType, Args ...> start() | |||
511 | } | 521 | } |
512 | #endif | 522 | #endif |
513 | 523 | ||
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 | 524 | ||
539 | template<typename Out> | 525 | template<typename Out> |
540 | Job<Out> null() | 526 | Job<Out> null() |