RunExtensions: Move onResultReady and onFinished into asynctask.h

Change-Id: I96dbf5b0253251224ae678172cd5fca12b34326a
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Jarek Kobus
2023-04-06 17:59:27 +02:00
parent 2abe6bbe2c
commit 7ab0fd56ae
21 changed files with 78 additions and 92 deletions

View File

@@ -14,7 +14,6 @@
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <utils/qtcprocess.h>
#include <utils/runextensions.h>
#include <QDir>
#include <QDirIterator>

View File

@@ -45,6 +45,76 @@ auto asyncRun(Function &&function, Args &&...args)
std::forward<Function>(function), std::forward<Args>(args)...);
}
/*!
Adds a handler for when a result is ready.
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
or create a QFutureWatcher already for other reasons.
*/
template <typename R, typename T>
const QFuture<T> &onResultReady(const QFuture<T> &future, R *receiver, void(R::*member)(const T &))
{
auto watcher = new QFutureWatcher<T>();
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
QObject::connect(watcher, &QFutureWatcherBase::resultReadyAt, receiver, [=](int index) {
(receiver->*member)(watcher->future().resultAt(index));
});
watcher->setFuture(future);
return future;
}
/*!
Adds a handler for when a result is ready. The guard object determines the lifetime of
the connection.
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
or create a QFutureWatcher already for other reasons.
*/
template <typename T, typename Function>
const QFuture<T> &onResultReady(const QFuture<T> &future, QObject *guard, Function f)
{
auto watcher = new QFutureWatcher<T>();
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
QObject::connect(watcher, &QFutureWatcherBase::resultReadyAt, guard, [f, watcher](int index) {
f(watcher->future().resultAt(index));
});
watcher->setFuture(future);
return future;
}
/*!
Adds a handler for when the future is finished.
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
or create a QFutureWatcher already for other reasons.
*/
template<typename R, typename T>
const QFuture<T> &onFinished(const QFuture<T> &future,
R *receiver, void (R::*member)(const QFuture<T> &))
{
auto watcher = new QFutureWatcher<T>();
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
QObject::connect(watcher, &QFutureWatcherBase::finished, receiver,
[=] { (receiver->*member)(watcher->future()); });
watcher->setFuture(future);
return future;
}
/*!
Adds a handler for when the future is finished. The guard object determines the lifetime of
the connection.
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
or create a QFutureWatcher already for other reasons.
*/
template<typename T, typename Function>
const QFuture<T> &onFinished(const QFuture<T> &future, QObject *guard, Function f)
{
auto watcher = new QFutureWatcher<T>();
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
QObject::connect(watcher, &QFutureWatcherBase::finished, guard, [f, watcher] {
f(watcher->future());
});
watcher->setFuture(future);
return future;
}
class QTCREATOR_UTILS_EXPORT AsyncTaskBase : public QObject
{
Q_OBJECT

View File

@@ -476,79 +476,4 @@ runAsync(QThreadPool *pool, Function &&function, Args&&... args)
std::forward<Args>(args)...);
}
/*!
Adds a handler for when a result is ready.
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
or create a QFutureWatcher already for other reasons.
*/
template <typename R, typename T>
const QFuture<T> &onResultReady(const QFuture<T> &future, R *receiver, void(R::*member)(const T &))
{
auto watcher = new QFutureWatcher<T>();
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
QObject::connect(watcher, &QFutureWatcherBase::resultReadyAt, receiver,
[receiver, member, watcher](int index) {
(receiver->*member)(watcher->future().resultAt(index));
});
watcher->setFuture(future);
return future;
}
/*!
Adds a handler for when a result is ready. The guard object determines the lifetime of
the connection.
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
or create a QFutureWatcher already for other reasons.
*/
template <typename T, typename Function>
const QFuture<T> &onResultReady(const QFuture<T> &future, QObject *guard, Function f)
{
auto watcher = new QFutureWatcher<T>();
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
QObject::connect(watcher, &QFutureWatcherBase::resultReadyAt, guard, [f, watcher](int index) {
f(watcher->future().resultAt(index));
});
watcher->setFuture(future);
return future;
}
/*!
Adds a handler for when the future is finished.
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
or create a QFutureWatcher already for other reasons.
*/
template<typename R, typename T>
const QFuture<T> &onFinished(const QFuture<T> &future,
R *receiver,
void (R::*member)(const QFuture<T> &))
{
auto watcher = new QFutureWatcher<T>();
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
QObject::connect(watcher,
&QFutureWatcherBase::finished,
receiver,
[receiver, member, watcher] { (receiver->*member)(watcher->future()); });
watcher->setFuture(future);
return future;
}
/*!
Adds a handler for when the future is finished. The guard object determines the lifetime of
the connection.
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
or create a QFutureWatcher already for other reasons.
*/
template<typename T, typename Function>
const QFuture<T> &onFinished(const QFuture<T> &future, QObject *guard, Function f)
{
auto watcher = new QFutureWatcher<T>();
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
QObject::connect(watcher, &QFutureWatcherBase::finished, guard, [f, watcher] {
f(watcher->future());
});
watcher->setFuture(future);
return future;
}
} // namespace Utils

View File

@@ -24,7 +24,6 @@
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <utils/qtcprocess.h>
#include <utils/runextensions.h>
#include <utils/stringutils.h>
#include <utils/temporaryfile.h>
#include <utils/url.h>

View File

@@ -9,10 +9,10 @@
#include <app/app_version.h>
#include <utils/asynctask.h>
#include <utils/layoutbuilder.h>
#include <utils/outputformatter.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/utilsicons.h>
#include <QAbstractButton>

View File

@@ -16,7 +16,6 @@
#include <utils/algorithm.h>
#include <utils/asynctask.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <QLoggingCategory>

View File

@@ -14,6 +14,7 @@
#include "../modemanager.h"
#include <utils/algorithm.h>
#include <utils/asynctask.h>
#include <utils/appmainwindow.h>
#include <utils/fancylineedit.h>
#include <utils/fsengine/fileiconprovider.h>

View File

@@ -18,7 +18,6 @@
#include <utils/pathchooser.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/runextensions.h>
#include <utils/temporarydirectory.h>
#include <utils/wizard.h>
#include <utils/wizardpage.h>

View File

@@ -109,7 +109,7 @@ namespace Core {
start a task concurrently in a different thread.
QtConcurrent has several different functions to run e.g.
a class function in a different thread. Qt Creator itself
adds a few more in \c{src/libs/qtconcurrent/runextensions.h}.
adds a few more in \c{src/libs/utils/asynctask.h}.
The QtConcurrent functions to run a concurrent task return a
\c QFuture object. This is what you want to give the
ProgressManager in the addTask() function.

View File

@@ -19,7 +19,6 @@
#include <utils/filesearch.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/runextensions.h>
#include <QCheckBox>
#include <QFuture>

View File

@@ -42,12 +42,12 @@
#include <texteditor/textmark.h>
#include <utils/algorithm.h>
#include <utils/asynctask.h>
#include <utils/commandline.h>
#include <utils/infobar.h>
#include <utils/parameteraction.h>
#include <utils/pathchooser.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/stringutils.h>
#include <utils/utilsicons.h>

View File

@@ -14,7 +14,6 @@
#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <QFile>
#include <QTextStream>

View File

@@ -12,7 +12,6 @@
#include <utils/asynctask.h>
#include <utils/filesystemwatcher.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <QDateTime>
#include <QDebug>

View File

@@ -7,8 +7,8 @@
#include "simulatorcontrol.h"
#include <utils/algorithm.h>
#include <utils/asynctask.h>
#include <utils/layoutbuilder.h>
#include <utils/runextensions.h>
#include <QApplication>
#include <QComboBox>

View File

@@ -12,9 +12,9 @@
#include "simulatoroperationdialog.h"
#include <utils/algorithm.h>
#include <utils/asynctask.h>
#include <utils/layoutbuilder.h>
#include <utils/pathchooser.h>
#include <utils/runextensions.h>
#include <QCheckBox>
#include <QDateTime>

View File

@@ -17,7 +17,6 @@
#include <utils/futuresynchronizer.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/runextensions.h>
#include <utils/temporarydirectory.h>
#include <QDir>

View File

@@ -8,7 +8,6 @@
#include <utils/asynctask.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/runextensions.h>
#ifdef Q_OS_MAC
#include <CoreFoundation/CoreFoundation.h>

View File

@@ -6,7 +6,7 @@
#include "iostr.h"
#include <utils/algorithm.h>
#include <utils/runextensions.h>
#include <utils/asynctask.h>
#include <QTimer>

View File

@@ -14,7 +14,6 @@
#include <utils/asynctask.h>
#include <utils/fileinprojectfinder.h>
#include <utils/runextensions.h>
#include <QStringList>
#include <QTextStream>

View File

@@ -50,7 +50,6 @@
#include <utils/algorithm.h>
#include <utils/asynctask.h>
#include <utils/qtcprocess.h>
#include <utils/runextensions.h>
#include <QDebug>
#include <QDir>

View File

@@ -1,6 +1,7 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <utils/asynctask.h>
#include <utils/runextensions.h>
#include <QtTest>