From e0c7e625e4e27195541f8aaf15c158de89e96130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sun, 22 Jul 2018 15:04:13 +0200 Subject: [PATCH] Add R2Task class --- src/Cutter.cpp | 35 ++++++++---------------------- src/Cutter.h | 4 ---- src/Cutter.pro | 6 ++++-- src/utils/R2Task.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++ src/utils/R2Task.h | 32 +++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 32 deletions(-) create mode 100644 src/utils/R2Task.cpp create mode 100644 src/utils/R2Task.h diff --git a/src/Cutter.cpp b/src/Cutter.cpp index 130f9ada..b9b44407 100644 --- a/src/Cutter.cpp +++ b/src/Cutter.cpp @@ -7,6 +7,7 @@ #include "utils/TempConfig.h" #include "utils/Configuration.h" #include "utils/AsyncTask.h" +#include "utils/R2Task.h" #include "Cutter.h" #include "sdb.h" @@ -194,20 +195,18 @@ QJsonDocument CutterCore::cmdj(const QString &str) QString CutterCore::cmdTask(const QString &str) { - RCoreTask *task = startTask(str); - joinTask(task); - QString ret = QString::fromUtf8(task->res); - deleteTask(task); - return ret; + R2Task task(str); + task.startTask(); + task.joinTask(); + return task.getResult(); } QJsonDocument CutterCore::cmdjTask(const QString &str) { - RCoreTask *task = startTask(str); - joinTask(task); - QJsonDocument ret = parseJson(task->res, str); - deleteTask(task); - return ret; + R2Task task(str); + task.startTask(); + task.joinTask(); + return parseJson(task.getResultRaw(), str); } QJsonDocument CutterCore::parseJson(const char *res, const QString &cmd) @@ -1749,22 +1748,6 @@ QList CutterCore::getColorThemes() return r; } -RCoreTask *CutterCore::startTask(const QString &cmd) -{ - RCoreTask *task = r_core_task_new (core_, true, cmd.toLocal8Bit().constData(), nullptr, nullptr); - r_core_task_enqueue(core_, task); - return task; -} - -void CutterCore::joinTask(RCoreTask *task) -{ - r_core_task_join(core_, nullptr, task); -} -void CutterCore::deleteTask(RCoreTask *task) -{ - r_core_task_del(core_, task->id); -} - void CutterCore::setCutterPlugins(QList plugins) { this->plugins = plugins; diff --git a/src/Cutter.h b/src/Cutter.h index 6c6b48ab..3e93df3f 100644 --- a/src/Cutter.h +++ b/src/Cutter.h @@ -524,10 +524,6 @@ public: void triggerAsmOptionsChanged(); void triggerGraphOptionsChanged(); - RCoreTask *startTask(const QString &cmd); - void joinTask(RCoreTask *task); - void deleteTask(RCoreTask *task); - void setCutterPlugins(QList plugins); QList getCutterPlugins(); diff --git a/src/Cutter.pro b/src/Cutter.pro index 2fb6ce50..fc2f13a0 100644 --- a/src/Cutter.pro +++ b/src/Cutter.pro @@ -171,7 +171,8 @@ SOURCES += \ widgets/BacktraceWidget.cpp \ dialogs/OpenFileDialog.cpp \ utils/CommandTask.cpp \ - utils/ProgressIndicator.cpp + utils/ProgressIndicator.cpp \ + utils/R2Task.cpp HEADERS += \ Cutter.h \ @@ -257,7 +258,8 @@ HEADERS += \ utils/FunctionsTask.h \ utils/CommandTask.h \ utils/ProgressIndicator.h \ - plugins/CutterPlugin.h + plugins/CutterPlugin.h \ + utils/R2Task.h FORMS += \ dialogs/AboutDialog.ui \ diff --git a/src/utils/R2Task.cpp b/src/utils/R2Task.cpp new file mode 100644 index 00000000..e2a54f7d --- /dev/null +++ b/src/utils/R2Task.cpp @@ -0,0 +1,51 @@ + +#include "R2Task.h" + +R2Task::R2Task(const QString &cmd) +{ + task = r_core_task_new(Core()->core(), + true, + cmd.toLocal8Bit().constData(), + static_cast(&R2Task::taskFinishedCallback), + this); +} + +R2Task::~R2Task() +{ + r_core_task_del(Core()->core(), task->id); +} + +void R2Task::taskFinishedCallback(void *user, char *) +{ + reinterpret_cast(user)->taskFinished(); +} + +void R2Task::taskFinished() +{ + emit finished(); +} + +void R2Task::startTask() +{ + r_core_task_enqueue(Core()->core(), task); +} + +void R2Task::breakTask() +{ + r_core_task_break(Core()->core(), task->id); +} + +void R2Task::joinTask() +{ + r_core_task_join(Core()->core(), nullptr, task); +} + +QString R2Task::getResult() +{ + return QString::fromUtf8(task->res); +} + +const char *R2Task::getResultRaw() +{ + return task->res; +} diff --git a/src/utils/R2Task.h b/src/utils/R2Task.h new file mode 100644 index 00000000..ff7ba537 --- /dev/null +++ b/src/utils/R2Task.h @@ -0,0 +1,32 @@ + +#ifndef R2TASK_H +#define R2TASK_H + +#include "Cutter.h" + +class R2Task: public QObject +{ + Q_OBJECT + +private: + RCoreTask *task; + + static void taskFinishedCallback(void *user, char *); + void taskFinished(); + +public: + explicit R2Task(const QString &cmd); + ~R2Task(); + + void startTask(); + void breakTask(); + void joinTask(); + + QString getResult(); + const char *getResultRaw(); + +signals: + void finished(); +}; + +#endif // R2TASK_H \ No newline at end of file