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  static const std::string DEFAULT_TASK_NAME;
54 
55  friend class TaskRunner;
56 
61  class GPGFRONTEND_CORE_EXPORT DataObject {
62  public:
63  struct Destructor {
64  const void *p_obj;
65  void (*destroy)(const void *);
66  };
67 
73  size_t GetObjectSize();
74 
81  template <typename T>
82  void AppendObject(T &&obj) {
83  SPDLOG_TRACE("append object: {}", static_cast<void *>(this));
84  auto *obj_dstr = this->get_heap_ptr(sizeof(T));
85  new ((void *)obj_dstr->p_obj) T(std::forward<T>(obj));
86 
87  if (std::is_class_v<T>) {
88  auto destructor = [](const void *x) {
89  static_cast<const T *>(x)->~T();
90  };
91  obj_dstr->destroy = destructor;
92  } else {
93  obj_dstr->destroy = nullptr;
94  }
95 
96  data_objects_.push(obj_dstr);
97  }
98 
105  template <typename T>
106  void AppendObject(T *obj) {
107  SPDLOG_TRACE("called: {}", static_cast<void *>(this));
108  auto *obj_dstr = this->get_heap_ptr(sizeof(T));
109  auto *ptr_heap = new ((void *)obj_dstr->p_obj) T(std::move(*obj));
110  if (std::is_class_v<T>) {
111  SPDLOG_TRACE("is class");
112  auto destructor = [](const void *x) {
113  static_cast<const T *>(x)->~T();
114  };
115  obj_dstr->destroy = destructor;
116  } else {
117  obj_dstr->destroy = nullptr;
118  }
119  data_objects_.push(std::move(obj_dstr));
120  }
121 
128  template <typename T>
129  T PopObject() {
130  SPDLOG_TRACE("pop object: {}", static_cast<void *>(this));
131  if (data_objects_.empty()) throw std::runtime_error("No object to pop");
132  auto *obj_dstr = data_objects_.top();
133  auto *heap_ptr = (T *)obj_dstr->p_obj;
134  auto obj = std::move(*(T *)(heap_ptr));
135  this->free_heap_ptr(obj_dstr);
136  data_objects_.pop();
137  return obj;
138  }
139 
144  ~DataObject();
145 
146  private:
147  std::stack<Destructor *> data_objects_;
148 
155  Destructor *get_heap_ptr(size_t bytes_size);
156 
162  void free_heap_ptr(Destructor *);
163  };
164 
169  Task(std::string name = DEFAULT_TASK_NAME);
170 
176  explicit Task(TaskRunnable runnable, std::string name = DEFAULT_TASK_NAME,
177  DataObjectPtr data_object = nullptr, bool sequency = true);
178 
184  explicit Task(
185  TaskRunnable runnable, std::string name, DataObjectPtr data,
186  TaskCallback callback = [](int, const std::shared_ptr<DataObject> &) {},
187  bool sequency = true);
188 
193  virtual ~Task() override;
194 
199  virtual void Run();
200 
206  std::string GetUUID() const;
207 
213  std::string GetFullID() const;
214 
220  bool GetSequency() const;
221 
222  public slots:
223 
228  void SlotRun();
229 
230  signals:
235  void SignalTaskRunnableEnd(int rtn);
236 
242 
243  protected:
249  void SetFinishAfterRun(bool finish_after_run);
250 
256  void SetRTN(int rtn);
257 
258  private:
259  const std::string uuid_;
260  const std::string name_;
261  const bool sequency_ = true;
262  TaskCallback callback_;
263  TaskRunnable runnable_;
264  bool run_callback_after_runnable_finished_ = true;
265  int rtn_ = 0;
266  QThread *callback_thread_ = nullptr;
267  DataObjectPtr data_object_ = nullptr;
268 
273  void init();
274 
279  virtual void run() override;
280 
286  static std::string generate_uuid();
287 
288  private slots:
293  void slot_task_run_callback(int rtn);
294 };
295 } // namespace GpgFrontend::Thread
296 
297 #endif // GPGFRONTEND_TASK_H
Definition: TaskRunner.h:40
DataObject to be passed to the callback function.
Definition: Task.h:61
void AppendObject(T &&obj)
Definition: Task.h:82
void AppendObject(T *obj)
Definition: Task.h:106
T PopObject()
Definition: Task.h:129
Definition: Task.h:45
void SignalTaskRunnableEnd(int rtn)
announce runnable finished
static const std::string DEFAULT_TASK_NAME
Definition: Task.h:53
void SignalTaskEnd()
runnable and callabck all finished
Definition: CtxCheckTask.h:33