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.44 by root, Wed Dec 3 15:23:44 2008 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;
247 { 241 {
248 ev_set_timeout_collect_interval (EV_AX_ interval); 242 ev_set_timeout_collect_interval (EV_AX_ interval);
249 } 243 }
250 244
251 // function callback 245 // function callback
252 void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void* arg = 0) throw () 246 void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void *arg = 0) throw ()
253 { 247 {
254 ev_once (EV_AX_ fd, events, timeout, cb, arg); 248 ev_once (EV_AX_ fd, events, timeout, cb, arg);
255 } 249 }
256 250
257 // method callback 251 // method callback
259 void once (int fd, int events, tstamp timeout, K *object) throw () 253 void once (int fd, int events, tstamp timeout, K *object) throw ()
260 { 254 {
261 once (fd, events, timeout, method_thunk<K, method>, object); 255 once (fd, events, timeout, method_thunk<K, method>, object);
262 } 256 }
263 257
258 // default method == operator ()
259 template<class K>
260 void once (int fd, int events, tstamp timeout, K *object) throw ()
261 {
262 once (fd, events, timeout, method_thunk<K, &K::operator ()>, object);
263 }
264
264 template<class K, void (K::*method)(int)> 265 template<class K, void (K::*method)(int)>
265 static void method_thunk (int revents, void* arg) 266 static void method_thunk (int revents, void *arg)
266 {
267 K *obj = static_cast<K *>(arg);
268 (obj->*method) (revents);
269 } 267 {
268 static_cast<K *>(arg)->*method
269 (revents);
270 }
270 271
271 // const method callback 272 // 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)()> 273 template<class K, void (K::*method)()>
287 void once (int fd, int events, tstamp timeout, K *object) throw () 274 void once (int fd, int events, tstamp timeout, K *object) throw ()
288 { 275 {
289 once (fd, events, timeout, method_noargs_thunk<K, method>, object); 276 once (fd, events, timeout, method_noargs_thunk<K, method>, object);
290 } 277 }
291 278
292 template<class K, void (K::*method)()> 279 template<class K, void (K::*method)()>
293 static void method_noargs_thunk (int revents, void* arg) 280 static void method_noargs_thunk (int revents, void *arg)
294 { 281 {
295 K *obj = static_cast<K *>(arg); 282 static_cast<K *>(arg)->*method
296 (obj->*method) (); 283 ();
297 } 284 }
298 285
299 // simpler function callback 286 // simpler function callback
300 template<void (*cb)(int)> 287 template<void (*cb)(int)>
301 void once (int fd, int events, tstamp timeout) throw () 288 void once (int fd, int events, tstamp timeout) throw ()
302 { 289 {
303 once (fd, events, timeout, simpler_func_thunk<cb>); 290 once (fd, events, timeout, simpler_func_thunk<cb>);
304 } 291 }
305 292
306 template<void (*cb)(int)> 293 template<void (*cb)(int)>
307 static void simpler_func_thunk (int revents, void* arg) 294 static void simpler_func_thunk (int revents, void *arg)
308 { 295 {
296 (*cb)
309 (*cb) (revents); 297 (revents);
310 } 298 }
311 299
312 // simplest function callback 300 // simplest function callback
313 template<void (*cb)()> 301 template<void (*cb)()>
314 void once (int fd, int events, tstamp timeout) throw () 302 void once (int fd, int events, tstamp timeout) throw ()
315 { 303 {
316 once (fd, events, timeout, simplest_func_thunk<cb>); 304 once (fd, events, timeout, simplest_func_thunk<cb>);
317 } 305 }
318 306
319 template<void (*cb)()> 307 template<void (*cb)()>
320 static void simplest_func_thunk (int revents, void* arg) 308 static void simplest_func_thunk (int revents, void *arg)
321 { 309 {
322 (*cb) (); 310 (*cb)
311 ();
323 } 312 }
324 313
325 void feed_fd_event (int fd, int revents) throw () 314 void feed_fd_event (int fd, int revents) throw ()
326 { 315 {
327 ev_feed_fd_event (EV_AX_ fd, revents); 316 ev_feed_fd_event (EV_AX_ fd, revents);
417 struct base : ev_watcher 406 struct base : ev_watcher
418 { 407 {
419 #if EV_MULTIPLICITY 408 #if EV_MULTIPLICITY
420 EV_PX; 409 EV_PX;
421 410
422 void set (EV_PX) throw () 411 void set (EV_P) throw ()
423 { 412 {
424 this->EV_A = EV_A; 413 this->EV_A = EV_A;
425 } 414 }
426 #endif 415 #endif
427 416
431 #endif 420 #endif
432 { 421 {
433 ev_init (this, 0); 422 ev_init (this, 0);
434 } 423 }
435 424
436 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () 425 void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw ()
437 { 426 {
438 this->data = data; 427 this->data = (void *)data;
439 ev_set_cb (static_cast<ev_watcher *>(this), cb); 428 ev_set_cb (static_cast<ev_watcher *>(this), cb);
429 }
430
431 // function callback
432 template<void (*function)(watcher &w, int)>
433 void set (void *data = 0) throw ()
434 {
435 set_ (data, function_thunk<function>);
436 }
437
438 template<void (*function)(watcher &w, int)>
439 static void function_thunk (EV_P_ ev_watcher *w, int revents)
440 {
441 function
442 (*static_cast<watcher *>(w), revents);
440 } 443 }
441 444
442 // method callback 445 // method callback
443 template<class K, void (K::*method)(watcher &w, int)> 446 template<class K, void (K::*method)(watcher &w, int)>
444 void set (K *object) throw () 447 void set (K *object) throw ()
445 { 448 {
446 set_ (object, method_thunk<K, method>); 449 set_ (object, method_thunk<K, method>);
447 } 450 }
448 451
452 // default method == operator ()
453 template<class K>
454 void set (K *object) throw ()
455 {
456 set_ (object, method_thunk<K, &K::operator ()>);
457 }
458
449 template<class K, void (K::*method)(watcher &w, int)> 459 template<class K, void (K::*method)(watcher &w, int)>
450 static void method_thunk (EV_P_ ev_watcher *w, int revents) 460 static void method_thunk (EV_P_ ev_watcher *w, int revents)
451 { 461 {
452 K *obj = static_cast<K *>(w->data); 462 (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); 463 (*static_cast<watcher *>(w), revents);
481 } 464 }
482 465
483 // simple callback 466 // no-argument callback
484 template<class K, void (K::*method)()> 467 template<class K, void (K::*method)()>
485 void set (K *object) throw () 468 void set (K *object) throw ()
486 { 469 {
487 set_ (object, method_noargs_thunk<K, method>); 470 set_ (object, method_noargs_thunk<K, method>);
488 } 471 }
489 472
490 template<class K, void (K::*method)()> 473 template<class K, void (K::*method)()>
491 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) 474 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
492 { 475 {
493 K *obj = static_cast<K *>(w->data); 476 static_cast<K *>(w->data)->*method
494 (obj->*method) (); 477 ();
495 } 478 }
496 479
497 void operator ()(int events = EV_UNDEF) 480 void operator ()(int events = EV_UNDEF)
498 { 481 {
482 return
499 return ev_cb (static_cast<ev_watcher *>(this)) 483 ev_cb (static_cast<ev_watcher *>(this))
500 (static_cast<ev_watcher *>(this), events); 484 (static_cast<ev_watcher *>(this), events);
501 } 485 }
502 486
503 bool is_active () const throw () 487 bool is_active () const throw ()
504 { 488 {
505 return ev_is_active (static_cast<const ev_watcher *>(this)); 489 return ev_is_active (static_cast<const ev_watcher *>(this));
561 ev_set_syserr_cb (cb); 545 ev_set_syserr_cb (cb);
562 } 546 }
563 547
564 #if EV_MULTIPLICITY 548 #if EV_MULTIPLICITY
565 #define EV_CONSTRUCT(cppstem,cstem) \ 549 #define EV_CONSTRUCT(cppstem,cstem) \
566 (EV_PX = get_default_loop ()) throw () \ 550 (EV_PX = get_default_loop ()) throw () \
567 : base<ev_ ## cstem, cppstem> (EV_A) \ 551 : base<ev_ ## cstem, cppstem> (EV_A) \
568 { \ 552 { \
569 } 553 }
570 #else 554 #else
571 #define EV_CONSTRUCT(cppstem,cstem) \ 555 #define EV_CONSTRUCT(cppstem,cstem) \
572 () throw () \ 556 () throw () \
573 { \ 557 { \
574 } 558 }
575 #endif 559 #endif
576 560
577 /* using a template here would require quite a bit more lines, 561 /* using a template here would require quite a bit more lines,
578 * so a macro solution was chosen */ 562 * so a macro solution was chosen */
579 #define EV_BEGIN_WATCHER(cppstem,cstem) \ 563 #define EV_BEGIN_WATCHER(cppstem,cstem) \
580 \ 564 \
581 struct cppstem : base<ev_ ## cstem, cppstem> \ 565 struct cppstem : base<ev_ ## cstem, cppstem> \
582 { \ 566 { \
583 void start () throw () \ 567 void start () throw () \
584 { \ 568 { \
585 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 569 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
586 } \ 570 } \
587 \ 571 \
588 void stop () throw () \ 572 void stop () throw () \
589 { \ 573 { \
590 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 574 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
591 } \ 575 } \
592 \ 576 \
593 cppstem EV_CONSTRUCT(cppstem,cstem) \ 577 cppstem EV_CONSTRUCT(cppstem,cstem) \
594 \ 578 \
595 ~cppstem () throw () \ 579 ~cppstem () throw () \
596 { \ 580 { \
597 stop (); \ 581 stop (); \
598 } \ 582 } \
599 \ 583 \
600 using base<ev_ ## cstem, cppstem>::set; \ 584 using base<ev_ ## cstem, cppstem>::set; \
601 \ 585 \
602 private: \ 586 private: \
603 \ 587 \
604 cppstem (const cppstem &o); \ 588 cppstem (const cppstem &o); \
605 \ 589 \
606 cppstem & operator =(const cppstem &o); \ 590 cppstem &operator =(const cppstem &o); \
607 \ 591 \
608 public: 592 public:
609 593
610 #define EV_END_WATCHER(cppstem,cstem) \ 594 #define EV_END_WATCHER(cppstem,cstem) \
611 }; 595 };
746 void set () throw () { } 730 void set () throw () { }
747 EV_END_WATCHER (check, check) 731 EV_END_WATCHER (check, check)
748 732
749 #if EV_EMBED_ENABLE 733 #if EV_EMBED_ENABLE
750 EV_BEGIN_WATCHER (embed, embed) 734 EV_BEGIN_WATCHER (embed, embed)
735 void set (struct ev_loop *embedded_loop) throw ()
736 {
737 int active = is_active ();
738 if (active) stop ();
739 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
740 if (active) start ();
741 }
742
751 void start (struct ev_loop *embedded_loop) throw () 743 void start (struct ev_loop *embedded_loop) throw ()
752 { 744 {
753 stop (); 745 set (embedded_loop);
754 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
755 start (); 746 start ();
756 } 747 }
757 748
758 void sweep () 749 void sweep ()
759 { 750 {
764 755
765 #if EV_FORK_ENABLE 756 #if EV_FORK_ENABLE
766 EV_BEGIN_WATCHER (fork, fork) 757 EV_BEGIN_WATCHER (fork, fork)
767 void set () throw () { } 758 void set () throw () { }
768 EV_END_WATCHER (fork, fork) 759 EV_END_WATCHER (fork, fork)
760 #endif
761
762 #if EV_ASYNC_ENABLE
763 EV_BEGIN_WATCHER (async, async)
764 void set () throw () { }
765
766 void send () throw ()
767 {
768 ev_async_send (EV_A_ static_cast<ev_async *>(this));
769 }
770
771 bool async_pending () throw ()
772 {
773 return ev_async_pending (static_cast<ev_async *>(this));
774 }
775 EV_END_WATCHER (async, async)
769 #endif 776 #endif
770 777
771 #undef EV_PX 778 #undef EV_PX
772 #undef EV_PX_ 779 #undef EV_PX_
773 #undef EV_CONSTRUCT 780 #undef EV_CONSTRUCT

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines