Task.h
#ifndef GPGFRONTEND_TASK_H
#define GPGFRONTEND_TASK_H
#include <functional>
#include <memory>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include "core/GpgFrontendCore.h"
namespace GpgFrontend::Thread {
class TaskRunner;
class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable {
Q_OBJECT
public:
class DataObject;
using DataObjectPtr = std::shared_ptr<DataObject>;
using TaskRunnable = std::function<int(DataObjectPtr)>;
using TaskCallback = std::function<void(int, DataObjectPtr)>;
friend class TaskRunner;
class GPGFRONTEND_CORE_EXPORT DataObject {
public:
struct Destructor {
const void *p_obj;
void (*destroy)(const void *);
};
size_t GetObjectSize();
template <typename T>
void AppendObject(T &&obj) {
DLOG(TRACE) << "called:" << this;
auto *obj_dstr = this->get_heap_ptr(sizeof(T));
auto *ptr_heap = new ((void *)obj_dstr->p_obj) T(std::move(obj));
if (std::is_class_v<T>) {
auto destructor = [](const void *x) {
static_cast<const T *>(x)->~T();
};
obj_dstr->destroy = destructor;
} else {
obj_dstr->destroy = nullptr;
}
data_objects_.push(std::move(obj_dstr));
}
template <typename T>
void AppendObject(T *obj) {
DLOG(TRACE) << "called:" << this;
auto *obj_dstr = this->get_heap_ptr(sizeof(T));
auto *ptr_heap = new ((void *)obj_dstr->p_obj) T(std::move(*obj));
if (std::is_class_v<T>) {
LOG(TRACE) << "is class";
auto destructor = [](const void *x) {
static_cast<const T *>(x)->~T();
};
obj_dstr->destroy = destructor;
} else {
obj_dstr->destroy = nullptr;
}
data_objects_.push(std::move(obj_dstr));
}
template <typename T>
T PopObject() {
DLOG(TRACE) << "called:" << this;
if (data_objects_.empty()) throw std::runtime_error("No object to pop");
auto *obj_dstr = data_objects_.top();
auto *heap_ptr = (T *)obj_dstr->p_obj;
auto obj = std::move(*(T *)(heap_ptr));
this->free_heap_ptr(obj_dstr);
data_objects_.pop();
return obj;
}
~DataObject();
private:
std::stack<Destructor *> data_objects_;
Destructor *get_heap_ptr(size_t bytes_size);
void free_heap_ptr(Destructor *);
};
Task();
Task(TaskCallback callback, DataObjectPtr data_object = nullptr);
Task(
TaskRunnable runnable,
TaskCallback callback = [](int, std::shared_ptr<DataObject>) {},
DataObjectPtr data = nullptr);
virtual ~Task() override;
virtual void Run();
std::string GetUUID() const;
signals:
void SignalTaskFinished();
void SignalTaskPostFinishedDone();
protected:
void SetFinishAfterRun(bool finish_after_run);
void SetRTN(int rtn);
private:
const std::string uuid_;
TaskCallback callback_;
TaskRunnable runnable_;
bool finish_after_run_ = true;
int rtn_ = 0;
QThread *callback_thread_ = nullptr;
DataObjectPtr data_object_ = nullptr;
void before_finish_task();
void init();
virtual void run() override;
static std::string generate_uuid();
};
} // namespace GpgFrontend::Thread
#endif // GPGFRONTEND_TASK_H
GpgFrontend::Thread::TaskRunner
Definition: TaskRunner.h:39
GpgFrontend::Thread::Task::DataObject::free_heap_ptr
void free_heap_ptr(Destructor *)
Definition: Task.cpp:141
GpgFrontend::Thread::Task::SetRTN
void SetRTN(int rtn)
Definition: Task.cpp:77
GpgFrontend::Thread::Task::DataObject::get_heap_ptr
Destructor * get_heap_ptr(size_t bytes_size)
Get the heap ptr object.
Definition: Task.cpp:121
GpgFrontend::Thread::Task::DataObject::~DataObject
~DataObject()
Destroy the Data Object object.
Definition: Task.cpp:127
GpgFrontend::Thread::Task::~Task
virtual ~Task() override
Destroy the Task object.
Definition: Task.cpp:67
GpgFrontend::Thread::Task::DataObject::PopObject
T PopObject()
Definition: Task.h:125
GpgFrontend::Thread::Task::Task
Task()
Construct a new Task object.
Definition: Task.cpp:39
GpgFrontend::Thread::Task::DataObject::AppendObject
void AppendObject(T &&obj)
Definition: Task.h:80
GpgFrontend::Thread
Definition: CtxCheckTask.h:33
GpgFrontend::Thread::Task::DataObject::AppendObject
void AppendObject(T *obj)
Definition: Task.h:102
GpgFrontend::Thread::Task::Run
virtual void Run()
Run - run the task.
Definition: Task.cpp:110
GpgFrontend::Thread::Task::generate_uuid
static std::string generate_uuid()
Definition: Task.cpp:151
GpgFrontend::Thread::Task::DataObject::GetObjectSize
size_t GetObjectSize()
Get the Objects Size.
Definition: Task.cpp:137
GpgFrontend::Thread::Task
Definition: Task.h:45
GpgFrontend::Thread::Task::SetFinishAfterRun
void SetFinishAfterRun(bool finish_after_run)
Set the Finish After Run object.
Definition: Task.cpp:73
GpgFrontend::Thread::Task::GetUUID
std::string GetUUID() const
Definition: Task.cpp:71
GpgFrontend::Thread::Task::DataObject::Destructor
Definition: Task.h:61
GpgFrontend::Thread::Task::DataObject
DataObject to be passed to the callback function.
Definition: Task.h:59