From 14cfe743245946bd0d6808559d914f0e7d3c7c91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Mon, 28 May 2018 16:19:04 +0200 Subject: [PATCH] Use QSharedPointer for AsyncTask --- src/AnalTask.cpp | 4 ++-- src/AnalTask.h | 2 +- src/dialogs/AsyncTaskDialog.cpp | 21 +++++++-------------- src/dialogs/AsyncTaskDialog.h | 8 ++++---- src/dialogs/OptionsDialog.cpp | 11 ++++++++--- src/utils/AsyncTask.cpp | 23 +++++++++++++++++------ src/utils/AsyncTask.h | 9 +++++++-- 7 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/AnalTask.cpp b/src/AnalTask.cpp index 9cac2fc6..d2e85eef 100644 --- a/src/AnalTask.cpp +++ b/src/AnalTask.cpp @@ -6,8 +6,8 @@ #include #include -AnalTask::AnalTask(QObject *parent) : - AsyncTask(parent) +AnalTask::AnalTask() : + AsyncTask() { } diff --git a/src/AnalTask.h b/src/AnalTask.h index 3a59de8e..4ff8e6ee 100644 --- a/src/AnalTask.h +++ b/src/AnalTask.h @@ -44,7 +44,7 @@ class AnalTask : public AsyncTask Q_OBJECT public: - explicit AnalTask(QObject *parent = nullptr); + explicit AnalTask(); ~AnalTask(); QString getTitle() override { return tr("Initial Analysis"); } diff --git a/src/dialogs/AsyncTaskDialog.cpp b/src/dialogs/AsyncTaskDialog.cpp index 3742e5bc..42fa8211 100644 --- a/src/dialogs/AsyncTaskDialog.cpp +++ b/src/dialogs/AsyncTaskDialog.cpp @@ -5,10 +5,10 @@ #include "ui_AsyncTaskDialog.h" -AsyncTaskDialog::AsyncTaskDialog(AsyncTask *task, QWidget *parent) +AsyncTaskDialog::AsyncTaskDialog(AsyncTask::Ptr task, QWidget *parent) : QDialog(parent), - ui(new Ui::AsyncTaskDialog), - task(task) + task(task), + ui(new Ui::AsyncTaskDialog) { ui->setupUi(this); @@ -17,9 +17,8 @@ AsyncTaskDialog::AsyncTaskDialog(AsyncTask *task, QWidget *parent) setWindowTitle(title); } - connect(task, &AsyncTask::logChanged, this, &AsyncTaskDialog::updateLog); - connect(task, &AsyncTask::finished, this, [this]() { - this->task = nullptr; + connect(task.data(), &AsyncTask::logChanged, this, &AsyncTaskDialog::updateLog); + connect(task.data(), &AsyncTask::finished, this, [this]() { close(); }); @@ -44,10 +43,6 @@ void AsyncTaskDialog::updateLog(const QString &log) void AsyncTaskDialog::updateProgressTimer() { - if(!task) { - return; - } - int secondsElapsed = (task->getTimer().elapsed() + 500) / 1000; int minutesElapsed = secondsElapsed / 60; int hoursElapsed = minutesElapsed / 60; @@ -67,7 +62,7 @@ void AsyncTaskDialog::updateProgressTimer() void AsyncTaskDialog::closeEvent(QCloseEvent *event) { - if (interruptOnClose && task) { + if (interruptOnClose) { task->interrupt(); task->wait(); } @@ -77,7 +72,5 @@ void AsyncTaskDialog::closeEvent(QCloseEvent *event) void AsyncTaskDialog::reject() { - if (task) { - task->interrupt(); - } + task->interrupt(); } diff --git a/src/dialogs/AsyncTaskDialog.h b/src/dialogs/AsyncTaskDialog.h index ed8c08dc..e73c68d2 100644 --- a/src/dialogs/AsyncTaskDialog.h +++ b/src/dialogs/AsyncTaskDialog.h @@ -7,18 +7,18 @@ #include #include +#include "utils/AsyncTask.h" + namespace Ui { class AsyncTaskDialog; } -class AsyncTask; - class AsyncTaskDialog : public QDialog { Q_OBJECT public: - AsyncTaskDialog(AsyncTask *task, QWidget *parent = nullptr); + AsyncTaskDialog(AsyncTask::Ptr task, QWidget *parent = nullptr); ~AsyncTaskDialog(); public slots: @@ -33,7 +33,7 @@ protected: private: std::unique_ptr ui; - AsyncTask *task; + AsyncTask::Ptr task; QTimer timer; bool interruptOnClose; diff --git a/src/dialogs/OptionsDialog.cpp b/src/dialogs/OptionsDialog.cpp index 240daaf1..f1475f7c 100644 --- a/src/dialogs/OptionsDialog.cpp +++ b/src/dialogs/OptionsDialog.cpp @@ -1,3 +1,6 @@ + +#include "utils/AsyncTask.h" + #include "OptionsDialog.h" #include "ui_OptionsDialog.h" #include "MainWindow.h" @@ -231,15 +234,17 @@ void OptionsDialog::setupAndStartAnalysis(int level, QList advanced) } - AnalTask *analTask = new AnalTask(main); + AnalTask *analTask = new AnalTask(); analTask->setOptions(options); connect(analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed); connect(analTask, &AsyncTask::finished, main, &MainWindow::finalizeOpen); - Core()->getAsyncTaskManager()->start(analTask); + AsyncTask::Ptr analTaskPtr(analTask); - AsyncTaskDialog *taskDialog = new AsyncTaskDialog(analTask); + Core()->getAsyncTaskManager()->start(analTaskPtr); + + AsyncTaskDialog *taskDialog = new AsyncTaskDialog(analTaskPtr); taskDialog->setAttribute(Qt::WA_DeleteOnClose); taskDialog->show(); diff --git a/src/utils/AsyncTask.cpp b/src/utils/AsyncTask.cpp index e0c32e6a..f6291e9a 100644 --- a/src/utils/AsyncTask.cpp +++ b/src/utils/AsyncTask.cpp @@ -1,11 +1,11 @@ #include "AsyncTask.h" -AsyncTask::AsyncTask(QObject *parent) - : QObject(parent), +AsyncTask::AsyncTask() + : QObject(nullptr), QRunnable() { - setAutoDelete(true); + setAutoDelete(false); running = false; } @@ -44,12 +44,17 @@ void AsyncTask::prepareRun() void AsyncTask::run() { runningMutex.lock(); + running = true; + logBuffer = ""; emit logChanged(logBuffer); runTask(); - emit finished(); + running = false; + + emit finished(); + runningMutex.unlock(); } @@ -69,8 +74,14 @@ AsyncTaskManager::~AsyncTaskManager() { } -void AsyncTaskManager::start(AsyncTask *task) +void AsyncTaskManager::start(AsyncTask::Ptr task) { + tasks.append(task); task->prepareRun(); - threadPool->start(task); + + QWeakPointer weakPtr = task; + connect(task.data(), &AsyncTask::finished, this, [this, weakPtr]() { + tasks.removeOne(weakPtr); + }); + threadPool->start(task.data()); } diff --git a/src/utils/AsyncTask.h b/src/utils/AsyncTask.h index e81ebc5e..1426d18c 100644 --- a/src/utils/AsyncTask.h +++ b/src/utils/AsyncTask.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include class AsyncTaskManager; @@ -16,7 +18,9 @@ class AsyncTask : public QObject, public QRunnable friend class AsyncTaskManager; public: - AsyncTask(QObject *parent = nullptr); + using Ptr = QSharedPointer; + + AsyncTask(); ~AsyncTask(); void run() override final; @@ -59,12 +63,13 @@ class AsyncTaskManager : public QObject private: QThreadPool *threadPool; + QList tasks; public: explicit AsyncTaskManager(QObject *parent = nullptr); ~AsyncTaskManager(); - void start(AsyncTask *task); + void start(AsyncTask::Ptr task); };