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 auto *ptr_heap =
new ((
void *)obj_dstr->p_obj) T(std::move(obj));
84 if (std::is_class_v<T>) {
85 auto destructor = [](
const void *x) {
86 static_cast<const T *
>(x)->~T();
88 obj_dstr->destroy = destructor;
90 obj_dstr->destroy =
nullptr;
92 data_objects_.push(std::move(obj_dstr));
101 template <
typename T>
103 DLOG(TRACE) <<
"called:" <<
this;
104 auto *obj_dstr = this->get_heap_ptr(
sizeof(T));
105 auto *ptr_heap =
new ((
void *)obj_dstr->p_obj) T(std::move(*obj));
106 if (std::is_class_v<T>) {
107 LOG(TRACE) <<
"is class";
108 auto destructor = [](
const void *x) {
109 static_cast<const T *
>(x)->~T();
111 obj_dstr->destroy = destructor;
113 obj_dstr->destroy =
nullptr;
115 data_objects_.push(std::move(obj_dstr));
124 template <
typename T>
126 DLOG(TRACE) <<
"called:" <<
this;
127 if (data_objects_.empty())
throw std::runtime_error(
"No object to pop");
128 auto *obj_dstr = data_objects_.top();
129 auto *heap_ptr = (T *)obj_dstr->p_obj;
130 auto obj = std::move(*(T *)(heap_ptr));
131 this->free_heap_ptr(obj_dstr);
143 std::stack<Destructor *> data_objects_;
151 Destructor *get_heap_ptr(
size_t bytes_size);
158 void free_heap_ptr(Destructor *);
172 Task(TaskCallback callback, DataObjectPtr data_object =
nullptr);
180 TaskRunnable runnable,
181 TaskCallback callback = [](
int, std::shared_ptr<DataObject>) {},
182 DataObjectPtr data =
nullptr);
188 virtual ~Task()
override;
201 std::string GetUUID()
const;
208 void SignalTaskFinished();
214 void SignalTaskPostFinishedDone();
222 void SetFinishAfterRun(
bool finish_after_run);
229 void SetRTN(
int rtn);
232 const std::string uuid_;
233 TaskCallback callback_;
234 TaskRunnable runnable_;
235 bool finish_after_run_ =
true;
237 QThread *callback_thread_ =
nullptr;
238 DataObjectPtr data_object_ =
nullptr;
244 void before_finish_task();
256 virtual void run()
override;
263 static std::string generate_uuid();
267 #endif // GPGFRONTEND_TASK_H