From 9ba0715f08a64758453b941c4add22a269a30f71 Mon Sep 17 00:00:00 2001 From: Vitaly Fanaskov Date: Tue, 14 Apr 2020 12:51:14 +0200 Subject: QFuture: the result type doesn't have to be a default-constructible Added asserts to insure the invariant declared in the documentation. Canceled future object is not valid, hence we don't need to handle this case separately. Fixes: QTBUG-83389 Change-Id: Ib0653ef40cd3135574a91740e4ce2c6dc4da8a71 Reviewed-by: Sona Kurazyan Reviewed-by: Timur Pocheptsov --- src/corelib/thread/qfutureinterface.h | 15 ++------- tests/auto/corelib/thread/qfuture/tst_qfuture.cpp | 37 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/corelib/thread/qfutureinterface.h b/src/corelib/thread/qfutureinterface.h index 04828609ba..02f854e0de 100644 --- a/src/corelib/thread/qfutureinterface.h +++ b/src/corelib/thread/qfutureinterface.h @@ -339,13 +339,8 @@ inline QList QFutureInterface::results() template T QFutureInterface::takeResult() { - if (isCanceled()) { - exceptionStore().throwPossibleException(); - return {}; - } + Q_ASSERT(isValid()); - if (!isValid()) - return {}; // Note: we wait for all, this is intentional, // not to mess with other unready results. waitForResult(-1); @@ -362,13 +357,7 @@ T QFutureInterface::takeResult() template std::vector QFutureInterface::takeResults() { - if (isCanceled()) { - exceptionStore().throwPossibleException(); - return {}; - } - - if (!isValid()) - return {}; + Q_ASSERT(isValid()); waitForResult(-1); std::vector res; diff --git a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp index 4784cf16db..8d4ae41fd5 100644 --- a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp +++ b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp @@ -116,6 +116,8 @@ private slots: void runAndTake(); void resultsReadyAt_data(); void resultsReadyAt(); + void takeResultWorksForTypesWithoutDefaultCtor(); + void canceledFutureIsNotValid(); private: using size_type = std::vector::size_type; @@ -2744,5 +2746,40 @@ void tst_QFuture::resultsReadyAt() QCOMPARE(taken, 0b1111); } +template +auto makeFutureInterface(T &&result) +{ + QFutureInterface f; + + f.reportStarted(); + f.reportResult(std::forward(result)); + f.reportFinished(); + + return f; +} + +void tst_QFuture::takeResultWorksForTypesWithoutDefaultCtor() +{ + struct Foo + { + Foo() = delete; + explicit Foo(int i) : _i(i) {} + + int _i = -1; + }; + + auto f = makeFutureInterface(Foo(42)); + + QCOMPARE(f.takeResult()._i, 42); +} +void tst_QFuture::canceledFutureIsNotValid() +{ + auto f = makeFutureInterface(42); + + f.cancel(); + + QVERIFY(!f.isValid()); +} + QTEST_MAIN(tst_QFuture) #include "tst_qfuture.moc" -- cgit v1.2.3