… | |
… | |
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, |