27 #ifndef GPGFRONTEND_TASK_H
28 #define GPGFRONTEND_TASK_H
34 #include <type_traits>
37 #include "core/GpgFrontendCore.h"
43 class GPGFRONTEND_CORE_EXPORT
Task :
public QObject,
public QRunnable {
47 using DataObjectPtr = std::shared_ptr<DataObject>;
48 using TaskRunnable = std::function<int(DataObjectPtr)>;
49 using TaskCallback = std::function<void(
int, DataObjectPtr)>;
61 void (*destroy)(
const void *);
69 size_t GetObjectSize();
79 LOG(INFO) <<
"called:" <<
this;
80 auto *obj_dstr = this->get_heap_ptr(
sizeof(T));
81 auto *ptr_heap =
new ((
void *)obj_dstr->p_obj) T(std::move(obj));
82 if (std::is_class_v<T>) {
83 auto destructor = [](
const void *x) {
84 static_cast<const T *
>(x)->~T();
86 obj_dstr->destroy = destructor;
88 obj_dstr->destroy =
nullptr;
90 data_objects_.push(std::move(obj_dstr));
101 LOG(INFO) <<
"called:" <<
this;
102 if (data_objects_.empty())
throw std::runtime_error(
"No object to pop");
103 auto *obj_dstr = data_objects_.top();
104 auto *heap_ptr = (T *)obj_dstr->p_obj;
105 auto obj = std::move(*(T *)(heap_ptr));
106 this->free_heap_ptr(obj_dstr);
118 std::stack<Destructor *> data_objects_;
126 Destructor *get_heap_ptr(
size_t bytes_size);
133 void free_heap_ptr(Destructor *);
147 Task(TaskCallback callback, DataObjectPtr data_object =
nullptr);
155 TaskRunnable runnable,
156 TaskCallback callback = [](
int, std::shared_ptr<DataObject>) {},
157 DataObjectPtr data =
nullptr);
163 virtual ~Task()
override;
176 std::string GetUUID()
const;
183 void SignalTaskFinished();
191 void SetFinishAfterRun(
bool finish_after_run);
198 void SetRTN(
int rtn);
201 const std::string uuid_;
202 TaskCallback callback_;
203 TaskRunnable runnable_;
204 bool finish_after_run_ =
true;
206 QThread *callback_thread_ =
nullptr;
207 DataObjectPtr data_object_ =
nullptr;
213 void before_finish_task();
225 virtual void run()
override;
232 static std::string generate_uuid();
236 #endif // GPGFRONTEND_TASK_H