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 <kurazyan.sona@gmail.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2022-12-29 08:03:49 +01:00
parent 114b94c26e
commit f68a2316d8
3 changed files with 34 additions and 0 deletions

View File

@ -51,6 +51,8 @@ public:
{
return d.reportResult(std::forward<U>(result), index);
}
bool addResults(const QList<T> &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)

View File

@ -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 <typename T> bool QPromise<T>::addResults(const QList<T> &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<typename T> void QPromise<T>::setException(const QException &e)

View File

@ -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;