GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
Task.h
1 
29 #ifndef GPGFRONTEND_TASK_H
30 #define GPGFRONTEND_TASK_H
31 
32 #include <functional>
33 #include <memory>
34 #include <stack>
35 #include <string>
36 #include <type_traits>
37 #include <utility>
38 
39 #include "core/GpgFrontendCore.h"
40 
41 namespace GpgFrontend::Thread {
42 
43 class TaskRunner;
44 
45 class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable {
46  Q_OBJECT
47  public:
48  class DataObject;
49  using DataObjectPtr = std::shared_ptr<DataObject>;
50  using TaskRunnable = std::function<int(DataObjectPtr)>;
51  using TaskCallback = std::function<void(int, DataObjectPtr)>;
52 
53  friend class TaskRunner;
54 
59  class GPGFRONTEND_CORE_EXPORT DataObject {
60  public:
61  struct Destructor {
62  const void *p_obj;
63  void (*destroy)(const void *);
64  };
65 
71  size_t GetObjectSize();
72 
79  template <typename T>
80  void AppendObject(T &&obj) {
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();
87  };
88  obj_dstr->destroy = destructor;
89  } else {
90  obj_dstr->destroy = nullptr;
91  }
92  data_objects_.push(std::move(obj_dstr));
93  }
94 
101  template <typename T>
102  void AppendObject(T *obj) {
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();
109  };
110  obj_dstr->destroy = destructor;
111  } else {
112  obj_dstr->destroy = nullptr;
113  }
114  data_objects_.push(std::move(obj_dstr));
115  }
116 
123  template <typename T>
124  T PopObject() {
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);
131  data_objects_.pop();
132  return obj;
133  }
134 
139  ~DataObject();
140 
141  private:
142  std::stack<Destructor *> data_objects_;
143 
150  Destructor *get_heap_ptr(size_t bytes_size);
151 
157  void free_heap_ptr(Destructor *);
158  };
159 
164  Task();
165 
171  Task(TaskCallback callback, DataObjectPtr data_object = nullptr);
172 
178  Task(
179  TaskRunnable runnable,
180  TaskCallback callback = [](int, std::shared_ptr<DataObject>) {},
181  DataObjectPtr data = nullptr);
182 
187  virtual ~Task() override;
188 
193  virtual void Run();
194 
200  std::string GetUUID() const;
201 
202  signals:
207  void SignalTaskFinished();
208 
209  protected:
215  void SetFinishAfterRun(bool finish_after_run);
216 
222  void SetRTN(int rtn);
223 
224  private:
225  const std::string uuid_;
226  TaskCallback callback_;
227  TaskRunnable runnable_;
228  bool finish_after_run_ = true;
229  int rtn_ = 0;
230  QThread *callback_thread_ = nullptr;
231  DataObjectPtr data_object_ = nullptr;
232 
237  void before_finish_task();
238 
243  void init();
244 
249  virtual void run() override;
250 
256  static std::string generate_uuid();
257 };
258 } // namespace GpgFrontend::Thread
259 
260 #endif // GPGFRONTEND_TASK_H
GpgFrontend::Thread::TaskRunner
Definition: TaskRunner.h:39
GpgFrontend::Thread::Task::DataObject::free_heap_ptr
void free_heap_ptr(Destructor *)
Definition: Task.cpp:133
GpgFrontend::Thread::Task::SetRTN
void SetRTN(int rtn)
Definition: Task.cpp:75
GpgFrontend::Thread::Task::DataObject::get_heap_ptr
Destructor * get_heap_ptr(size_t bytes_size)
Get the heap ptr object.
Definition: Task.cpp:113
GpgFrontend::Thread::Task::DataObject::~DataObject
~DataObject()
Destroy the Data Object object.
Definition: Task.cpp:119
GpgFrontend::Thread::Task::~Task
virtual ~Task() override
Destroy the Task object.
GpgFrontend::Thread::Task::DataObject::PopObject
T PopObject()
Definition: Task.h:124
GpgFrontend::Thread::Task::Task
Task()
Construct a new Task object.
Definition: Task.cpp:39
GpgFrontend::Thread::Task::DataObject::AppendObject
void AppendObject(T &&obj)
Definition: Task.h:80
GpgFrontend::Thread
Definition: CtxCheckTask.h:33
GpgFrontend::Thread::Task::DataObject::AppendObject
void AppendObject(T *obj)
Definition: Task.h:102
GpgFrontend::Thread::Task::Run
virtual void Run()
Run - run the task.
Definition: Task.cpp:102
GpgFrontend::Thread::Task::generate_uuid
static std::string generate_uuid()
Definition: Task.cpp:143
GpgFrontend::Thread::Task::DataObject::GetObjectSize
size_t GetObjectSize()
Get the Objects Size.
Definition: Task.cpp:129
GpgFrontend::Thread::Task
Definition: Task.h:45
GpgFrontend::Thread::Task::SetFinishAfterRun
void SetFinishAfterRun(bool finish_after_run)
Set the Finish After Run object.
Definition: Task.cpp:71
GpgFrontend::Thread::Task::GetUUID
std::string GetUUID() const
Definition: Task.cpp:69
GpgFrontend::Thread::Task::DataObject::Destructor
Definition: Task.h:61
GpgFrontend::Thread::Task::DataObject
DataObject to be passed to the callback function.
Definition: Task.h:59