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/src/async.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'async/src/async.cpp') 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(); + }); + }); +} + -- cgit v1.2.3