ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev++.h
(Generate patch)

Comparing libev/ev++.h (file contents):
Revision 1.38 by root, Mon Jan 28 11:43:37 2008 UTC vs.
Revision 1.47 by root, Sat Dec 26 09:21:54 2009 UTC

1/* 1/*
2 * libev simple C++ wrapper classes 2 * libev simple C++ wrapper classes
3 * 3 *
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007,2008 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
57namespace ev { 57namespace 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,
70 STAT = EV_STAT, 71 STAT = EV_STAT,
71 IDLE = EV_IDLE, 72 IDLE = EV_IDLE,
72 CHECK = EV_CHECK, 73 CHECK = EV_CHECK,
73 PREPARE = EV_PREPARE, 74 PREPARE = EV_PREPARE,
74 FORK = EV_FORK, 75 FORK = EV_FORK,
76 ASYNC = EV_ASYNC,
75 EMBED = EV_EMBED, 77 EMBED = EV_EMBED,
78# undef ERROR // some systems stupidly #define ERROR
76 ERROR = EV_ERROR, 79 ERROR = EV_ERROR,
77 }; 80 };
78 81
79 enum 82 enum
80 { 83 {
81 AUTO = EVFLAG_AUTO, 84 AUTO = EVFLAG_AUTO,
82 NOENV = EVFLAG_NOENV, 85 NOENV = EVFLAG_NOENV,
83 FORKCHECK = EVFLAG_FORKCHECK, 86 FORKCHECK = EVFLAG_FORKCHECK,
87
84 SELECT = EVBACKEND_SELECT, 88 SELECT = EVBACKEND_SELECT,
85 POLL = EVBACKEND_POLL, 89 POLL = EVBACKEND_POLL,
86 EPOLL = EVBACKEND_EPOLL, 90 EPOLL = EVBACKEND_EPOLL,
87 KQUEUE = EVBACKEND_KQUEUE, 91 KQUEUE = EVBACKEND_KQUEUE,
88 DEVPOLL = EVBACKEND_DEVPOLL, 92 DEVPOLL = EVBACKEND_DEVPOLL,
89 PORT = EVBACKEND_PORT 93 PORT = EVBACKEND_PORT
90 }; 94 };
91 95
92 enum 96 enum
93 { 97 {
94 NONBLOCK = EVLOOP_NONBLOCK, 98 NONBLOCK = EVLOOP_NONBLOCK,
95 ONESHOT = EVLOOP_ONESHOT 99 ONESHOT = EVLOOP_ONESHOT
96 }; 100 };
97 101
98 enum how_t 102 enum how_t
99 { 103 {
100 ONE = EVUNLOOP_ONE, 104 ONE = EVUNLOOP_ONE,
156 return false; 160 return false;
157#endif 161#endif
158 } 162 }
159 163
160#if EV_MULTIPLICITY 164#if EV_MULTIPLICITY
161 bool operator == (struct ev_loop *other) const throw () 165 bool operator == (const EV_P) const throw ()
162 { 166 {
163 return this->EV_AX == other; 167 return this->EV_AX == EV_A;
164 }
165
166 bool operator != (struct ev_loop *other) const throw ()
167 { 168 }
168 return ! (*this == other); 169
170 bool operator != (const EV_P) const throw ()
169 } 171 {
170
171 bool operator == (const struct ev_loop *other) const throw ()
172 {
173 return this->EV_AX == other;
174 }
175
176 bool operator != (const struct ev_loop *other) const throw ()
177 {
178 return (*this == other); 172 return (*this == EV_A);
179 } 173 }
180 174
181 operator struct ev_loop * () const throw () 175 operator struct ev_loop * () const throw ()
182 { 176 {
183 return EV_AX; 177 return EV_AX;
211#else 205#else
212 ev_default_fork (); 206 ev_default_fork ();
213#endif 207#endif
214 } 208 }
215 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
216 unsigned int count () const throw () 231 unsigned int count () const throw ()
217 { 232 {
218 return ev_loop_count (EV_AX); 233 return ev_loop_count (EV_AX);
219 } 234 }
220 235
221 unsigned int backend () const throw () 236 unsigned int depth () const throw ()
222 {
223 return ev_backend (EV_AX);
224 } 237 {
225
226 tstamp now () const throw ()
227 {
228 return ev_now (EV_AX); 238 return ev_loop_depth (EV_AX);
229 }
230
231 void ref () throw ()
232 {
233 ev_ref (EV_AX);
234 }
235
236 void unref () throw ()
237 {
238 ev_unref (EV_AX);
239 } 239 }
240 240
241 void set_io_collect_interval (tstamp interval) throw () 241 void set_io_collect_interval (tstamp interval) throw ()
242 { 242 {
243 ev_set_io_collect_interval (EV_AX_ interval); 243 ev_set_io_collect_interval (EV_AX_ interval);
245 245
246 void set_timeout_collect_interval (tstamp interval) throw () 246 void set_timeout_collect_interval (tstamp interval) throw ()
247 { 247 {
248 ev_set_timeout_collect_interval (EV_AX_ interval); 248 ev_set_timeout_collect_interval (EV_AX_ interval);
249 } 249 }
250#endif
250 251
251 // function callback 252 // function callback
252 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 ()
253 { 254 {
254 ev_once (EV_AX_ fd, events, timeout, cb, arg); 255 ev_once (EV_AX_ fd, events, timeout, cb, arg);
255 } 256 }
256 257
257 // method callback 258 // method callback
259 void once (int fd, int events, tstamp timeout, K *object) throw () 260 void once (int fd, int events, tstamp timeout, K *object) throw ()
260 { 261 {
261 once (fd, events, timeout, method_thunk<K, method>, object); 262 once (fd, events, timeout, method_thunk<K, method>, object);
262 } 263 }
263 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
264 template<class K, void (K::*method)(int)> 272 template<class K, void (K::*method)(int)>
265 static void method_thunk (int revents, void* arg) 273 static void method_thunk (int revents, void *arg)
266 {
267 K *obj = static_cast<K *>(arg);
268 (obj->*method) (revents);
269 } 274 {
275 static_cast<K *>(arg)->*method
276 (revents);
277 }
270 278
271 // const method callback 279 // no-argument method callback
272 template<class K, void (K::*method)(int) const>
273 void once (int fd, int events, tstamp timeout, const K *object) throw ()
274 {
275 once (fd, events, timeout, const_method_thunk<K, method>, object);
276 }
277
278 template<class K, void (K::*method)(int) const>
279 static void const_method_thunk (int revents, void* arg)
280 {
281 K *obj = static_cast<K *>(arg);
282 (obj->*method) (revents);
283 }
284
285 // simple method callback
286 template<class K, void (K::*method)()> 280 template<class K, void (K::*method)()>
287 void once (int fd, int events, tstamp timeout, K *object) throw () 281 void once (int fd, int events, tstamp timeout, K *object) throw ()
288 { 282 {
289 once (fd, events, timeout, method_noargs_thunk<K, method>, object); 283 once (fd, events, timeout, method_noargs_thunk<K, method>, object);
290 } 284 }
291 285
292 template<class K, void (K::*method)()> 286 template<class K, void (K::*method)()>
293 static void method_noargs_thunk (int revents, void* arg) 287 static void method_noargs_thunk (int revents, void *arg)
294 { 288 {
295 K *obj = static_cast<K *>(arg); 289 static_cast<K *>(arg)->*method
296 (obj->*method) (); 290 ();
297 } 291 }
298 292
299 // simpler function callback 293 // simpler function callback
300 template<void (*cb)(int)> 294 template<void (*cb)(int)>
301 void once (int fd, int events, tstamp timeout) throw () 295 void once (int fd, int events, tstamp timeout) throw ()
302 { 296 {
303 once (fd, events, timeout, simpler_func_thunk<cb>); 297 once (fd, events, timeout, simpler_func_thunk<cb>);
304 } 298 }
305 299
306 template<void (*cb)(int)> 300 template<void (*cb)(int)>
307 static void simpler_func_thunk (int revents, void* arg) 301 static void simpler_func_thunk (int revents, void *arg)
308 { 302 {
303 (*cb)
309 (*cb) (revents); 304 (revents);
310 } 305 }
311 306
312 // simplest function callback 307 // simplest function callback
313 template<void (*cb)()> 308 template<void (*cb)()>
314 void once (int fd, int events, tstamp timeout) throw () 309 void once (int fd, int events, tstamp timeout) throw ()
315 { 310 {
316 once (fd, events, timeout, simplest_func_thunk<cb>); 311 once (fd, events, timeout, simplest_func_thunk<cb>);
317 } 312 }
318 313
319 template<void (*cb)()> 314 template<void (*cb)()>
320 static void simplest_func_thunk (int revents, void* arg) 315 static void simplest_func_thunk (int revents, void *arg)
321 { 316 {
322 (*cb) (); 317 (*cb)
318 ();
323 } 319 }
324 320
325 void feed_fd_event (int fd, int revents) throw () 321 void feed_fd_event (int fd, int revents) throw ()
326 { 322 {
327 ev_feed_fd_event (EV_AX_ fd, revents); 323 ev_feed_fd_event (EV_AX_ fd, revents);
417 struct base : ev_watcher 413 struct base : ev_watcher
418 { 414 {
419 #if EV_MULTIPLICITY 415 #if EV_MULTIPLICITY
420 EV_PX; 416 EV_PX;
421 417
422 void set (EV_PX) throw () 418 void set (EV_P) throw ()
423 { 419 {
424 this->EV_A = EV_A; 420 this->EV_A = EV_A;
425 } 421 }
426 #endif 422 #endif
427 423
431 #endif 427 #endif
432 { 428 {
433 ev_init (this, 0); 429 ev_init (this, 0);
434 } 430 }
435 431
436 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 ()
437 { 433 {
438 this->data = data; 434 this->data = (void *)data;
439 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);
440 } 450 }
441 451
442 // method callback 452 // method callback
443 template<class K, void (K::*method)(watcher &w, int)> 453 template<class K, void (K::*method)(watcher &w, int)>
444 void set (K *object) throw () 454 void set (K *object) throw ()
445 { 455 {
446 set_ (object, method_thunk<K, method>); 456 set_ (object, method_thunk<K, method>);
447 } 457 }
448 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
449 template<class K, void (K::*method)(watcher &w, int)> 466 template<class K, void (K::*method)(watcher &w, int)>
450 static void method_thunk (EV_P_ ev_watcher *w, int revents) 467 static void method_thunk (EV_P_ ev_watcher *w, int revents)
451 { 468 {
452 K *obj = static_cast<K *>(w->data); 469 (static_cast<K *>(w->data)->*method)
453 (obj->*method) (*static_cast<watcher *>(w), revents);
454 }
455
456 // const method callback
457 template<class K, void (K::*method)(watcher &w, int) const>
458 void set (const K *object) throw ()
459 {
460 set_ (object, const_method_thunk<K, method>);
461 }
462
463 template<class K, void (K::*method)(watcher &w, int) const>
464 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
465 {
466 K *obj = static_cast<K *>(w->data);
467 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
468 }
469
470 // function callback
471 template<void (*function)(watcher &w, int)>
472 void set (void *data = 0) throw ()
473 {
474 set_ (data, function_thunk<function>);
475 }
476
477 template<void (*function)(watcher &w, int)>
478 static void function_thunk (EV_P_ ev_watcher *w, int revents)
479 {
480 function (*static_cast<watcher *>(w), revents); 470 (*static_cast<watcher *>(w), revents);
481 } 471 }
482 472
483 // simple callback 473 // no-argument callback
484 template<class K, void (K::*method)()> 474 template<class K, void (K::*method)()>
485 void set (K *object) throw () 475 void set (K *object) throw ()
486 { 476 {
487 set_ (object, method_noargs_thunk<K, method>); 477 set_ (object, method_noargs_thunk<K, method>);
488 } 478 }
489 479
490 template<class K, void (K::*method)()> 480 template<class K, void (K::*method)()>
491 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)
492 { 482 {
493 K *obj = static_cast<K *>(w->data); 483 static_cast<K *>(w->data)->*method
494 (obj->*method) (); 484 ();
495 } 485 }
496 486
497 void operator ()(int events = EV_UNDEF) 487 void operator ()(int events = EV_UNDEF)
498 { 488 {
489 return
499 return ev_cb (static_cast<ev_watcher *>(this)) 490 ev_cb (static_cast<ev_watcher *>(this))
500 (static_cast<ev_watcher *>(this), events); 491 (static_cast<ev_watcher *>(this), events);
501 } 492 }
502 493
503 bool is_active () const throw () 494 bool is_active () const throw ()
504 { 495 {
505 return ev_is_active (static_cast<const ev_watcher *>(this)); 496 return ev_is_active (static_cast<const ev_watcher *>(this));
561 ev_set_syserr_cb (cb); 552 ev_set_syserr_cb (cb);
562 } 553 }
563 554
564 #if EV_MULTIPLICITY 555 #if EV_MULTIPLICITY
565 #define EV_CONSTRUCT(cppstem,cstem) \ 556 #define EV_CONSTRUCT(cppstem,cstem) \
566 (EV_PX = get_default_loop ()) throw () \ 557 (EV_PX = get_default_loop ()) throw () \
567 : base<ev_ ## cstem, cppstem> (EV_A) \ 558 : base<ev_ ## cstem, cppstem> (EV_A) \
568 { \ 559 { \
569 } 560 }
570 #else 561 #else
571 #define EV_CONSTRUCT(cppstem,cstem) \ 562 #define EV_CONSTRUCT(cppstem,cstem) \
572 () throw () \ 563 () throw () \
573 { \ 564 { \
574 } 565 }
575 #endif 566 #endif
576 567
577 /* using a template here would require quite a bit more lines, 568 /* using a template here would require quite a bit more lines,
578 * so a macro solution was chosen */ 569 * so a macro solution was chosen */
579 #define EV_BEGIN_WATCHER(cppstem,cstem) \ 570 #define EV_BEGIN_WATCHER(cppstem,cstem) \
580 \ 571 \
581 struct cppstem : base<ev_ ## cstem, cppstem> \ 572 struct cppstem : base<ev_ ## cstem, cppstem> \
582 { \ 573 { \
583 void start () throw () \ 574 void start () throw () \
584 { \ 575 { \
585 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 576 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
586 } \ 577 } \
587 \ 578 \
588 void stop () throw () \ 579 void stop () throw () \
589 { \ 580 { \
590 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 581 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
591 } \ 582 } \
592 \ 583 \
593 cppstem EV_CONSTRUCT(cppstem,cstem) \ 584 cppstem EV_CONSTRUCT(cppstem,cstem) \
594 \ 585 \
595 ~cppstem () throw () \ 586 ~cppstem () throw () \
596 { \ 587 { \
597 stop (); \ 588 stop (); \
598 } \ 589 } \
599 \ 590 \
600 using base<ev_ ## cstem, cppstem>::set; \ 591 using base<ev_ ## cstem, cppstem>::set; \
601 \ 592 \
602 private: \ 593 private: \
603 \ 594 \
604 cppstem (const cppstem &o); \ 595 cppstem (const cppstem &o); \
605 \ 596 \
606 cppstem & operator =(const cppstem &o); \ 597 cppstem &operator =(const cppstem &o); \
607 \ 598 \
608 public: 599 public:
609 600
610 #define EV_END_WATCHER(cppstem,cstem) \ 601 #define EV_END_WATCHER(cppstem,cstem) \
611 }; 602 };
651 642
652 void again () throw () 643 void again () throw ()
653 { 644 {
654 ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 645 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
655 } 646 }
647
648 ev_tstamp remaining ()
649 {
650 return ev_timer_remaining (EV_A_ static_cast<ev_timer *>(this));
651 }
656 EV_END_WATCHER (timer, timer) 652 EV_END_WATCHER (timer, timer)
657 653
658 #if EV_PERIODIC_ENABLE 654 #if EV_PERIODIC_ENABLE
659 EV_BEGIN_WATCHER (periodic, periodic) 655 EV_BEGIN_WATCHER (periodic, periodic)
660 void set (ev_tstamp at, ev_tstamp interval = 0.) throw () 656 void set (ev_tstamp at, ev_tstamp interval = 0.) throw ()
732 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); 728 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
733 } 729 }
734 EV_END_WATCHER (stat, stat) 730 EV_END_WATCHER (stat, stat)
735 #endif 731 #endif
736 732
733#if EV_IDLE_ENABLE
737 EV_BEGIN_WATCHER (idle, idle) 734 EV_BEGIN_WATCHER (idle, idle)
738 void set () throw () { } 735 void set () throw () { }
739 EV_END_WATCHER (idle, idle) 736 EV_END_WATCHER (idle, idle)
737#endif
740 738
741 EV_BEGIN_WATCHER (prepare, prepare) 739 EV_BEGIN_WATCHER (prepare, prepare)
742 void set () throw () { } 740 void set () throw () { }
743 EV_END_WATCHER (prepare, prepare) 741 EV_END_WATCHER (prepare, prepare)
744 742
746 void set () throw () { } 744 void set () throw () { }
747 EV_END_WATCHER (check, check) 745 EV_END_WATCHER (check, check)
748 746
749 #if EV_EMBED_ENABLE 747 #if EV_EMBED_ENABLE
750 EV_BEGIN_WATCHER (embed, embed) 748 EV_BEGIN_WATCHER (embed, embed)
749 void set (struct ev_loop *embedded_loop) throw ()
750 {
751 int active = is_active ();
752 if (active) stop ();
753 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
754 if (active) start ();
755 }
756
751 void start (struct ev_loop *embedded_loop) throw () 757 void start (struct ev_loop *embedded_loop) throw ()
752 { 758 {
753 stop (); 759 set (embedded_loop);
754 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
755 start (); 760 start ();
756 } 761 }
757 762
758 void sweep () 763 void sweep ()
759 { 764 {
764 769
765 #if EV_FORK_ENABLE 770 #if EV_FORK_ENABLE
766 EV_BEGIN_WATCHER (fork, fork) 771 EV_BEGIN_WATCHER (fork, fork)
767 void set () throw () { } 772 void set () throw () { }
768 EV_END_WATCHER (fork, fork) 773 EV_END_WATCHER (fork, fork)
774 #endif
775
776 #if EV_ASYNC_ENABLE
777 EV_BEGIN_WATCHER (async, async)
778 void set () throw () { }
779
780 void send () throw ()
781 {
782 ev_async_send (EV_A_ static_cast<ev_async *>(this));
783 }
784
785 bool async_pending () throw ()
786 {
787 return ev_async_pending (static_cast<ev_async *>(this));
788 }
789 EV_END_WATCHER (async, async)
769 #endif 790 #endif
770 791
771 #undef EV_PX 792 #undef EV_PX
772 #undef EV_PX_ 793 #undef EV_PX_
773 #undef EV_CONSTRUCT 794 #undef EV_CONSTRUCT

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines