8 template<class watcher>
14 void (object::*meth)(watcher &, int);
16 /* a proxy is a kind of recipe on how to call a specific class method */
18 virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int) const = 0;
20 template<class O1, class O2>
21 struct proxy : proxy_base {
22 virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int e) const
24 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth)))
32 template<class O1, class O2>
33 explicit callback (O1 *object, void (O2::*method)(watcher &, int))
35 static proxy<O1,O2> p;
36 obj = reinterpret_cast<void *>(object);
37 meth = reinterpret_cast<void (object::*)(watcher &, int)>(method);
41 void call (watcher *w, int e) const
43 return prxy->call (obj, meth, *w, e);
53 PERIODIC = EV_PERIODIC,
65 typedef ev_tstamp tstamp;
67 inline ev_tstamp now (EV_P)
74 #define EV_CONSTRUCT(cppstem) \
82 template<class O1, class O2> \
83 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \
84 : callback<cppstem> (object, method), EV_A (EV_A)
88 #define EV_CONSTRUCT(cppstem) \
89 template<class O1, class O2> \
90 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \
91 : callback<cppstem> (object, method)
95 /* using a template here would require quite a bit more lines,
96 * so a macro solution was chosen */
97 #define EV_BEGIN_WATCHER(cppstem,cstem) \
99 struct cppstem : ev_ ## cstem, callback<cppstem> \
101 EV_CONSTRUCT (cppstem) \
103 ev_init (static_cast<ev_ ## cstem *>(this), thunk); \
106 bool is_active () const \
108 return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \
111 bool is_pending () const \
113 return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \
118 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
123 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
126 void operator ()(int events = EV_UNDEF) \
128 return call (this, events); \
138 cppstem (const cppstem &o) \
139 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
142 void operator =(const cppstem &o) { /* disabled */ } \
144 static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \
146 (*static_cast<cppstem *>(w))(revents); \
151 #define EV_END_WATCHER(cppstem,cstem) \
154 EV_BEGIN_WATCHER (io, io)
155 void set (int fd, int events)
157 int active = is_active ();
159 ev_io_set (static_cast<ev_io *>(this), fd, events);
160 if (active) start ();
163 void set (int events)
165 int active = is_active ();
167 ev_io_set (static_cast<ev_io *>(this), fd, events);
168 if (active) start ();
171 void start (int fd, int events)
176 EV_END_WATCHER (io, io)
178 EV_BEGIN_WATCHER (timer, timer)
179 void set (ev_tstamp after, ev_tstamp repeat = 0.)
181 int active = is_active ();
183 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
184 if (active) start ();
187 void start (ev_tstamp after, ev_tstamp repeat = 0.)
195 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
197 EV_END_WATCHER (timer, timer)
199 #if EV_PERIODIC_ENABLE
200 EV_BEGIN_WATCHER (periodic, periodic)
201 void set (ev_tstamp at, ev_tstamp interval = 0.)
203 int active = is_active ();
205 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
206 if (active) start ();
209 void start (ev_tstamp at, ev_tstamp interval = 0.)
217 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
219 EV_END_WATCHER (periodic, periodic)
222 EV_BEGIN_WATCHER (sig, signal)
223 void set (int signum)
225 int active = is_active ();
227 ev_signal_set (static_cast<ev_signal *>(this), signum);
228 if (active) start ();
231 void start (int signum)
236 EV_END_WATCHER (sig, signal)
238 EV_BEGIN_WATCHER (child, child)
241 int active = is_active ();
243 ev_child_set (static_cast<ev_child *>(this), pid);
244 if (active) start ();
252 EV_END_WATCHER (child, child)
255 EV_BEGIN_WATCHER (stat, stat)
256 void set (const char *path, ev_tstamp interval = 0.)
258 int active = is_active ();
260 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
261 if (active) start ();
264 void start (const char *path, ev_tstamp interval = 0.)
266 set (path, interval);
272 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
274 EV_END_WATCHER (stat, stat)
277 EV_BEGIN_WATCHER (idle, idle)
279 EV_END_WATCHER (idle, idle)
281 EV_BEGIN_WATCHER (prepare, prepare)
283 EV_END_WATCHER (prepare, prepare)
285 EV_BEGIN_WATCHER (check, check)
287 EV_END_WATCHER (check, check)
290 EV_BEGIN_WATCHER (embed, embed)
291 void set (struct ev_loop *loop)
293 int active = is_active ();
295 ev_embed_set (static_cast<ev_embed *>(this), loop);
296 if (active) start ();
299 void start (struct ev_loop *embedded_loop)
307 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
309 EV_END_WATCHER (embed, embed)
313 #undef EV_BEGIN_WATCHER
314 #undef EV_END_WATCHER