From b002722dabef794da0e80010b115b2c6cd6dc6b8 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Sun, 29 Nov 2020 20:46:02 +0100 Subject: Fix perfect forwarding of callables in QFuture's continuations Use universal references instead of rvalue references for passing callables in the implementations of QFuture's continuations. Pick-to: dev 6.0 Change-Id: I1288c78f78f84f30c6607e505e7f9807a9272071 Reviewed-by: Andrei Golubev Reviewed-by: Ivan Solovev Reviewed-by: Jarek Kobus --- src/corelib/thread/qfuture.h | 10 ++++--- src/corelib/thread/qfuture_impl.h | 58 +++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/corelib/thread/qfuture.h b/src/corelib/thread/qfuture.h index 44c5331261..101bdc7808 100644 --- a/src/corelib/thread/qfuture.h +++ b/src/corelib/thread/qfuture.h @@ -347,7 +347,7 @@ QFuture::template ResultType> QFuture::then(QtFuture::Launch policy, Function &&function) { QFutureInterface> promise(QFutureInterfaceBase::State::Pending); - QtPrivate::Continuation, T>::create( + QtPrivate::Continuation, ResultType, T>::create( std::forward(function), this, promise, policy); return promise.future(); } @@ -358,7 +358,7 @@ QFuture::template ResultType> QFuture::then(QTh Function &&function) { QFutureInterface> promise(QFutureInterfaceBase::State::Pending); - QtPrivate::Continuation, T>::create( + QtPrivate::Continuation, ResultType, T>::create( std::forward(function), this, promise, pool); return promise.future(); } @@ -370,7 +370,8 @@ template QFuture QFuture::onFailed(Function &&handler) { QFutureInterface promise(QFutureInterfaceBase::State::Pending); - QtPrivate::FailureHandler::create(std::forward(handler), this, promise); + QtPrivate::FailureHandler, T>::create(std::forward(handler), + this, promise); return promise.future(); } @@ -381,7 +382,8 @@ template QFuture QFuture::onCanceled(Function &&handler) { QFutureInterface promise(QFutureInterfaceBase::State::Pending); - QtPrivate::CanceledHandler::create(std::forward(handler), this, promise); + QtPrivate::CanceledHandler, T>::create(std::forward(handler), + this, promise); return promise.future(); } diff --git a/src/corelib/thread/qfuture_impl.h b/src/corelib/thread/qfuture_impl.h index b8b9f39951..2d3ea148e9 100644 --- a/src/corelib/thread/qfuture_impl.h +++ b/src/corelib/thread/qfuture_impl.h @@ -246,20 +246,23 @@ template class Continuation { public: - Continuation(Function &&func, const QFuture &f, + template + Continuation(F &&func, const QFuture &f, const QFutureInterface &p) - : promise(p), parentFuture(f), function(std::forward(func)) + : promise(p), parentFuture(f), function(std::forward(func)) { } virtual ~Continuation() = default; bool execute(); - static void create(Function &&func, QFuture *f, - QFutureInterface &p, QtFuture::Launch policy); + template + static void create(F &&func, QFuture *f, QFutureInterface &p, + QtFuture::Launch policy); - static void create(Function &&func, QFuture *f, - QFutureInterface &p, QThreadPool *pool); + template + static void create(F &&func, QFuture *f, QFutureInterface &p, + QThreadPool *pool); private: void fulfillPromiseWithResult(); @@ -284,9 +287,10 @@ template class SyncContinuation final : public Continuation { public: - SyncContinuation(Function &&func, const QFuture &f, + template + SyncContinuation(F &&func, const QFuture &f, const QFutureInterface &p) - : Continuation(std::forward(func), f, p) + : Continuation(std::forward(func), f, p) { } @@ -301,9 +305,10 @@ class AsyncContinuation final : public QRunnable, public Continuation { public: - AsyncContinuation(Function &&func, const QFuture &f, + template + AsyncContinuation(F &&func, const QFuture &f, const QFutureInterface &p, QThreadPool *pool = nullptr) - : Continuation(std::forward(func), f, p), + : Continuation(std::forward(func), f, p), threadPool(pool) { this->promise.setRunnable(this); @@ -333,12 +338,13 @@ template class FailureHandler { public: - static void create(Function &&function, QFuture *future, + template + static void create(F &&function, QFuture *future, const QFutureInterface &promise); - FailureHandler(Function &&func, const QFuture &f, - const QFutureInterface &p) - : promise(p), parentFuture(f), handler(std::forward(func)) + template + FailureHandler(F &&func, const QFuture &f, const QFutureInterface &p) + : promise(p), parentFuture(f), handler(std::forward(func)) { } @@ -437,7 +443,8 @@ bool Continuation::execute() } template -void Continuation::create(Function &&func, +template +void Continuation::create(F &&func, QFuture *f, QFutureInterface &p, QtFuture::Launch policy) @@ -460,10 +467,10 @@ void Continuation::create(Function &&fun Continuation *continuationJob = nullptr; if (launchAsync) { continuationJob = new AsyncContinuation( - std::forward(func), *f, p, pool); + std::forward(func), *f, p, pool); } else { continuationJob = new SyncContinuation( - std::forward(func), *f, p); + std::forward(func), *f, p); } p.setLaunchAsync(launchAsync); @@ -483,7 +490,8 @@ void Continuation::create(Function &&fun } template -void Continuation::create(Function &&func, +template +void Continuation::create(F &&func, QFuture *f, QFutureInterface &p, QThreadPool *pool) @@ -491,7 +499,7 @@ void Continuation::create(Function &&fun Q_ASSERT(f); auto continuationJob = new AsyncContinuation( - std::forward(func), *f, p, pool); + std::forward(func), *f, p, pool); p.setLaunchAsync(true); p.setThreadPool(pool); @@ -570,13 +578,14 @@ void fulfillPromise(QFutureInterface &promise, Function &&handler) #ifndef QT_NO_EXCEPTIONS template -void FailureHandler::create(Function &&function, QFuture *future, +template +void FailureHandler::create(F &&function, QFuture *future, const QFutureInterface &promise) { Q_ASSERT(future); - FailureHandler *failureHandler = new FailureHandler( - std::forward(function), *future, promise); + FailureHandler *failureHandler = + new FailureHandler(std::forward(function), *future, promise); auto failureContinuation = [failureHandler]() mutable { failureHandler->run(); @@ -653,13 +662,14 @@ template class CanceledHandler { public: - static QFuture create(Function &&handler, QFuture *future, + template + static QFuture create(F &&handler, QFuture *future, QFutureInterface promise) { Q_ASSERT(future); auto canceledContinuation = [parentFuture = *future, promise, - handler = std::move(handler)]() mutable { + handler = std::forward(handler)]() mutable { promise.reportStarted(); if (parentFuture.isCanceled()) { -- cgit v1.2.3