libcopp  2.2.0
task_actions.h
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
3 #pragma once
4 
5 #include <libcopp/utils/config/libcopp_build_features.h>
6 
8 
9 LIBCOPP_COTASK_NAMESPACE_BEGIN
10 
11 namespace detail {
12 
14  // ================================================
15  template <typename TR, typename TF>
16  static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR (TF::*)(void *), TF &fn, void *priv_data) {
17  fn(priv_data);
18  return 0;
19  }
20 
21  template <typename TR, typename TF>
22  static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR (TF::*)(void *) const, const TF &fn, void *priv_data) {
23  fn(priv_data);
24  return 0;
25  }
26 
27  // ------------------------------------------------
28  template <typename TF>
29  static LIBCOPP_COTASK_API_HEAD_ONLY int call(int (TF::*)(void *), TF &fn, void *priv_data) {
30  return fn(priv_data);
31  }
32 
33  template <typename TF>
34  static LIBCOPP_COTASK_API_HEAD_ONLY int call(int (TF::*)(void *) const, const TF &fn, void *priv_data) {
35  return fn(priv_data);
36  }
37 
38  // ------------------------------------------------
39  template <typename TR, typename TF>
40  static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR (TF::*)(), TF &fn, void * /*priv_data*/) {
41  fn();
42  return 0;
43  }
44 
45  template <typename TR, typename TF>
46  static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR (TF::*)() const, const TF &fn, void * /*priv_data*/) {
47  fn();
48  return 0;
49  }
50 
51  // ------------------------------------------------
52  template <typename TF>
53  static LIBCOPP_COTASK_API_HEAD_ONLY int call(int (TF::*)(), TF &fn, void * /*priv_data*/) {
54  return fn();
55  }
56 
57  template <typename TF>
58  static LIBCOPP_COTASK_API_HEAD_ONLY int call(int (TF::*)() const, const TF &fn, void * /*priv_data*/) {
59  return fn();
60  }
61 };
62 } // namespace detail
63 
64 // functor
65 template <typename Ty>
66 class LIBCOPP_COTASK_API_HEAD_ONLY task_action_functor : public impl::task_action_impl {
67  public:
68  using value_type = typename std::decay<Ty>::type;
69 
70  template <typename... TARG>
71  task_action_functor(TARG &&...arg) : functor_(std::forward<TARG>(arg)...) {}
72 
73  template <typename... TARG>
74  task_action_functor(task_action_functor &&other, TARG &&...) : functor_(std::move(other.functor_)) {}
75 
76  template <typename... TARG>
77  task_action_functor(const task_action_functor &other, TARG &&...) : functor_(other.functor_) {}
78 
80  int operator()(void *priv_data) override {
81  return detail::task_action_functor_check::call(&value_type::operator(), functor_, priv_data);
82  }
83 
84  static void placement_destroy(void *selfp) {
85  if (nullptr == selfp) {
86  return;
87  }
88 
89  task_action_functor<Ty> *self = reinterpret_cast<task_action_functor<Ty> *>(selfp);
90  self->~task_action_functor();
91  }
92 
93  private:
95 };
96 
97 // function
98 template <typename Ty>
99 class LIBCOPP_COTASK_API_HEAD_ONLY task_action_function : public impl::task_action_impl {
100  public:
101  using value_type = Ty (*)(void *);
102 
103  private:
104  template <class Tz, bool IS_INTEGRAL>
105  struct invoker;
106 
107  template <class Tz>
108  struct invoker<Tz, true> {
109  UTIL_FORCEINLINE static int invoke(task_action_function &in, void *priv_data) {
110  return static_cast<int>((*in.func_)(priv_data));
111  }
112  };
113 
114  template <class Tz>
115  struct invoker<Tz, false> {
116  UTIL_FORCEINLINE static int invoke(task_action_function &in, void *priv_data) {
117  (*in.func_)(priv_data);
118  return 0;
119  }
120  };
121 
122  public:
123  task_action_function(value_type func) : func_(func) {}
125 
126  int operator()(void *priv_data) override {
127  return invoker<Ty, std::is_integral<typename std::decay<Ty>::type>::value>::invoke(*this, priv_data);
128  }
129 
130  static void placement_destroy(void *selfp) {
131  if (nullptr == selfp) {
132  return;
133  }
134 
135  task_action_function<Ty> *self = reinterpret_cast<task_action_function<Ty> *>(selfp);
136  self->~task_action_function();
137  }
138 
139  private:
141 };
142 
143 // mem function
144 template <typename Ty, typename Tc>
145 class LIBCOPP_COTASK_API_HEAD_ONLY task_action_mem_function : public impl::task_action_impl {
146  public:
147  using value_type = Ty Tc::*;
148 
149  private:
150  template <class Tz, bool IS_INTEGRAL>
151  struct invoker;
152 
153  template <class Tz>
154  struct invoker<Tz, true> {
155  UTIL_FORCEINLINE static int invoke(task_action_mem_function &in, void *priv_data) {
156  return static_cast<int>((in.instacne_->*in.func_)(priv_data));
157  }
158  };
159 
160  template <class Tz>
161  struct invoker<Tz, false> {
162  UTIL_FORCEINLINE static int invoke(task_action_mem_function &in, void *priv_data) {
163  (in.instacne_->*in.func_)(priv_data);
164  return 0;
165  }
166  };
167 
168  public:
169  task_action_mem_function(value_type func, Tc *inst) : instacne_(inst), func_(func) {}
171 
172  int operator()(void *priv_data) override {
173  return invoker<Ty, std::is_integral<typename std::decay<Ty>::type>::value>::invoke(*this, priv_data);
174  }
175 
176  static void placement_destroy(void *selfp) {
177  if (nullptr == selfp) {
178  return;
179  }
180 
181  task_action_mem_function<Ty, Tc> *self = reinterpret_cast<task_action_mem_function<Ty, Tc> *>(selfp);
183  }
184 
185  private:
188 };
189 
190 template <typename Ty>
191 LIBCOPP_COTASK_API_HEAD_ONLY void placement_destroy(void *selfp) {
192  if (nullptr == selfp) {
193  return;
194  }
195 
196  Ty *self = reinterpret_cast<Ty *>(selfp);
197  self->~Ty();
198 }
199 
200 using placement_destroy_fn_t = void (*)(void *);
201 
202 template <typename Ty>
203 LIBCOPP_COTASK_API_HEAD_ONLY placement_destroy_fn_t get_placement_destroy(task_action_functor<Ty> * /*selfp*/) {
205 }
206 
207 template <typename Ty>
208 LIBCOPP_COTASK_API_HEAD_ONLY placement_destroy_fn_t get_placement_destroy(task_action_function<Ty> * /*selfp*/) {
210 }
211 
212 template <typename Ty, typename Tc>
213 LIBCOPP_COTASK_API_HEAD_ONLY placement_destroy_fn_t
216 }
217 
218 template <typename Ty>
219 LIBCOPP_COTASK_API_HEAD_ONLY placement_destroy_fn_t get_placement_destroy(Ty * /*selfp*/) {
220  return &placement_destroy<Ty>;
221 }
222 LIBCOPP_COTASK_NAMESPACE_END
task_action_function(value_type func)
Definition: task_actions.h:123
static void placement_destroy(void *selfp)
Definition: task_actions.h:130
int operator()(void *priv_data) override
Definition: task_actions.h:126
Ty(*)(void *) value_type
Definition: task_actions.h:101
value_type functor_
Definition: task_actions.h:94
int operator()(void *priv_data) override
Definition: task_actions.h:80
static void placement_destroy(void *selfp)
Definition: task_actions.h:84
task_action_functor(task_action_functor &&other, TARG &&...)
Definition: task_actions.h:74
task_action_functor(TARG &&...arg)
Definition: task_actions.h:71
task_action_functor(const task_action_functor &other, TARG &&...)
Definition: task_actions.h:77
typename std::decay< Ty >::type value_type
Definition: task_actions.h:68
task_action_mem_function(value_type func, Tc *inst)
Definition: task_actions.h:169
int operator()(void *priv_data) override
Definition: task_actions.h:172
static void placement_destroy(void *selfp)
Definition: task_actions.h:176
#define UTIL_FORCEINLINE
static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR(TF::*)() const, const TF &fn, void *)
Definition: task_actions.h:46
static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR(TF::*)(void *), TF &fn, void *priv_data)
Definition: task_actions.h:16
static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR(TF::*)(void *) const, const TF &fn, void *priv_data)
Definition: task_actions.h:22
static LIBCOPP_COTASK_API_HEAD_ONLY int call(int(TF::*)(void *) const, const TF &fn, void *priv_data)
Definition: task_actions.h:34
static LIBCOPP_COTASK_API_HEAD_ONLY int call(int(TF::*)(), TF &fn, void *)
Definition: task_actions.h:53
static LIBCOPP_COTASK_API_HEAD_ONLY int call(int(TF::*)() const, const TF &fn, void *)
Definition: task_actions.h:58
static LIBCOPP_COTASK_API_HEAD_ONLY int call(int(TF::*)(void *), TF &fn, void *priv_data)
Definition: task_actions.h:29
static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR(TF::*)(), TF &fn, void *)
Definition: task_actions.h:40
static UTIL_FORCEINLINE int invoke(task_action_function &in, void *priv_data)
Definition: task_actions.h:116
static UTIL_FORCEINLINE int invoke(task_action_function &in, void *priv_data)
Definition: task_actions.h:109
static UTIL_FORCEINLINE int invoke(task_action_mem_function &in, void *priv_data)
Definition: task_actions.h:162
static UTIL_FORCEINLINE int invoke(task_action_mem_function &in, void *priv_data)
Definition: task_actions.h:155
void(*)(void *) placement_destroy_fn_t
Definition: task_actions.h:200
LIBCOPP_COTASK_API_HEAD_ONLY void placement_destroy(void *selfp)
Definition: task_actions.h:191
LIBCOPP_COTASK_API_HEAD_ONLY placement_destroy_fn_t get_placement_destroy(task_action_functor< Ty > *)
Definition: task_actions.h:203