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 LOG(INFO) <<
"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 LOG(INFO) <<
"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 auto destructor = [](
const void *x) {
108 static_cast<const T *
>(x)->~T();
110 obj_dstr->destroy = destructor;
112 obj_dstr->destroy =
nullptr;
114 data_objects_.push(std::move(obj_dstr));
123 template <
typename T>
125 LOG(INFO) <<
"called:" <<
this;
126 if (data_objects_.empty())
throw std::runtime_error(
"No object to pop");
127 auto *obj_dstr = data_objects_.top();
128 auto *heap_ptr = (T *)obj_dstr->p_obj;
129 auto obj = std::move(*(T *)(heap_ptr));
130 this->free_heap_ptr(obj_dstr);
142 std::stack<Destructor *> data_objects_;
150 Destructor *get_heap_ptr(
size_t bytes_size);
157 void free_heap_ptr(Destructor *);
171 Task(TaskCallback callback, DataObjectPtr data_object =
nullptr);
179 TaskRunnable runnable,
180 TaskCallback callback = [](
int, std::shared_ptr<DataObject>) {},
181 DataObjectPtr data =
nullptr);
187 virtual ~Task()
override;
200 std::string GetUUID()
const;
207 void SignalTaskFinished();
215 void SetFinishAfterRun(
bool finish_after_run);
222 void SetRTN(
int rtn);
225 const std::string uuid_;
226 TaskCallback callback_;
227 TaskRunnable runnable_;
228 bool finish_after_run_ =
true;
230 QThread *callback_thread_ =
nullptr;
231 DataObjectPtr data_object_ =
nullptr;
237 void before_finish_task();
249 virtual void run()
override;
256 static std::string generate_uuid();
260 #endif // GPGFRONTEND_TASK_H