From 1aee1bda9fc81c888ad18fea107c271133dd5442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Vr=C3=A1til?= Date: Thu, 11 Dec 2014 14:22:27 +0100 Subject: Async: change syntax of callables We now pass our own Async::Future to each task, instead of expecting tasks to return their future. This allows us to re-use the same Future for repeated invocations, like in the Each task. --- async/autotests/asynctest.cpp | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'async/autotests') diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index 786ff3c..19e40f7 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp @@ -40,28 +40,26 @@ public: private Q_SLOTS: void testSyncPromises(); void testAsyncPromises(); + void testSyncEach(); }; void AsyncTest::testSyncPromises() { auto baseJob = Async::start( - []() -> Async::Future { - auto f = Async::Future(42); + [](Async::Future &f) { + f.setValue(42); f.setFinished(); - return f; }) .then( - [](int v) -> Async::Future { - auto f = Async::Future("Result is " + QString::number(v)); + [](int v, Async::Future &f) { + f.setValue("Result is " + QString::number(v)); f.setFinished(); - return f; }); auto job = baseJob.then( - [](const QString &v) -> Async::Future { - auto f = Async::Future(v.toUpper()); + [](const QString &v, Async::Future &f) { + f.setValue(v.toUpper()); f.setFinished(); - return f; }); job.exec(); @@ -73,8 +71,7 @@ void AsyncTest::testSyncPromises() void AsyncTest::testAsyncPromises() { auto job = Async::start( - []() -> Async::Future { - Async::Future future(-1); + [](Async::Future &future) { QTimer *timer = new QTimer(); QObject::connect(timer, &QTimer::timeout, [&]() { @@ -85,7 +82,6 @@ void AsyncTest::testAsyncPromises() timer, &QObject::deleteLater); timer->setSingleShot(true); timer->start(200); - return future; }); job.exec(); @@ -93,6 +89,22 @@ void AsyncTest::testAsyncPromises() QCOMPARE(future.value(), 42); } +void AsyncTest::testSyncEach() +{ + /* + auto job = Async::start>( + []() -> Async::Future> { + Async::Future> future(QList{ 1, 2, 3, 4 }); + future.setFinished(); + return future; + }) + .each, int>( + [](const int &v, Async::Future> &future) { + setFinished(); + }); + */ +} + QTEST_MAIN(AsyncTest); -- cgit v1.2.3