12 template<class ev_watcher, class watcher>
13 struct base : ev_watcher
29 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
32 ev_set_cb (static_cast<ev_watcher *>(this), cb);
36 template<class K, void (K::*method)(watcher &w, int)>
39 set_ (object, method_thunk<K, method>);
42 template<class K, void (K::*method)(watcher &w, int)>
43 static void method_thunk (EV_P_ ev_watcher *w, int revents)
45 K *obj = static_cast<K *>(w->data);
46 (obj->*method) (*static_cast<watcher *>(w), revents);
49 // const method callback
50 template<class K, void (K::*method)(watcher &w, int) const>
51 void set (const K *object)
53 set_ (object, const_method_thunk<K, method>);
56 template<class K, void (K::*method)(watcher &w, int) const>
57 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
59 K *obj = static_cast<K *>(w->data);
60 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
64 template<void (*function)(watcher &w, int)>
65 void set (void *data = 0)
67 set_ (data, function_thunk<function>);
70 template<void (*function)(watcher &w, int)>
71 static void function_thunk (EV_P_ ev_watcher *w, int revents)
73 function (*static_cast<watcher *>(w), revents);
77 template<class K, void (K::*method)()>
80 set_ (object, method_noargs_thunk<K, method>);
83 template<class K, void (K::*method)()>
84 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
86 K *obj = static_cast<K *>(w->data);
90 void operator ()(int events = EV_UNDEF)
92 return ev_cb (static_cast<ev_watcher *>(this))
93 (static_cast<ev_watcher *>(this), events);
96 bool is_active () const
98 return ev_is_active (static_cast<const ev_watcher *>(this));
101 bool is_pending () const
103 return ev_is_pending (static_cast<const ev_watcher *>(this));
112 TIMEOUT = EV_TIMEOUT,
113 PERIODIC = EV_PERIODIC,
119 PREPARE = EV_PREPARE,
125 typedef ev_tstamp tstamp;
127 inline ev_tstamp now (EV_P)
129 return ev_now (EV_A);
133 #define EV_CONSTRUCT \
134 (EV_P = EV_DEFAULT) \
139 #define EV_CONSTRUCT \
145 /* using a template here would require quite a bit more lines,
146 * so a macro solution was chosen */
147 #define EV_BEGIN_WATCHER(cppstem,cstem) \
149 struct cppstem : base<ev_ ## cstem, cppstem> \
153 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
158 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
161 cppstem EV_CONSTRUCT \
168 using base<ev_ ## cstem, cppstem>::set; \
172 cppstem (const cppstem &o) \
175 void operator =(const cppstem &o) { /* disabled */ } \
179 #define EV_END_WATCHER(cppstem,cstem) \
182 EV_BEGIN_WATCHER (io, io)
183 void set (int fd, int events)
185 int active = is_active ();
187 ev_io_set (static_cast<ev_io *>(this), fd, events);
188 if (active) start ();
191 void set (int events)
193 int active = is_active ();
195 ev_io_set (static_cast<ev_io *>(this), fd, events);
196 if (active) start ();
199 void start (int fd, int events)
204 EV_END_WATCHER (io, io)
206 EV_BEGIN_WATCHER (timer, timer)
207 void set (ev_tstamp after, ev_tstamp repeat = 0.)
209 int active = is_active ();
211 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
212 if (active) start ();
215 void start (ev_tstamp after, ev_tstamp repeat = 0.)
223 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
225 EV_END_WATCHER (timer, timer)
227 #if EV_PERIODIC_ENABLE
228 EV_BEGIN_WATCHER (periodic, periodic)
229 void set (ev_tstamp at, ev_tstamp interval = 0.)
231 int active = is_active ();
233 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
234 if (active) start ();
237 void start (ev_tstamp at, ev_tstamp interval = 0.)
245 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
247 EV_END_WATCHER (periodic, periodic)
250 EV_BEGIN_WATCHER (sig, signal)
251 void set (int signum)
253 int active = is_active ();
255 ev_signal_set (static_cast<ev_signal *>(this), signum);
256 if (active) start ();
259 void start (int signum)
264 EV_END_WATCHER (sig, signal)
266 EV_BEGIN_WATCHER (child, child)
269 int active = is_active ();
271 ev_child_set (static_cast<ev_child *>(this), pid);
272 if (active) start ();
280 EV_END_WATCHER (child, child)
283 EV_BEGIN_WATCHER (stat, stat)
284 void set (const char *path, ev_tstamp interval = 0.)
286 int active = is_active ();
288 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
289 if (active) start ();
292 void start (const char *path, ev_tstamp interval = 0.)
295 set (path, interval);
301 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
303 EV_END_WATCHER (stat, stat)
306 EV_BEGIN_WATCHER (idle, idle)
308 EV_END_WATCHER (idle, idle)
310 EV_BEGIN_WATCHER (prepare, prepare)
312 EV_END_WATCHER (prepare, prepare)
314 EV_BEGIN_WATCHER (check, check)
316 EV_END_WATCHER (check, check)
319 EV_BEGIN_WATCHER (embed, embed)
320 void start (struct ev_loop *embedded_loop)
323 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
329 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
331 EV_END_WATCHER (embed, embed)
335 EV_BEGIN_WATCHER (fork, fork)
337 EV_END_WATCHER (fork, fork)
341 #undef EV_BEGIN_WATCHER
342 #undef EV_END_WATCHER