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

Comparing libev/ev++.h (file contents):
Revision 1.35 by llucax, Tue Jan 22 17:57:17 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,
130# define EV_AX_ 134# define EV_AX_
131#endif 135#endif
132 136
133 struct loop_ref 137 struct loop_ref
134 { 138 {
135
136 loop_ref (EV_P) 139 loop_ref (EV_P) throw ()
137#if EV_MULTIPLICITY 140#if EV_MULTIPLICITY
138 throw (bad_loop) : EV_AX (EV_A) 141 : EV_AX (EV_A)
139 { 142#endif
140 if (!EV_AX)
141 throw bad_loop ();
142 } 143 {
143#else
144 throw ()
145 { 144 }
146 }
147#endif
148 145
149 bool operator == (const loop_ref &other) const throw () 146 bool operator == (const loop_ref &other) const throw ()
150 { 147 {
151#if EV_MULTIPLICITY 148#if EV_MULTIPLICITY
152 return EV_AX == other.EV_AX; 149 return EV_AX == other.EV_AX;
163 return false; 160 return false;
164#endif 161#endif
165 } 162 }
166 163
167#if EV_MULTIPLICITY 164#if EV_MULTIPLICITY
168 bool operator== (struct ev_loop *other) const throw () 165 bool operator == (const EV_P) const throw ()
169 { 166 {
170 return this->EV_AX == other; 167 return this->EV_AX == EV_A;
171 }
172
173 bool operator!= (struct ev_loop *other) const throw ()
174 { 168 }
175 return ! (*this == other); 169
170 bool operator != (const EV_P) const throw ()
176 } 171 {
177
178 bool operator== (const struct ev_loop *other) const throw ()
179 {
180 return this->EV_AX == other;
181 }
182
183 bool operator!= (const struct ev_loop *other) const throw ()
184 {
185 return (*this == other); 172 return (*this == EV_A);
186 } 173 }
187 174
188 operator struct ev_loop * () const throw () 175 operator struct ev_loop * () const throw ()
189 { 176 {
190 return EV_AX; 177 return EV_AX;
254 { 241 {
255 ev_set_timeout_collect_interval (EV_AX_ interval); 242 ev_set_timeout_collect_interval (EV_AX_ interval);
256 } 243 }
257 244
258 // function callback 245 // function callback
259 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 ()
260 { 247 {
261 ev_once (EV_AX_ fd, events, timeout, cb, arg); 248 ev_once (EV_AX_ fd, events, timeout, cb, arg);
262 } 249 }
263 250
264 // method callback 251 // method callback
266 void once (int fd, int events, tstamp timeout, K *object) throw () 253 void once (int fd, int events, tstamp timeout, K *object) throw ()
267 { 254 {
268 once (fd, events, timeout, method_thunk<K, method>, object); 255 once (fd, events, timeout, method_thunk<K, method>, object);
269 } 256 }
270 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
271 template<class K, void (K::*method)(int)> 265 template<class K, void (K::*method)(int)>
272 static void method_thunk (int revents, void* arg) 266 static void method_thunk (int revents, void *arg)
273 {
274 K *obj = static_cast<K *>(arg);
275 (obj->*method) (revents);
276 } 267 {
268 static_cast<K *>(arg)->*method
269 (revents);
270 }
277 271
278 // const method callback 272 // no-argument method callback
279 template<class K, void (K::*method)(int) const>
280 void once (int fd, int events, tstamp timeout, const K *object) throw ()
281 {
282 once (fd, events, timeout, const_method_thunk<K, method>, object);
283 }
284
285 template<class K, void (K::*method)(int) const>
286 static void const_method_thunk (int revents, void* arg)
287 {
288 K *obj = static_cast<K *>(arg);
289 (obj->*method) (revents);
290 }
291
292 // simple method callback
293 template<class K, void (K::*method)()> 273 template<class K, void (K::*method)()>
294 void once (int fd, int events, tstamp timeout, K *object) throw () 274 void once (int fd, int events, tstamp timeout, K *object) throw ()
295 { 275 {
296 once (fd, events, timeout, method_noargs_thunk<K, method>, object); 276 once (fd, events, timeout, method_noargs_thunk<K, method>, object);
297 } 277 }
298 278
299 template<class K, void (K::*method)()> 279 template<class K, void (K::*method)()>
300 static void method_noargs_thunk (int revents, void* arg) 280 static void method_noargs_thunk (int revents, void *arg)
301 { 281 {
302 K *obj = static_cast<K *>(arg); 282 static_cast<K *>(arg)->*method
303 (obj->*method) (); 283 ();
304 } 284 }
305 285
306 // simpler function callback 286 // simpler function callback
307 template<void (*cb)(int)> 287 template<void (*cb)(int)>
308 void once (int fd, int events, tstamp timeout) throw () 288 void once (int fd, int events, tstamp timeout) throw ()
309 { 289 {
310 once (fd, events, timeout, simpler_func_thunk<cb>); 290 once (fd, events, timeout, simpler_func_thunk<cb>);
311 } 291 }
312 292
313 template<void (*cb)(int)> 293 template<void (*cb)(int)>
314 static void simpler_func_thunk (int revents, void* arg) 294 static void simpler_func_thunk (int revents, void *arg)
315 { 295 {
296 (*cb)
316 (*cb) (revents); 297 (revents);
317 } 298 }
318 299
319 // simplest function callback 300 // simplest function callback
320 template<void (*cb)()> 301 template<void (*cb)()>
321 void once (int fd, int events, tstamp timeout) throw () 302 void once (int fd, int events, tstamp timeout) throw ()
322 { 303 {
323 once (fd, events, timeout, simplest_func_thunk<cb>); 304 once (fd, events, timeout, simplest_func_thunk<cb>);
324 } 305 }
325 306
326 template<void (*cb)()> 307 template<void (*cb)()>
327 static void simplest_func_thunk (int revents, void* arg) 308 static void simplest_func_thunk (int revents, void *arg)
328 { 309 {
329 (*cb) (); 310 (*cb)
311 ();
330 } 312 }
331 313
332 void feed_fd_event (int fd, int revents) throw () 314 void feed_fd_event (int fd, int revents) throw ()
333 { 315 {
334 ev_feed_fd_event (EV_AX_ fd, revents); 316 ev_feed_fd_event (EV_AX_ fd, revents);
344#endif 326#endif
345 327
346 }; 328 };
347 329
348#if EV_MULTIPLICITY 330#if EV_MULTIPLICITY
349 struct dynamic_loop: loop_ref 331 struct dynamic_loop : loop_ref
350 { 332 {
351 333
352 dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) 334 dynamic_loop (unsigned int flags = AUTO) throw (bad_loop)
353 : loop_ref (ev_loop_new (flags)) 335 : loop_ref (ev_loop_new (flags))
354 { 336 {
337 if (!EV_AX)
338 throw bad_loop ();
355 } 339 }
356 340
357 ~dynamic_loop () throw () 341 ~dynamic_loop () throw ()
358 { 342 {
359 ev_loop_destroy (EV_AX); 343 ev_loop_destroy (EV_AX);
367 dynamic_loop & operator= (const dynamic_loop &); 351 dynamic_loop & operator= (const dynamic_loop &);
368 352
369 }; 353 };
370#endif 354#endif
371 355
372 struct default_loop: loop_ref 356 struct default_loop : loop_ref
373 { 357 {
374
375 default_loop (unsigned int flags = AUTO) throw (bad_loop) 358 default_loop (unsigned int flags = AUTO) throw (bad_loop)
376#if EV_MULTIPLICITY 359#if EV_MULTIPLICITY
377 : loop_ref (ev_default_loop (flags)) 360 : loop_ref (ev_default_loop (flags))
378#endif 361#endif
379 { 362 {
363 if (
380#if !EV_MULTIPLICITY 364#if EV_MULTIPLICITY
365 !EV_AX
366#else
381 if (!ev_default_loop (flags)) 367 !ev_default_loop (flags)
368#endif
369 )
382 throw bad_loop (); 370 throw bad_loop ();
383#endif
384 } 371 }
385 372
386 ~default_loop () throw () 373 ~default_loop () throw ()
387 { 374 {
388 ev_default_destroy (); 375 ev_default_destroy ();
419 struct base : ev_watcher 406 struct base : ev_watcher
420 { 407 {
421 #if EV_MULTIPLICITY 408 #if EV_MULTIPLICITY
422 EV_PX; 409 EV_PX;
423 410
424 void set (EV_PX) throw () 411 void set (EV_P) throw ()
425 { 412 {
426 this->EV_A = EV_A; 413 this->EV_A = EV_A;
427 } 414 }
428 #endif 415 #endif
429 416
433 #endif 420 #endif
434 { 421 {
435 ev_init (this, 0); 422 ev_init (this, 0);
436 } 423 }
437 424
438 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 ()
439 { 426 {
440 this->data = data; 427 this->data = (void *)data;
441 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);
442 } 443 }
443 444
444 // method callback 445 // method callback
445 template<class K, void (K::*method)(watcher &w, int)> 446 template<class K, void (K::*method)(watcher &w, int)>
446 void set (K *object) throw () 447 void set (K *object) throw ()
447 { 448 {
448 set_ (object, method_thunk<K, method>); 449 set_ (object, method_thunk<K, method>);
449 } 450 }
450 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
451 template<class K, void (K::*method)(watcher &w, int)> 459 template<class K, void (K::*method)(watcher &w, int)>
452 static void method_thunk (EV_P_ ev_watcher *w, int revents) 460 static void method_thunk (EV_P_ ev_watcher *w, int revents)
453 { 461 {
454 K *obj = static_cast<K *>(w->data); 462 (static_cast<K *>(w->data)->*method)
455 (obj->*method) (*static_cast<watcher *>(w), revents);
456 }
457
458 // const method callback
459 template<class K, void (K::*method)(watcher &w, int) const>
460 void set (const K *object) throw ()
461 {
462 set_ (object, const_method_thunk<K, method>);
463 }
464
465 template<class K, void (K::*method)(watcher &w, int) const>
466 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
467 {
468 K *obj = static_cast<K *>(w->data);
469 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
470 }
471
472 // function callback
473 template<void (*function)(watcher &w, int)>
474 void set (void *data = 0) throw ()
475 {
476 set_ (data, function_thunk<function>);
477 }
478
479 template<void (*function)(watcher &w, int)>
480 static void function_thunk (EV_P_ ev_watcher *w, int revents)
481 {
482 function (*static_cast<watcher *>(w), revents); 463 (*static_cast<watcher *>(w), revents);
483 } 464 }
484 465
485 // simple callback 466 // no-argument callback
486 template<class K, void (K::*method)()> 467 template<class K, void (K::*method)()>
487 void set (K *object) throw () 468 void set (K *object) throw ()
488 { 469 {
489 set_ (object, method_noargs_thunk<K, method>); 470 set_ (object, method_noargs_thunk<K, method>);
490 } 471 }
491 472
492 template<class K, void (K::*method)()> 473 template<class K, void (K::*method)()>
493 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)
494 { 475 {
495 K *obj = static_cast<K *>(w->data); 476 static_cast<K *>(w->data)->*method
496 (obj->*method) (); 477 ();
497 } 478 }
498 479
499 void operator ()(int events = EV_UNDEF) 480 void operator ()(int events = EV_UNDEF)
500 { 481 {
482 return
501 return ev_cb (static_cast<ev_watcher *>(this)) 483 ev_cb (static_cast<ev_watcher *>(this))
502 (static_cast<ev_watcher *>(this), events); 484 (static_cast<ev_watcher *>(this), events);
503 } 485 }
504 486
505 bool is_active () const throw () 487 bool is_active () const throw ()
506 { 488 {
507 return ev_is_active (static_cast<const ev_watcher *>(this)); 489 return ev_is_active (static_cast<const ev_watcher *>(this));
563 ev_set_syserr_cb (cb); 545 ev_set_syserr_cb (cb);
564 } 546 }
565 547
566 #if EV_MULTIPLICITY 548 #if EV_MULTIPLICITY
567 #define EV_CONSTRUCT(cppstem,cstem) \ 549 #define EV_CONSTRUCT(cppstem,cstem) \
568 (EV_PX = get_default_loop ()) throw () \ 550 (EV_PX = get_default_loop ()) throw () \
569 : base<ev_ ## cstem, cppstem> (EV_A) \ 551 : base<ev_ ## cstem, cppstem> (EV_A) \
570 { \ 552 { \
571 } 553 }
572 #else 554 #else
573 #define EV_CONSTRUCT(cppstem,cstem) \ 555 #define EV_CONSTRUCT(cppstem,cstem) \
574 () throw () \ 556 () throw () \
575 { \ 557 { \
576 } 558 }
577 #endif 559 #endif
578 560
579 /* using a template here would require quite a bit more lines, 561 /* using a template here would require quite a bit more lines,
580 * so a macro solution was chosen */ 562 * so a macro solution was chosen */
581 #define EV_BEGIN_WATCHER(cppstem,cstem) \ 563 #define EV_BEGIN_WATCHER(cppstem,cstem) \
582 \ 564 \
583 struct cppstem : base<ev_ ## cstem, cppstem> \ 565 struct cppstem : base<ev_ ## cstem, cppstem> \
584 { \ 566 { \
585 void start () throw () \ 567 void start () throw () \
586 { \ 568 { \
587 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 569 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
588 } \ 570 } \
589 \ 571 \
590 void stop () throw () \ 572 void stop () throw () \
591 { \ 573 { \
592 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 574 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
593 } \ 575 } \
594 \ 576 \
595 cppstem EV_CONSTRUCT(cppstem,cstem) \ 577 cppstem EV_CONSTRUCT(cppstem,cstem) \
596 \ 578 \
597 ~cppstem () throw () \ 579 ~cppstem () throw () \
598 { \ 580 { \
599 stop (); \ 581 stop (); \
600 } \ 582 } \
601 \ 583 \
602 using base<ev_ ## cstem, cppstem>::set; \ 584 using base<ev_ ## cstem, cppstem>::set; \
603 \ 585 \
604 private: \ 586 private: \
605 \ 587 \
606 cppstem (const cppstem &o); \ 588 cppstem (const cppstem &o); \
607 \ 589 \
608 cppstem & operator =(const cppstem &o); \ 590 cppstem &operator =(const cppstem &o); \
609 \ 591 \
610 public: 592 public:
611 593
612 #define EV_END_WATCHER(cppstem,cstem) \ 594 #define EV_END_WATCHER(cppstem,cstem) \
613 }; 595 };
695 start (); 677 start ();
696 } 678 }
697 EV_END_WATCHER (sig, signal) 679 EV_END_WATCHER (sig, signal)
698 680
699 EV_BEGIN_WATCHER (child, child) 681 EV_BEGIN_WATCHER (child, child)
700 void set (int pid) throw () 682 void set (int pid, int trace = 0) throw ()
701 { 683 {
702 int active = is_active (); 684 int active = is_active ();
703 if (active) stop (); 685 if (active) stop ();
704 ev_child_set (static_cast<ev_child *>(this), pid); 686 ev_child_set (static_cast<ev_child *>(this), pid, trace);
705 if (active) start (); 687 if (active) start ();
706 } 688 }
707 689
708 void start (int pid) throw () 690 void start (int pid, int trace = 0) throw ()
709 { 691 {
710 set (pid); 692 set (pid, trace);
711 start (); 693 start ();
712 } 694 }
713 EV_END_WATCHER (child, child) 695 EV_END_WATCHER (child, child)
714 696
715 #if EV_STAT_ENABLE 697 #if EV_STAT_ENABLE
748 void set () throw () { } 730 void set () throw () { }
749 EV_END_WATCHER (check, check) 731 EV_END_WATCHER (check, check)
750 732
751 #if EV_EMBED_ENABLE 733 #if EV_EMBED_ENABLE
752 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
753 void start (struct ev_loop *embedded_loop) throw () 743 void start (struct ev_loop *embedded_loop) throw ()
754 { 744 {
755 stop (); 745 set (embedded_loop);
756 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
757 start (); 746 start ();
758 } 747 }
759 748
760 void sweep () 749 void sweep ()
761 { 750 {
768 EV_BEGIN_WATCHER (fork, fork) 757 EV_BEGIN_WATCHER (fork, fork)
769 void set () throw () { } 758 void set () throw () { }
770 EV_END_WATCHER (fork, fork) 759 EV_END_WATCHER (fork, fork)
771 #endif 760 #endif
772 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)
776 #endif
777
773 #undef EV_PX 778 #undef EV_PX
774 #undef EV_PX_ 779 #undef EV_PX_
775 #undef EV_CONSTRUCT 780 #undef EV_CONSTRUCT
776 #undef EV_BEGIN_WATCHER 781 #undef EV_BEGIN_WATCHER
777 #undef EV_END_WATCHER 782 #undef EV_END_WATCHER
778
779} 783}
780 784
781#endif 785#endif
782 786

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines