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
96 template<class ev_watcher, class watcher>
97 struct base : ev_watcher
113 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
116 ev_set_cb (static_cast<ev_watcher *>(this), cb);
120 template<class K, void (K::*method)(watcher &w, int)>
123 set_ (object, method_thunk<K, method>);
126 template<class K, void (K::*method)(watcher &w, int)>
127 static void method_thunk (EV_P_ ev_watcher *w, int revents)
129 K *obj = static_cast<K *>(w->data);
130 (obj->*method) (*static_cast<watcher *>(w), revents);
133 // const method callback
134 template<class K, void (K::*method)(watcher &w, int) const>
135 void set (const K *object)
137 set_ (object, const_method_thunk<K, method>);
140 template<class K, void (K::*method)(watcher &w, int) const>
141 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
143 K *obj = static_cast<K *>(w->data);
144 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
148 template<void (*function)(watcher &w, int)>
149 void set (void *data = 0)
151 set_ (data, function_thunk<function>);
154 template<void (*function)(watcher &w, int)>
155 static void function_thunk (EV_P_ ev_watcher *w, int revents)
157 function (*static_cast<watcher *>(w), revents);
161 template<class K, void (K::*method)()>
164 set_ (object, method_noargs_thunk<K, method>);
167 template<class K, void (K::*method)()>
168 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
170 K *obj = static_cast<K *>(w->data);
174 void operator ()(int events = EV_UNDEF)
176 return ev_cb (static_cast<ev_watcher *>(this))
177 (static_cast<ev_watcher *>(this), events);
180 bool is_active () const
182 return ev_is_active (static_cast<const ev_watcher *>(this));
185 bool is_pending () const
187 return ev_is_pending (static_cast<const ev_watcher *>(this));
190 void feed_event (int revents)
192 ev_feed_event (EV_A_ static_cast<const ev_watcher *>(this), revents);
196 inline void delay (tstamp interval)
201 inline int version_major ()
203 return ev_version_major ();
206 inline int version_minor ()
208 return ev_version_minor ();
211 inline unsigned int supported_backends ()
213 return ev_supported_backends ();
216 inline unsigned int recommended_backends ()
218 return ev_recommended_backends ();
221 inline unsigned int embeddable_backends ()
223 return ev_embeddable_backends ();
226 inline void set_allocator (void *(*cb)(void *ptr, long size))
228 ev_set_allocator (cb);
231 inline void set_syserr_cb (void (*cb)(const char *msg))
233 ev_set_syserr_cb (cb);
237 inline ev_tstamp now (EV_P)
239 return ev_now (EV_A);
243 #define EV_CONSTRUCT \
244 (EV_P = EV_DEFAULT) \
249 #define EV_CONSTRUCT \
255 /* using a template here would require quite a bit more lines,
256 * so a macro solution was chosen */
257 #define EV_BEGIN_WATCHER(cppstem,cstem) \
259 struct cppstem : base<ev_ ## cstem, cppstem> \
263 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
268 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
271 cppstem EV_CONSTRUCT \
278 using base<ev_ ## cstem, cppstem>::set; \
282 cppstem (const cppstem &o); \
284 cppstem & operator =(const cppstem &o); \
288 #define EV_END_WATCHER(cppstem,cstem) \
291 EV_BEGIN_WATCHER (io, io)
292 void set (int fd, int events)
294 int active = is_active ();
296 ev_io_set (static_cast<ev_io *>(this), fd, events);
297 if (active) start ();
300 void set (int events)
302 int active = is_active ();
304 ev_io_set (static_cast<ev_io *>(this), fd, events);
305 if (active) start ();
308 void start (int fd, int events)
313 EV_END_WATCHER (io, io)
315 EV_BEGIN_WATCHER (timer, timer)
316 void set (ev_tstamp after, ev_tstamp repeat = 0.)
318 int active = is_active ();
320 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
321 if (active) start ();
324 void start (ev_tstamp after, ev_tstamp repeat = 0.)
332 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
334 EV_END_WATCHER (timer, timer)
336 #if EV_PERIODIC_ENABLE
337 EV_BEGIN_WATCHER (periodic, periodic)
338 void set (ev_tstamp at, ev_tstamp interval = 0.)
340 int active = is_active ();
342 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
343 if (active) start ();
346 void start (ev_tstamp at, ev_tstamp interval = 0.)
354 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
356 EV_END_WATCHER (periodic, periodic)
359 EV_BEGIN_WATCHER (sig, signal)
360 void set (int signum)
362 int active = is_active ();
364 ev_signal_set (static_cast<ev_signal *>(this), signum);
365 if (active) start ();
368 void start (int signum)
373 EV_END_WATCHER (sig, signal)
375 EV_BEGIN_WATCHER (child, child)
378 int active = is_active ();
380 ev_child_set (static_cast<ev_child *>(this), pid);
381 if (active) start ();
389 EV_END_WATCHER (child, child)
392 EV_BEGIN_WATCHER (stat, stat)
393 void set (const char *path, ev_tstamp interval = 0.)
395 int active = is_active ();
397 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
398 if (active) start ();
401 void start (const char *path, ev_tstamp interval = 0.)
404 set (path, interval);
410 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
412 EV_END_WATCHER (stat, stat)
415 EV_BEGIN_WATCHER (idle, idle)
417 EV_END_WATCHER (idle, idle)
419 EV_BEGIN_WATCHER (prepare, prepare)
421 EV_END_WATCHER (prepare, prepare)
423 EV_BEGIN_WATCHER (check, check)
425 EV_END_WATCHER (check, check)
428 EV_BEGIN_WATCHER (embed, embed)
429 void start (struct ev_loop *embedded_loop)
432 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
438 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
440 EV_END_WATCHER (embed, embed)
444 EV_BEGIN_WATCHER (fork, fork)
446 EV_END_WATCHER (fork, fork)
450 #undef EV_BEGIN_WATCHER
451 #undef EV_END_WATCHER