nndeploy C++ API  0.2.0
nndeploy C++ API
runnable_task.h
Go to the documentation of this file.
1 #ifndef _NNDEPLOY_THREAD_POOL_RUNNABLE_TASK_H_
2 #define _NNDEPLOY_THREAD_POOL_RUNNABLE_TASK_H_
3 
4 #include <functional>
5 #include <memory>
6 
7 namespace nndeploy {
8 namespace thread_pool {
9 
10 class RunnableTask {
11  struct TaskBased {
12  explicit TaskBased() = default;
13  virtual void call() = 0;
14  virtual ~TaskBased() = default;
15  };
16 
17  template <typename F, typename T = typename std::decay<F>::type>
18  struct TaskDerived : TaskBased {
19  T func_;
20  explicit TaskDerived(F &&func) : func_(std::forward<F>(func)) {}
21  void call() override { func_(); }
22  };
23 
24  public:
25  // Keep the original templated constructor for lambdas and other callable
26  // objects
27  template <typename F>
28  RunnableTask(F &&f) : impl_(new TaskDerived<F>(std::forward<F>(f))) {}
29 
30  void operator()() {
31  if (impl_) {
32  impl_->call();
33  }
34  }
35 
36  RunnableTask() = default;
37 
38  RunnableTask(RunnableTask &&task) noexcept : impl_(std::move(task.impl_)) {}
39 
40  RunnableTask &operator=(RunnableTask &&task) noexcept {
41  impl_ = std::move(task.impl_);
42  return *this;
43  }
44 
45  private:
46  std::unique_ptr<TaskBased> impl_;
47 };
48 
50 
51 } // namespace thread_pool
52 } // namespace nndeploy
53 
54 #endif //_NNDEPLOY_THREAD_POOL_RUNNABLE_TASK_H_
RunnableTask & operator=(RunnableTask &&task) noexcept
Definition: runnable_task.h:40
RunnableTask(RunnableTask &&task) noexcept
Definition: runnable_task.h:38