… | |
… | |
6 | namespace ev { |
6 | namespace ev { |
7 | |
7 | |
8 | template<class watcher> |
8 | template<class watcher> |
9 | class callback |
9 | class callback |
10 | { |
10 | { |
11 | struct object { }; |
11 | struct klass; // it is vital that this is never defined |
12 | |
12 | |
13 | void *obj; |
13 | klass *o; |
14 | void (object::*meth)(watcher &, int); |
14 | void (klass::*m)(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 | { |
|
|
24 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) |
|
|
25 | (w, e); |
|
|
26 | } |
|
|
27 | }; |
|
|
28 | |
|
|
29 | proxy_base *prxy; |
|
|
30 | |
15 | |
31 | public: |
16 | public: |
32 | template<class O1, class O2> |
17 | template<class O1, class O2> |
33 | explicit callback (O1 *object, void (O2::*method)(watcher &, int)) |
18 | explicit callback (O1 *object, void (O2::*method)(watcher &, int)) |
34 | { |
19 | { |
35 | static proxy<O1,O2> p; |
|
|
36 | obj = reinterpret_cast<void *>(object); |
20 | o = reinterpret_cast<klass *>(object); |
37 | meth = reinterpret_cast<void (object::*)(watcher &, int)>(method); |
21 | m = reinterpret_cast<void (klass::*)(watcher &, int)>(method); |
38 | prxy = &p; |
|
|
39 | } |
22 | } |
40 | |
23 | |
|
|
24 | // this works because a standards-compliant C++ compiler |
|
|
25 | // basically can't help it: it doesn't have the knowledge |
|
|
26 | // required to miscompile (klass is not defined anywhere |
|
|
27 | // and nothing is known about the constructor arguments) :) |
41 | void call (watcher *w, int e) const |
28 | void call (watcher *w, int revents) |
42 | { |
29 | { |
43 | return prxy->call (obj, meth, *w, e); |
30 | (o->*m) (*w, revents); |
44 | } |
31 | } |
45 | }; |
32 | }; |
46 | |
33 | |
47 | enum { |
34 | enum { |
48 | UNDEF = EV_UNDEF, |
35 | UNDEF = EV_UNDEF, |
… | |
… | |
261 | if (active) start (); |
248 | if (active) start (); |
262 | } |
249 | } |
263 | |
250 | |
264 | void start (const char *path, ev_tstamp interval = 0.) |
251 | void start (const char *path, ev_tstamp interval = 0.) |
265 | { |
252 | { |
|
|
253 | stop (); |
266 | set (path, interval); |
254 | set (path, interval); |
267 | start (); |
255 | start (); |
268 | } |
256 | } |
269 | |
257 | |
270 | void update () |
258 | void update () |
… | |
… | |
286 | void set () { } |
274 | void set () { } |
287 | EV_END_WATCHER (check, check) |
275 | EV_END_WATCHER (check, check) |
288 | |
276 | |
289 | #if EV_EMBED_ENABLE |
277 | #if EV_EMBED_ENABLE |
290 | EV_BEGIN_WATCHER (embed, embed) |
278 | EV_BEGIN_WATCHER (embed, embed) |
291 | void set (struct ev_loop *loop) |
|
|
292 | { |
|
|
293 | int active = is_active (); |
|
|
294 | if (active) stop (); |
|
|
295 | ev_embed_set (static_cast<ev_embed *>(this), loop); |
|
|
296 | if (active) start (); |
|
|
297 | } |
|
|
298 | |
|
|
299 | void start (struct ev_loop *embedded_loop) |
279 | void start (struct ev_loop *embedded_loop) |
300 | { |
280 | { |
301 | set (embedded_loop); |
281 | stop (); |
|
|
282 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
302 | start (); |
283 | start (); |
303 | } |
284 | } |
304 | |
285 | |
305 | void sweep () |
286 | void sweep () |
306 | { |
287 | { |
307 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
288 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
308 | } |
289 | } |
309 | EV_END_WATCHER (embed, embed) |
290 | EV_END_WATCHER (embed, embed) |
|
|
291 | #endif |
|
|
292 | |
|
|
293 | #if EV_FORK_ENABLE |
|
|
294 | EV_BEGIN_WATCHER (fork, fork) |
|
|
295 | void set () { } |
|
|
296 | EV_END_WATCHER (fork, fork) |
310 | #endif |
297 | #endif |
311 | |
298 | |
312 | #undef EV_CONSTRUCT |
299 | #undef EV_CONSTRUCT |
313 | #undef EV_BEGIN_WATCHER |
300 | #undef EV_BEGIN_WATCHER |
314 | #undef EV_END_WATCHER |
301 | #undef EV_END_WATCHER |