8 template<class ev_watcher, class watcher>
9 struct base : ev_watcher
25 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
28 ev_set_cb (static_cast<ev_watcher *>(this), cb);
32 template<class K, void (K::*method)(watcher &w, int)>
35 set_ (object, method_thunk<K, method>);
38 template<class K, void (K::*method)(watcher &w, int)>
39 static void method_thunk (EV_P_ ev_watcher *w, int revents)
41 K *obj = static_cast<K *>(w->data);
42 (obj->*method) (*static_cast<watcher *>(w), revents);
45 // const method callback
46 template<class K, void (K::*method)(watcher &w, int) const>
47 void set (const K *object)
49 set_ (object, const_method_thunk<K, method>);
52 template<class K, void (K::*method)(watcher &w, int) const>
53 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
55 K *obj = static_cast<K *>(w->data);
56 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
60 template<void (*function)(watcher &w, int)>
61 void set (void *data = 0)
63 set_ (data, function_thunk<function>);
66 template<void (*function)(watcher &w, int)>
67 static void function_thunk (EV_P_ ev_watcher *w, int revents)
69 function (*static_cast<watcher *>(w), revents);
73 template<class K, void (K::*method)()>
76 set_ (object, method_noargs_thunk<K, method>);
79 template<class K, void (K::*method)()>
80 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
82 K *obj = static_cast<K *>(w->data);
86 void operator ()(int events = EV_UNDEF)
88 return ev_cb (static_cast<ev_watcher *>(this))
89 (static_cast<ev_watcher *>(this), events);
92 bool is_active () const
94 return ev_is_active (static_cast<const ev_watcher *>(this));
97 bool is_pending () const
99 return ev_is_pending (static_cast<const ev_watcher *>(this));
108 TIMEOUT = EV_TIMEOUT,
109 PERIODIC = EV_PERIODIC,
115 PREPARE = EV_PREPARE,
121 typedef ev_tstamp tstamp;
123 inline ev_tstamp now (EV_P)
125 return ev_now (EV_A);
129 #define EV_CONSTRUCT \
130 (EV_P = EV_DEFAULT) \
135 #define EV_CONSTRUCT \
141 /* using a template here would require quite a bit more lines,
142 * so a macro solution was chosen */
143 #define EV_BEGIN_WATCHER(cppstem,cstem) \
145 struct cppstem : base<ev_ ## cstem, cppstem> \
149 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
154 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
157 cppstem EV_CONSTRUCT \
164 using base<ev_ ## cstem, cppstem>::set; \
168 cppstem (const cppstem &o) \
171 void operator =(const cppstem &o) { /* disabled */ } \
175 #define EV_END_WATCHER(cppstem,cstem) \
178 EV_BEGIN_WATCHER (io, io)
179 void set (int fd, int events)
181 int active = is_active ();
183 ev_io_set (static_cast<ev_io *>(this), fd, events);
184 if (active) start ();
187 void set (int events)
189 int active = is_active ();
191 ev_io_set (static_cast<ev_io *>(this), fd, events);
192 if (active) start ();
195 void start (int fd, int events)
200 EV_END_WATCHER (io, io)
202 EV_BEGIN_WATCHER (timer, timer)
203 void set (ev_tstamp after, ev_tstamp repeat = 0.)
205 int active = is_active ();
207 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
208 if (active) start ();
211 void start (ev_tstamp after, ev_tstamp repeat = 0.)
219 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
221 EV_END_WATCHER (timer, timer)
223 #if EV_PERIODIC_ENABLE
224 EV_BEGIN_WATCHER (periodic, periodic)
225 void set (ev_tstamp at, ev_tstamp interval = 0.)
227 int active = is_active ();
229 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
230 if (active) start ();
233 void start (ev_tstamp at, ev_tstamp interval = 0.)
241 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
243 EV_END_WATCHER (periodic, periodic)
246 EV_BEGIN_WATCHER (sig, signal)
247 void set (int signum)
249 int active = is_active ();
251 ev_signal_set (static_cast<ev_signal *>(this), signum);
252 if (active) start ();
255 void start (int signum)
260 EV_END_WATCHER (sig, signal)
262 EV_BEGIN_WATCHER (child, child)
265 int active = is_active ();
267 ev_child_set (static_cast<ev_child *>(this), pid);
268 if (active) start ();
276 EV_END_WATCHER (child, child)
279 EV_BEGIN_WATCHER (stat, stat)
280 void set (const char *path, ev_tstamp interval = 0.)
282 int active = is_active ();
284 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
285 if (active) start ();
288 void start (const char *path, ev_tstamp interval = 0.)
291 set (path, interval);
297 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
299 EV_END_WATCHER (stat, stat)
302 EV_BEGIN_WATCHER (idle, idle)
304 EV_END_WATCHER (idle, idle)
306 EV_BEGIN_WATCHER (prepare, prepare)
308 EV_END_WATCHER (prepare, prepare)
310 EV_BEGIN_WATCHER (check, check)
312 EV_END_WATCHER (check, check)
315 EV_BEGIN_WATCHER (embed, embed)
316 void start (struct ev_loop *embedded_loop)
319 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
325 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
327 EV_END_WATCHER (embed, embed)
331 EV_BEGIN_WATCHER (fork, fork)
333 EV_END_WATCHER (fork, fork)
337 #undef EV_BEGIN_WATCHER
338 #undef EV_END_WATCHER