summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--async/autotests/asynctest.cpp30
-rw-r--r--async/src/async.h4
-rw-r--r--async/src/async_impl.h14
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:
55 void testAsyncEach(); 55 void testAsyncEach();
56 void testSyncEach(); 56 void testSyncEach();
57 void testJoinedEach(); 57 void testJoinedEach();
58 void testVoidEach(); 58 void testVoidEachThen();
59 void testAsyncVoidEachThen();
59 60
60 void testAsyncReduce(); 61 void testAsyncReduce();
61 void testSyncReduce(); 62 void testSyncReduce();
@@ -382,7 +383,7 @@ void AsyncTest::testJoinedEach()
382 QCOMPARE(future.value(), expected); 383 QCOMPARE(future.value(), expected);
383} 384}
384 385
385void AsyncTest::testVoidEach() 386void AsyncTest::testVoidEachThen()
386{ 387{
387 QList<int> check; 388 QList<int> check;
388 auto job = Async::start<QList<int>>( 389 auto job = Async::start<QList<int>>(
@@ -391,12 +392,37 @@ void AsyncTest::testVoidEach()
391 }).each<void, int>( 392 }).each<void, int>(
392 [&check](const int &v) { 393 [&check](const int &v) {
393 check << v; 394 check << v;
395 }).then<void>([](){});
396
397 auto future = job.exec();
398
399 const QList<int> expected({ 1, 2, 3, 4 });
400 QVERIFY(future.isFinished());
401 QCOMPARE(check, expected);
402}
403
404void AsyncTest::testAsyncVoidEachThen()
405{
406 bool completedJob = false;
407 QList<int> check;
408 auto job = Async::start<QList<int>>(
409 [](Async::Future<QList<int> > &future) {
410 new AsyncSimulator<QList<int>>(future, { 1, 2, 3, 4 });
411 }).each<void, int>(
412 [&check](const int &v, Async::Future<void> &future) {
413 check << v;
414 new AsyncSimulator<void>(future);
415 }).then<void>([&completedJob](Async::Future<void> &future) {
416 completedJob = true;
417 future.setFinished();
394 }); 418 });
395 419
396 auto future = job.exec(); 420 auto future = job.exec();
421 future.waitForFinished();
397 422
398 const QList<int> expected({ 1, 2, 3, 4 }); 423 const QList<int> expected({ 1, 2, 3, 4 });
399 QVERIFY(future.isFinished()); 424 QVERIFY(future.isFinished());
425 QVERIFY(completedJob);
400 QCOMPARE(check, expected); 426 QCOMPARE(check, expected);
401} 427}
402 428
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:
193 void run(const ExecutionPtr &execution); 193 void run(const ExecutionPtr &execution);
194private: 194private:
195 EachTask<Out, In> mFunc; 195 EachTask<Out, In> mFunc;
196 QVector<Async::FutureWatcher<PrevOut>*> mFutureWatchers; 196 QVector<Async::FutureWatcher<Out>*> mFutureWatchers;
197}; 197};
198 198
199template<typename Out, typename In> 199template<typename Out, typename In>
@@ -729,7 +729,7 @@ void EachExecutor<PrevOut, Out, In>::run(const ExecutionPtr &execution)
729 const int index = mFutureWatchers.indexOf(fw); 729 const int index = mFutureWatchers.indexOf(fw);
730 assert(index > -1); 730 assert(index > -1);
731 mFutureWatchers.removeAt(index); 731 mFutureWatchers.removeAt(index);
732 out->setValue(out->value() + future.value()); 732 Async::detail::aggregateFutureValue<Out>(fw->future(), *out);
733 if (mFutureWatchers.isEmpty()) { 733 if (mFutureWatchers.isEmpty()) {
734 out->setFinished(); 734 out->setFinished();
735 } 735 }
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<T> &in, Async::Future<T> &out)
60 // noop 60 // noop
61} 61}
62 62
63template<typename T>
64inline typename std::enable_if<!std::is_void<T>::value, void>::type
65aggregateFutureValue(const Async::Future<T> &in, Async::Future<T> &out)
66{
67 out.setValue(out.value() + in.value());
68}
69
70template<typename T>
71inline typename std::enable_if<std::is_void<T>::value, void>::type
72aggregateFutureValue(const Async::Future<T> &in, Async::Future<T> &out)
73{
74 // noop
75}
76
63} // namespace Detail 77} // namespace Detail
64 78
65} // namespace Async 79} // namespace Async