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