… | |
… | |
3 | |
3 | |
4 | #include "ev.h" |
4 | #include "ev.h" |
5 | |
5 | |
6 | namespace ev { |
6 | namespace ev { |
7 | |
7 | |
8 | template<class watcher> |
8 | template<class ev_watcher, class watcher> |
9 | class callback |
9 | struct base : ev_watcher |
10 | { |
10 | { |
11 | struct object { }; |
11 | #if EV_MULTIPLICITY |
|
|
12 | EV_P; |
12 | |
13 | |
13 | void *obj; |
14 | void set (EV_P) |
14 | void (object::*meth)(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 | { |
15 | { |
24 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) |
16 | this->EV_A = EV_A; |
25 | (w, e); |
|
|
26 | } |
17 | } |
|
|
18 | #endif |
|
|
19 | |
|
|
20 | base () |
|
|
21 | { |
|
|
22 | ev_init (this, 0); |
27 | }; |
23 | } |
28 | |
24 | |
29 | proxy_base *prxy; |
25 | void set_ (void *object, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
30 | |
|
|
31 | public: |
|
|
32 | template<class O1, class O2> |
|
|
33 | explicit callback (O1 *object, void (O2::*method)(watcher &, int)) |
|
|
34 | { |
|
|
35 | static proxy<O1,O2> p; |
|
|
36 | obj = reinterpret_cast<void *>(object); |
|
|
37 | meth = reinterpret_cast<void (object::*)(watcher &, int)>(method); |
|
|
38 | prxy = &p; |
|
|
39 | } |
26 | { |
40 | |
27 | this->data = object; |
41 | void call (watcher *w, int e) const |
28 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
42 | { |
29 | } |
43 | return prxy->call (obj, meth, *w, e); |
30 | |
|
|
31 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
32 | void set (K *object) |
|
|
33 | { |
|
|
34 | set_ (object, method_thunk<K, method>); |
|
|
35 | } |
|
|
36 | |
|
|
37 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
38 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
39 | { |
|
|
40 | watcher *self = static_cast<watcher *>(w); |
|
|
41 | K *obj = static_cast<K *>(self->data); |
|
|
42 | (obj->*method) (*self, revents); |
|
|
43 | } |
|
|
44 | |
|
|
45 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
46 | void set (const K *object) |
|
|
47 | { |
|
|
48 | set_ (object, const_method_thunk<K, method>); |
|
|
49 | } |
|
|
50 | |
|
|
51 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
52 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
53 | { |
|
|
54 | watcher *self = static_cast<watcher *>(w); |
|
|
55 | K *obj = static_cast<K *>(self->data); |
|
|
56 | (obj->*method) (*self, revents); |
|
|
57 | } |
|
|
58 | |
|
|
59 | template<void (*function)(watcher &w, int), void *data = 0> |
|
|
60 | void set () |
|
|
61 | { |
|
|
62 | set_ (data, function_thunk<function>); |
|
|
63 | } |
|
|
64 | |
|
|
65 | template<void (*function)(watcher &w, int)> |
|
|
66 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
67 | { |
|
|
68 | watcher *self = static_cast<watcher *>(w); |
|
|
69 | function (*self, revents); |
|
|
70 | } |
|
|
71 | |
|
|
72 | void operator ()(int events = EV_UNDEF) |
|
|
73 | { |
|
|
74 | return ev_cb (static_cast<ev_watcher *>(this)) |
|
|
75 | (static_cast<ev_watcher *>(this), events); |
|
|
76 | } |
|
|
77 | |
|
|
78 | bool is_active () const |
|
|
79 | { |
|
|
80 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
|
|
81 | } |
|
|
82 | |
|
|
83 | bool is_pending () const |
|
|
84 | { |
|
|
85 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
44 | } |
86 | } |
45 | }; |
87 | }; |
46 | |
88 | |
47 | enum { |
89 | enum { |
48 | UNDEF = EV_UNDEF, |
90 | UNDEF = EV_UNDEF, |
… | |
… | |
68 | { |
110 | { |
69 | return ev_now (EV_A); |
111 | return ev_now (EV_A); |
70 | } |
112 | } |
71 | |
113 | |
72 | #if EV_MULTIPLICITY |
114 | #if EV_MULTIPLICITY |
73 | |
|
|
74 | #define EV_CONSTRUCT(cppstem) \ |
|
|
75 | EV_P; \ |
115 | #define EV_CONSTRUCT \ |
76 | \ |
|
|
77 | void set (EV_P) \ |
116 | (EV_P = EV_DEFAULT) \ |
78 | { \ |
117 | { \ |
79 | this->EV_A = EV_A; \ |
|
|
80 | } \ |
|
|
81 | \ |
118 | set (EV_A); \ |
82 | template<class O1, class O2> \ |
119 | } |
83 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \ |
|
|
84 | : callback<cppstem> (object, method), EV_A (EV_A) |
|
|
85 | |
|
|
86 | #else |
120 | #else |
87 | |
121 | #define EV_CONSTRUCT \ |
88 | #define EV_CONSTRUCT(cppstem) \ |
122 | () \ |
89 | template<class O1, class O2> \ |
123 | { \ |
90 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
124 | } |
91 | : callback<cppstem> (object, method) |
|
|
92 | |
|
|
93 | #endif |
125 | #endif |
94 | |
126 | |
95 | /* using a template here would require quite a bit more lines, |
127 | /* using a template here would require quite a bit more lines, |
96 | * so a macro solution was chosen */ |
128 | * so a macro solution was chosen */ |
97 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
129 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
98 | \ |
130 | \ |
99 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
131 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
100 | { \ |
132 | { \ |
101 | EV_CONSTRUCT (cppstem) \ |
|
|
102 | { \ |
|
|
103 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
|
|
104 | } \ |
|
|
105 | \ |
|
|
106 | bool is_active () const \ |
|
|
107 | { \ |
|
|
108 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
109 | } \ |
|
|
110 | \ |
|
|
111 | bool is_pending () const \ |
|
|
112 | { \ |
|
|
113 | return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
114 | } \ |
|
|
115 | \ |
|
|
116 | void start () \ |
133 | void start () \ |
117 | { \ |
134 | { \ |
118 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
135 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
119 | } \ |
136 | } \ |
120 | \ |
137 | \ |
121 | void stop () \ |
138 | void stop () \ |
122 | { \ |
139 | { \ |
123 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
140 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
124 | } \ |
141 | } \ |
125 | \ |
142 | \ |
126 | void operator ()(int events = EV_UNDEF) \ |
|
|
127 | { \ |
143 | cppstem EV_CONSTRUCT \ |
128 | return call (this, events); \ |
|
|
129 | } \ |
|
|
130 | \ |
144 | \ |
131 | ~cppstem () \ |
145 | ~cppstem () \ |
132 | { \ |
146 | { \ |
133 | stop (); \ |
147 | stop (); \ |
134 | } \ |
148 | } \ |
135 | \ |
149 | \ |
|
|
150 | using base<ev_ ## cstem, cppstem>::set; \ |
|
|
151 | \ |
136 | private: \ |
152 | private: \ |
137 | \ |
153 | \ |
138 | cppstem (const cppstem &o) \ |
154 | cppstem (const cppstem &o) \ |
139 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
|
|
140 | { /* disabled */ } \ |
155 | { /* disabled */ } \ |
141 | \ |
156 | \ |
142 | void operator =(const cppstem &o) { /* disabled */ } \ |
157 | void operator =(const cppstem &o) { /* disabled */ } \ |
143 | \ |
|
|
144 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
145 | { \ |
|
|
146 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
147 | } \ |
|
|
148 | \ |
158 | \ |
149 | public: |
159 | public: |
150 | |
160 | |
151 | #define EV_END_WATCHER(cppstem,cstem) \ |
161 | #define EV_END_WATCHER(cppstem,cstem) \ |
152 | }; |
162 | }; |
… | |
… | |
261 | if (active) start (); |
271 | if (active) start (); |
262 | } |
272 | } |
263 | |
273 | |
264 | void start (const char *path, ev_tstamp interval = 0.) |
274 | void start (const char *path, ev_tstamp interval = 0.) |
265 | { |
275 | { |
|
|
276 | stop (); |
266 | set (path, interval); |
277 | set (path, interval); |
267 | start (); |
278 | start (); |
268 | } |
279 | } |
269 | |
280 | |
270 | void update () |
281 | void update () |
… | |
… | |
286 | void set () { } |
297 | void set () { } |
287 | EV_END_WATCHER (check, check) |
298 | EV_END_WATCHER (check, check) |
288 | |
299 | |
289 | #if EV_EMBED_ENABLE |
300 | #if EV_EMBED_ENABLE |
290 | EV_BEGIN_WATCHER (embed, embed) |
301 | 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) |
302 | void start (struct ev_loop *embedded_loop) |
300 | { |
303 | { |
301 | set (embedded_loop); |
304 | stop (); |
|
|
305 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
302 | start (); |
306 | start (); |
303 | } |
307 | } |
304 | |
308 | |
305 | void sweep () |
309 | void sweep () |
306 | { |
310 | { |
307 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
311 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
308 | } |
312 | } |
309 | EV_END_WATCHER (embed, embed) |
313 | EV_END_WATCHER (embed, embed) |
|
|
314 | #endif |
|
|
315 | |
|
|
316 | #if EV_FORK_ENABLE |
|
|
317 | EV_BEGIN_WATCHER (fork, fork) |
|
|
318 | void set () { } |
|
|
319 | EV_END_WATCHER (fork, fork) |
310 | #endif |
320 | #endif |
311 | |
321 | |
312 | #undef EV_CONSTRUCT |
322 | #undef EV_CONSTRUCT |
313 | #undef EV_BEGIN_WATCHER |
323 | #undef EV_BEGIN_WATCHER |
314 | #undef EV_END_WATCHER |
324 | #undef EV_END_WATCHER |