GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
Task.h
1 
27 #ifndef GPGFRONTEND_TASK_H
28 #define GPGFRONTEND_TASK_H
29 
30 #include <functional>
31 #include <memory>
32 #include <stack>
33 #include <string>
34 #include <type_traits>
35 #include <utility>
36 
37 #include "core/GpgFrontendCore.h"
38 
39 namespace GpgFrontend::Thread {
40 
41 class TaskRunner;
42 
43 class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable {
44  Q_OBJECT
45  public:
46  class DataObject;
47  using DataObjectPtr = std::shared_ptr<DataObject>;
48  using TaskRunnable = std::function<int(DataObjectPtr)>;
49  using TaskCallback = std::function<void(int, DataObjectPtr)>;
50 
51  friend class TaskRunner;
52 
57  class DataObject {
58  public:
59  struct Destructor {
60  const void *p_obj;
61  void (*destroy)(const void *);
62  };
63 
69  size_t GetObjectSize();
70 
77  template <typename T>
78  void AppendObject(T &&obj) {
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();
85  };
86  obj_dstr->destroy = destructor;
87  } else {
88  obj_dstr->destroy = nullptr;
89  }
90  data_objects_.push(std::move(obj_dstr));
91  }
92 
99  template <typename T>
100  T PopObject() {
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);
107  data_objects_.pop();
108  return obj;
109  }
110 
115  ~DataObject();
116 
117  private:
118  std::stack<Destructor *> data_objects_;
119 
126  Destructor *get_heap_ptr(size_t bytes_size);
127 
133  void free_heap_ptr(Destructor *);
134  };
135 
140  Task();
141 
147  Task(TaskCallback callback, DataObjectPtr data_object = nullptr);
148 
154  Task(
155  TaskRunnable runnable,
156  TaskCallback callback = [](int, std::shared_ptr<DataObject>) {},
157  DataObjectPtr data = nullptr);
158 
163  virtual ~Task() override;
164 
169  virtual void Run();
170 
176  std::string GetUUID() const;
177 
178  signals:
183  void SignalTaskFinished();
184 
185  protected:
191  void SetFinishAfterRun(bool finish_after_run);
192 
198  void SetRTN(int rtn);
199 
200  private:
201  const std::string uuid_;
202  TaskCallback callback_;
203  TaskRunnable runnable_;
204  bool finish_after_run_ = true;
205  int rtn_ = 0;
206  QThread *callback_thread_ = nullptr;
207  DataObjectPtr data_object_ = nullptr;
208 
213  void before_finish_task();
214 
219  void init();
220 
225  virtual void run() override;
226 
232  static std::string generate_uuid();
233 };
234 } // namespace GpgFrontend::Thread
235 
236 #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:100
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:78
GpgFrontend::Thread
Definition: CtxCheckTask.h:33
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:43
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:59
GpgFrontend::Thread::Task::DataObject
DataObject to be passed to the callback function.
Definition: Task.h:57