Lely core libraries  2.3.4
task.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_EV_TASK_HPP_
25 #define LELY_EV_TASK_HPP_
26 
27 #include <lely/ev/task.h>
28 #include <lely/util/invoker.hpp>
29 
30 #include <functional>
31 #include <utility>
32 
33 namespace lely {
34 namespace ev {
35 
36 class Executor;
37 
38 namespace detail {
39 
40 template <class Invoker>
41 class TaskWrapper : public ev_task {
42  public:
43  template <class F, class... Args>
44  TaskWrapper(ev_exec_t* exec, F&& f, Args&&... args)
46  [](ev_task* task) noexcept {
47  auto self = static_cast<TaskWrapper*>(task);
48  self->invoker_();
49  delete self;
50  }),
51  invoker_(::std::forward<F>(f), ::std::forward<Args>(args)...) {}
52 
53  TaskWrapper(const TaskWrapper&) = delete;
54 
55  TaskWrapper& operator=(const TaskWrapper&) = delete;
56 
57  private:
58  Invoker invoker_;
59 };
60 
61 } // namespace detail
62 
67 template <class F, class... Args>
69 
75 template <class F, class... Args>
76 inline TaskWrapper<F, Args...>*
77 make_task_wrapper(ev_exec_t* exec, F&& f, Args&&... args) {
78  return new TaskWrapper<F, Args...>(exec, ::std::forward<F>(f),
79  ::std::forward<Args>(args)...);
80 }
81 
86 class Task : public ev_task {
87  public:
88  using Signature = void();
89 
94  template <class F>
95  Task(ev_exec_t* exec, F&& f)
97  [](ev_task* task) noexcept {
98  auto self = static_cast<Task*>(task);
99  if (self->func_) self->func_();
100  }),
101  func_(::std::forward<F>(f)) {}
102 
104  template <class F>
105  explicit Task(F&& f) : Task(nullptr, ::std::forward<F>(f)) {}
106 
107  Task(const Task&) = delete;
108 
109  Task& operator=(const Task&) = delete;
110 
112  Executor get_executor() const noexcept;
113 
114  private:
115  ::std::function<Signature> func_;
116 };
117 
118 } // namespace ev
119 } // namespace lely
120 
121 #endif // !LELY_EV_TASK_HPP_
An abstract task executor. This class is a wrapper around #ev_exec_t*.
Definition: exec.hpp:38
A basic task.
Definition: task.hpp:86
Task(ev_exec_t *exec, F &&f)
Constructs a task from a callable object with an associated executor (can be nullptr).
Definition: task.hpp:95
Task(F &&f)
Constructs a task from a callable object.
Definition: task.hpp:105
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
This header file is part of the utilities library; it contains a function object that can be used to ...
An executable task.
Definition: task.h:41
ev_exec_t * exec
A pointer to the executor to which the task is (to be) submitted.
Definition: task.h:43
This header file is part of the event library; it contains the task declarations.
#define EV_TASK_INIT(exec, func)
The static initializer for ev_task.
Definition: task.h:53
TaskWrapper< F, Args... > * make_task_wrapper(ev_exec_t *exec, F &&f, Args &&... args)
Creates a temporary task from a callable object with an associated executor (can be nullptr).
Definition: task.hpp:77