diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-01 01:35:51 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-01 01:35:51 +0200 |
commit | a40160b8f8930dbe2a0abde4b45e5796264f5782 (patch) | |
tree | 93787b92bfd1873e8f5138db3b0f11ddffef3461 | |
parent | 2cc09c118f73b2f71b48eb1fd57eea7a49a022dd (diff) | |
download | sink-a40160b8f8930dbe2a0abde4b45e5796264f5782.tar.gz sink-a40160b8f8930dbe2a0abde4b45e5796264f5782.zip |
Added Async::dowhile
-rw-r--r-- | async/autotests/asynctest.cpp | 19 | ||||
-rw-r--r-- | async/src/async.h | 29 |
2 files changed, 48 insertions, 0 deletions
diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index 85f5ea5..a387dd6 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp | |||
@@ -68,6 +68,8 @@ private Q_SLOTS: | |||
68 | void testLifetimeWithoutHandle(); | 68 | void testLifetimeWithoutHandle(); |
69 | void testLifetimeWithHandle(); | 69 | void testLifetimeWithHandle(); |
70 | 70 | ||
71 | void testWhile(); | ||
72 | |||
71 | void benchmarkSyncThenExecutor(); | 73 | void benchmarkSyncThenExecutor(); |
72 | 74 | ||
73 | private: | 75 | private: |
@@ -377,6 +379,23 @@ void AsyncTest::testVoidEach() | |||
377 | QCOMPARE(check, expected); | 379 | QCOMPARE(check, expected); |
378 | } | 380 | } |
379 | 381 | ||
382 | void AsyncTest::testWhile() | ||
383 | { | ||
384 | |||
385 | QList<int> processed; | ||
386 | QList<int> list({1, 2, 3, 4}); | ||
387 | auto it = QSharedPointer<QListIterator<int> >::create(list); | ||
388 | Async::dowhile<void>( | ||
389 | [it]() -> bool { return it->hasNext(); }, | ||
390 | [it, &processed](Async::Future<void> future) { | ||
391 | auto value = it->next(); | ||
392 | processed << value; | ||
393 | future.setFinished(); | ||
394 | } | ||
395 | ).exec().waitForFinished(); | ||
396 | QCOMPARE(processed, list); | ||
397 | } | ||
398 | |||
380 | 399 | ||
381 | 400 | ||
382 | 401 | ||
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 | { |