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

Comparing libev/ev++.h (file contents):
Revision 1.41 by root, Tue Sep 30 18:35:47 2008 UTC vs.
Revision 1.53 by root, Thu Oct 21 14:50:58 2010 UTC

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
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,
158 return false; 160 return false;
159#endif 161#endif
160 } 162 }
161 163
162#if EV_MULTIPLICITY 164#if EV_MULTIPLICITY
163 bool operator == (struct ev_loop *other) const throw () 165 bool operator == (const EV_P) const throw ()
164 { 166 {
165 return this->EV_AX == other; 167 return this->EV_AX == EV_A;
166 }
167
168 bool operator != (struct ev_loop *other) const throw ()
169 { 168 }
170 return ! (*this == other); 169
170 bool operator != (const EV_P) const throw ()
171 } 171 {
172
173 bool operator == (const struct ev_loop *other) const throw ()
174 {
175 return this->EV_AX == other;
176 }
177
178 bool operator != (const struct ev_loop *other) const throw ()
179 {
180 return (*this == other); 172 return (*this == EV_A);
181 } 173 }
182 174
183 operator struct ev_loop * () const throw () 175 operator struct ev_loop * () const throw ()
184 { 176 {
185 return EV_AX; 177 return EV_AX;
213#else 205#else
214 ev_default_fork (); 206 ev_default_fork ();
215#endif 207#endif
216 } 208 }
217 209
218 unsigned int count () const throw ()
219 {
220 return ev_loop_count (EV_AX);
221 }
222
223 unsigned int backend () const throw () 210 unsigned int backend () const throw ()
224 { 211 {
225 return ev_backend (EV_AX); 212 return ev_backend (EV_AX);
226 } 213 }
227 214
238 void unref () throw () 225 void unref () throw ()
239 { 226 {
240 ev_unref (EV_AX); 227 ev_unref (EV_AX);
241 } 228 }
242 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
243 void set_io_collect_interval (tstamp interval) throw () 241 void set_io_collect_interval (tstamp interval) throw ()
244 { 242 {
245 ev_set_io_collect_interval (EV_AX_ interval); 243 ev_set_io_collect_interval (EV_AX_ interval);
246 } 244 }
247 245
248 void set_timeout_collect_interval (tstamp interval) throw () 246 void set_timeout_collect_interval (tstamp interval) throw ()
249 { 247 {
250 ev_set_timeout_collect_interval (EV_AX_ interval); 248 ev_set_timeout_collect_interval (EV_AX_ interval);
251 } 249 }
250#endif
252 251
253 // function callback 252 // function callback
254 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 ()
255 { 254 {
256 ev_once (EV_AX_ fd, events, timeout, cb, arg); 255 ev_once (EV_AX_ fd, events, timeout, cb, arg);
257 } 256 }
258 257
259 // method callback 258 // method callback
261 void once (int fd, int events, tstamp timeout, K *object) throw () 260 void once (int fd, int events, tstamp timeout, K *object) throw ()
262 { 261 {
263 once (fd, events, timeout, method_thunk<K, method>, object); 262 once (fd, events, timeout, method_thunk<K, method>, object);
264 } 263 }
265 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
266 template<class K, void (K::*method)(int)> 272 template<class K, void (K::*method)(int)>
267 static void method_thunk (int revents, void* arg) 273 static void method_thunk (int revents, void *arg)
268 {
269 K *obj = static_cast<K *>(arg);
270 (obj->*method) (revents);
271 } 274 {
275 static_cast<K *>(arg)->*method
276 (revents);
277 }
272 278
273 // const method callback 279 // no-argument method callback
274 template<class K, void (K::*method)(int) const>
275 void once (int fd, int events, tstamp timeout, const K *object) throw ()
276 {
277 once (fd, events, timeout, const_method_thunk<K, method>, object);
278 }
279
280 template<class K, void (K::*method)(int) const>
281 static void const_method_thunk (int revents, void* arg)
282 {
283 K *obj = static_cast<K *>(arg);
284 (obj->*method) (revents);
285 }
286
287 // simple method callback
288 template<class K, void (K::*method)()> 280 template<class K, void (K::*method)()>
289 void once (int fd, int events, tstamp timeout, K *object) throw () 281 void once (int fd, int events, tstamp timeout, K *object) throw ()
290 { 282 {
291 once (fd, events, timeout, method_noargs_thunk<K, method>, object); 283 once (fd, events, timeout, method_noargs_thunk<K, method>, object);
292 } 284 }
293 285
294 template<class K, void (K::*method)()> 286 template<class K, void (K::*method)()>
295 static void method_noargs_thunk (int revents, void* arg) 287 static void method_noargs_thunk (int revents, void *arg)
296 { 288 {
297 K *obj = static_cast<K *>(arg); 289 static_cast<K *>(arg)->*method
298 (obj->*method) (); 290 ();
299 } 291 }
300 292
301 // simpler function callback 293 // simpler function callback
302 template<void (*cb)(int)> 294 template<void (*cb)(int)>
303 void once (int fd, int events, tstamp timeout) throw () 295 void once (int fd, int events, tstamp timeout) throw ()
304 { 296 {
305 once (fd, events, timeout, simpler_func_thunk<cb>); 297 once (fd, events, timeout, simpler_func_thunk<cb>);
306 } 298 }
307 299
308 template<void (*cb)(int)> 300 template<void (*cb)(int)>
309 static void simpler_func_thunk (int revents, void* arg) 301 static void simpler_func_thunk (int revents, void *arg)
310 { 302 {
303 (*cb)
311 (*cb) (revents); 304 (revents);
312 } 305 }
313 306
314 // simplest function callback 307 // simplest function callback
315 template<void (*cb)()> 308 template<void (*cb)()>
316 void once (int fd, int events, tstamp timeout) throw () 309 void once (int fd, int events, tstamp timeout) throw ()
317 { 310 {
318 once (fd, events, timeout, simplest_func_thunk<cb>); 311 once (fd, events, timeout, simplest_func_thunk<cb>);
319 } 312 }
320 313
321 template<void (*cb)()> 314 template<void (*cb)()>
322 static void simplest_func_thunk (int revents, void* arg) 315 static void simplest_func_thunk (int revents, void *arg)
323 { 316 {
324 (*cb) (); 317 (*cb)
318 ();
325 } 319 }
326 320
327 void feed_fd_event (int fd, int revents) throw () 321 void feed_fd_event (int fd, int revents) throw ()
328 { 322 {
329 ev_feed_fd_event (EV_AX_ fd, revents); 323 ev_feed_fd_event (EV_AX_ fd, revents);
419 struct base : ev_watcher 413 struct base : ev_watcher
420 { 414 {
421 #if EV_MULTIPLICITY 415 #if EV_MULTIPLICITY
422 EV_PX; 416 EV_PX;
423 417
418 // loop set
424 void set (EV_PX) throw () 419 void set (EV_P) throw ()
425 { 420 {
426 this->EV_A = EV_A; 421 this->EV_A = EV_A;
427 } 422 }
428 #endif 423 #endif
429 424
433 #endif 428 #endif
434 { 429 {
435 ev_init (this, 0); 430 ev_init (this, 0);
436 } 431 }
437 432
438 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 ()
439 { 434 {
440 this->data = data; 435 this->data = (void *)data;
441 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);
442 } 451 }
443 452
444 // method callback 453 // method callback
445 template<class K, void (K::*method)(watcher &w, int)> 454 template<class K, void (K::*method)(watcher &w, int)>
446 void set (K *object) throw () 455 void set (K *object) throw ()
447 { 456 {
448 set_ (object, method_thunk<K, method>); 457 set_ (object, method_thunk<K, method>);
449 } 458 }
450 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
451 template<class K, void (K::*method)(watcher &w, int)> 467 template<class K, void (K::*method)(watcher &w, int)>
452 static void method_thunk (EV_P_ ev_watcher *w, int revents) 468 static void method_thunk (EV_P_ ev_watcher *w, int revents)
453 { 469 {
454 K *obj = static_cast<K *>(w->data); 470 (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); 471 (*static_cast<watcher *>(w), revents);
483 } 472 }
484 473
485 // simple callback 474 // no-argument callback
486 template<class K, void (K::*method)()> 475 template<class K, void (K::*method)()>
487 void set (K *object) throw () 476 void set (K *object) throw ()
488 { 477 {
489 set_ (object, method_noargs_thunk<K, method>); 478 set_ (object, method_noargs_thunk<K, method>);
490 } 479 }
491 480
492 template<class K, void (K::*method)()> 481 template<class K, void (K::*method)()>
493 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)
494 { 483 {
495 K *obj = static_cast<K *>(w->data); 484 (static_cast<K *>(w->data)->*method)
496 (obj->*method) (); 485 ();
497 } 486 }
498 487
499 void operator ()(int events = EV_UNDEF) 488 void operator ()(int events = EV_UNDEF)
500 { 489 {
490 return
501 return ev_cb (static_cast<ev_watcher *>(this)) 491 ev_cb (static_cast<ev_watcher *>(this))
502 (static_cast<ev_watcher *>(this), events); 492 (static_cast<ev_watcher *>(this), events);
503 } 493 }
504 494
505 bool is_active () const throw () 495 bool is_active () const throw ()
506 { 496 {
507 return ev_is_active (static_cast<const ev_watcher *>(this)); 497 return ev_is_active (static_cast<const ev_watcher *>(this));
653 643
654 void again () throw () 644 void again () throw ()
655 { 645 {
656 ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 646 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
657 } 647 }
648
649 ev_tstamp remaining ()
650 {
651 return ev_timer_remaining (EV_A_ static_cast<ev_timer *>(this));
652 }
658 EV_END_WATCHER (timer, timer) 653 EV_END_WATCHER (timer, timer)
659 654
660 #if EV_PERIODIC_ENABLE 655 #if EV_PERIODIC_ENABLE
661 EV_BEGIN_WATCHER (periodic, periodic) 656 EV_BEGIN_WATCHER (periodic, periodic)
662 void set (ev_tstamp at, ev_tstamp interval = 0.) throw () 657 void set (ev_tstamp at, ev_tstamp interval = 0.) throw ()
678 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); 673 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
679 } 674 }
680 EV_END_WATCHER (periodic, periodic) 675 EV_END_WATCHER (periodic, periodic)
681 #endif 676 #endif
682 677
678 #if EV_SIGNAL_ENABLE
683 EV_BEGIN_WATCHER (sig, signal) 679 EV_BEGIN_WATCHER (sig, signal)
684 void set (int signum) throw () 680 void set (int signum) throw ()
685 { 681 {
686 int active = is_active (); 682 int active = is_active ();
687 if (active) stop (); 683 if (active) stop ();
693 { 689 {
694 set (signum); 690 set (signum);
695 start (); 691 start ();
696 } 692 }
697 EV_END_WATCHER (sig, signal) 693 EV_END_WATCHER (sig, signal)
694 #endif
698 695
696 #if EV_CHILD_ENABLE
699 EV_BEGIN_WATCHER (child, child) 697 EV_BEGIN_WATCHER (child, child)
700 void set (int pid, int trace = 0) throw () 698 void set (int pid, int trace = 0) throw ()
701 { 699 {
702 int active = is_active (); 700 int active = is_active ();
703 if (active) stop (); 701 if (active) stop ();
709 { 707 {
710 set (pid, trace); 708 set (pid, trace);
711 start (); 709 start ();
712 } 710 }
713 EV_END_WATCHER (child, child) 711 EV_END_WATCHER (child, child)
712 #endif
714 713
715 #if EV_STAT_ENABLE 714 #if EV_STAT_ENABLE
716 EV_BEGIN_WATCHER (stat, stat) 715 EV_BEGIN_WATCHER (stat, stat)
717 void set (const char *path, ev_tstamp interval = 0.) throw () 716 void set (const char *path, ev_tstamp interval = 0.) throw ()
718 { 717 {
734 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); 733 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
735 } 734 }
736 EV_END_WATCHER (stat, stat) 735 EV_END_WATCHER (stat, stat)
737 #endif 736 #endif
738 737
738 #if EV_IDLE_ENABLE
739 EV_BEGIN_WATCHER (idle, idle) 739 EV_BEGIN_WATCHER (idle, idle)
740 void set () throw () { } 740 void set () throw () { }
741 EV_END_WATCHER (idle, idle) 741 EV_END_WATCHER (idle, idle)
742 #endif
742 743
744 #if EV_PREPARE_ENABLE
743 EV_BEGIN_WATCHER (prepare, prepare) 745 EV_BEGIN_WATCHER (prepare, prepare)
744 void set () throw () { } 746 void set () throw () { }
745 EV_END_WATCHER (prepare, prepare) 747 EV_END_WATCHER (prepare, prepare)
748 #endif
746 749
750 #if EV_CHECK_ENABLE
747 EV_BEGIN_WATCHER (check, check) 751 EV_BEGIN_WATCHER (check, check)
748 void set () throw () { } 752 void set () throw () { }
749 EV_END_WATCHER (check, check) 753 EV_END_WATCHER (check, check)
754 #endif
750 755
751 #if EV_EMBED_ENABLE 756 #if EV_EMBED_ENABLE
752 EV_BEGIN_WATCHER (embed, embed) 757 EV_BEGIN_WATCHER (embed, embed)
753 void set (struct ev_loop *embedded_loop) throw () 758 void set (struct ev_loop *embedded_loop) throw ()
754 { 759 {
777 EV_END_WATCHER (fork, fork) 782 EV_END_WATCHER (fork, fork)
778 #endif 783 #endif
779 784
780 #if EV_ASYNC_ENABLE 785 #if EV_ASYNC_ENABLE
781 EV_BEGIN_WATCHER (async, async) 786 EV_BEGIN_WATCHER (async, async)
782 void set () throw () { }
783
784 void send () throw () 787 void send () throw ()
785 { 788 {
786 ev_async_send (EV_A_ static_cast<ev_async *>(this)); 789 ev_async_send (EV_A_ static_cast<ev_async *>(this));
787 } 790 }
788 791

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines