From 2f95cd8f8b3a4d2216916fc55f15df5c901648f4 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 28 Mar 2023 16:32:00 +0200 Subject: [PATCH] Long live QPromise::emplaceResult/At()! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And implement the rvalue overload of addResult() using it. [ChangeLog][QtCore][QPromise] Added emplaceResult() and emplaceResultAt() member functions. Fixes: QTBUG-112270 Change-Id: Id369542215a60c0818f1afa8d564498be84732e8 Reviewed-by: Qt CI Bot Reviewed-by: MÃ¥rten Nordheim --- src/corelib/thread/qpromise.h | 12 ++++++- src/corelib/thread/qpromise.qdoc | 33 ++++++++++++++++--- .../corelib/thread/qpromise/tst_qpromise.cpp | 8 +++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/corelib/thread/qpromise.h b/src/corelib/thread/qpromise.h index 9db58ad595c..c2b6c119aed 100644 --- a/src/corelib/thread/qpromise.h +++ b/src/corelib/thread/qpromise.h @@ -46,10 +46,20 @@ public: // Core QPromise APIs QFuture future() const { return d.future(); } + template, bool> = true> + bool emplaceResultAt(int index, Args&&...args) + { + return d.reportAndEmplaceResult(index, std::forward(args)...); + } + template, bool> = true> + bool emplaceResult(Args&&...args) + { + return d.reportAndEmplaceResult(-1, std::forward(args)...); + } template> bool addResult(U &&result, int index = -1) { - return d.reportResult(std::forward(result), index); + return d.reportAndEmplaceResult(index, std::forward(result)); } bool addResults(const QList &result) { return d.reportResults(result); } diff --git a/src/corelib/thread/qpromise.qdoc b/src/corelib/thread/qpromise.qdoc index 37ffa8485e0..aaef6121752 100644 --- a/src/corelib/thread/qpromise.qdoc +++ b/src/corelib/thread/qpromise.qdoc @@ -91,15 +91,38 @@ /*! \fn template bool QPromise::addResult(const T &result, int index = -1) \fn template bool QPromise::addResult(T &&result, int index = -1) - Adds \a result to the internal result collection at \a index position. If - index is unspecified, \a result is added to the end of the collection. + Same as + \code + emplaceResultAt(index, result); // first overload + emplaceResultAt(index, std::move(result)); // second overload + \endcode + or, if \c{index == -1} (the default) + \code + emplaceResult(result); // first overload + emplaceResult(std::move(result)); // second overload + \endcode - Returns \c true when \a result is added to the collection. + \sa emplaceResultAt(), emplaceResult(), addResults() +*/ + +/*! + \fn template template , bool> = true> QPromise::emplaceResultAt(int index, Args&&...args) + \fn template template , bool> = true> QPromise::emplaceResult(Args&&...args) + \since 6.6 + + Adds a result constructed from \a args... to the internal result collection + at \a index position (emplaceResultAt()) or the end of of the collection + (emplaceResult()). + + Returns \c true when the result was added to the collection. Returns \c false when this promise is in canceled or finished state or when - \a result is rejected. addResult() rejects \a result if there's already + the result was rejected. addResult() rejects to add a result if there's already another result in the collection stored at the same index. + These functions only participate in overload resolutions if \c T is + constructible from \a args.... + You can get a result at a specific index by calling QFuture::resultAt(). \note It is possible to specify an arbitrary index and request result at @@ -108,7 +131,7 @@ QFuture::const_iterator. In order to get all available results without thinking if there are index gaps or not, use QFuture::results(). - \sa addResults() + \sa addResult(), addResults() */ /*! diff --git a/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp b/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp index 09332f92ef0..0b0e6c321f9 100644 --- a/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp +++ b/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp @@ -203,12 +203,20 @@ void tst_QPromise::addResultWithBracedInitializer() // QTBUG-111826 { QString strValue; int intValue = 0; +#ifndef __cpp_aggregate_paren_init // make emplacement work with MyClass + MyClass(QString s, int i) : strValue(std::move(s)), intValue(i) {} +#endif }; { QPromise myPromise; myPromise.addResult({"bar", 1}); } + + { + QPromise myPromise; + myPromise.emplaceResult("bar", 1); + } } void tst_QPromise::addResultOutOfOrder()