1 | #ifndef EVPP_H__ |
1 | #ifndef EVPP_H__ |
2 | #define EVPP_H__ |
2 | #define EVPP_H__ |
3 | |
3 | |
|
|
4 | #ifdef EV_H |
|
|
5 | # include EV_H |
|
|
6 | #else |
4 | #include "ev.h" |
7 | # include <ev.h> |
|
|
8 | #endif |
5 | |
9 | |
6 | namespace ev { |
10 | namespace ev { |
7 | |
11 | |
8 | template<class watcher> |
12 | template<class ev_watcher, class watcher> |
9 | class callback |
13 | struct base : ev_watcher |
10 | { |
14 | { |
11 | struct object { }; |
15 | #if EV_MULTIPLICITY |
|
|
16 | EV_P; |
12 | |
17 | |
13 | void *obj; |
18 | void set (EV_P) |
14 | void (object::*meth)(watcher &, int); |
|
|
15 | |
|
|
16 | /* a proxy is a kind of recipe on how to call a specific class method */ |
|
|
17 | struct proxy_base { |
|
|
18 | virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int) const = 0; |
|
|
19 | }; |
|
|
20 | template<class O1, class O2> |
|
|
21 | struct proxy : proxy_base { |
|
|
22 | virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int e) const |
|
|
23 | { |
19 | { |
24 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) |
20 | this->EV_A = EV_A; |
25 | (w, e); |
|
|
26 | } |
21 | } |
|
|
22 | #endif |
|
|
23 | |
|
|
24 | base () |
|
|
25 | { |
|
|
26 | ev_init (this, 0); |
27 | }; |
27 | } |
28 | |
28 | |
29 | proxy_base *prxy; |
29 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
30 | |
|
|
31 | public: |
|
|
32 | template<class O1, class O2> |
|
|
33 | explicit callback (O1 *object, void (O2::*method)(watcher &, int)) |
|
|
34 | { |
|
|
35 | static proxy<O1,O2> p; |
|
|
36 | obj = reinterpret_cast<void *>(object); |
|
|
37 | meth = reinterpret_cast<void (object::*)(watcher &, int)>(method); |
|
|
38 | prxy = &p; |
|
|
39 | } |
30 | { |
40 | |
31 | this->data = data; |
41 | void call (watcher *w, int e) const |
32 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
42 | { |
33 | } |
43 | return prxy->call (obj, meth, *w, e); |
34 | |
|
|
35 | // method callback |
|
|
36 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
37 | void set (K *object) |
|
|
38 | { |
|
|
39 | set_ (object, method_thunk<K, method>); |
|
|
40 | } |
|
|
41 | |
|
|
42 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
43 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
44 | { |
|
|
45 | K *obj = static_cast<K *>(w->data); |
|
|
46 | (obj->*method) (*static_cast<watcher *>(w), revents); |
|
|
47 | } |
|
|
48 | |
|
|
49 | // const method callback |
|
|
50 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
51 | void set (const K *object) |
|
|
52 | { |
|
|
53 | set_ (object, const_method_thunk<K, method>); |
|
|
54 | } |
|
|
55 | |
|
|
56 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
57 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
58 | { |
|
|
59 | K *obj = static_cast<K *>(w->data); |
|
|
60 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
|
|
61 | } |
|
|
62 | |
|
|
63 | // function callback |
|
|
64 | template<void (*function)(watcher &w, int)> |
|
|
65 | void set (void *data = 0) |
|
|
66 | { |
|
|
67 | set_ (data, function_thunk<function>); |
|
|
68 | } |
|
|
69 | |
|
|
70 | template<void (*function)(watcher &w, int)> |
|
|
71 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
72 | { |
|
|
73 | function (*static_cast<watcher *>(w), revents); |
|
|
74 | } |
|
|
75 | |
|
|
76 | // simple callback |
|
|
77 | template<class K, void (K::*method)()> |
|
|
78 | void set (K *object) |
|
|
79 | { |
|
|
80 | set_ (object, method_noargs_thunk<K, method>); |
|
|
81 | } |
|
|
82 | |
|
|
83 | template<class K, void (K::*method)()> |
|
|
84 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
85 | { |
|
|
86 | K *obj = static_cast<K *>(w->data); |
|
|
87 | (obj->*method) (); |
|
|
88 | } |
|
|
89 | |
|
|
90 | void operator ()(int events = EV_UNDEF) |
|
|
91 | { |
|
|
92 | return ev_cb (static_cast<ev_watcher *>(this)) |
|
|
93 | (static_cast<ev_watcher *>(this), events); |
|
|
94 | } |
|
|
95 | |
|
|
96 | bool is_active () const |
|
|
97 | { |
|
|
98 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
|
|
99 | } |
|
|
100 | |
|
|
101 | bool is_pending () const |
|
|
102 | { |
|
|
103 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
44 | } |
104 | } |
45 | }; |
105 | }; |
46 | |
106 | |
47 | enum { |
107 | enum { |
48 | UNDEF = EV_UNDEF, |
108 | UNDEF = EV_UNDEF, |
… | |
… | |
50 | READ = EV_READ, |
110 | READ = EV_READ, |
51 | WRITE = EV_WRITE, |
111 | WRITE = EV_WRITE, |
52 | TIMEOUT = EV_TIMEOUT, |
112 | TIMEOUT = EV_TIMEOUT, |
53 | PERIODIC = EV_PERIODIC, |
113 | PERIODIC = EV_PERIODIC, |
54 | SIGNAL = EV_SIGNAL, |
114 | SIGNAL = EV_SIGNAL, |
|
|
115 | CHILD = EV_CHILD, |
|
|
116 | STAT = EV_STAT, |
55 | IDLE = EV_IDLE, |
117 | IDLE = EV_IDLE, |
56 | CHECK = EV_CHECK, |
118 | CHECK = EV_CHECK, |
57 | PREPARE = EV_PREPARE, |
119 | PREPARE = EV_PREPARE, |
58 | CHILD = EV_CHILD, |
120 | FORK = EV_FORK, |
|
|
121 | EMBED = EV_EMBED, |
59 | ERROR = EV_ERROR, |
122 | ERROR = EV_ERROR, |
60 | }; |
123 | }; |
61 | |
124 | |
62 | typedef ev_tstamp tstamp; |
125 | typedef ev_tstamp tstamp; |
63 | |
126 | |
… | |
… | |
65 | { |
128 | { |
66 | return ev_now (EV_A); |
129 | return ev_now (EV_A); |
67 | } |
130 | } |
68 | |
131 | |
69 | #if EV_MULTIPLICITY |
132 | #if EV_MULTIPLICITY |
70 | |
|
|
71 | #define EV_CONSTRUCT(cppstem) \ |
|
|
72 | EV_P; \ |
133 | #define EV_CONSTRUCT \ |
73 | \ |
|
|
74 | void set (EV_P) \ |
134 | (EV_P = EV_DEFAULT) \ |
75 | { \ |
135 | { \ |
76 | this->EV_A = EV_A; \ |
|
|
77 | } \ |
|
|
78 | \ |
136 | set (EV_A); \ |
79 | template<class O1, class O2> \ |
137 | } |
80 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \ |
|
|
81 | : callback<cppstem> (object, method), EV_A (EV_A) |
|
|
82 | |
|
|
83 | #else |
138 | #else |
84 | |
139 | #define EV_CONSTRUCT \ |
85 | #define EV_CONSTRUCT(cppstem) \ |
140 | () \ |
86 | template<class O1, class O2> \ |
141 | { \ |
87 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
142 | } |
88 | : callback<cppstem> (object, method) |
|
|
89 | |
|
|
90 | #endif |
143 | #endif |
91 | |
144 | |
92 | /* using a template here would require quite a bit more lines, |
145 | /* using a template here would require quite a bit more lines, |
93 | * so a macro solution was chosen */ |
146 | * so a macro solution was chosen */ |
94 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
147 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
95 | \ |
148 | \ |
96 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
149 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
97 | { \ |
150 | { \ |
98 | EV_CONSTRUCT (cppstem) \ |
|
|
99 | { \ |
|
|
100 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
|
|
101 | } \ |
|
|
102 | \ |
|
|
103 | bool is_active () const \ |
|
|
104 | { \ |
|
|
105 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
106 | } \ |
|
|
107 | \ |
|
|
108 | bool is_pending () const \ |
|
|
109 | { \ |
|
|
110 | return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
111 | } \ |
|
|
112 | \ |
|
|
113 | void start () \ |
151 | void start () \ |
114 | { \ |
152 | { \ |
115 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
153 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
116 | } \ |
154 | } \ |
117 | \ |
155 | \ |
118 | void stop () \ |
156 | void stop () \ |
119 | { \ |
157 | { \ |
120 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
158 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
121 | } \ |
159 | } \ |
122 | \ |
160 | \ |
123 | void operator ()(int events = EV_UNDEF) \ |
|
|
124 | { \ |
161 | cppstem EV_CONSTRUCT \ |
125 | return call (this, events); \ |
|
|
126 | } \ |
|
|
127 | \ |
162 | \ |
128 | ~cppstem () \ |
163 | ~cppstem () \ |
129 | { \ |
164 | { \ |
130 | stop (); \ |
165 | stop (); \ |
131 | } \ |
166 | } \ |
132 | \ |
167 | \ |
|
|
168 | using base<ev_ ## cstem, cppstem>::set; \ |
|
|
169 | \ |
133 | private: \ |
170 | private: \ |
134 | \ |
171 | \ |
135 | cppstem (const cppstem &o) \ |
172 | cppstem (const cppstem &o) \ |
136 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
|
|
137 | { /* disabled */ } \ |
173 | { /* disabled */ } \ |
138 | \ |
174 | \ |
139 | void operator =(const cppstem &o) { /* disabled */ } \ |
175 | void operator =(const cppstem &o) { /* disabled */ } \ |
140 | \ |
|
|
141 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
142 | { \ |
|
|
143 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
144 | } \ |
|
|
145 | \ |
176 | \ |
146 | public: |
177 | public: |
147 | |
178 | |
148 | #define EV_END_WATCHER(cppstem,cstem) \ |
179 | #define EV_END_WATCHER(cppstem,cstem) \ |
149 | }; |
180 | }; |
… | |
… | |
191 | { |
222 | { |
192 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
223 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
193 | } |
224 | } |
194 | EV_END_WATCHER (timer, timer) |
225 | EV_END_WATCHER (timer, timer) |
195 | |
226 | |
196 | #if EV_PERIODICS |
227 | #if EV_PERIODIC_ENABLE |
197 | EV_BEGIN_WATCHER (periodic, periodic) |
228 | EV_BEGIN_WATCHER (periodic, periodic) |
198 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
229 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
199 | { |
230 | { |
200 | int active = is_active (); |
231 | int active = is_active (); |
201 | if (active) stop (); |
232 | if (active) stop (); |
… | |
… | |
212 | void again () |
243 | void again () |
213 | { |
244 | { |
214 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
245 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
215 | } |
246 | } |
216 | EV_END_WATCHER (periodic, periodic) |
247 | EV_END_WATCHER (periodic, periodic) |
|
|
248 | #endif |
|
|
249 | |
|
|
250 | EV_BEGIN_WATCHER (sig, signal) |
|
|
251 | void set (int signum) |
|
|
252 | { |
|
|
253 | int active = is_active (); |
|
|
254 | if (active) stop (); |
|
|
255 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
|
|
256 | if (active) start (); |
|
|
257 | } |
|
|
258 | |
|
|
259 | void start (int signum) |
|
|
260 | { |
|
|
261 | set (signum); |
|
|
262 | start (); |
|
|
263 | } |
|
|
264 | EV_END_WATCHER (sig, signal) |
|
|
265 | |
|
|
266 | EV_BEGIN_WATCHER (child, child) |
|
|
267 | void set (int pid) |
|
|
268 | { |
|
|
269 | int active = is_active (); |
|
|
270 | if (active) stop (); |
|
|
271 | ev_child_set (static_cast<ev_child *>(this), pid); |
|
|
272 | if (active) start (); |
|
|
273 | } |
|
|
274 | |
|
|
275 | void start (int pid) |
|
|
276 | { |
|
|
277 | set (pid); |
|
|
278 | start (); |
|
|
279 | } |
|
|
280 | EV_END_WATCHER (child, child) |
|
|
281 | |
|
|
282 | #if EV_STAT_ENABLE |
|
|
283 | EV_BEGIN_WATCHER (stat, stat) |
|
|
284 | void set (const char *path, ev_tstamp interval = 0.) |
|
|
285 | { |
|
|
286 | int active = is_active (); |
|
|
287 | if (active) stop (); |
|
|
288 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
|
|
289 | if (active) start (); |
|
|
290 | } |
|
|
291 | |
|
|
292 | void start (const char *path, ev_tstamp interval = 0.) |
|
|
293 | { |
|
|
294 | stop (); |
|
|
295 | set (path, interval); |
|
|
296 | start (); |
|
|
297 | } |
|
|
298 | |
|
|
299 | void update () |
|
|
300 | { |
|
|
301 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
|
|
302 | } |
|
|
303 | EV_END_WATCHER (stat, stat) |
217 | #endif |
304 | #endif |
218 | |
305 | |
219 | EV_BEGIN_WATCHER (idle, idle) |
306 | EV_BEGIN_WATCHER (idle, idle) |
220 | void set () { } |
307 | void set () { } |
221 | EV_END_WATCHER (idle, idle) |
308 | EV_END_WATCHER (idle, idle) |
… | |
… | |
226 | |
313 | |
227 | EV_BEGIN_WATCHER (check, check) |
314 | EV_BEGIN_WATCHER (check, check) |
228 | void set () { } |
315 | void set () { } |
229 | EV_END_WATCHER (check, check) |
316 | EV_END_WATCHER (check, check) |
230 | |
317 | |
231 | EV_BEGIN_WATCHER (sig, signal) |
318 | #if EV_EMBED_ENABLE |
232 | void set (int signum) |
|
|
233 | { |
|
|
234 | int active = is_active (); |
|
|
235 | if (active) stop (); |
|
|
236 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
|
|
237 | if (active) start (); |
|
|
238 | } |
|
|
239 | |
|
|
240 | void start (int signum) |
|
|
241 | { |
|
|
242 | set (signum); |
|
|
243 | start (); |
|
|
244 | } |
|
|
245 | EV_END_WATCHER (sig, signal) |
|
|
246 | |
|
|
247 | EV_BEGIN_WATCHER (child, child) |
|
|
248 | void set (int pid) |
|
|
249 | { |
|
|
250 | int active = is_active (); |
|
|
251 | if (active) stop (); |
|
|
252 | ev_child_set (static_cast<ev_child *>(this), pid); |
|
|
253 | if (active) start (); |
|
|
254 | } |
|
|
255 | |
|
|
256 | void start (int pid) |
|
|
257 | { |
|
|
258 | set (pid); |
|
|
259 | start (); |
|
|
260 | } |
|
|
261 | EV_END_WATCHER (child, child) |
|
|
262 | |
|
|
263 | #if EV_MULTIPLICITY |
|
|
264 | |
|
|
265 | EV_BEGIN_WATCHER (embed, embed) |
319 | EV_BEGIN_WATCHER (embed, embed) |
266 | void set (struct ev_loop *loop) |
|
|
267 | { |
|
|
268 | int active = is_active (); |
|
|
269 | if (active) stop (); |
|
|
270 | ev_embed_set (static_cast<ev_embed *>(this), loop); |
|
|
271 | if (active) start (); |
|
|
272 | } |
|
|
273 | |
|
|
274 | void start (struct ev_loop *embedded_loop) |
320 | void start (struct ev_loop *embedded_loop) |
275 | { |
321 | { |
276 | set (embedded_loop); |
322 | stop (); |
|
|
323 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
277 | start (); |
324 | start (); |
278 | } |
325 | } |
279 | |
326 | |
280 | void sweep () |
327 | void sweep () |
281 | { |
328 | { |
282 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
329 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
283 | } |
330 | } |
284 | EV_END_WATCHER (embed, embed) |
331 | EV_END_WATCHER (embed, embed) |
|
|
332 | #endif |
285 | |
333 | |
|
|
334 | #if EV_FORK_ENABLE |
|
|
335 | EV_BEGIN_WATCHER (fork, fork) |
|
|
336 | void set () { } |
|
|
337 | EV_END_WATCHER (fork, fork) |
286 | #endif |
338 | #endif |
287 | |
339 | |
288 | #undef EV_CONSTRUCT |
340 | #undef EV_CONSTRUCT |
289 | #undef EV_BEGIN_WATCHER |
341 | #undef EV_BEGIN_WATCHER |
290 | #undef EV_END_WATCHER |
342 | #undef EV_END_WATCHER |