… | |
… | |
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, |
… | |
… | |
50 | READ = EV_READ, |
37 | READ = EV_READ, |
51 | WRITE = EV_WRITE, |
38 | WRITE = EV_WRITE, |
52 | TIMEOUT = EV_TIMEOUT, |
39 | TIMEOUT = EV_TIMEOUT, |
53 | PERIODIC = EV_PERIODIC, |
40 | PERIODIC = EV_PERIODIC, |
54 | SIGNAL = EV_SIGNAL, |
41 | SIGNAL = EV_SIGNAL, |
|
|
42 | CHILD = EV_CHILD, |
|
|
43 | STAT = EV_STAT, |
55 | IDLE = EV_IDLE, |
44 | IDLE = EV_IDLE, |
56 | CHECK = EV_CHECK, |
45 | CHECK = EV_CHECK, |
57 | PREPARE = EV_PREPARE, |
46 | PREPARE = EV_PREPARE, |
58 | CHILD = EV_CHILD, |
47 | FORK = EV_FORK, |
|
|
48 | EMBED = EV_EMBED, |
59 | ERROR = EV_ERROR, |
49 | ERROR = EV_ERROR, |
60 | }; |
50 | }; |
61 | |
51 | |
62 | typedef ev_tstamp tstamp; |
52 | typedef ev_tstamp tstamp; |
63 | |
53 | |
… | |
… | |
258 | if (active) start (); |
248 | if (active) start (); |
259 | } |
249 | } |
260 | |
250 | |
261 | void start (const char *path, ev_tstamp interval = 0.) |
251 | void start (const char *path, ev_tstamp interval = 0.) |
262 | { |
252 | { |
|
|
253 | stop (); |
263 | set (path, interval); |
254 | set (path, interval); |
264 | start (); |
255 | start (); |
265 | } |
256 | } |
266 | |
257 | |
267 | void update () |
258 | void update () |
… | |
… | |
283 | void set () { } |
274 | void set () { } |
284 | EV_END_WATCHER (check, check) |
275 | EV_END_WATCHER (check, check) |
285 | |
276 | |
286 | #if EV_EMBED_ENABLE |
277 | #if EV_EMBED_ENABLE |
287 | EV_BEGIN_WATCHER (embed, embed) |
278 | EV_BEGIN_WATCHER (embed, embed) |
288 | void set (struct ev_loop *loop) |
|
|
289 | { |
|
|
290 | int active = is_active (); |
|
|
291 | if (active) stop (); |
|
|
292 | ev_embed_set (static_cast<ev_embed *>(this), loop); |
|
|
293 | if (active) start (); |
|
|
294 | } |
|
|
295 | |
|
|
296 | void start (struct ev_loop *embedded_loop) |
279 | void start (struct ev_loop *embedded_loop) |
297 | { |
280 | { |
298 | set (embedded_loop); |
281 | stop (); |
|
|
282 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
299 | start (); |
283 | start (); |
300 | } |
284 | } |
301 | |
285 | |
302 | void sweep () |
286 | void sweep () |
303 | { |
287 | { |
304 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
288 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
305 | } |
289 | } |
306 | 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) |
307 | #endif |
297 | #endif |
308 | |
298 | |
309 | #undef EV_CONSTRUCT |
299 | #undef EV_CONSTRUCT |
310 | #undef EV_BEGIN_WATCHER |
300 | #undef EV_BEGIN_WATCHER |
311 | #undef EV_END_WATCHER |
301 | #undef EV_END_WATCHER |