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)>;
63 void (*destroy)(
const void *);
71 size_t GetObjectSize();
81 DLOG(TRACE) <<
"called:" <<
this;
82 auto *obj_dstr = this->get_heap_ptr(
sizeof(T));
83 new ((
void *)obj_dstr->p_obj) T(std::forward<T>(obj));
85 if (std::is_class_v<T>) {
86 auto destructor = [](
const void *x) {
87 static_cast<const T *
>(x)->~T();
89 obj_dstr->destroy = destructor;
91 obj_dstr->destroy =
nullptr;
94 data_objects_.push(obj_dstr);
103 template <
typename T>
105 DLOG(TRACE) <<
"called:" <<
this;
106 auto *obj_dstr = this->get_heap_ptr(
sizeof(T));
107 auto *ptr_heap =
new ((
void *)obj_dstr->p_obj) T(std::move(*obj));
108 if (std::is_class_v<T>) {
109 LOG(TRACE) <<
"is class";
110 auto destructor = [](
const void *x) {
111 static_cast<const T *
>(x)->~T();
113 obj_dstr->destroy = destructor;
115 obj_dstr->destroy =
nullptr;
117 data_objects_.push(std::move(obj_dstr));
126 template <
typename T>
128 DLOG(TRACE) <<
"called:" <<
this;
129 if (data_objects_.empty())
throw std::runtime_error(
"No object to pop");
130 auto *obj_dstr = data_objects_.top();
131 auto *heap_ptr = (T *)obj_dstr->p_obj;
132 auto obj = std::move(*(T *)(heap_ptr));
133 this->free_heap_ptr(obj_dstr);
145 std::stack<Destructor *> data_objects_;
153 Destructor *get_heap_ptr(
size_t bytes_size);
160 void free_heap_ptr(Destructor *);
174 explicit Task(TaskCallback callback, DataObjectPtr data_object =
nullptr);
182 TaskRunnable runnable,
183 TaskCallback callback = [](
int,
const std::shared_ptr<DataObject> &) {},
184 DataObjectPtr data =
nullptr);
190 virtual ~Task()
override;
203 std::string GetUUID()
const;
210 void SignalTaskFinished();
216 void SignalTaskPostFinishedDone();
224 void SetFinishAfterRun(
bool finish_after_run);
231 void SetRTN(
int rtn);
234 const std::string uuid_;
235 TaskCallback callback_;
236 TaskRunnable runnable_;
237 bool finish_after_run_ =
true;
239 QThread *callback_thread_ =
nullptr;
240 DataObjectPtr data_object_ =
nullptr;
246 void before_finish_task();
258 virtual void run()
override;
265 static std::string generate_uuid();
Definition: TaskRunner.h:39
DataObject to be passed to the callback function.
Definition: Task.h:59
void AppendObject(T &&obj)
Definition: Task.h:80
void AppendObject(T *obj)
Definition: Task.h:104
T PopObject()
Definition: Task.h:127
Definition: CtxCheckTask.h:33