8 template<class ev_watcher, class watcher>
9 struct base : ev_watcher
25 void set_ (void *object, void (*cb)(ev_watcher *w, int revents))
28 ev_set_cb (static_cast<ev_watcher *>(this), cb);
31 template<class K, void (K::*method)(watcher &w, int)>
34 set_ (object, method_thunk<K, method>);
37 template<class K, void (K::*method)(watcher &w, int)>
38 static void method_thunk (ev_watcher *w, int revents)
40 watcher *self = static_cast<watcher *>(w);
41 K *obj = static_cast<K *>(self->data);
42 (obj->*method) (*self, revents);
45 template<class K, void (K::*method)(watcher &w, int) const>
46 void set (const K *object)
48 set_ (object, const_method_thunk<K, method>);
51 template<class K, void (K::*method)(watcher &w, int) const>
52 static void const_method_thunk (ev_watcher *w, int revents)
54 watcher *self = static_cast<watcher *>(w);
55 K *obj = static_cast<K *>(self->data);
56 (obj->*method) (*self, revents);
59 template<void (*function)(watcher &w, int)>
62 set_ (0, function_thunk<function>);
65 template<void (*function)(watcher &w, int)>
66 static void function_thunk (ev_watcher *w, int revents)
68 watcher *self = static_cast<watcher *>(w);
69 function (*self, revents);
72 void operator ()(int events = EV_UNDEF)
74 return e_cb (static_cast<ev_watcher *>(this)) (events);
77 bool is_active () const
79 return ev_is_active (static_cast<const ev_watcher *>(this));
82 bool is_pending () const
84 return ev_is_pending (static_cast<const ev_watcher *>(this));
94 PERIODIC = EV_PERIODIC,
100 PREPARE = EV_PREPARE,
106 typedef ev_tstamp tstamp;
108 inline ev_tstamp now (EV_P)
110 return ev_now (EV_A);
114 #define EV_CONSTRUCT \
115 (EV_P = EV_DEFAULT) \
120 #define EV_CONSTRUCT \
126 /* using a template here would require quite a bit more lines,
127 * so a macro solution was chosen */
128 #define EV_BEGIN_WATCHER(cppstem,cstem) \
130 struct cppstem : base<ev_ ## cstem, cppstem> \
134 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
139 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
142 cppstem EV_CONSTRUCT \
149 using base<ev_ ## cstem, cppstem>::set; \
153 cppstem (const cppstem &o) \
156 void operator =(const cppstem &o) { /* disabled */ } \
160 #define EV_END_WATCHER(cppstem,cstem) \
163 EV_BEGIN_WATCHER (io, io)
164 void set (int fd, int events)
166 int active = is_active ();
168 ev_io_set (static_cast<ev_io *>(this), fd, events);
169 if (active) start ();
172 void set (int events)
174 int active = is_active ();
176 ev_io_set (static_cast<ev_io *>(this), fd, events);
177 if (active) start ();
180 void start (int fd, int events)
185 EV_END_WATCHER (io, io)
187 EV_BEGIN_WATCHER (timer, timer)
188 void set (ev_tstamp after, ev_tstamp repeat = 0.)
190 int active = is_active ();
192 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
193 if (active) start ();
196 void start (ev_tstamp after, ev_tstamp repeat = 0.)
204 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
206 EV_END_WATCHER (timer, timer)
208 #if EV_PERIODIC_ENABLE
209 EV_BEGIN_WATCHER (periodic, periodic)
210 void set (ev_tstamp at, ev_tstamp interval = 0.)
212 int active = is_active ();
214 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
215 if (active) start ();
218 void start (ev_tstamp at, ev_tstamp interval = 0.)
226 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
228 EV_END_WATCHER (periodic, periodic)
231 EV_BEGIN_WATCHER (sig, signal)
232 void set (int signum)
234 int active = is_active ();
236 ev_signal_set (static_cast<ev_signal *>(this), signum);
237 if (active) start ();
240 void start (int signum)
245 EV_END_WATCHER (sig, signal)
247 EV_BEGIN_WATCHER (child, child)
250 int active = is_active ();
252 ev_child_set (static_cast<ev_child *>(this), pid);
253 if (active) start ();
261 EV_END_WATCHER (child, child)
264 EV_BEGIN_WATCHER (stat, stat)
265 void set (const char *path, ev_tstamp interval = 0.)
267 int active = is_active ();
269 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
270 if (active) start ();
273 void start (const char *path, ev_tstamp interval = 0.)
276 set (path, interval);
282 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
284 EV_END_WATCHER (stat, stat)
287 EV_BEGIN_WATCHER (idle, idle)
289 EV_END_WATCHER (idle, idle)
291 EV_BEGIN_WATCHER (prepare, prepare)
293 EV_END_WATCHER (prepare, prepare)
295 EV_BEGIN_WATCHER (check, check)
297 EV_END_WATCHER (check, check)
300 EV_BEGIN_WATCHER (embed, embed)
301 void start (struct ev_loop *embedded_loop)
304 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
310 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
312 EV_END_WATCHER (embed, embed)
316 EV_BEGIN_WATCHER (fork, fork)
318 EV_END_WATCHER (fork, fork)
322 #undef EV_BEGIN_WATCHER
323 #undef EV_END_WATCHER