libcopp  1.1.0
task_actions.h
Go to the documentation of this file.
1 /*
2  * task_actions.h
3  *
4  * Created on: 2014年4月1日
5  * Author: owent
6  *
7  * Released under the MIT license
8  */
9 
10 #ifndef COTASK_TASK_ACTIONS_H
11 #define COTASK_TASK_ACTIONS_H
12 
13 #pragma once
14 
16 
17 namespace cotask {
18 
19  namespace detail {
20 
22  // ================================================
23  template <typename TR, typename TF>
24  static int call(TR (TF::*)(void *), TF &fn, void *priv_data) {
25  fn(priv_data);
26  return 0;
27  }
28 
29  template <typename TR, typename TF>
30  static int call(TR (TF::*)(void *) const, const TF &fn, void *priv_data) {
31  fn(priv_data);
32  return 0;
33  }
34 
35  // ------------------------------------------------
36  template <typename TF>
37  static int call(int (TF::*)(void *), TF &fn, void *priv_data) {
38  return fn(priv_data);
39  }
40 
41  template <typename TF>
42  static int call(int (TF::*)(void *) const, const TF &fn, void *priv_data) {
43  return fn(priv_data);
44  }
45 
46  // ------------------------------------------------
47  template <typename TR, typename TF>
48  static int call(TR (TF::*)(), TF &fn, void * /*priv_data*/) {
49  fn();
50  return 0;
51  }
52 
53  template <typename TR, typename TF>
54  static int call(TR (TF::*)() const, const TF &fn, void * /*priv_data*/) {
55  fn();
56  return 0;
57  }
58 
59  // ------------------------------------------------
60  template <typename TF>
61  static int call(int (TF::*)(), TF &fn, void * /*priv_data*/) {
62  return fn();
63  }
64 
65  template <typename TF>
66  static int call(int (TF::*)() const, const TF &fn, void * /*priv_data*/) {
67  return fn();
68  }
69  };
70  } // namespace detail
71 
72  // functor
73  template <typename Ty>
75 #if defined(UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES) && UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES
76  public:
77  typedef typename std::remove_cv<Ty>::type value_type;
78 
79  task_action_functor(value_type &&functor) : functor_(functor) {}
80 
81  task_action_functor(task_action_functor &&other) : functor_(std::move(other.functor_)) {}
82  inline task_action_functor &operator=(task_action_functor &&other) { functor_ = std::move(other.functor_); }
83 
84 #else
85  public:
86  typedef Ty value_type;
87  task_action_functor(const value_type &functor) : functor_(functor) {}
88 #endif
90  virtual int operator()(void *priv_data) {
91  return detail::task_action_functor_check::call(&value_type::operator(), functor_, priv_data);
92  }
93 
94  static void placement_destroy(void *selfp) {
95  if (UTIL_CONFIG_NULLPTR == selfp) {
96  return;
97  }
98 
99  task_action_functor<Ty> *self = reinterpret_cast<task_action_functor<Ty> *>(selfp);
100  self->~task_action_functor();
101  }
102 
103  private:
104  value_type functor_;
105  };
106 
107  // function
108  template <typename Ty>
110  public:
111  typedef Ty (*value_type)(void *);
112 
113  public:
114  task_action_function(value_type func) : func_(func) {}
116 
117  virtual int operator()(void *priv_data) {
118  (*func_)(priv_data);
119  return 0;
120  }
121 
122  static void placement_destroy(void *selfp) {
123  if (UTIL_CONFIG_NULLPTR == selfp) {
124  return;
125  }
126 
127  task_action_function<Ty> *self = reinterpret_cast<task_action_function<Ty> *>(selfp);
128  self->~task_action_function();
129  }
130 
131  private:
133  };
134 
135  template <>
137  public:
138  typedef int (*value_type)(void *);
139 
140  public:
141  task_action_function(value_type func) : func_(func) {}
143 
144  virtual int operator()(void *priv_data) { return (*func_)(priv_data); }
145 
146  static void placement_destroy(void *selfp) {
147  if (UTIL_CONFIG_NULLPTR == selfp) {
148  return;
149  }
150 
151  task_action_function<int> *self = reinterpret_cast<task_action_function<int> *>(selfp);
152  self->~task_action_function();
153  }
154 
155  private:
157  };
158 
159  // mem function
160  template <typename Ty, typename Tc>
162  public:
163  typedef Ty Tc::*value_type;
164 
165  public:
166  task_action_mem_function(value_type func, Tc *inst) : instacne_(inst), func_(func) {}
168 
169  virtual int operator()(void *priv_data) {
170  (instacne_->*func_)(priv_data);
171  return 0;
172  }
173 
174  static void placement_destroy(void *selfp) {
175  if (UTIL_CONFIG_NULLPTR == selfp) {
176  return;
177  }
178 
179  task_action_mem_function<Ty, Tc> *self = reinterpret_cast<task_action_mem_function<Ty, Tc> *>(selfp);
181  }
182 
183  private:
184  Tc * instacne_;
186  };
187 
188  template <typename Tc>
190  public:
191  typedef int Tc::*value_type;
192 
193  public:
194  task_action_mem_function(value_type func, Tc *inst) : instacne_(inst), func_(func) {}
196 
197  virtual int operator()(void *priv_data) { return (instacne_->*func_)(priv_data); }
198 
199  static void placement_destroy(void *selfp) {
200  if (UTIL_CONFIG_NULLPTR == selfp) {
201  return;
202  }
203 
204  task_action_mem_function<int, Tc> *self = reinterpret_cast<task_action_mem_function<int, Tc> *>(selfp);
206  }
207 
208  private:
209  Tc * instacne_;
211  };
212 
213  template <typename Ty>
214  void placement_destroy(void *selfp) {
215  if (UTIL_CONFIG_NULLPTR == selfp) {
216  return;
217  }
218 
219  Ty *self = reinterpret_cast<Ty *>(selfp);
220  self->~Ty();
221  }
222 
223  typedef void (*placement_destroy_fn_t)(void *);
224 
225  template <typename Ty>
228  }
229 
230  template <typename Ty>
233  }
234 
235  template <typename Ty, typename Tc>
238  }
239 
240  template <typename Ty>
242  return &placement_destroy<Ty>;
243  }
244 } // namespace cotask
245 
246 #endif /* _COTASK_TASK_ACTIONS_H_ */
static void placement_destroy(void *selfp)
Definition: task_actions.h:174
virtual int operator()(void *priv_data)
Definition: task_actions.h:144
task_action_mem_function(value_type func, Tc *inst)
Definition: task_actions.h:194
void(* placement_destroy_fn_t)(void *)
Definition: task_actions.h:223
virtual int operator()(void *priv_data)
Definition: task_actions.h:117
task_action_function(value_type func)
Definition: task_actions.h:114
virtual int operator()(void *priv_data)
Definition: task_actions.h:90
void placement_destroy(void *selfp)
Definition: task_actions.h:214
static void placement_destroy(void *selfp)
Definition: task_actions.h:199
task_action_mem_function(value_type func, Tc *inst)
Definition: task_actions.h:166
static int call(TR(TF::*)(void *) const, const TF &fn, void *priv_data)
Definition: task_actions.h:30
static int call(int(TF::*)(void *), TF &fn, void *priv_data)
Definition: task_actions.h:37
placement_destroy_fn_t get_placement_destroy(task_action_functor< Ty > *)
Definition: task_actions.h:226
static void placement_destroy(void *selfp)
Definition: task_actions.h:146
static int call(int(TF::*)(void *) const, const TF &fn, void *priv_data)
Definition: task_actions.h:42
static void placement_destroy(void *selfp)
Definition: task_actions.h:122
virtual int operator()(void *priv_data)
Definition: task_actions.h:169
task_action_functor(const value_type &functor)
Definition: task_actions.h:87
static int call(TR(TF::*)(void *), TF &fn, void *priv_data)
Definition: task_actions.h:24
static int call(TR(TF::*)() const, const TF &fn, void *)
Definition: task_actions.h:54
static int call(int(TF::*)(), TF &fn, void *)
Definition: task_actions.h:61
static int call(TR(TF::*)(), TF &fn, void *)
Definition: task_actions.h:48
static void placement_destroy(void *selfp)
Definition: task_actions.h:94
virtual int operator()(void *priv_data)
Definition: task_actions.h:197
static int call(int(TF::*)() const, const TF &fn, void *)
Definition: task_actions.h:66
std::shared_ptr< cli::cmd_option_value > value_type
Definition: cmd_option.h:53