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  }
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) { return detail::task_action_functor_check::call(&value_type::operator(), functor_, priv_data); }
91 
92  static void placement_destroy(void* selfp) {
93  if (UTIL_CONFIG_NULLPTR == selfp) {
94  return;
95  }
96 
97  task_action_functor<Ty>* self = reinterpret_cast<task_action_functor<Ty>*>(selfp);
98  self->~task_action_functor();
99  }
100 
101  private:
102  value_type functor_;
103  };
104 
105  // function
106  template <typename Ty>
108  public:
109  typedef Ty (*value_type)(void*);
110 
111  public:
112  task_action_function(value_type func) : func_(func) {}
114 
115  virtual int operator()(void* priv_data) {
116  (*func_)(priv_data);
117  return 0;
118  }
119 
120  static void placement_destroy(void* selfp) {
121  if (UTIL_CONFIG_NULLPTR == selfp) {
122  return;
123  }
124 
125  task_action_function<Ty>* self = reinterpret_cast<task_action_function<Ty>*>(selfp);
126  self->~task_action_function();
127  }
128  private:
130  };
131 
132  template <>
134  public:
135  typedef int (*value_type)(void*);
136 
137  public:
138  task_action_function(value_type func) : func_(func) {}
140 
141  virtual int operator()(void* priv_data) { return (*func_)(priv_data); }
142 
143  static void placement_destroy(void* selfp) {
144  if (UTIL_CONFIG_NULLPTR == selfp) {
145  return;
146  }
147 
148  task_action_function<int>* self = reinterpret_cast<task_action_function<int>*>(selfp);
149  self->~task_action_function();
150  }
151 
152  private:
154  };
155 
156  // mem function
157  template <typename Ty, typename Tc>
159  public:
160  typedef Ty Tc::*value_type;
161 
162  public:
163  task_action_mem_function(value_type func, Tc *inst) : instacne_(inst), func_(func) {}
165 
166  virtual int operator()(void* priv_data) {
167  (instacne_->*func_)(priv_data);
168  return 0;
169  }
170 
171  static void placement_destroy(void* selfp) {
172  if (UTIL_CONFIG_NULLPTR == selfp) {
173  return;
174  }
175 
176  task_action_mem_function<Ty, Tc>* self = reinterpret_cast<task_action_mem_function<Ty, Tc>*>(selfp);
178  }
179 
180  private:
183  };
184 
185  template <typename Tc>
187  public:
188  typedef int Tc::*value_type;
189 
190  public:
191  task_action_mem_function(value_type func, Tc *inst) : instacne_(inst), func_(func) {}
193 
194  virtual int operator()(void* priv_data) { return (instacne_->*func_)(priv_data); }
195 
196  static void placement_destroy(void* selfp) {
197  if (UTIL_CONFIG_NULLPTR == selfp) {
198  return;
199  }
200 
203  }
204  private:
207  };
208 
209  template <typename Ty>
210  void placement_destroy(void* selfp) {
211  if (UTIL_CONFIG_NULLPTR == selfp) {
212  return;
213  }
214 
215  Ty* self = reinterpret_cast<Ty*>(selfp);
216  self->~Ty();
217  }
218 
219  typedef void(*placement_destroy_fn_t)(void*);
220 
221  template <typename Ty>
224  }
225 
226  template <typename Ty>
229  }
230 
231  template <typename Ty, typename Tc>
234  }
235 
236  template <typename Ty>
238  return &placement_destroy<Ty>;
239  }
240 }
241 
242 #endif /* _COTASK_TASK_ACTIONS_H_ */
static void placement_destroy(void *selfp)
Definition: task_actions.h:171
virtual int operator()(void *priv_data)
Definition: task_actions.h:141
task_action_mem_function(value_type func, Tc *inst)
Definition: task_actions.h:191
void(* placement_destroy_fn_t)(void *)
Definition: task_actions.h:219
virtual int operator()(void *priv_data)
Definition: task_actions.h:115
task_action_function(value_type func)
Definition: task_actions.h:112
virtual int operator()(void *priv_data)
Definition: task_actions.h:90
void placement_destroy(void *selfp)
Definition: task_actions.h:210
static void placement_destroy(void *selfp)
Definition: task_actions.h:196
task_action_mem_function(value_type func, Tc *inst)
Definition: task_actions.h:163
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
static int call(TR(TF::*)(), TF &fn, void *priv_data)
Definition: task_actions.h:48
static int call(int(TF::*)() const, const TF &fn, void *priv_data)
Definition: task_actions.h:66
static int call(TR(TF::*)() const, const TF &fn, void *priv_data)
Definition: task_actions.h:54
static void placement_destroy(void *selfp)
Definition: task_actions.h:143
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:120
virtual int operator()(void *priv_data)
Definition: task_actions.h:166
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
placement_destroy_fn_t get_placement_destroy(task_action_functor< Ty > *selfp)
Definition: task_actions.h:222
static void placement_destroy(void *selfp)
Definition: task_actions.h:92
virtual int operator()(void *priv_data)
Definition: task_actions.h:194
static int call(int(TF::*)(), TF &fn, void *priv_data)
Definition: task_actions.h:61
std::shared_ptr< cli::cmd_option_value > value_type
Definition: cmd_option.h:53