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

Comparing libev/ev++.h (file contents):
Revision 1.39 by root, Tue Apr 15 04:34:07 2008 UTC vs.
Revision 1.52 by root, Thu Oct 21 02:33:08 2010 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,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
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,
72 CHECK = EV_CHECK, 73 CHECK = EV_CHECK,
73 PREPARE = EV_PREPARE, 74 PREPARE = EV_PREPARE,
74 FORK = EV_FORK, 75 FORK = EV_FORK,
75 ASYNC = EV_ASYNC, 76 ASYNC = EV_ASYNC,
76 EMBED = EV_EMBED, 77 EMBED = EV_EMBED,
78# undef ERROR // some systems stupidly #define ERROR
77 ERROR = EV_ERROR, 79 ERROR = EV_ERROR,
78 }; 80 };
79 81
80 enum 82 enum
81 { 83 {
82 AUTO = EVFLAG_AUTO, 84 AUTO = EVFLAG_AUTO,
83 NOENV = EVFLAG_NOENV, 85 NOENV = EVFLAG_NOENV,
84 FORKCHECK = EVFLAG_FORKCHECK, 86 FORKCHECK = EVFLAG_FORKCHECK,
87
85 SELECT = EVBACKEND_SELECT, 88 SELECT = EVBACKEND_SELECT,
86 POLL = EVBACKEND_POLL, 89 POLL = EVBACKEND_POLL,
87 EPOLL = EVBACKEND_EPOLL, 90 EPOLL = EVBACKEND_EPOLL,
88 KQUEUE = EVBACKEND_KQUEUE, 91 KQUEUE = EVBACKEND_KQUEUE,
89 DEVPOLL = EVBACKEND_DEVPOLL, 92 DEVPOLL = EVBACKEND_DEVPOLL,
90 PORT = EVBACKEND_PORT 93 PORT = EVBACKEND_PORT
91 }; 94 };
92 95
93 enum 96 enum
94 { 97 {
95 NONBLOCK = EVLOOP_NONBLOCK, 98 NONBLOCK = EVLOOP_NONBLOCK,
96 ONESHOT = EVLOOP_ONESHOT 99 ONESHOT = EVLOOP_ONESHOT
97 }; 100 };
98 101
99 enum how_t 102 enum how_t
100 { 103 {
101 ONE = EVUNLOOP_ONE, 104 ONE = EVUNLOOP_ONE,
157 return false; 160 return false;
158#endif 161#endif
159 } 162 }
160 163
161#if EV_MULTIPLICITY 164#if EV_MULTIPLICITY
162 bool operator == (struct ev_loop *other) const throw () 165 bool operator == (const EV_P) const throw ()
163 { 166 {
164 return this->EV_AX == other; 167 return this->EV_AX == EV_A;
165 }
166
167 bool operator != (struct ev_loop *other) const throw ()
168 { 168 }
169 return ! (*this == other); 169
170 bool operator != (const EV_P) const throw ()
170 } 171 {
171
172 bool operator == (const struct ev_loop *other) const throw ()
173 {
174 return this->EV_AX == other;
175 }
176
177 bool operator != (const struct ev_loop *other) const throw ()
178 {
179 return (*this == other); 172 return (*this == EV_A);
180 } 173 }
181 174
182 operator struct ev_loop * () const throw () 175 operator struct ev_loop * () const throw ()
183 { 176 {
184 return EV_AX; 177 return EV_AX;
212#else 205#else
213 ev_default_fork (); 206 ev_default_fork ();
214#endif 207#endif
215 } 208 }
216 209
217 unsigned int count () const throw ()
218 {
219 return ev_loop_count (EV_AX);
220 }
221
222 unsigned int backend () const throw () 210 unsigned int backend () const throw ()
223 { 211 {
224 return ev_backend (EV_AX); 212 return ev_backend (EV_AX);
225 } 213 }
226 214
237 void unref () throw () 225 void unref () throw ()
238 { 226 {
239 ev_unref (EV_AX); 227 ev_unref (EV_AX);
240 } 228 }
241 229
230#if EV_FEATURE_API
231 unsigned int iteration () const throw ()
232 {
233 return ev_iteration (EV_AX);
234 }
235
236 unsigned int depth () const throw ()
237 {
238 return ev_depth (EV_AX);
239 }
240
242 void set_io_collect_interval (tstamp interval) throw () 241 void set_io_collect_interval (tstamp interval) throw ()
243 { 242 {
244 ev_set_io_collect_interval (EV_AX_ interval); 243 ev_set_io_collect_interval (EV_AX_ interval);
245 } 244 }
246 245
247 void set_timeout_collect_interval (tstamp interval) throw () 246 void set_timeout_collect_interval (tstamp interval) throw ()
248 { 247 {
249 ev_set_timeout_collect_interval (EV_AX_ interval); 248 ev_set_timeout_collect_interval (EV_AX_ interval);
250 } 249 }
250#endif
251 251
252 // function callback 252 // function callback
253 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 ()
254 { 254 {
255 ev_once (EV_AX_ fd, events, timeout, cb, arg); 255 ev_once (EV_AX_ fd, events, timeout, cb, arg);
256 } 256 }
257 257
258 // method callback 258 // method callback
260 void once (int fd, int events, tstamp timeout, K *object) throw () 260 void once (int fd, int events, tstamp timeout, K *object) throw ()
261 { 261 {
262 once (fd, events, timeout, method_thunk<K, method>, object); 262 once (fd, events, timeout, method_thunk<K, method>, object);
263 } 263 }
264 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
265 template<class K, void (K::*method)(int)> 272 template<class K, void (K::*method)(int)>
266 static void method_thunk (int revents, void* arg) 273 static void method_thunk (int revents, void *arg)
267 {
268 K *obj = static_cast<K *>(arg);
269 (obj->*method) (revents);
270 } 274 {
275 static_cast<K *>(arg)->*method
276 (revents);
277 }
271 278
272 // const method callback 279 // no-argument method callback
273 template<class K, void (K::*method)(int) const>
274 void once (int fd, int events, tstamp timeout, const K *object) throw ()
275 {
276 once (fd, events, timeout, const_method_thunk<K, method>, object);
277 }
278
279 template<class K, void (K::*method)(int) const>
280 static void const_method_thunk (int revents, void* arg)
281 {
282 K *obj = static_cast<K *>(arg);
283 (obj->*method) (revents);
284 }
285
286 // simple method callback
287 template<class K, void (K::*method)()> 280 template<class K, void (K::*method)()>
288 void once (int fd, int events, tstamp timeout, K *object) throw () 281 void once (int fd, int events, tstamp timeout, K *object) throw ()
289 { 282 {
290 once (fd, events, timeout, method_noargs_thunk<K, method>, object); 283 once (fd, events, timeout, method_noargs_thunk<K, method>, object);
291 } 284 }
292 285
293 template<class K, void (K::*method)()> 286 template<class K, void (K::*method)()>
294 static void method_noargs_thunk (int revents, void* arg) 287 static void method_noargs_thunk (int revents, void *arg)
295 { 288 {
296 K *obj = static_cast<K *>(arg); 289 static_cast<K *>(arg)->*method
297 (obj->*method) (); 290 ();
298 } 291 }
299 292
300 // simpler function callback 293 // simpler function callback
301 template<void (*cb)(int)> 294 template<void (*cb)(int)>
302 void once (int fd, int events, tstamp timeout) throw () 295 void once (int fd, int events, tstamp timeout) throw ()
303 { 296 {
304 once (fd, events, timeout, simpler_func_thunk<cb>); 297 once (fd, events, timeout, simpler_func_thunk<cb>);
305 } 298 }
306 299
307 template<void (*cb)(int)> 300 template<void (*cb)(int)>
308 static void simpler_func_thunk (int revents, void* arg) 301 static void simpler_func_thunk (int revents, void *arg)
309 { 302 {
303 (*cb)
310 (*cb) (revents); 304 (revents);
311 } 305 }
312 306
313 // simplest function callback 307 // simplest function callback
314 template<void (*cb)()> 308 template<void (*cb)()>
315 void once (int fd, int events, tstamp timeout) throw () 309 void once (int fd, int events, tstamp timeout) throw ()
316 { 310 {
317 once (fd, events, timeout, simplest_func_thunk<cb>); 311 once (fd, events, timeout, simplest_func_thunk<cb>);
318 } 312 }
319 313
320 template<void (*cb)()> 314 template<void (*cb)()>
321 static void simplest_func_thunk (int revents, void* arg) 315 static void simplest_func_thunk (int revents, void *arg)
322 { 316 {
323 (*cb) (); 317 (*cb)
318 ();
324 } 319 }
325 320
326 void feed_fd_event (int fd, int revents) throw () 321 void feed_fd_event (int fd, int revents) throw ()
327 { 322 {
328 ev_feed_fd_event (EV_AX_ fd, revents); 323 ev_feed_fd_event (EV_AX_ fd, revents);
418 struct base : ev_watcher 413 struct base : ev_watcher
419 { 414 {
420 #if EV_MULTIPLICITY 415 #if EV_MULTIPLICITY
421 EV_PX; 416 EV_PX;
422 417
418 // loop set
423 void set (EV_PX) throw () 419 void set (EV_P) throw ()
424 { 420 {
425 this->EV_A = EV_A; 421 this->EV_A = EV_A;
426 } 422 }
427 #endif 423 #endif
428 424
432 #endif 428 #endif
433 { 429 {
434 ev_init (this, 0); 430 ev_init (this, 0);
435 } 431 }
436 432
437 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () 433 void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw ()
438 { 434 {
439 this->data = data; 435 this->data = (void *)data;
440 ev_set_cb (static_cast<ev_watcher *>(this), cb); 436 ev_set_cb (static_cast<ev_watcher *>(this), cb);
437 }
438
439 // function callback
440 template<void (*function)(watcher &w, int)>
441 void set (void *data = 0) throw ()
442 {
443 set_ (data, function_thunk<function>);
444 }
445
446 template<void (*function)(watcher &w, int)>
447 static void function_thunk (EV_P_ ev_watcher *w, int revents)
448 {
449 function
450 (*static_cast<watcher *>(w), revents);
441 } 451 }
442 452
443 // method callback 453 // method callback
444 template<class K, void (K::*method)(watcher &w, int)> 454 template<class K, void (K::*method)(watcher &w, int)>
445 void set (K *object) throw () 455 void set (K *object) throw ()
446 { 456 {
447 set_ (object, method_thunk<K, method>); 457 set_ (object, method_thunk<K, method>);
448 } 458 }
449 459
460 // default method == operator ()
461 template<class K>
462 void set (K *object) throw ()
463 {
464 set_ (object, method_thunk<K, &K::operator ()>);
465 }
466
450 template<class K, void (K::*method)(watcher &w, int)> 467 template<class K, void (K::*method)(watcher &w, int)>
451 static void method_thunk (EV_P_ ev_watcher *w, int revents) 468 static void method_thunk (EV_P_ ev_watcher *w, int revents)
452 { 469 {
453 K *obj = static_cast<K *>(w->data); 470 (static_cast<K *>(w->data)->*method)
454 (obj->*method) (*static_cast<watcher *>(w), revents);
455 }
456
457 // const method callback
458 template<class K, void (K::*method)(watcher &w, int) const>
459 void set (const K *object) throw ()
460 {
461 set_ (object, const_method_thunk<K, method>);
462 }
463
464 template<class K, void (K::*method)(watcher &w, int) const>
465 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
466 {
467 K *obj = static_cast<K *>(w->data);
468 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
469 }
470
471 // function callback
472 template<void (*function)(watcher &w, int)>
473 void set (void *data = 0) throw ()
474 {
475 set_ (data, function_thunk<function>);
476 }
477
478 template<void (*function)(watcher &w, int)>
479 static void function_thunk (EV_P_ ev_watcher *w, int revents)
480 {
481 function (*static_cast<watcher *>(w), revents); 471 (*static_cast<watcher *>(w), revents);
482 } 472 }
483 473
484 // simple callback 474 // no-argument callback
485 template<class K, void (K::*method)()> 475 template<class K, void (K::*method)()>
486 void set (K *object) throw () 476 void set (K *object) throw ()
487 { 477 {
488 set_ (object, method_noargs_thunk<K, method>); 478 set_ (object, method_noargs_thunk<K, method>);
489 } 479 }
490 480
491 template<class K, void (K::*method)()> 481 template<class K, void (K::*method)()>
492 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) 482 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
493 { 483 {
494 K *obj = static_cast<K *>(w->data); 484 (static_cast<K *>(w->data)->*method)
495 (obj->*method) (); 485 ();
496 } 486 }
497 487
498 void operator ()(int events = EV_UNDEF) 488 void operator ()(int events = EV_UNDEF)
499 { 489 {
490 return
500 return ev_cb (static_cast<ev_watcher *>(this)) 491 ev_cb (static_cast<ev_watcher *>(this))
501 (static_cast<ev_watcher *>(this), events); 492 (static_cast<ev_watcher *>(this), events);
502 } 493 }
503 494
504 bool is_active () const throw () 495 bool is_active () const throw ()
505 { 496 {
506 return ev_is_active (static_cast<const ev_watcher *>(this)); 497 return ev_is_active (static_cast<const ev_watcher *>(this));
652 643
653 void again () throw () 644 void again () throw ()
654 { 645 {
655 ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 646 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
656 } 647 }
648
649 ev_tstamp remaining ()
650 {
651 return ev_timer_remaining (EV_A_ static_cast<ev_timer *>(this));
652 }
657 EV_END_WATCHER (timer, timer) 653 EV_END_WATCHER (timer, timer)
658 654
659 #if EV_PERIODIC_ENABLE 655 #if EV_PERIODIC_ENABLE
660 EV_BEGIN_WATCHER (periodic, periodic) 656 EV_BEGIN_WATCHER (periodic, periodic)
661 void set (ev_tstamp at, ev_tstamp interval = 0.) throw () 657 void set (ev_tstamp at, ev_tstamp interval = 0.) throw ()
677 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); 673 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
678 } 674 }
679 EV_END_WATCHER (periodic, periodic) 675 EV_END_WATCHER (periodic, periodic)
680 #endif 676 #endif
681 677
678 #if EV_SIGNAL_ENABLE
682 EV_BEGIN_WATCHER (sig, signal) 679 EV_BEGIN_WATCHER (sig, signal)
683 void set (int signum) throw () 680 void set (int signum) throw ()
684 { 681 {
685 int active = is_active (); 682 int active = is_active ();
686 if (active) stop (); 683 if (active) stop ();
692 { 689 {
693 set (signum); 690 set (signum);
694 start (); 691 start ();
695 } 692 }
696 EV_END_WATCHER (sig, signal) 693 EV_END_WATCHER (sig, signal)
694 #endif
697 695
696 #if EV_CHILD_ENABLE
698 EV_BEGIN_WATCHER (child, child) 697 EV_BEGIN_WATCHER (child, child)
699 void set (int pid, int trace = 0) throw () 698 void set (int pid, int trace = 0) throw ()
700 { 699 {
701 int active = is_active (); 700 int active = is_active ();
702 if (active) stop (); 701 if (active) stop ();
708 { 707 {
709 set (pid, trace); 708 set (pid, trace);
710 start (); 709 start ();
711 } 710 }
712 EV_END_WATCHER (child, child) 711 EV_END_WATCHER (child, child)
712 #endif
713 713
714 #if EV_STAT_ENABLE 714 #if EV_STAT_ENABLE
715 EV_BEGIN_WATCHER (stat, stat) 715 EV_BEGIN_WATCHER (stat, stat)
716 void set (const char *path, ev_tstamp interval = 0.) throw () 716 void set (const char *path, ev_tstamp interval = 0.) throw ()
717 { 717 {
733 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); 733 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
734 } 734 }
735 EV_END_WATCHER (stat, stat) 735 EV_END_WATCHER (stat, stat)
736 #endif 736 #endif
737 737
738 #if EV_IDLE_ENABLE
738 EV_BEGIN_WATCHER (idle, idle) 739 EV_BEGIN_WATCHER (idle, idle)
739 void set () throw () { } 740 void set () throw () { }
740 EV_END_WATCHER (idle, idle) 741 EV_END_WATCHER (idle, idle)
742 #endif
741 743
744 #if EV_PREPARE_ENABLE
742 EV_BEGIN_WATCHER (prepare, prepare) 745 EV_BEGIN_WATCHER (prepare, prepare)
743 void set () throw () { } 746 void set () throw () { }
744 EV_END_WATCHER (prepare, prepare) 747 EV_END_WATCHER (prepare, prepare)
748 #endif
745 749
750 #if EV_CHECK_ENABLE
746 EV_BEGIN_WATCHER (check, check) 751 EV_BEGIN_WATCHER (check, check)
747 void set () throw () { } 752 void set () throw () { }
748 EV_END_WATCHER (check, check) 753 EV_END_WATCHER (check, check)
754 #endif
749 755
750 #if EV_EMBED_ENABLE 756 #if EV_EMBED_ENABLE
751 EV_BEGIN_WATCHER (embed, embed) 757 EV_BEGIN_WATCHER (embed, embed)
752 void set (struct ev_loop *embedded_loop) throw () 758 void set (struct ev_loop *embedded_loop) throw ()
753 { 759 {
776 EV_END_WATCHER (fork, fork) 782 EV_END_WATCHER (fork, fork)
777 #endif 783 #endif
778 784
779 #if EV_ASYNC_ENABLE 785 #if EV_ASYNC_ENABLE
780 EV_BEGIN_WATCHER (async, async) 786 EV_BEGIN_WATCHER (async, async)
781 void set () throw () { }
782
783 void send () throw () 787 void send () throw ()
784 { 788 {
785 ev_async_send (EV_A_ static_cast<ev_async *>(this)); 789 ev_async_send (EV_A_ static_cast<ev_async *>(this));
786 } 790 }
787 791

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines