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 ev_watcher, class watcher> |
12 | template<class ev_watcher, class watcher> |
9 | struct base : ev_watcher |
13 | struct base : ev_watcher |
… | |
… | |
20 | base () |
24 | base () |
21 | { |
25 | { |
22 | ev_init (this, 0); |
26 | ev_init (this, 0); |
23 | } |
27 | } |
24 | |
28 | |
25 | void set_ (void *data, void (*cb)(EV_P_ watcher *w, int revents)) |
29 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
26 | { |
30 | { |
27 | this->data = data; |
31 | this->data = data; |
28 | ev_set_cb (static_cast<watcher *>(this), cb); |
32 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
29 | } |
33 | } |
30 | |
34 | |
|
|
35 | // method callback |
31 | template<class K, void (K::*method)(watcher &w, int)> |
36 | template<class K, void (K::*method)(watcher &w, int)> |
32 | void set (K *object) |
37 | void set (K *object) |
33 | { |
38 | { |
34 | set_ (object, method_thunk<K, method>); |
39 | set_ (object, method_thunk<K, method>); |
35 | } |
40 | } |
36 | |
41 | |
37 | template<class K, void (K::*method)(watcher &w, int)> |
42 | template<class K, void (K::*method)(watcher &w, int)> |
38 | static void method_thunk (EV_P_ watcher *w, int revents) |
43 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
39 | { |
44 | { |
40 | K *obj = static_cast<K *>(w->data); |
45 | K *obj = static_cast<K *>(w->data); |
41 | (obj->*method) (*w, revents); |
46 | (obj->*method) (*static_cast<watcher *>(w), revents); |
42 | } |
47 | } |
43 | |
48 | |
|
|
49 | // const method callback |
44 | template<class K, void (K::*method)(watcher &w, int) const> |
50 | template<class K, void (K::*method)(watcher &w, int) const> |
45 | void set (const K *object) |
51 | void set (const K *object) |
46 | { |
52 | { |
47 | set_ (object, const_method_thunk<K, method>); |
53 | set_ (object, const_method_thunk<K, method>); |
48 | } |
54 | } |
49 | |
55 | |
50 | template<class K, void (K::*method)(watcher &w, int) const> |
56 | template<class K, void (K::*method)(watcher &w, int) const> |
51 | static void const_method_thunk (EV_P_ watcher *w, int revents) |
57 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
52 | { |
58 | { |
53 | K *obj = static_cast<K *>(w->data); |
59 | K *obj = static_cast<K *>(w->data); |
54 | (obj->*method) (*w, revents); |
60 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
55 | } |
61 | } |
56 | |
62 | |
|
|
63 | // function callback |
57 | template<void (*function)(watcher &w, int)> |
64 | template<void (*function)(watcher &w, int)> |
58 | void set (void *data = 0) |
65 | void set (void *data = 0) |
59 | { |
66 | { |
60 | set_ (data, function_thunk<function>); |
67 | set_ (data, function_thunk<function>); |
61 | } |
68 | } |
62 | |
69 | |
63 | template<void (*function)(watcher &w, int)> |
70 | template<void (*function)(watcher &w, int)> |
64 | static void function_thunk (EV_P_ watcher *w, int revents) |
71 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
72 | { |
|
|
73 | function (*static_cast<watcher *>(w), revents); |
65 | { |
74 | } |
66 | function (*w, revents); |
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) (); |
67 | } |
88 | } |
68 | |
89 | |
69 | void operator ()(int events = EV_UNDEF) |
90 | void operator ()(int events = EV_UNDEF) |
70 | { |
91 | { |
71 | return ev_cb (static_cast<ev_watcher *>(this)) |
92 | return ev_cb (static_cast<ev_watcher *>(this)) |