From cd787dbac9e789e63d81f8ab7241ca734faffb44 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Fri, 10 Jul 2020 14:12:33 +0200 Subject: [PATCH] QtConcurrent: Get rid of multi-inheritance inside RunFunctionTaskBase Use aggregation instead. Prepare for using QPromise instead of QFutureInterface. Task-number: QTBUG-84702 Change-Id: Ic88564dca8c83a178a281cb843032292210a6d25 Reviewed-by: Sona Kurazyan --- src/concurrent/qtconcurrentrunbase.h | 26 ++++++++++--------- .../qfuturewatcher/tst_qfuturewatcher.cpp | 18 ++++++------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/concurrent/qtconcurrentrunbase.h b/src/concurrent/qtconcurrentrunbase.h index 176097c04f..c748d8e2b9 100644 --- a/src/concurrent/qtconcurrentrunbase.h +++ b/src/concurrent/qtconcurrentrunbase.h @@ -79,7 +79,7 @@ struct TaskStartParameters }; template -class RunFunctionTaskBase : public QFutureInterface , public QRunnable +class RunFunctionTaskBase : public QRunnable { public: QFuture start() @@ -89,10 +89,10 @@ public: QFuture start(const TaskStartParameters ¶meters) { - this->setThreadPool(parameters.threadPool); - this->setRunnable(this); - this->reportStarted(); - QFuture theFuture = this->future(); + promise.setThreadPool(parameters.threadPool); + promise.setRunnable(this); + promise.reportStarted(); + QFuture theFuture = promise.future(); parameters.threadPool->start(this, parameters.priority); return theFuture; } @@ -102,8 +102,8 @@ public: void run() override { - if (this->isCanceled()) { - this->reportFinished(); + if (promise.isCanceled()) { + promise.reportFinished(); return; } #ifndef QT_NO_EXCEPTIONS @@ -112,20 +112,22 @@ public: runFunctor(); #ifndef QT_NO_EXCEPTIONS } catch (QException &e) { - QFutureInterface::reportException(e); + promise.reportException(e); } catch (...) { - QFutureInterface::reportException(QUnhandledException()); + promise.reportException(QUnhandledException()); } #endif reportResult(); - this->reportFinished(); + promise.reportFinished(); } protected: virtual void runFunctor() = 0; virtual void reportResult() {} + + QFutureInterface promise; }; template @@ -135,9 +137,9 @@ protected: void reportResult() override { if constexpr (std::is_move_constructible_v) - this->reportAndMoveResult(std::move(result)); + this->promise.reportAndMoveResult(std::move(result)); else if constexpr (std::is_copy_constructible_v) - this->reportResult(result); + this->promise.reportResult(result); } T result; diff --git a/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp b/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp index d4f609874e..63277b02df 100644 --- a/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp +++ b/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp @@ -457,9 +457,9 @@ class ProgressEmitterTask : public RunFunctionTask public: void runFunctor() { - setProgressRange(0, maxProgress); + promise.setProgressRange(0, maxProgress); for (int p = 0; p <= maxProgress; ++p) - setProgressValue(p); + promise.setProgressValue(p); } }; @@ -487,19 +487,19 @@ class ProgressTextTask : public RunFunctionTask public: void runFunctor() { - this->setProgressValueAndText(1, QLatin1String("Foo 1")); + this->promise.setProgressValueAndText(1, QLatin1String("Foo 1")); - while (this->isProgressUpdateNeeded() == false) + while (this->promise.isProgressUpdateNeeded() == false) QTest::qSleep(1); - this->setProgressValueAndText(2, QLatin1String("Foo 2")); + this->promise.setProgressValueAndText(2, QLatin1String("Foo 2")); - while (this->isProgressUpdateNeeded() == false) + while (this->promise.isProgressUpdateNeeded() == false) QTest::qSleep(1); - this->setProgressValueAndText(3, QLatin1String("Foo 3")); + this->promise.setProgressValueAndText(3, QLatin1String("Foo 3")); - while (this->isProgressUpdateNeeded() == false) + while (this->promise.isProgressUpdateNeeded() == false) QTest::qSleep(1); - this->setProgressValueAndText(4, QLatin1String("Foo 4")); + this->promise.setProgressValueAndText(4, QLatin1String("Foo 4")); } };