From 453526c7553c650f9a5bb3fe4452306d9dd3740f Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 19 Apr 2015 12:22:28 +0200 Subject: Fixed void async each. --- async/autotests/asynctest.cpp | 30 ++++++++++++++++++++++++++++-- async/src/async.h | 4 ++-- async/src/async_impl.h | 14 ++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index d709567..9789950 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp @@ -55,7 +55,8 @@ private Q_SLOTS: void testAsyncEach(); void testSyncEach(); void testJoinedEach(); - void testVoidEach(); + void testVoidEachThen(); + void testAsyncVoidEachThen(); void testAsyncReduce(); void testSyncReduce(); @@ -382,7 +383,7 @@ void AsyncTest::testJoinedEach() QCOMPARE(future.value(), expected); } -void AsyncTest::testVoidEach() +void AsyncTest::testVoidEachThen() { QList check; auto job = Async::start>( @@ -391,12 +392,37 @@ void AsyncTest::testVoidEach() }).each( [&check](const int &v) { check << v; + }).then([](){}); + + auto future = job.exec(); + + const QList expected({ 1, 2, 3, 4 }); + QVERIFY(future.isFinished()); + QCOMPARE(check, expected); +} + +void AsyncTest::testAsyncVoidEachThen() +{ + bool completedJob = false; + QList check; + auto job = Async::start>( + [](Async::Future > &future) { + new AsyncSimulator>(future, { 1, 2, 3, 4 }); + }).each( + [&check](const int &v, Async::Future &future) { + check << v; + new AsyncSimulator(future); + }).then([&completedJob](Async::Future &future) { + completedJob = true; + future.setFinished(); }); auto future = job.exec(); + future.waitForFinished(); const QList expected({ 1, 2, 3, 4 }); QVERIFY(future.isFinished()); + QVERIFY(completedJob); QCOMPARE(check, expected); } diff --git a/async/src/async.h b/async/src/async.h index a72d058..62fb463 100644 --- a/async/src/async.h +++ b/async/src/async.h @@ -193,7 +193,7 @@ public: void run(const ExecutionPtr &execution); private: EachTask mFunc; - QVector*> mFutureWatchers; + QVector*> mFutureWatchers; }; template @@ -729,7 +729,7 @@ void EachExecutor::run(const ExecutionPtr &execution) const int index = mFutureWatchers.indexOf(fw); assert(index > -1); mFutureWatchers.removeAt(index); - out->setValue(out->value() + future.value()); + Async::detail::aggregateFutureValue(fw->future(), *out); if (mFutureWatchers.isEmpty()) { out->setFinished(); } diff --git a/async/src/async_impl.h b/async/src/async_impl.h index eccbc9b..8c74193 100644 --- a/async/src/async_impl.h +++ b/async/src/async_impl.h @@ -60,6 +60,20 @@ copyFutureValue(const Async::Future &in, Async::Future &out) // noop } +template +inline typename std::enable_if::value, void>::type +aggregateFutureValue(const Async::Future &in, Async::Future &out) +{ + out.setValue(out.value() + in.value()); +} + +template +inline typename std::enable_if::value, void>::type +aggregateFutureValue(const Async::Future &in, Async::Future &out) +{ + // noop +} + } // namespace Detail } // namespace Async -- cgit v1.2.3