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,
71 template<class ev_watcher, class watcher>
72 struct base : ev_watcher
88 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
91 ev_set_cb (static_cast<ev_watcher *>(this), cb);
95 template<class K, void (K::*method)(watcher &w, int)>
98 set_ (object, method_thunk<K, method>);
101 template<class K, void (K::*method)(watcher &w, int)>
102 static void method_thunk (EV_P_ ev_watcher *w, int revents)
104 K *obj = static_cast<K *>(w->data);
105 (obj->*method) (*static_cast<watcher *>(w), revents);
108 // const method callback
109 template<class K, void (K::*method)(watcher &w, int) const>
110 void set (const K *object)
112 set_ (object, const_method_thunk<K, method>);
115 template<class K, void (K::*method)(watcher &w, int) const>
116 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
118 K *obj = static_cast<K *>(w->data);
119 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
123 template<void (*function)(watcher &w, int)>
124 void set (void *data = 0)
126 set_ (data, function_thunk<function>);
129 template<void (*function)(watcher &w, int)>
130 static void function_thunk (EV_P_ ev_watcher *w, int revents)
132 function (*static_cast<watcher *>(w), revents);
136 template<class K, void (K::*method)()>
139 set_ (object, method_noargs_thunk<K, method>);
142 template<class K, void (K::*method)()>
143 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
145 K *obj = static_cast<K *>(w->data);
149 void operator ()(int events = EV_UNDEF)
151 return ev_cb (static_cast<ev_watcher *>(this))
152 (static_cast<ev_watcher *>(this), events);
155 bool is_active () const
157 return ev_is_active (static_cast<const ev_watcher *>(this));
160 bool is_pending () const
162 return ev_is_pending (static_cast<const ev_watcher *>(this));
167 inline ev_tstamp now (EV_P)
169 return ev_now (EV_A);
173 #define EV_CONSTRUCT \
174 (EV_P = EV_DEFAULT) \
179 #define EV_CONSTRUCT \
185 /* using a template here would require quite a bit more lines,
186 * so a macro solution was chosen */
187 #define EV_BEGIN_WATCHER(cppstem,cstem) \
189 struct cppstem : base<ev_ ## cstem, cppstem> \
193 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
198 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
201 cppstem EV_CONSTRUCT \
208 using base<ev_ ## cstem, cppstem>::set; \
212 cppstem (const cppstem &o); \
214 cppstem & operator =(const cppstem &o); \
218 #define EV_END_WATCHER(cppstem,cstem) \
221 EV_BEGIN_WATCHER (io, io)
222 void set (int fd, int events)
224 int active = is_active ();
226 ev_io_set (static_cast<ev_io *>(this), fd, events);
227 if (active) start ();
230 void set (int events)
232 int active = is_active ();
234 ev_io_set (static_cast<ev_io *>(this), fd, events);
235 if (active) start ();
238 void start (int fd, int events)
243 EV_END_WATCHER (io, io)
245 EV_BEGIN_WATCHER (timer, timer)
246 void set (ev_tstamp after, ev_tstamp repeat = 0.)
248 int active = is_active ();
250 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
251 if (active) start ();
254 void start (ev_tstamp after, ev_tstamp repeat = 0.)
262 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
264 EV_END_WATCHER (timer, timer)
266 #if EV_PERIODIC_ENABLE
267 EV_BEGIN_WATCHER (periodic, periodic)
268 void set (ev_tstamp at, ev_tstamp interval = 0.)
270 int active = is_active ();
272 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
273 if (active) start ();
276 void start (ev_tstamp at, ev_tstamp interval = 0.)
284 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
286 EV_END_WATCHER (periodic, periodic)
289 EV_BEGIN_WATCHER (sig, signal)
290 void set (int signum)
292 int active = is_active ();
294 ev_signal_set (static_cast<ev_signal *>(this), signum);
295 if (active) start ();
298 void start (int signum)
303 EV_END_WATCHER (sig, signal)
305 EV_BEGIN_WATCHER (child, child)
308 int active = is_active ();
310 ev_child_set (static_cast<ev_child *>(this), pid);
311 if (active) start ();
319 EV_END_WATCHER (child, child)
322 EV_BEGIN_WATCHER (stat, stat)
323 void set (const char *path, ev_tstamp interval = 0.)
325 int active = is_active ();
327 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
328 if (active) start ();
331 void start (const char *path, ev_tstamp interval = 0.)
334 set (path, interval);
340 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
342 EV_END_WATCHER (stat, stat)
345 EV_BEGIN_WATCHER (idle, idle)
347 EV_END_WATCHER (idle, idle)
349 EV_BEGIN_WATCHER (prepare, prepare)
351 EV_END_WATCHER (prepare, prepare)
353 EV_BEGIN_WATCHER (check, check)
355 EV_END_WATCHER (check, check)
358 EV_BEGIN_WATCHER (embed, embed)
359 void start (struct ev_loop *embedded_loop)
362 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
368 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
370 EV_END_WATCHER (embed, embed)
374 EV_BEGIN_WATCHER (fork, fork)
376 EV_END_WATCHER (fork, fork)
380 #undef EV_BEGIN_WATCHER
381 #undef EV_END_WATCHER