29 #ifndef GPGFRONTEND_TASK_H
30 #define GPGFRONTEND_TASK_H
36 #include <type_traits>
39 #include "core/GpgFrontendCore.h"
45 class GPGFRONTEND_CORE_EXPORT
Task :
public QObject,
public QRunnable {
49 using DataObjectPtr = std::shared_ptr<DataObject>;
50 using TaskRunnable = std::function<int(DataObjectPtr)>;
51 using TaskCallback = std::function<void(
int, DataObjectPtr)>;
65 void (*destroy)(
const void *);
73 size_t GetObjectSize();
83 SPDLOG_TRACE(
"append object: {}",
static_cast<void *
>(
this));
84 auto *obj_dstr = this->get_heap_ptr(
sizeof(T));
85 new ((
void *)obj_dstr->p_obj) T(std::forward<T>(obj));
87 if (std::is_class_v<T>) {
88 auto destructor = [](
const void *x) {
89 static_cast<const T *
>(x)->~T();
91 obj_dstr->destroy = destructor;
93 obj_dstr->destroy =
nullptr;
96 data_objects_.push(obj_dstr);
105 template <
typename T>
107 SPDLOG_TRACE(
"called: {}",
static_cast<void *
>(
this));
108 auto *obj_dstr = this->get_heap_ptr(
sizeof(T));
109 auto *ptr_heap =
new ((
void *)obj_dstr->p_obj) T(std::move(*obj));
110 if (std::is_class_v<T>) {
111 SPDLOG_TRACE(
"is class");
112 auto destructor = [](
const void *x) {
113 static_cast<const T *
>(x)->~T();
115 obj_dstr->destroy = destructor;
117 obj_dstr->destroy =
nullptr;
119 data_objects_.push(std::move(obj_dstr));
128 template <
typename T>
130 SPDLOG_TRACE(
"pop object: {}",
static_cast<void *
>(
this));
131 if (data_objects_.empty())
throw std::runtime_error(
"No object to pop");
132 auto *obj_dstr = data_objects_.top();
133 auto *heap_ptr = (T *)obj_dstr->p_obj;
134 auto obj = std::move(*(T *)(heap_ptr));
135 this->free_heap_ptr(obj_dstr);
147 std::stack<Destructor *> data_objects_;
155 Destructor *get_heap_ptr(
size_t bytes_size);
162 void free_heap_ptr(Destructor *);
169 Task(std::string name = DEFAULT_TASK_NAME);
176 explicit Task(TaskRunnable runnable, std::string name = DEFAULT_TASK_NAME,
177 DataObjectPtr data_object =
nullptr,
bool sequency =
true);
185 TaskRunnable runnable, std::string name, DataObjectPtr data,
186 TaskCallback callback = [](
int,
const std::shared_ptr<DataObject> &) {},
187 bool sequency =
true);
193 virtual ~Task()
override;
206 std::string GetUUID()
const;
213 std::string GetFullID()
const;
220 bool GetSequency()
const;
249 void SetFinishAfterRun(
bool finish_after_run);
256 void SetRTN(
int rtn);
259 const std::string uuid_;
260 const std::string name_;
261 const bool sequency_ =
true;
262 TaskCallback callback_;
263 TaskRunnable runnable_;
264 bool run_callback_after_runnable_finished_ =
true;
266 QThread *callback_thread_ =
nullptr;
267 DataObjectPtr data_object_ =
nullptr;
279 virtual void run()
override;
286 static std::string generate_uuid();
293 void slot_task_run_callback(
int rtn);
Definition: TaskRunner.h:40
DataObject to be passed to the callback function.
Definition: Task.h:61
void AppendObject(T &&obj)
Definition: Task.h:82
void AppendObject(T *obj)
Definition: Task.h:106
T PopObject()
Definition: Task.h:129
void SignalTaskRunnableEnd(int rtn)
announce runnable finished
static const std::string DEFAULT_TASK_NAME
Definition: Task.h:53
void SignalTaskEnd()
runnable and callabck all finished
Definition: CtxCheckTask.h:33