1 | /* |
1 | /* |
2 | * libev simple C++ wrapper classes |
2 | * libev simple C++ wrapper classes |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2010 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
56 | |
56 | |
57 | namespace ev { |
57 | namespace ev { |
58 | |
58 | |
59 | typedef ev_tstamp tstamp; |
59 | typedef ev_tstamp tstamp; |
60 | |
60 | |
61 | enum { |
61 | enum |
|
|
62 | { |
62 | UNDEF = EV_UNDEF, |
63 | UNDEF = EV_UNDEF, |
63 | NONE = EV_NONE, |
64 | NONE = EV_NONE, |
64 | READ = EV_READ, |
65 | READ = EV_READ, |
65 | WRITE = EV_WRITE, |
66 | WRITE = EV_WRITE, |
66 | TIMEOUT = EV_TIMEOUT, |
67 | TIMEOUT = EV_TIMEOUT, |
… | |
… | |
159 | return false; |
160 | return false; |
160 | #endif |
161 | #endif |
161 | } |
162 | } |
162 | |
163 | |
163 | #if EV_MULTIPLICITY |
164 | #if EV_MULTIPLICITY |
164 | bool operator == (struct ev_loop *other) const throw () |
165 | bool operator == (const EV_P) const throw () |
165 | { |
166 | { |
166 | return this->EV_AX == other; |
167 | return this->EV_AX == EV_A; |
167 | } |
|
|
168 | |
|
|
169 | bool operator != (struct ev_loop *other) const throw () |
|
|
170 | { |
168 | } |
171 | return ! (*this == other); |
169 | |
|
|
170 | bool operator != (const EV_P) const throw () |
172 | } |
171 | { |
173 | |
|
|
174 | bool operator == (const struct ev_loop *other) const throw () |
|
|
175 | { |
|
|
176 | return this->EV_AX == other; |
|
|
177 | } |
|
|
178 | |
|
|
179 | bool operator != (const struct ev_loop *other) const throw () |
|
|
180 | { |
|
|
181 | return (*this == other); |
172 | return (*this == EV_A); |
182 | } |
173 | } |
183 | |
174 | |
184 | operator struct ev_loop * () const throw () |
175 | operator struct ev_loop * () const throw () |
185 | { |
176 | { |
186 | return EV_AX; |
177 | return EV_AX; |
… | |
… | |
214 | #else |
205 | #else |
215 | ev_default_fork (); |
206 | ev_default_fork (); |
216 | #endif |
207 | #endif |
217 | } |
208 | } |
218 | |
209 | |
|
|
210 | unsigned int backend () const throw () |
|
|
211 | { |
|
|
212 | return ev_backend (EV_AX); |
|
|
213 | } |
|
|
214 | |
|
|
215 | tstamp now () const throw () |
|
|
216 | { |
|
|
217 | return ev_now (EV_AX); |
|
|
218 | } |
|
|
219 | |
|
|
220 | void ref () throw () |
|
|
221 | { |
|
|
222 | ev_ref (EV_AX); |
|
|
223 | } |
|
|
224 | |
|
|
225 | void unref () throw () |
|
|
226 | { |
|
|
227 | ev_unref (EV_AX); |
|
|
228 | } |
|
|
229 | |
|
|
230 | #if EV_MINIMAL < 2 |
219 | unsigned int count () const throw () |
231 | unsigned int count () const throw () |
220 | { |
232 | { |
221 | return ev_loop_count (EV_AX); |
233 | return ev_loop_count (EV_AX); |
222 | } |
234 | } |
223 | |
235 | |
224 | unsigned int backend () const throw () |
236 | unsigned int depth () const throw () |
225 | { |
|
|
226 | return ev_backend (EV_AX); |
|
|
227 | } |
237 | { |
228 | |
|
|
229 | tstamp now () const throw () |
|
|
230 | { |
|
|
231 | return ev_now (EV_AX); |
238 | return ev_loop_depth (EV_AX); |
232 | } |
|
|
233 | |
|
|
234 | void ref () throw () |
|
|
235 | { |
|
|
236 | ev_ref (EV_AX); |
|
|
237 | } |
|
|
238 | |
|
|
239 | void unref () throw () |
|
|
240 | { |
|
|
241 | ev_unref (EV_AX); |
|
|
242 | } |
239 | } |
243 | |
240 | |
244 | void set_io_collect_interval (tstamp interval) throw () |
241 | void set_io_collect_interval (tstamp interval) throw () |
245 | { |
242 | { |
246 | ev_set_io_collect_interval (EV_AX_ interval); |
243 | ev_set_io_collect_interval (EV_AX_ interval); |
… | |
… | |
248 | |
245 | |
249 | void set_timeout_collect_interval (tstamp interval) throw () |
246 | void set_timeout_collect_interval (tstamp interval) throw () |
250 | { |
247 | { |
251 | ev_set_timeout_collect_interval (EV_AX_ interval); |
248 | ev_set_timeout_collect_interval (EV_AX_ interval); |
252 | } |
249 | } |
|
|
250 | #endif |
253 | |
251 | |
254 | // function callback |
252 | // function callback |
255 | void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void* arg = 0) throw () |
253 | void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void *arg = 0) throw () |
256 | { |
254 | { |
257 | ev_once (EV_AX_ fd, events, timeout, cb, arg); |
255 | ev_once (EV_AX_ fd, events, timeout, cb, arg); |
258 | } |
256 | } |
259 | |
257 | |
260 | // method callback |
258 | // method callback |
… | |
… | |
262 | void once (int fd, int events, tstamp timeout, K *object) throw () |
260 | void once (int fd, int events, tstamp timeout, K *object) throw () |
263 | { |
261 | { |
264 | once (fd, events, timeout, method_thunk<K, method>, object); |
262 | once (fd, events, timeout, method_thunk<K, method>, object); |
265 | } |
263 | } |
266 | |
264 | |
|
|
265 | // default method == operator () |
|
|
266 | template<class K> |
|
|
267 | void once (int fd, int events, tstamp timeout, K *object) throw () |
|
|
268 | { |
|
|
269 | once (fd, events, timeout, method_thunk<K, &K::operator ()>, object); |
|
|
270 | } |
|
|
271 | |
267 | template<class K, void (K::*method)(int)> |
272 | template<class K, void (K::*method)(int)> |
268 | static void method_thunk (int revents, void* arg) |
273 | static void method_thunk (int revents, void *arg) |
269 | { |
|
|
270 | K *obj = static_cast<K *>(arg); |
|
|
271 | (obj->*method) (revents); |
|
|
272 | } |
274 | { |
|
|
275 | static_cast<K *>(arg)->*method |
|
|
276 | (revents); |
|
|
277 | } |
273 | |
278 | |
274 | // const method callback |
279 | // no-argument method callback |
275 | template<class K, void (K::*method)(int) const> |
|
|
276 | void once (int fd, int events, tstamp timeout, const K *object) throw () |
|
|
277 | { |
|
|
278 | once (fd, events, timeout, const_method_thunk<K, method>, object); |
|
|
279 | } |
|
|
280 | |
|
|
281 | template<class K, void (K::*method)(int) const> |
|
|
282 | static void const_method_thunk (int revents, void* arg) |
|
|
283 | { |
|
|
284 | K *obj = static_cast<K *>(arg); |
|
|
285 | (obj->*method) (revents); |
|
|
286 | } |
|
|
287 | |
|
|
288 | // simple method callback |
|
|
289 | template<class K, void (K::*method)()> |
280 | template<class K, void (K::*method)()> |
290 | void once (int fd, int events, tstamp timeout, K *object) throw () |
281 | void once (int fd, int events, tstamp timeout, K *object) throw () |
291 | { |
282 | { |
292 | once (fd, events, timeout, method_noargs_thunk<K, method>, object); |
283 | once (fd, events, timeout, method_noargs_thunk<K, method>, object); |
293 | } |
284 | } |
294 | |
285 | |
295 | template<class K, void (K::*method)()> |
286 | template<class K, void (K::*method)()> |
296 | static void method_noargs_thunk (int revents, void* arg) |
287 | static void method_noargs_thunk (int revents, void *arg) |
297 | { |
288 | { |
298 | K *obj = static_cast<K *>(arg); |
289 | static_cast<K *>(arg)->*method |
299 | (obj->*method) (); |
290 | (); |
300 | } |
291 | } |
301 | |
292 | |
302 | // simpler function callback |
293 | // simpler function callback |
303 | template<void (*cb)(int)> |
294 | template<void (*cb)(int)> |
304 | void once (int fd, int events, tstamp timeout) throw () |
295 | void once (int fd, int events, tstamp timeout) throw () |
305 | { |
296 | { |
306 | once (fd, events, timeout, simpler_func_thunk<cb>); |
297 | once (fd, events, timeout, simpler_func_thunk<cb>); |
307 | } |
298 | } |
308 | |
299 | |
309 | template<void (*cb)(int)> |
300 | template<void (*cb)(int)> |
310 | static void simpler_func_thunk (int revents, void* arg) |
301 | static void simpler_func_thunk (int revents, void *arg) |
311 | { |
302 | { |
|
|
303 | (*cb) |
312 | (*cb) (revents); |
304 | (revents); |
313 | } |
305 | } |
314 | |
306 | |
315 | // simplest function callback |
307 | // simplest function callback |
316 | template<void (*cb)()> |
308 | template<void (*cb)()> |
317 | void once (int fd, int events, tstamp timeout) throw () |
309 | void once (int fd, int events, tstamp timeout) throw () |
318 | { |
310 | { |
319 | once (fd, events, timeout, simplest_func_thunk<cb>); |
311 | once (fd, events, timeout, simplest_func_thunk<cb>); |
320 | } |
312 | } |
321 | |
313 | |
322 | template<void (*cb)()> |
314 | template<void (*cb)()> |
323 | static void simplest_func_thunk (int revents, void* arg) |
315 | static void simplest_func_thunk (int revents, void *arg) |
324 | { |
316 | { |
325 | (*cb) (); |
317 | (*cb) |
|
|
318 | (); |
326 | } |
319 | } |
327 | |
320 | |
328 | void feed_fd_event (int fd, int revents) throw () |
321 | void feed_fd_event (int fd, int revents) throw () |
329 | { |
322 | { |
330 | ev_feed_fd_event (EV_AX_ fd, revents); |
323 | ev_feed_fd_event (EV_AX_ fd, revents); |
… | |
… | |
420 | struct base : ev_watcher |
413 | struct base : ev_watcher |
421 | { |
414 | { |
422 | #if EV_MULTIPLICITY |
415 | #if EV_MULTIPLICITY |
423 | EV_PX; |
416 | EV_PX; |
424 | |
417 | |
425 | void set (EV_PX) throw () |
418 | void set (EV_P) throw () |
426 | { |
419 | { |
427 | this->EV_A = EV_A; |
420 | this->EV_A = EV_A; |
428 | } |
421 | } |
429 | #endif |
422 | #endif |
430 | |
423 | |
… | |
… | |
434 | #endif |
427 | #endif |
435 | { |
428 | { |
436 | ev_init (this, 0); |
429 | ev_init (this, 0); |
437 | } |
430 | } |
438 | |
431 | |
439 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () |
432 | void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () |
440 | { |
433 | { |
441 | this->data = data; |
434 | this->data = (void *)data; |
442 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
435 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
|
|
436 | } |
|
|
437 | |
|
|
438 | // function callback |
|
|
439 | template<void (*function)(watcher &w, int)> |
|
|
440 | void set (void *data = 0) throw () |
|
|
441 | { |
|
|
442 | set_ (data, function_thunk<function>); |
|
|
443 | } |
|
|
444 | |
|
|
445 | template<void (*function)(watcher &w, int)> |
|
|
446 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
447 | { |
|
|
448 | function |
|
|
449 | (*static_cast<watcher *>(w), revents); |
443 | } |
450 | } |
444 | |
451 | |
445 | // method callback |
452 | // method callback |
446 | template<class K, void (K::*method)(watcher &w, int)> |
453 | template<class K, void (K::*method)(watcher &w, int)> |
447 | void set (K *object) throw () |
454 | void set (K *object) throw () |
448 | { |
455 | { |
449 | set_ (object, method_thunk<K, method>); |
456 | set_ (object, method_thunk<K, method>); |
450 | } |
457 | } |
451 | |
458 | |
|
|
459 | // default method == operator () |
|
|
460 | template<class K> |
|
|
461 | void set (K *object) throw () |
|
|
462 | { |
|
|
463 | set_ (object, method_thunk<K, &K::operator ()>); |
|
|
464 | } |
|
|
465 | |
452 | template<class K, void (K::*method)(watcher &w, int)> |
466 | template<class K, void (K::*method)(watcher &w, int)> |
453 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
467 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
454 | { |
468 | { |
455 | K *obj = static_cast<K *>(w->data); |
469 | (static_cast<K *>(w->data)->*method) |
456 | (obj->*method) (*static_cast<watcher *>(w), revents); |
|
|
457 | } |
|
|
458 | |
|
|
459 | // const method callback |
|
|
460 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
461 | void set (const K *object) throw () |
|
|
462 | { |
|
|
463 | set_ (object, const_method_thunk<K, method>); |
|
|
464 | } |
|
|
465 | |
|
|
466 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
467 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
468 | { |
|
|
469 | K *obj = static_cast<K *>(w->data); |
|
|
470 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
|
|
471 | } |
|
|
472 | |
|
|
473 | // function callback |
|
|
474 | template<void (*function)(watcher &w, int)> |
|
|
475 | void set (void *data = 0) throw () |
|
|
476 | { |
|
|
477 | set_ (data, function_thunk<function>); |
|
|
478 | } |
|
|
479 | |
|
|
480 | template<void (*function)(watcher &w, int)> |
|
|
481 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
482 | { |
|
|
483 | function (*static_cast<watcher *>(w), revents); |
470 | (*static_cast<watcher *>(w), revents); |
484 | } |
471 | } |
485 | |
472 | |
486 | // simple callback |
473 | // no-argument callback |
487 | template<class K, void (K::*method)()> |
474 | template<class K, void (K::*method)()> |
488 | void set (K *object) throw () |
475 | void set (K *object) throw () |
489 | { |
476 | { |
490 | set_ (object, method_noargs_thunk<K, method>); |
477 | set_ (object, method_noargs_thunk<K, method>); |
491 | } |
478 | } |
492 | |
479 | |
493 | template<class K, void (K::*method)()> |
480 | template<class K, void (K::*method)()> |
494 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
481 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
495 | { |
482 | { |
496 | K *obj = static_cast<K *>(w->data); |
483 | static_cast<K *>(w->data)->*method |
497 | (obj->*method) (); |
484 | (); |
498 | } |
485 | } |
499 | |
486 | |
500 | void operator ()(int events = EV_UNDEF) |
487 | void operator ()(int events = EV_UNDEF) |
501 | { |
488 | { |
|
|
489 | return |
502 | return ev_cb (static_cast<ev_watcher *>(this)) |
490 | ev_cb (static_cast<ev_watcher *>(this)) |
503 | (static_cast<ev_watcher *>(this), events); |
491 | (static_cast<ev_watcher *>(this), events); |
504 | } |
492 | } |
505 | |
493 | |
506 | bool is_active () const throw () |
494 | bool is_active () const throw () |
507 | { |
495 | { |
508 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
496 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
… | |
… | |
654 | |
642 | |
655 | void again () throw () |
643 | void again () throw () |
656 | { |
644 | { |
657 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
645 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
658 | } |
646 | } |
|
|
647 | |
|
|
648 | ev_tstamp remaining () |
|
|
649 | { |
|
|
650 | return ev_timer_remaining (EV_A_ static_cast<ev_timer *>(this)); |
|
|
651 | } |
659 | EV_END_WATCHER (timer, timer) |
652 | EV_END_WATCHER (timer, timer) |
660 | |
653 | |
661 | #if EV_PERIODIC_ENABLE |
654 | #if EV_PERIODIC_ENABLE |
662 | EV_BEGIN_WATCHER (periodic, periodic) |
655 | EV_BEGIN_WATCHER (periodic, periodic) |
663 | void set (ev_tstamp at, ev_tstamp interval = 0.) throw () |
656 | void set (ev_tstamp at, ev_tstamp interval = 0.) throw () |
… | |
… | |
679 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
672 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
680 | } |
673 | } |
681 | EV_END_WATCHER (periodic, periodic) |
674 | EV_END_WATCHER (periodic, periodic) |
682 | #endif |
675 | #endif |
683 | |
676 | |
|
|
677 | #if EV_SIGNAL_ENABLE |
684 | EV_BEGIN_WATCHER (sig, signal) |
678 | EV_BEGIN_WATCHER (sig, signal) |
685 | void set (int signum) throw () |
679 | void set (int signum) throw () |
686 | { |
680 | { |
687 | int active = is_active (); |
681 | int active = is_active (); |
688 | if (active) stop (); |
682 | if (active) stop (); |
… | |
… | |
694 | { |
688 | { |
695 | set (signum); |
689 | set (signum); |
696 | start (); |
690 | start (); |
697 | } |
691 | } |
698 | EV_END_WATCHER (sig, signal) |
692 | EV_END_WATCHER (sig, signal) |
|
|
693 | #endif |
699 | |
694 | |
|
|
695 | #if EV_CHILD_ENABLE |
700 | EV_BEGIN_WATCHER (child, child) |
696 | EV_BEGIN_WATCHER (child, child) |
701 | void set (int pid, int trace = 0) throw () |
697 | void set (int pid, int trace = 0) throw () |
702 | { |
698 | { |
703 | int active = is_active (); |
699 | int active = is_active (); |
704 | if (active) stop (); |
700 | if (active) stop (); |
… | |
… | |
710 | { |
706 | { |
711 | set (pid, trace); |
707 | set (pid, trace); |
712 | start (); |
708 | start (); |
713 | } |
709 | } |
714 | EV_END_WATCHER (child, child) |
710 | EV_END_WATCHER (child, child) |
|
|
711 | #endif |
715 | |
712 | |
716 | #if EV_STAT_ENABLE |
713 | #if EV_STAT_ENABLE |
717 | EV_BEGIN_WATCHER (stat, stat) |
714 | EV_BEGIN_WATCHER (stat, stat) |
718 | void set (const char *path, ev_tstamp interval = 0.) throw () |
715 | void set (const char *path, ev_tstamp interval = 0.) throw () |
719 | { |
716 | { |
… | |
… | |
735 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
732 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
736 | } |
733 | } |
737 | EV_END_WATCHER (stat, stat) |
734 | EV_END_WATCHER (stat, stat) |
738 | #endif |
735 | #endif |
739 | |
736 | |
|
|
737 | #if EV_IDLE_ENABLE |
740 | EV_BEGIN_WATCHER (idle, idle) |
738 | EV_BEGIN_WATCHER (idle, idle) |
741 | void set () throw () { } |
739 | void set () throw () { } |
742 | EV_END_WATCHER (idle, idle) |
740 | EV_END_WATCHER (idle, idle) |
|
|
741 | #endif |
743 | |
742 | |
|
|
743 | #if EV_PREPARE_ENABLE |
744 | EV_BEGIN_WATCHER (prepare, prepare) |
744 | EV_BEGIN_WATCHER (prepare, prepare) |
745 | void set () throw () { } |
745 | void set () throw () { } |
746 | EV_END_WATCHER (prepare, prepare) |
746 | EV_END_WATCHER (prepare, prepare) |
|
|
747 | #endif |
747 | |
748 | |
|
|
749 | #if EV_CHECK_ENABLE |
748 | EV_BEGIN_WATCHER (check, check) |
750 | EV_BEGIN_WATCHER (check, check) |
749 | void set () throw () { } |
751 | void set () throw () { } |
750 | EV_END_WATCHER (check, check) |
752 | EV_END_WATCHER (check, check) |
|
|
753 | #endif |
751 | |
754 | |
752 | #if EV_EMBED_ENABLE |
755 | #if EV_EMBED_ENABLE |
753 | EV_BEGIN_WATCHER (embed, embed) |
756 | EV_BEGIN_WATCHER (embed, embed) |
754 | void set (struct ev_loop *embedded_loop) throw () |
757 | void set (struct ev_loop *embedded_loop) throw () |
755 | { |
758 | { |