2 * libev simple C++ wrapper classes
4 * Copyright (c) 2007 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;
67 PERIODIC = EV_PERIODIC,
83 FORKCHECK = EVFLAG_FORKCHECK,
84 SELECT = EVBACKEND_SELECT,
85 POLL = EVBACKEND_POLL,
86 EPOLL = EVBACKEND_EPOLL,
87 KQUEUE = EVBACKEND_KQUEUE,
88 DEVPOLL = EVBACKEND_DEVPOLL,
94 NONBLOCK = EVLOOP_NONBLOCK,
95 ONESHOT = EVLOOP_ONESHOT
111 : std::runtime_error ("libev event loop cannot be initialized, bad value of LIBEV_FLAGS?")
126 # define EV_AX raw_loop
127 # define EV_AX_ raw_loop,
136 loop_ref (EV_P) throw (bad_loop)
147 bool operator == (const loop_ref &other) const throw ()
150 return EV_AX == other.EV_AX;
156 bool operator != (const loop_ref &other) const throw ()
159 return ! (*this == other);
166 bool operator== (struct ev_loop *other) const throw ()
168 return this->EV_AX == other;
171 bool operator!= (struct ev_loop *other) const throw ()
173 return ! (*this == other);
176 bool operator== (const struct ev_loop *other) const throw ()
178 return this->EV_AX == other;
181 bool operator!= (const struct ev_loop *other) const throw ()
183 return (*this == other);
186 operator struct ev_loop * () const throw ()
191 operator const struct ev_loop * () const throw ()
196 bool is_default () const throw ()
198 return EV_AX == ev_default_loop (0);
202 void loop (int flags = 0)
204 ev_loop (EV_AX_ flags);
207 void unloop (how_t how = ONE) throw ()
209 ev_unloop (EV_AX_ how);
212 void post_fork () throw ()
215 ev_loop_fork (EV_AX);
221 unsigned int count () const throw ()
223 return ev_loop_count (EV_AX);
226 unsigned int backend () const throw ()
228 return ev_backend (EV_AX);
231 tstamp now () const throw ()
233 return ev_now (EV_AX);
241 void unref () throw ()
246 void set_io_collect_interval (tstamp interval) throw ()
248 ev_set_io_collect_interval (EV_AX_ interval);
251 void set_timeout_collect_interval (tstamp interval) throw ()
253 ev_set_timeout_collect_interval (EV_AX_ interval);
257 void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void* arg = 0) throw ()
259 ev_once (EV_AX_ fd, events, timeout, cb, arg);
263 template<class K, void (K::*method)(int)>
264 void once (int fd, int events, tstamp timeout, K *object) throw ()
266 once (fd, events, timeout, method_thunk<K, method>, object);
269 template<class K, void (K::*method)(int)>
270 static void method_thunk (int revents, void* arg)
272 K *obj = static_cast<K *>(arg);
273 (obj->*method) (revents);
276 // const method callback
277 template<class K, void (K::*method)(int) const>
278 void once (int fd, int events, tstamp timeout, const K *object) throw ()
280 once (fd, events, timeout, const_method_thunk<K, method>, object);
283 template<class K, void (K::*method)(int) const>
284 static void const_method_thunk (int revents, void* arg)
286 K *obj = static_cast<K *>(arg);
287 (obj->*method) (revents);
290 // simple method callback
291 template<class K, void (K::*method)()>
292 void once (int fd, int events, tstamp timeout, K *object) throw ()
294 once (fd, events, timeout, method_noargs_thunk<K, method>, object);
297 template<class K, void (K::*method)()>
298 static void method_noargs_thunk (int revents, void* arg)
300 K *obj = static_cast<K *>(arg);
304 // simpler function callback
305 template<void (*cb)(int)>
306 void once (int fd, int events, tstamp timeout) throw ()
308 once (fd, events, timeout, simpler_func_thunk<cb>);
311 template<void (*cb)(int)>
312 static void simpler_func_thunk (int revents, void* arg)
317 // simplest function callback
318 template<void (*cb)()>
319 void once (int fd, int events, tstamp timeout) throw ()
321 once (fd, events, timeout, simplest_func_thunk<cb>);
324 template<void (*cb)()>
325 static void simplest_func_thunk (int revents, void* arg)
330 void feed_fd_event (int fd, int revents) throw ()
332 ev_feed_fd_event (EV_AX_ fd, revents);
335 void feed_signal_event (int signum) throw ()
337 ev_feed_signal_event (EV_AX_ signum);
341 struct ev_loop* EV_AX;
347 struct dynamic_loop: loop_ref
350 dynamic_loop (unsigned int flags = AUTO) EV_THROW (bad_loop)
351 : loop_ref (ev_loop_new (flags))
355 ~dynamic_loop () throw ()
357 ev_loop_destroy (EV_AX);
363 dynamic_loop (const dynamic_loop &);
365 dynamic_loop & operator= (const dynamic_loop &);
370 struct default_loop: loop_ref
373 default_loop (unsigned int flags = AUTO) throw (bad_loop)
375 : loop_ref (ev_default_loop (flags))
378 #ifndef EV_MULTIPLICITY
379 if (!ev_default_loop (flags))
384 ~default_loop () throw ()
386 ev_default_destroy ();
390 default_loop (const default_loop &);
391 default_loop &operator = (const default_loop &);
394 inline loop_ref get_default_loop () throw ()
397 return ev_default_loop (0);
409 # define EV_PX loop_ref EV_A
410 # define EV_PX_ loop_ref EV_A_
416 template<class ev_watcher, class watcher>
417 struct base : ev_watcher
422 void set (EV_PX) throw ()
428 base (EV_PX) throw ()
436 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw ()
439 ev_set_cb (static_cast<ev_watcher *>(this), cb);
443 template<class K, void (K::*method)(watcher &w, int)>
444 void set (K *object) throw ()
446 set_ (object, method_thunk<K, method>);
449 template<class K, void (K::*method)(watcher &w, int)>
450 static void method_thunk (EV_P_ ev_watcher *w, int revents)
452 K *obj = static_cast<K *>(w->data);
453 (obj->*method) (*static_cast<watcher *>(w), revents);
456 // const method callback
457 template<class K, void (K::*method)(watcher &w, int) const>
458 void set (const K *object) throw ()
460 set_ (object, const_method_thunk<K, method>);
463 template<class K, void (K::*method)(watcher &w, int) const>
464 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
466 K *obj = static_cast<K *>(w->data);
467 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
471 template<void (*function)(watcher &w, int)>
472 void set (void *data = 0) throw ()
474 set_ (data, function_thunk<function>);
477 template<void (*function)(watcher &w, int)>
478 static void function_thunk (EV_P_ ev_watcher *w, int revents)
480 function (*static_cast<watcher *>(w), revents);
484 template<class K, void (K::*method)()>
485 void set (K *object) throw ()
487 set_ (object, method_noargs_thunk<K, method>);
490 template<class K, void (K::*method)()>
491 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
493 K *obj = static_cast<K *>(w->data);
497 void operator ()(int events = EV_UNDEF)
499 return ev_cb (static_cast<ev_watcher *>(this))
500 (static_cast<ev_watcher *>(this), events);
503 bool is_active () const throw ()
505 return ev_is_active (static_cast<const ev_watcher *>(this));
508 bool is_pending () const throw ()
510 return ev_is_pending (static_cast<const ev_watcher *>(this));
513 void feed_event (int revents) throw ()
515 ev_feed_event (EV_A_ static_cast<const ev_watcher *>(this), revents);
519 inline tstamp now () throw ()
524 inline void delay (tstamp interval) throw ()
529 inline int version_major () throw ()
531 return ev_version_major ();
534 inline int version_minor () throw ()
536 return ev_version_minor ();
539 inline unsigned int supported_backends () throw ()
541 return ev_supported_backends ();
544 inline unsigned int recommended_backends () throw ()
546 return ev_recommended_backends ();
549 inline unsigned int embeddable_backends () throw ()
551 return ev_embeddable_backends ();
554 inline void set_allocator (void *(*cb)(void *ptr, long size)) throw ()
556 ev_set_allocator (cb);
559 inline void set_syserr_cb (void (*cb)(const char *msg)) throw ()
561 ev_set_syserr_cb (cb);
565 #define EV_CONSTRUCT(cppstem,cstem) \
566 (EV_PX = get_default_loop ()) throw () \
567 : base<ev_ ## cstem, cppstem> (EV_A) \
571 #define EV_CONSTRUCT(cppstem,cstem) \
577 /* using a template here would require quite a bit more lines,
578 * so a macro solution was chosen */
579 #define EV_BEGIN_WATCHER(cppstem,cstem) \
581 struct cppstem : base<ev_ ## cstem, cppstem> \
583 void start () throw () \
585 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
588 void stop () throw () \
590 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
593 cppstem EV_CONSTRUCT(cppstem,cstem) \
595 ~cppstem () throw () \
600 using base<ev_ ## cstem, cppstem>::set; \
604 cppstem (const cppstem &o); \
606 cppstem & operator =(const cppstem &o); \
610 #define EV_END_WATCHER(cppstem,cstem) \
613 EV_BEGIN_WATCHER (io, io)
614 void set (int fd, int events) throw ()
616 int active = is_active ();
618 ev_io_set (static_cast<ev_io *>(this), fd, events);
619 if (active) start ();
622 void set (int events) throw ()
624 int active = is_active ();
626 ev_io_set (static_cast<ev_io *>(this), fd, events);
627 if (active) start ();
630 void start (int fd, int events) throw ()
635 EV_END_WATCHER (io, io)
637 EV_BEGIN_WATCHER (timer, timer)
638 void set (ev_tstamp after, ev_tstamp repeat = 0.) throw ()
640 int active = is_active ();
642 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
643 if (active) start ();
646 void start (ev_tstamp after, ev_tstamp repeat = 0.) throw ()
652 void again () throw ()
654 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
656 EV_END_WATCHER (timer, timer)
658 #if EV_PERIODIC_ENABLE
659 EV_BEGIN_WATCHER (periodic, periodic)
660 void set (ev_tstamp at, ev_tstamp interval = 0.) throw ()
662 int active = is_active ();
664 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
665 if (active) start ();
668 void start (ev_tstamp at, ev_tstamp interval = 0.) throw ()
674 void again () throw ()
676 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
678 EV_END_WATCHER (periodic, periodic)
681 EV_BEGIN_WATCHER (sig, signal)
682 void set (int signum) throw ()
684 int active = is_active ();
686 ev_signal_set (static_cast<ev_signal *>(this), signum);
687 if (active) start ();
690 void start (int signum) throw ()
695 EV_END_WATCHER (sig, signal)
697 EV_BEGIN_WATCHER (child, child)
698 void set (int pid) throw ()
700 int active = is_active ();
702 ev_child_set (static_cast<ev_child *>(this), pid);
703 if (active) start ();
706 void start (int pid) throw ()
711 EV_END_WATCHER (child, child)
714 EV_BEGIN_WATCHER (stat, stat)
715 void set (const char *path, ev_tstamp interval = 0.) throw ()
717 int active = is_active ();
719 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
720 if (active) start ();
723 void start (const char *path, ev_tstamp interval = 0.) throw ()
726 set (path, interval);
730 void update () throw ()
732 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
734 EV_END_WATCHER (stat, stat)
737 EV_BEGIN_WATCHER (idle, idle)
738 void set () throw () { }
739 EV_END_WATCHER (idle, idle)
741 EV_BEGIN_WATCHER (prepare, prepare)
742 void set () throw () { }
743 EV_END_WATCHER (prepare, prepare)
745 EV_BEGIN_WATCHER (check, check)
746 void set () throw () { }
747 EV_END_WATCHER (check, check)
750 EV_BEGIN_WATCHER (embed, embed)
751 void start (struct ev_loop *embedded_loop) throw ()
754 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
760 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
762 EV_END_WATCHER (embed, embed)
766 EV_BEGIN_WATCHER (fork, fork)
767 void set () throw () { }
768 EV_END_WATCHER (fork, fork)
774 #undef EV_BEGIN_WATCHER
775 #undef EV_END_WATCHER