8 template<class ev_watcher, class watcher>
9 struct base : ev_watcher
25 void set_ (void *object, void (*cb)(EV_P_ 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_P_ 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_P_ 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_P_ 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 ev_cb (static_cast<ev_watcher *>(this))
75 (static_cast<ev_watcher *>(this), events);
78 bool is_active () const
80 return ev_is_active (static_cast<const ev_watcher *>(this));
83 bool is_pending () const
85 return ev_is_pending (static_cast<const ev_watcher *>(this));
95 PERIODIC = EV_PERIODIC,
101 PREPARE = EV_PREPARE,
107 typedef ev_tstamp tstamp;
109 inline ev_tstamp now (EV_P)
111 return ev_now (EV_A);
115 #define EV_CONSTRUCT \
116 (EV_P = EV_DEFAULT) \
121 #define EV_CONSTRUCT \
127 /* using a template here would require quite a bit more lines,
128 * so a macro solution was chosen */
129 #define EV_BEGIN_WATCHER(cppstem,cstem) \
131 struct cppstem : base<ev_ ## cstem, cppstem> \
135 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
140 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
143 cppstem EV_CONSTRUCT \
150 using base<ev_ ## cstem, cppstem>::set; \
154 cppstem (const cppstem &o) \
157 void operator =(const cppstem &o) { /* disabled */ } \
161 #define EV_END_WATCHER(cppstem,cstem) \
164 EV_BEGIN_WATCHER (io, io)
165 void set (int fd, int events)
167 int active = is_active ();
169 ev_io_set (static_cast<ev_io *>(this), fd, events);
170 if (active) start ();
173 void set (int events)
175 int active = is_active ();
177 ev_io_set (static_cast<ev_io *>(this), fd, events);
178 if (active) start ();
181 void start (int fd, int events)
186 EV_END_WATCHER (io, io)
188 EV_BEGIN_WATCHER (timer, timer)
189 void set (ev_tstamp after, ev_tstamp repeat = 0.)
191 int active = is_active ();
193 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
194 if (active) start ();
197 void start (ev_tstamp after, ev_tstamp repeat = 0.)
205 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
207 EV_END_WATCHER (timer, timer)
209 #if EV_PERIODIC_ENABLE
210 EV_BEGIN_WATCHER (periodic, periodic)
211 void set (ev_tstamp at, ev_tstamp interval = 0.)
213 int active = is_active ();
215 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
216 if (active) start ();
219 void start (ev_tstamp at, ev_tstamp interval = 0.)
227 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
229 EV_END_WATCHER (periodic, periodic)
232 EV_BEGIN_WATCHER (sig, signal)
233 void set (int signum)
235 int active = is_active ();
237 ev_signal_set (static_cast<ev_signal *>(this), signum);
238 if (active) start ();
241 void start (int signum)
246 EV_END_WATCHER (sig, signal)
248 EV_BEGIN_WATCHER (child, child)
251 int active = is_active ();
253 ev_child_set (static_cast<ev_child *>(this), pid);
254 if (active) start ();
262 EV_END_WATCHER (child, child)
265 EV_BEGIN_WATCHER (stat, stat)
266 void set (const char *path, ev_tstamp interval = 0.)
268 int active = is_active ();
270 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
271 if (active) start ();
274 void start (const char *path, ev_tstamp interval = 0.)
277 set (path, interval);
283 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
285 EV_END_WATCHER (stat, stat)
288 EV_BEGIN_WATCHER (idle, idle)
290 EV_END_WATCHER (idle, idle)
292 EV_BEGIN_WATCHER (prepare, prepare)
294 EV_END_WATCHER (prepare, prepare)
296 EV_BEGIN_WATCHER (check, check)
298 EV_END_WATCHER (check, check)
301 EV_BEGIN_WATCHER (embed, embed)
302 void start (struct ev_loop *embedded_loop)
305 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
311 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
313 EV_END_WATCHER (embed, embed)
317 EV_BEGIN_WATCHER (fork, fork)
319 EV_END_WATCHER (fork, fork)
323 #undef EV_BEGIN_WATCHER
324 #undef EV_END_WATCHER