… | |
… | |
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 | |
… | |
… | |
191 | { |
181 | { |
192 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
182 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
193 | } |
183 | } |
194 | EV_END_WATCHER (timer, timer) |
184 | EV_END_WATCHER (timer, timer) |
195 | |
185 | |
196 | #if EV_PERIODICS |
186 | #if EV_PERIODIC_ENABLE |
197 | EV_BEGIN_WATCHER (periodic, periodic) |
187 | EV_BEGIN_WATCHER (periodic, periodic) |
198 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
188 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
199 | { |
189 | { |
200 | int active = is_active (); |
190 | int active = is_active (); |
201 | if (active) stop (); |
191 | if (active) stop (); |
… | |
… | |
212 | void again () |
202 | void again () |
213 | { |
203 | { |
214 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
204 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
215 | } |
205 | } |
216 | EV_END_WATCHER (periodic, periodic) |
206 | EV_END_WATCHER (periodic, periodic) |
|
|
207 | #endif |
|
|
208 | |
|
|
209 | EV_BEGIN_WATCHER (sig, signal) |
|
|
210 | void set (int signum) |
|
|
211 | { |
|
|
212 | int active = is_active (); |
|
|
213 | if (active) stop (); |
|
|
214 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
|
|
215 | if (active) start (); |
|
|
216 | } |
|
|
217 | |
|
|
218 | void start (int signum) |
|
|
219 | { |
|
|
220 | set (signum); |
|
|
221 | start (); |
|
|
222 | } |
|
|
223 | EV_END_WATCHER (sig, signal) |
|
|
224 | |
|
|
225 | EV_BEGIN_WATCHER (child, child) |
|
|
226 | void set (int pid) |
|
|
227 | { |
|
|
228 | int active = is_active (); |
|
|
229 | if (active) stop (); |
|
|
230 | ev_child_set (static_cast<ev_child *>(this), pid); |
|
|
231 | if (active) start (); |
|
|
232 | } |
|
|
233 | |
|
|
234 | void start (int pid) |
|
|
235 | { |
|
|
236 | set (pid); |
|
|
237 | start (); |
|
|
238 | } |
|
|
239 | EV_END_WATCHER (child, child) |
|
|
240 | |
|
|
241 | #if EV_STAT_ENABLE |
|
|
242 | EV_BEGIN_WATCHER (stat, stat) |
|
|
243 | void set (const char *path, ev_tstamp interval = 0.) |
|
|
244 | { |
|
|
245 | int active = is_active (); |
|
|
246 | if (active) stop (); |
|
|
247 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
|
|
248 | if (active) start (); |
|
|
249 | } |
|
|
250 | |
|
|
251 | void start (const char *path, ev_tstamp interval = 0.) |
|
|
252 | { |
|
|
253 | set (path, interval); |
|
|
254 | start (); |
|
|
255 | } |
|
|
256 | |
|
|
257 | void update () |
|
|
258 | { |
|
|
259 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
|
|
260 | } |
|
|
261 | EV_END_WATCHER (stat, stat) |
217 | #endif |
262 | #endif |
218 | |
263 | |
219 | EV_BEGIN_WATCHER (idle, idle) |
264 | EV_BEGIN_WATCHER (idle, idle) |
220 | void set () { } |
265 | void set () { } |
221 | EV_END_WATCHER (idle, idle) |
266 | EV_END_WATCHER (idle, idle) |
… | |
… | |
226 | |
271 | |
227 | EV_BEGIN_WATCHER (check, check) |
272 | EV_BEGIN_WATCHER (check, check) |
228 | void set () { } |
273 | void set () { } |
229 | EV_END_WATCHER (check, check) |
274 | EV_END_WATCHER (check, check) |
230 | |
275 | |
231 | EV_BEGIN_WATCHER (sig, signal) |
276 | #if EV_EMBED_ENABLE |
232 | void set (int signum) |
|
|
233 | { |
|
|
234 | int active = is_active (); |
|
|
235 | if (active) stop (); |
|
|
236 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
|
|
237 | if (active) start (); |
|
|
238 | } |
|
|
239 | |
|
|
240 | void start (int signum) |
|
|
241 | { |
|
|
242 | set (signum); |
|
|
243 | start (); |
|
|
244 | } |
|
|
245 | EV_END_WATCHER (sig, signal) |
|
|
246 | |
|
|
247 | EV_BEGIN_WATCHER (child, child) |
|
|
248 | void set (int pid) |
|
|
249 | { |
|
|
250 | int active = is_active (); |
|
|
251 | if (active) stop (); |
|
|
252 | ev_child_set (static_cast<ev_child *>(this), pid); |
|
|
253 | if (active) start (); |
|
|
254 | } |
|
|
255 | |
|
|
256 | void start (int pid) |
|
|
257 | { |
|
|
258 | set (pid); |
|
|
259 | start (); |
|
|
260 | } |
|
|
261 | EV_END_WATCHER (child, child) |
|
|
262 | |
|
|
263 | #if EV_MULTIPLICITY |
|
|
264 | |
|
|
265 | EV_BEGIN_WATCHER (embed, embed) |
277 | EV_BEGIN_WATCHER (embed, embed) |
266 | void set (struct ev_loop *loop) |
278 | void set (struct ev_loop *loop) |
267 | { |
279 | { |
268 | int active = is_active (); |
280 | int active = is_active (); |
269 | if (active) stop (); |
281 | if (active) stop (); |
… | |
… | |
280 | void sweep () |
292 | void sweep () |
281 | { |
293 | { |
282 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
294 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
283 | } |
295 | } |
284 | EV_END_WATCHER (embed, embed) |
296 | EV_END_WATCHER (embed, embed) |
|
|
297 | #endif |
285 | |
298 | |
|
|
299 | #if EV_FORK_ENABLE |
|
|
300 | EV_BEGIN_WATCHER (fork, fork) |
|
|
301 | void set () { } |
|
|
302 | EV_END_WATCHER (fork, fork) |
286 | #endif |
303 | #endif |
287 | |
304 | |
288 | #undef EV_CONSTRUCT |
305 | #undef EV_CONSTRUCT |
289 | #undef EV_BEGIN_WATCHER |
306 | #undef EV_BEGIN_WATCHER |
290 | #undef EV_END_WATCHER |
307 | #undef EV_END_WATCHER |