From f407e12321dada7470e561cefd576031cd9e4168 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 1 Apr 2015 10:59:36 +0200 Subject: dowhile cleanup, second dowhile version without separate condition. --- async/autotests/asynctest.cpp | 19 ++++++++++++++++++- async/src/async.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ async/src/async.h | 40 +++++++++++++--------------------------- 3 files changed, 71 insertions(+), 28 deletions(-) diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index a387dd6..78a834e 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp @@ -69,6 +69,7 @@ private Q_SLOTS: void testLifetimeWithHandle(); void testWhile(); + void testWhileWithoutCondition(); void benchmarkSyncThenExecutor(); @@ -385,7 +386,7 @@ void AsyncTest::testWhile() QList processed; QList list({1, 2, 3, 4}); auto it = QSharedPointer >::create(list); - Async::dowhile( + Async::dowhile( [it]() -> bool { return it->hasNext(); }, [it, &processed](Async::Future future) { auto value = it->next(); @@ -396,6 +397,22 @@ void AsyncTest::testWhile() QCOMPARE(processed, list); } +void AsyncTest::testWhileWithoutCondition() +{ + + QList processed; + QList list({1, 2, 3, 4}); + auto it = QSharedPointer >::create(list); + Async::dowhile( + [it, &processed](Async::Future future) { + auto value = it->next(); + processed << value; + future.setValue(it->hasNext()); + future.setFinished(); + } + ).exec().waitForFinished(); + QCOMPARE(processed, list); +} 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() { } +static void asyncWhile(const std::function)> &body, const std::function &completionHandler) { + body([body, completionHandler](bool complete) { + if (complete) { + completionHandler(); + } else { + asyncWhile(body, completionHandler); + } + }); +} + +Job Async::dowhile(Condition condition, ThenTask body) +{ + return Async::start([body, condition](Async::Future &future) { + asyncWhile([condition, body](std::function whileCallback) { + Async::start(body).then([whileCallback, condition]() { + whileCallback(!condition()); + }).exec(); + }, + [&future]() { //while complete + future.setFinished(); + }); + }); +} + +Job Async::dowhile(ThenTask body) +{ + return Async::start([body](Async::Future &future) { + asyncWhile([body](std::function whileCallback) { + Async::start(body).then([whileCallback](bool result) { + whileCallback(!result); + //FIXME this return value is only required because .then doesn't work + return true; + }).exec(); + }, + [&future]() { //while complete + future.setFinished(); + }); + }); +} + 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 start(); #endif -template -Job dowhile(Condition condition, ThenTask func); +/** + * Async while loop. + * + * The loop continues while @param condition returns true. + */ +Job dowhile(Condition condition, ThenTask func); + +/** + * Async while loop. + * + * Loop continues while body returns true. + */ +Job dowhile(ThenTask body); /** @@ -249,7 +260,6 @@ Job null(); template Job error(int errorCode = 1, const QString &errorMessage = QString()); - class JobBase { template @@ -511,30 +521,6 @@ Job start() } #endif -static void asyncWhile(const std::function)> &body, const std::function &completionHandler) { - body([body, completionHandler](bool complete) { - if (complete) { - completionHandler(); - } else { - asyncWhile(body, completionHandler); - } - }); -} - } -template -Job dowhile(Condition condition, ThenTask body) -{ - return Async::start([body, condition](Async::Future &future) { - asyncWhile([condition, body](std::function whileCallback) { - Async::start(body).then([whileCallback, condition]() { - whileCallback(!condition()); - }).exec(); - }, - [&future]() { //while complete - future.setFinished(); - }); - }); -} template Job null() -- cgit v1.2.3