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);
505 inline void delay (tstamp interval)
510 inline int version_major ()
512 return ev_version_major ();
515 inline int version_minor ()
517 return ev_version_minor ();
520 inline unsigned int supported_backends ()
522 return ev_supported_backends ();
525 inline unsigned int recommended_backends ()
527 return ev_recommended_backends ();
530 inline unsigned int embeddable_backends ()
532 return ev_embeddable_backends ();
535 inline void set_allocator (void *(*cb)(void *ptr, long size))
537 ev_set_allocator (cb);
540 inline void set_syserr_cb (void (*cb)(const char *msg))
542 ev_set_syserr_cb (cb);
546 #define EV_CONSTRUCT(cppstem,cstem) \
547 (EV_PX = get_default_loop ()) \
548 : base<ev_ ## cstem, cppstem> (EV_A) \
552 #define EV_CONSTRUCT(cppstem,cstem) \
558 /* using a template here would require quite a bit more lines,
559 * so a macro solution was chosen */
560 #define EV_BEGIN_WATCHER(cppstem,cstem) \
562 struct cppstem : base<ev_ ## cstem, cppstem> \
566 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
571 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
574 cppstem EV_CONSTRUCT(cppstem,cstem) \
581 using base<ev_ ## cstem, cppstem>::set; \
585 cppstem (const cppstem &o); \
587 cppstem & operator =(const cppstem &o); \
591 #define EV_END_WATCHER(cppstem,cstem) \
594 EV_BEGIN_WATCHER (io, io)
595 void set (int fd, int events)
597 int active = is_active ();
599 ev_io_set (static_cast<ev_io *>(this), fd, events);
600 if (active) start ();
603 void set (int events)
605 int active = is_active ();
607 ev_io_set (static_cast<ev_io *>(this), fd, events);
608 if (active) start ();
611 void start (int fd, int events)
616 EV_END_WATCHER (io, io)
618 EV_BEGIN_WATCHER (timer, timer)
619 void set (ev_tstamp after, ev_tstamp repeat = 0.)
621 int active = is_active ();
623 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
624 if (active) start ();
627 void start (ev_tstamp after, ev_tstamp repeat = 0.)
635 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
637 EV_END_WATCHER (timer, timer)
639 #if EV_PERIODIC_ENABLE
640 EV_BEGIN_WATCHER (periodic, periodic)
641 void set (ev_tstamp at, ev_tstamp interval = 0.)
643 int active = is_active ();
645 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
646 if (active) start ();
649 void start (ev_tstamp at, ev_tstamp interval = 0.)
657 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
659 EV_END_WATCHER (periodic, periodic)
662 EV_BEGIN_WATCHER (sig, signal)
663 void set (int signum)
665 int active = is_active ();
667 ev_signal_set (static_cast<ev_signal *>(this), signum);
668 if (active) start ();
671 void start (int signum)
676 EV_END_WATCHER (sig, signal)
678 EV_BEGIN_WATCHER (child, child)
681 int active = is_active ();
683 ev_child_set (static_cast<ev_child *>(this), pid);
684 if (active) start ();
692 EV_END_WATCHER (child, child)
695 EV_BEGIN_WATCHER (stat, stat)
696 void set (const char *path, ev_tstamp interval = 0.)
698 int active = is_active ();
700 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
701 if (active) start ();
704 void start (const char *path, ev_tstamp interval = 0.)
707 set (path, interval);
713 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
715 EV_END_WATCHER (stat, stat)
718 EV_BEGIN_WATCHER (idle, idle)
720 EV_END_WATCHER (idle, idle)
722 EV_BEGIN_WATCHER (prepare, prepare)
724 EV_END_WATCHER (prepare, prepare)
726 EV_BEGIN_WATCHER (check, check)
728 EV_END_WATCHER (check, check)
731 EV_BEGIN_WATCHER (embed, embed)
732 void start (struct ev_loop *embedded_loop)
735 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
741 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
743 EV_END_WATCHER (embed, embed)
747 EV_BEGIN_WATCHER (fork, fork)
749 EV_END_WATCHER (fork, fork)
755 #undef EV_BEGIN_WATCHER
756 #undef EV_END_WATCHER