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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines