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);
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 K *obj = static_cast<K *>(w->data);
41 (obj->*method) (*static_cast<watcher *>(w), revents);
44 template<class K, void (K::*method)(watcher &w, int) const>
45 void set (const K *object)
47 set_ (object, const_method_thunk<K, method>);
50 template<class K, void (K::*method)(watcher &w, int) const>
51 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
53 K *obj = static_cast<K *>(w->data);
54 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
57 template<void (*function)(watcher &w, int)>
58 void set (void *data = 0)
60 set_ (data, function_thunk<function>);
63 template<void (*function)(watcher &w, int)>
64 static void function_thunk (EV_P_ ev_watcher *w, int revents)
66 function (*static_cast<watcher *>(w), revents);
69 void operator ()(int events = EV_UNDEF)
71 return ev_cb (static_cast<ev_watcher *>(this))
72 (static_cast<ev_watcher *>(this), events);
75 bool is_active () const
77 return ev_is_active (static_cast<const ev_watcher *>(this));
80 bool is_pending () const
82 return ev_is_pending (static_cast<const ev_watcher *>(this));
92 PERIODIC = EV_PERIODIC,
104 typedef ev_tstamp tstamp;
106 inline ev_tstamp now (EV_P)
108 return ev_now (EV_A);
112 #define EV_CONSTRUCT \
113 (EV_P = EV_DEFAULT) \
118 #define EV_CONSTRUCT \
124 /* using a template here would require quite a bit more lines,
125 * so a macro solution was chosen */
126 #define EV_BEGIN_WATCHER(cppstem,cstem) \
128 struct cppstem : base<ev_ ## cstem, cppstem> \
132 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
137 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
140 cppstem EV_CONSTRUCT \
147 using base<ev_ ## cstem, cppstem>::set; \
151 cppstem (const cppstem &o) \
154 void operator =(const cppstem &o) { /* disabled */ } \
158 #define EV_END_WATCHER(cppstem,cstem) \
161 EV_BEGIN_WATCHER (io, io)
162 void set (int fd, int events)
164 int active = is_active ();
166 ev_io_set (static_cast<ev_io *>(this), fd, events);
167 if (active) start ();
170 void set (int events)
172 int active = is_active ();
174 ev_io_set (static_cast<ev_io *>(this), fd, events);
175 if (active) start ();
178 void start (int fd, int events)
183 EV_END_WATCHER (io, io)
185 EV_BEGIN_WATCHER (timer, timer)
186 void set (ev_tstamp after, ev_tstamp repeat = 0.)
188 int active = is_active ();
190 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
191 if (active) start ();
194 void start (ev_tstamp after, ev_tstamp repeat = 0.)
202 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
204 EV_END_WATCHER (timer, timer)
206 #if EV_PERIODIC_ENABLE
207 EV_BEGIN_WATCHER (periodic, periodic)
208 void set (ev_tstamp at, ev_tstamp interval = 0.)
210 int active = is_active ();
212 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
213 if (active) start ();
216 void start (ev_tstamp at, ev_tstamp interval = 0.)
224 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
226 EV_END_WATCHER (periodic, periodic)
229 EV_BEGIN_WATCHER (sig, signal)
230 void set (int signum)
232 int active = is_active ();
234 ev_signal_set (static_cast<ev_signal *>(this), signum);
235 if (active) start ();
238 void start (int signum)
243 EV_END_WATCHER (sig, signal)
245 EV_BEGIN_WATCHER (child, child)
248 int active = is_active ();
250 ev_child_set (static_cast<ev_child *>(this), pid);
251 if (active) start ();
259 EV_END_WATCHER (child, child)
262 EV_BEGIN_WATCHER (stat, stat)
263 void set (const char *path, ev_tstamp interval = 0.)
265 int active = is_active ();
267 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
268 if (active) start ();
271 void start (const char *path, ev_tstamp interval = 0.)
274 set (path, interval);
280 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
282 EV_END_WATCHER (stat, stat)
285 EV_BEGIN_WATCHER (idle, idle)
287 EV_END_WATCHER (idle, idle)
289 EV_BEGIN_WATCHER (prepare, prepare)
291 EV_END_WATCHER (prepare, prepare)
293 EV_BEGIN_WATCHER (check, check)
295 EV_END_WATCHER (check, check)
298 EV_BEGIN_WATCHER (embed, embed)
299 void start (struct ev_loop *embedded_loop)
302 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
308 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
310 EV_END_WATCHER (embed, embed)
314 EV_BEGIN_WATCHER (fork, fork)
316 EV_END_WATCHER (fork, fork)
320 #undef EV_BEGIN_WATCHER
321 #undef EV_END_WATCHER