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 template<class ev_watcher, class watcher>
52 struct base : ev_watcher
68 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
71 ev_set_cb (static_cast<ev_watcher *>(this), cb);
75 template<class K, void (K::*method)(watcher &w, int)>
78 set_ (object, method_thunk<K, method>);
81 template<class K, void (K::*method)(watcher &w, int)>
82 static void method_thunk (EV_P_ ev_watcher *w, int revents)
84 K *obj = static_cast<K *>(w->data);
85 (obj->*method) (*static_cast<watcher *>(w), revents);
88 // const method callback
89 template<class K, void (K::*method)(watcher &w, int) const>
90 void set (const K *object)
92 set_ (object, const_method_thunk<K, method>);
95 template<class K, void (K::*method)(watcher &w, int) const>
96 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
98 K *obj = static_cast<K *>(w->data);
99 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
103 template<void (*function)(watcher &w, int)>
104 void set (void *data = 0)
106 set_ (data, function_thunk<function>);
109 template<void (*function)(watcher &w, int)>
110 static void function_thunk (EV_P_ ev_watcher *w, int revents)
112 function (*static_cast<watcher *>(w), revents);
116 template<class K, void (K::*method)()>
119 set_ (object, method_noargs_thunk<K, method>);
122 template<class K, void (K::*method)()>
123 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
125 K *obj = static_cast<K *>(w->data);
129 void operator ()(int events = EV_UNDEF)
131 return ev_cb (static_cast<ev_watcher *>(this))
132 (static_cast<ev_watcher *>(this), events);
135 bool is_active () const
137 return ev_is_active (static_cast<const ev_watcher *>(this));
140 bool is_pending () const
142 return ev_is_pending (static_cast<const ev_watcher *>(this));
151 TIMEOUT = EV_TIMEOUT,
152 PERIODIC = EV_PERIODIC,
158 PREPARE = EV_PREPARE,
164 typedef ev_tstamp tstamp;
166 inline ev_tstamp now (EV_P)
168 return ev_now (EV_A);
172 #define EV_CONSTRUCT \
173 (EV_P = EV_DEFAULT) \
178 #define EV_CONSTRUCT \
184 /* using a template here would require quite a bit more lines,
185 * so a macro solution was chosen */
186 #define EV_BEGIN_WATCHER(cppstem,cstem) \
188 struct cppstem : base<ev_ ## cstem, cppstem> \
192 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
197 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
200 cppstem EV_CONSTRUCT \
207 using base<ev_ ## cstem, cppstem>::set; \
211 cppstem (const cppstem &o) \
214 void operator =(const cppstem &o) { /* disabled */ } \
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