From f68a2316d8b265a1827bd905170b3006f8533db4 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 29 Dec 2022 08:03:49 +0100 Subject: [PATCH] Long live QPromise::addResults()! Makes the pre-existing QFutureInterface functionality available via the public QPromise API. [ChangeLog][QtCore][QPromise] Added addResults() to report multiple results at once. Change-Id: I18e6ef2781df422020b9022d78d6c45107b01668 Reviewed-by: Sona Kurazyan Reviewed-by: Qt CI Bot --- src/corelib/thread/qpromise.h | 2 ++ src/corelib/thread/qpromise.qdoc | 23 +++++++++++++++++++ .../corelib/thread/qpromise/tst_qpromise.cpp | 9 ++++++++ 3 files changed, 34 insertions(+) diff --git a/src/corelib/thread/qpromise.h b/src/corelib/thread/qpromise.h index 5620811bae..9be9526ef6 100644 --- a/src/corelib/thread/qpromise.h +++ b/src/corelib/thread/qpromise.h @@ -51,6 +51,8 @@ public: { return d.reportResult(std::forward(result), index); } + bool addResults(const QList &result) + { return d.reportResults(result); } #ifndef QT_NO_EXCEPTIONS void setException(const QException &e) { d.reportException(e); } #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) diff --git a/src/corelib/thread/qpromise.qdoc b/src/corelib/thread/qpromise.qdoc index 09907c4e0e..144ad16974 100644 --- a/src/corelib/thread/qpromise.qdoc +++ b/src/corelib/thread/qpromise.qdoc @@ -105,6 +105,29 @@ For instance, iterative approaches that use QFuture::resultCount() or QFuture::const_iterator. In order to get all available results without thinking if there are index gaps or not, use QFuture::results(). + + \sa addResults() +*/ + +/*! + \fn template bool QPromise::addResults(const QList &results) + \since 6.6 + + Adds \a results at the end of the internal result collection. + + Returns \c true when \a results are added to the collection. + + Returns \c false when this promise is in canceled or finished state. + + This is more efficient than looping over addResult(), because associated + futures will be notified only once per addResults() call, instead of once + per element contained in \a results, as would be the case with individual + addResult() calls. But if the calculation of each element takes time, then + the code on the receiving end (future) cannot make progress until all + results are reported, so use this function only if the calculation of + consecutive elements is relatively fast. + + \sa addResult() */ /*! \fn template void QPromise::setException(const QException &e) diff --git a/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp b/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp index 12206aa9fc..c63fb74616 100644 --- a/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp +++ b/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp @@ -168,6 +168,15 @@ void tst_QPromise::addResult() QCOMPARE(f.resultCount(), 3); QCOMPARE(f.resultAt(2), result); } + // add multiple results in one go: + { + QList results = {42, 4242, 424242}; + QVERIFY(promise.addResults(results)); + QCOMPARE(f.resultCount(), 6); + QCOMPARE(f.resultAt(3), 42); + QCOMPARE(f.resultAt(4), 4242); + QCOMPARE(f.resultAt(5), 424242); + } // add as lvalue at position and overwrite { int result = -1;