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.
51 typedef ev_tstamp tstamp;
59 PERIODIC = EV_PERIODIC,
75 FORKCHECK = EVFLAG_FORKCHECK,
76 SELECT = EVBACKEND_SELECT,
77 POLL = EVBACKEND_POLL,
78 EPOLL = EVBACKEND_EPOLL,
79 KQUEUE = EVBACKEND_KQUEUE,
80 DEVPOLL = EVBACKEND_DEVPOLL,
86 NONBLOCK = EVLOOP_NONBLOCK,
87 ONESHOT = EVLOOP_ONESHOT
105 # define EV_AX raw_loop
106 # define EV_AX_ raw_loop,
122 bool operator== (const loop_ref &other) const
125 return this->EV_AX == other.EV_AX;
131 bool operator!= (const loop_ref &other) const
134 return ! (*this == other);
141 bool operator== (struct ev_loop *other) const
143 return this->EV_AX == other;
146 bool operator!= (struct ev_loop *other) const
148 return ! (*this == other);
151 bool operator== (const struct ev_loop *other) const
153 return this->EV_AX == other;
156 bool operator!= (const struct ev_loop *other) const
158 return (*this == other);
161 operator struct ev_loop * () const
166 operator const struct ev_loop * () const
171 bool is_default () const
173 return EV_AX == ev_default_loop (0);
177 void loop (int flags = 0)
179 ev_loop (EV_AX_ flags);
182 void unloop (how_t how = ONE)
184 ev_unloop (EV_AX_ how);
190 ev_loop_fork (EV_AX);
196 unsigned int count () const
198 return ev_loop_count (EV_AX);
201 unsigned int backend () const
203 return ev_backend (EV_AX);
208 return ev_now (EV_AX);
221 void set_io_collect_interval (tstamp interval)
223 ev_set_io_collect_interval (EV_AX_ interval);
226 void set_timeout_collect_interval (tstamp interval)
228 ev_set_timeout_collect_interval (EV_AX_ interval);
232 void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void* arg = 0)
234 ev_once (EV_AX_ fd, events, timeout, cb, arg);
238 template<class K, void (K::*method)(int)>
239 void once (int fd, int events, tstamp timeout, K *object)
241 once (fd, events, timeout, method_thunk<K, method>, object);
244 template<class K, void (K::*method)(int)>
245 static void method_thunk (int revents, void* arg)
247 K *obj = static_cast<K *>(arg);
248 (obj->*method) (revents);
251 // const method callback
252 template<class K, void (K::*method)(int) const>
253 void once (int fd, int events, tstamp timeout, const K *object)
255 once (fd, events, timeout, const_method_thunk<K, method>, object);
258 template<class K, void (K::*method)(int) const>
259 static void const_method_thunk (int revents, void* arg)
261 K *obj = static_cast<K *>(arg);
262 (obj->*method) (revents);
265 // simple method callback
266 template<class K, void (K::*method)()>
267 void once (int fd, int events, tstamp timeout, K *object)
269 once (fd, events, timeout, method_noargs_thunk<K, method>, object);
272 template<class K, void (K::*method)()>
273 static void method_noargs_thunk (int revents, void* arg)
275 K *obj = static_cast<K *>(arg);
279 // simpler function callback
280 template<void (*cb)(int)>
281 void once (int fd, int events, tstamp timeout)
283 once (fd, events, timeout, simpler_func_thunk<cb>);
286 template<void (*cb)(int)>
287 static void simpler_func_thunk (int revents, void* arg)
292 // simplest function callback
293 template<void (*cb)()>
294 void once (int fd, int events, tstamp timeout)
296 once (fd, events, timeout, simplest_func_thunk<cb>);
299 template<void (*cb)()>
300 static void simplest_func_thunk (int revents, void* arg)
305 void feed_fd_event (int fd, int revents)
307 ev_feed_fd_event (EV_AX_ fd, revents);
310 void feed_signal_event (int signum)
312 ev_feed_signal_event (EV_AX_ signum);
316 struct ev_loop* EV_AX;
322 struct dynamic_loop: loop_ref
325 dynamic_loop (unsigned int flags = AUTO)
326 : loop_ref (ev_loop_new (flags))
332 ev_loop_destroy (EV_AX);
338 dynamic_loop (const dynamic_loop &);
340 dynamic_loop & operator= (const dynamic_loop &);
345 struct default_loop: loop_ref
348 default_loop (unsigned int flags = AUTO)
350 : loop_ref (ev_default_loop (flags))
355 ev_default_loop (flags);
361 ev_default_destroy ();
369 default_loop (const default_loop &);
371 default_loop & operator= (const default_loop &);
375 inline loop_ref get_default_loop ()
378 return ev_default_loop (0);
390 # define EV_PX loop_ref EV_A
391 # define EV_PX_ loop_ref EV_A_
397 template<class ev_watcher, class watcher>
398 struct base : ev_watcher
417 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
420 ev_set_cb (static_cast<ev_watcher *>(this), cb);
424 template<class K, void (K::*method)(watcher &w, int)>
427 set_ (object, method_thunk<K, method>);
430 template<class K, void (K::*method)(watcher &w, int)>
431 static void method_thunk (EV_P_ ev_watcher *w, int revents)
433 K *obj = static_cast<K *>(w->data);
434 (obj->*method) (*static_cast<watcher *>(w), revents);
437 // const method callback
438 template<class K, void (K::*method)(watcher &w, int) const>
439 void set (const K *object)
441 set_ (object, const_method_thunk<K, method>);
444 template<class K, void (K::*method)(watcher &w, int) const>
445 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
447 K *obj = static_cast<K *>(w->data);
448 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
452 template<void (*function)(watcher &w, int)>
453 void set (void *data = 0)
455 set_ (data, function_thunk<function>);
458 template<void (*function)(watcher &w, int)>
459 static void function_thunk (EV_P_ ev_watcher *w, int revents)
461 function (*static_cast<watcher *>(w), revents);
465 template<class K, void (K::*method)()>
468 set_ (object, method_noargs_thunk<K, method>);
471 template<class K, void (K::*method)()>
472 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
474 K *obj = static_cast<K *>(w->data);
478 void operator ()(int events = EV_UNDEF)
480 return ev_cb (static_cast<ev_watcher *>(this))
481 (static_cast<ev_watcher *>(this), events);
484 bool is_active () const
486 return ev_is_active (static_cast<const ev_watcher *>(this));
489 bool is_pending () const
491 return ev_is_pending (static_cast<const ev_watcher *>(this));
494 void feed_event (int revents)
496 ev_feed_event (EV_A_ static_cast<const ev_watcher *>(this), revents);
500 inline void delay (tstamp interval)
505 inline int version_major ()
507 return ev_version_major ();
510 inline int version_minor ()
512 return ev_version_minor ();
515 inline unsigned int supported_backends ()
517 return ev_supported_backends ();
520 inline unsigned int recommended_backends ()
522 return ev_recommended_backends ();
525 inline unsigned int embeddable_backends ()
527 return ev_embeddable_backends ();
530 inline void set_allocator (void *(*cb)(void *ptr, long size))
532 ev_set_allocator (cb);
535 inline void set_syserr_cb (void (*cb)(const char *msg))
537 ev_set_syserr_cb (cb);
541 #define EV_CONSTRUCT(cppstem,cstem) \
542 (EV_PX = get_default_loop ()) \
543 : base<ev_ ## cstem, cppstem> (EV_A) \
547 #define EV_CONSTRUCT(cppstem,cstem) \
553 /* using a template here would require quite a bit more lines,
554 * so a macro solution was chosen */
555 #define EV_BEGIN_WATCHER(cppstem,cstem) \
557 struct cppstem : base<ev_ ## cstem, cppstem> \
561 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
566 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
569 cppstem EV_CONSTRUCT(cppstem,cstem) \
576 using base<ev_ ## cstem, cppstem>::set; \
580 cppstem (const cppstem &o); \
582 cppstem & operator =(const cppstem &o); \
586 #define EV_END_WATCHER(cppstem,cstem) \
589 EV_BEGIN_WATCHER (io, io)
590 void set (int fd, int events)
592 int active = is_active ();
594 ev_io_set (static_cast<ev_io *>(this), fd, events);
595 if (active) start ();
598 void set (int events)
600 int active = is_active ();
602 ev_io_set (static_cast<ev_io *>(this), fd, events);
603 if (active) start ();
606 void start (int fd, int events)
611 EV_END_WATCHER (io, io)
613 EV_BEGIN_WATCHER (timer, timer)
614 void set (ev_tstamp after, ev_tstamp repeat = 0.)
616 int active = is_active ();
618 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
619 if (active) start ();
622 void start (ev_tstamp after, ev_tstamp repeat = 0.)
630 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
632 EV_END_WATCHER (timer, timer)
634 #if EV_PERIODIC_ENABLE
635 EV_BEGIN_WATCHER (periodic, periodic)
636 void set (ev_tstamp at, ev_tstamp interval = 0.)
638 int active = is_active ();
640 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
641 if (active) start ();
644 void start (ev_tstamp at, ev_tstamp interval = 0.)
652 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
654 EV_END_WATCHER (periodic, periodic)
657 EV_BEGIN_WATCHER (sig, signal)
658 void set (int signum)
660 int active = is_active ();
662 ev_signal_set (static_cast<ev_signal *>(this), signum);
663 if (active) start ();
666 void start (int signum)
671 EV_END_WATCHER (sig, signal)
673 EV_BEGIN_WATCHER (child, child)
676 int active = is_active ();
678 ev_child_set (static_cast<ev_child *>(this), pid);
679 if (active) start ();
687 EV_END_WATCHER (child, child)
690 EV_BEGIN_WATCHER (stat, stat)
691 void set (const char *path, ev_tstamp interval = 0.)
693 int active = is_active ();
695 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
696 if (active) start ();
699 void start (const char *path, ev_tstamp interval = 0.)
702 set (path, interval);
708 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
710 EV_END_WATCHER (stat, stat)
713 EV_BEGIN_WATCHER (idle, idle)
715 EV_END_WATCHER (idle, idle)
717 EV_BEGIN_WATCHER (prepare, prepare)
719 EV_END_WATCHER (prepare, prepare)
721 EV_BEGIN_WATCHER (check, check)
723 EV_END_WATCHER (check, check)
726 EV_BEGIN_WATCHER (embed, embed)
727 void start (struct ev_loop *embedded_loop)
730 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
736 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
738 EV_END_WATCHER (embed, embed)
742 EV_BEGIN_WATCHER (fork, fork)
744 EV_END_WATCHER (fork, fork)
750 #undef EV_BEGIN_WATCHER
751 #undef EV_END_WATCHER