2 * libev simple C++ wrapper classes
4 * Copyright (c) 2007,2008,2010 Marc Alexander Lehmann <libev@schmorp.de>
7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License ("GPL") version 2 or any later version,
30 * in which case the provisions of the GPL are applicable instead of
31 * the above. If you wish to allow the use of your version of this file
32 * only under the terms of the GPL and not to allow others to use your
33 * version of this file under the BSD license, indicate your decision
34 * by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL.
49 #ifndef EV_USE_STDEXCEPT
50 # define EV_USE_STDEXCEPT 1
59 typedef ev_tstamp tstamp;
66 EXCEPTION= EV_EXCEPTION,
71 PERIODIC = EV_PERIODIC,
81 # undef ERROR // some systems stupidly #define ERROR
89 FORKCHECK = EVFLAG_FORKCHECK,
91 SELECT = EVBACKEND_SELECT,
92 POLL = EVBACKEND_POLL,
93 EPOLL = EVBACKEND_EPOLL,
94 KQUEUE = EVBACKEND_KQUEUE,
95 DEVPOLL = EVBACKEND_DEVPOLL,
102 NONBLOCK = EVLOOP_NONBLOCK,
103 ONESHOT = EVLOOP_ONESHOT,
105 NOWAIT = EVRUN_NOWAIT,
122 : std::runtime_error ("libev event loop cannot be initialized, bad value of LIBEV_FLAGS?")
137 # define EV_AX raw_loop
138 # define EV_AX_ raw_loop,
146 loop_ref (EV_P) throw ()
153 bool operator == (const loop_ref &other) const throw ()
156 return EV_AX == other.EV_AX;
162 bool operator != (const loop_ref &other) const throw ()
165 return ! (*this == other);
172 bool operator == (const EV_P) const throw ()
174 return this->EV_AX == EV_A;
177 bool operator != (const EV_P) const throw ()
179 return (*this == EV_A);
182 operator struct ev_loop * () const throw ()
187 operator const struct ev_loop * () const throw ()
192 bool is_default () const throw ()
194 return EV_AX == ev_default_loop (0);
199 void loop (int flags = 0)
201 ev_run (EV_AX_ flags);
204 void unloop (how_t how = ONE) throw ()
206 ev_break (EV_AX_ how);
210 void run (int flags = 0)
212 ev_run (EV_AX_ flags);
215 void break_loop (how_t how = ONE) throw ()
217 ev_break (EV_AX_ how);
220 void post_fork () throw ()
222 ev_loop_fork (EV_AX);
225 unsigned int backend () const throw ()
227 return ev_backend (EV_AX);
230 tstamp now () const throw ()
232 return ev_now (EV_AX);
240 void unref () throw ()
246 unsigned int iteration () const throw ()
248 return ev_iteration (EV_AX);
251 unsigned int depth () const throw ()
253 return ev_depth (EV_AX);
256 void set_io_collect_interval (tstamp interval) throw ()
258 ev_set_io_collect_interval (EV_AX_ interval);
261 void set_timeout_collect_interval (tstamp interval) throw ()
263 ev_set_timeout_collect_interval (EV_AX_ interval);
268 void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void *arg = 0) throw ()
270 ev_once (EV_AX_ fd, events, timeout, cb, arg);
274 template<class K, void (K::*method)(int)>
275 void once (int fd, int events, tstamp timeout, K *object) throw ()
277 once (fd, events, timeout, method_thunk<K, method>, object);
280 // default method == operator ()
282 void once (int fd, int events, tstamp timeout, K *object) throw ()
284 once (fd, events, timeout, method_thunk<K, &K::operator ()>, object);
287 template<class K, void (K::*method)(int)>
288 static void method_thunk (int revents, void *arg)
290 (static_cast<K *>(arg)->*method)
294 // no-argument method callback
295 template<class K, void (K::*method)()>
296 void once (int fd, int events, tstamp timeout, K *object) throw ()
298 once (fd, events, timeout, method_noargs_thunk<K, method>, object);
301 template<class K, void (K::*method)()>
302 static void method_noargs_thunk (int revents, void *arg)
304 (static_cast<K *>(arg)->*method)
308 // simpler function callback
309 template<void (*cb)(int)>
310 void once (int fd, int events, tstamp timeout) throw ()
312 once (fd, events, timeout, simpler_func_thunk<cb>);
315 template<void (*cb)(int)>
316 static void simpler_func_thunk (int revents, void *arg)
322 // simplest function callback
323 template<void (*cb)()>
324 void once (int fd, int events, tstamp timeout) throw ()
326 once (fd, events, timeout, simplest_func_thunk<cb>);
329 template<void (*cb)()>
330 static void simplest_func_thunk (int revents, void *arg)
336 void feed_fd_event (int fd, int revents) throw ()
338 ev_feed_fd_event (EV_AX_ fd, revents);
341 void feed_signal_event (int signum) throw ()
343 ev_feed_signal_event (EV_AX_ signum);
347 struct ev_loop* EV_AX;
353 struct dynamic_loop : loop_ref
356 dynamic_loop (unsigned int flags = AUTO) throw (bad_loop)
357 : loop_ref (ev_loop_new (flags))
363 ~dynamic_loop () throw ()
365 ev_loop_destroy (EV_AX);
371 dynamic_loop (const dynamic_loop &);
373 dynamic_loop & operator= (const dynamic_loop &);
378 struct default_loop : loop_ref
380 default_loop (unsigned int flags = AUTO) throw (bad_loop)
382 : loop_ref (ev_default_loop (flags))
389 !ev_default_loop (flags)
396 default_loop (const default_loop &);
397 default_loop &operator = (const default_loop &);
400 inline loop_ref get_default_loop () throw ()
403 return ev_default_loop (0);
415 # define EV_PX loop_ref EV_A
416 # define EV_PX_ loop_ref EV_A_
422 template<class ev_watcher, class watcher>
423 struct base : ev_watcher
429 void set (EV_P) throw ()
435 base (EV_PX) throw ()
443 void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw ()
445 this->data = (void *)data;
446 ev_set_cb (static_cast<ev_watcher *>(this), cb);
450 template<void (*function)(watcher &w, int)>
451 void set (void *data = 0) throw ()
453 set_ (data, function_thunk<function>);
456 template<void (*function)(watcher &w, int)>
457 static void function_thunk (EV_P_ ev_watcher *w, int revents)
460 (*static_cast<watcher *>(w), revents);
464 template<class K, void (K::*method)(watcher &w, int)>
465 void set (K *object) throw ()
467 set_ (object, method_thunk<K, method>);
470 // default method == operator ()
472 void set (K *object) throw ()
474 set_ (object, method_thunk<K, &K::operator ()>);
477 template<class K, void (K::*method)(watcher &w, int)>
478 static void method_thunk (EV_P_ ev_watcher *w, int revents)
480 (static_cast<K *>(w->data)->*method)
481 (*static_cast<watcher *>(w), revents);
484 // no-argument callback
485 template<class K, void (K::*method)()>
486 void set (K *object) throw ()
488 set_ (object, method_noargs_thunk<K, method>);
491 template<class K, void (K::*method)()>
492 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
494 (static_cast<K *>(w->data)->*method)
498 void operator ()(int events = EV_UNDEF)
501 ev_cb (static_cast<ev_watcher *>(this))
502 (static_cast<ev_watcher *>(this), events);
505 bool is_active () const throw ()
507 return ev_is_active (static_cast<const ev_watcher *>(this));
510 bool is_pending () const throw ()
512 return ev_is_pending (static_cast<const ev_watcher *>(this));
515 void feed_event (int revents) throw ()
517 ev_feed_event (EV_A_ static_cast<ev_watcher *>(this), revents);
521 inline tstamp now (EV_P) throw ()
523 return ev_now (EV_A);
526 inline void delay (tstamp interval) throw ()
531 inline int version_major () throw ()
533 return ev_version_major ();
536 inline int version_minor () throw ()
538 return ev_version_minor ();
541 inline unsigned int supported_backends () throw ()
543 return ev_supported_backends ();
546 inline unsigned int recommended_backends () throw ()
548 return ev_recommended_backends ();
551 inline unsigned int embeddable_backends () throw ()
553 return ev_embeddable_backends ();
556 inline void set_allocator (void *(*cb)(void *ptr, long size) throw ()) throw ()
558 ev_set_allocator (cb);
561 inline void set_syserr_cb (void (*cb)(const char *msg) throw ()) throw ()
563 ev_set_syserr_cb (cb);
567 #define EV_CONSTRUCT(cppstem,cstem) \
568 (EV_PX = get_default_loop ()) throw () \
569 : base<ev_ ## cstem, cppstem> (EV_A) \
573 #define EV_CONSTRUCT(cppstem,cstem) \
579 /* using a template here would require quite a few more lines,
580 * so a macro solution was chosen */
581 #define EV_BEGIN_WATCHER(cppstem,cstem) \
583 struct cppstem : base<ev_ ## cstem, cppstem> \
585 void start () throw () \
587 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
590 void stop () throw () \
592 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
595 cppstem EV_CONSTRUCT(cppstem,cstem) \
597 ~cppstem () throw () \
602 using base<ev_ ## cstem, cppstem>::set; \
606 cppstem (const cppstem &o); \
608 cppstem &operator =(const cppstem &o); \
612 #define EV_END_WATCHER(cppstem,cstem) \
615 EV_BEGIN_WATCHER (io, io)
616 void set (int fd, int events) throw ()
618 int active = is_active ();
620 ev_io_set (static_cast<ev_io *>(this), fd, events);
621 if (active) start ();
624 void set (int events) throw ()
626 int active = is_active ();
628 ev_io_set (static_cast<ev_io *>(this), fd, events);
629 if (active) start ();
632 void start (int fd, int events) throw ()
637 EV_END_WATCHER (io, io)
639 EV_BEGIN_WATCHER (timer, timer)
640 void set (ev_tstamp after, ev_tstamp repeat = 0.) throw ()
642 int active = is_active ();
644 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
645 if (active) start ();
648 void start (ev_tstamp after, ev_tstamp repeat = 0.) throw ()
654 void again () throw ()
656 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
659 ev_tstamp remaining ()
661 return ev_timer_remaining (EV_A_ static_cast<ev_timer *>(this));
663 EV_END_WATCHER (timer, timer)
665 #if EV_PERIODIC_ENABLE
666 EV_BEGIN_WATCHER (periodic, periodic)
667 void set (ev_tstamp at, ev_tstamp interval = 0.) throw ()
669 int active = is_active ();
671 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
672 if (active) start ();
675 void start (ev_tstamp at, ev_tstamp interval = 0.) throw ()
681 void again () throw ()
683 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
685 EV_END_WATCHER (periodic, periodic)
689 EV_BEGIN_WATCHER (sig, signal)
690 void set (int signum) throw ()
692 int active = is_active ();
694 ev_signal_set (static_cast<ev_signal *>(this), signum);
695 if (active) start ();
698 void start (int signum) throw ()
703 EV_END_WATCHER (sig, signal)
707 EV_BEGIN_WATCHER (child, child)
708 void set (int pid, int trace = 0) throw ()
710 int active = is_active ();
712 ev_child_set (static_cast<ev_child *>(this), pid, trace);
713 if (active) start ();
716 void start (int pid, int trace = 0) throw ()
721 EV_END_WATCHER (child, child)
725 EV_BEGIN_WATCHER (stat, stat)
726 void set (const char *path, ev_tstamp interval = 0.) throw ()
728 int active = is_active ();
730 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
731 if (active) start ();
734 void start (const char *path, ev_tstamp interval = 0.) throw ()
737 set (path, interval);
741 void update () throw ()
743 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
745 EV_END_WATCHER (stat, stat)
749 EV_BEGIN_WATCHER (idle, idle)
750 void set () throw () { }
751 EV_END_WATCHER (idle, idle)
754 #if EV_PREPARE_ENABLE
755 EV_BEGIN_WATCHER (prepare, prepare)
756 void set () throw () { }
757 EV_END_WATCHER (prepare, prepare)
761 EV_BEGIN_WATCHER (check, check)
762 void set () throw () { }
763 EV_END_WATCHER (check, check)
767 EV_BEGIN_WATCHER (embed, embed)
768 void set_embed (struct ev_loop *embedded_loop) throw ()
770 int active = is_active ();
772 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
773 if (active) start ();
776 void start (struct ev_loop *embedded_loop) throw ()
784 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
786 EV_END_WATCHER (embed, embed)
790 EV_BEGIN_WATCHER (fork, fork)
791 void set () throw () { }
792 EV_END_WATCHER (fork, fork)
796 EV_BEGIN_WATCHER (async, async)
797 void send () throw ()
799 ev_async_send (EV_A_ static_cast<ev_async *>(this));
802 bool async_pending () throw ()
804 return ev_async_pending (static_cast<ev_async *>(this));
806 EV_END_WATCHER (async, async)
812 #undef EV_BEGIN_WATCHER
813 #undef EV_END_WATCHER