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,
62 typedef ev_tstamp tstamp;
64 inline ev_tstamp now (EV_P)
71 #define EV_CONSTRUCT(cppstem) \
79 template<class O1, class O2> \
80 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \
81 : callback<cppstem> (object, method), EV_A (EV_A)
85 #define EV_CONSTRUCT(cppstem) \
86 template<class O1, class O2> \
87 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \
88 : callback<cppstem> (object, method)
92 /* using a template here would require quite a bit more lines,
93 * so a macro solution was chosen */
94 #define EV_BEGIN_WATCHER(cppstem,cstem) \
96 struct cppstem : ev_ ## cstem, callback<cppstem> \
98 EV_CONSTRUCT (cppstem) \
100 ev_init (static_cast<ev_ ## cstem *>(this), thunk); \
103 bool is_active () const \
105 return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \
108 bool is_pending () const \
110 return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \
115 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
120 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
123 void operator ()(int events = EV_UNDEF) \
125 return call (this, events); \
135 cppstem (const cppstem &o) \
136 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
139 void operator =(const cppstem &o) { /* disabled */ } \
141 static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \
143 (*static_cast<cppstem *>(w))(revents); \
148 #define EV_END_WATCHER(cppstem,cstem) \
151 EV_BEGIN_WATCHER (io, io)
152 void set (int fd, int events)
154 int active = is_active ();
156 ev_io_set (static_cast<ev_io *>(this), fd, events);
157 if (active) start ();
160 void set (int events)
162 int active = is_active ();
164 ev_io_set (static_cast<ev_io *>(this), fd, events);
165 if (active) start ();
168 void start (int fd, int events)
173 EV_END_WATCHER (io, io)
175 EV_BEGIN_WATCHER (timer, timer)
176 void set (ev_tstamp after, ev_tstamp repeat = 0.)
178 int active = is_active ();
180 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
181 if (active) start ();
184 void start (ev_tstamp after, ev_tstamp repeat = 0.)
192 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
194 EV_END_WATCHER (timer, timer)
197 EV_BEGIN_WATCHER (periodic, periodic)
198 void set (ev_tstamp at, ev_tstamp interval = 0.)
200 int active = is_active ();
202 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
203 if (active) start ();
206 void start (ev_tstamp at, ev_tstamp interval = 0.)
214 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
216 EV_END_WATCHER (periodic, periodic)
219 EV_BEGIN_WATCHER (idle, idle)
221 EV_END_WATCHER (idle, idle)
223 EV_BEGIN_WATCHER (prepare, prepare)
225 EV_END_WATCHER (prepare, prepare)
227 EV_BEGIN_WATCHER (check, check)
229 EV_END_WATCHER (check, check)
231 EV_BEGIN_WATCHER (sig, signal)
232 void set (int signum)
234 int active = is_active ();
236 ev_signal_set (static_cast<ev_signal *>(this), signum);
237 if (active) start ();
240 void start (int signum)
245 EV_END_WATCHER (sig, signal)
247 EV_BEGIN_WATCHER (child, child)
250 int active = is_active ();
252 ev_child_set (static_cast<ev_child *>(this), pid);
253 if (active) start ();
261 EV_END_WATCHER (child, child)
265 EV_BEGIN_WATCHER (embed, embed)
266 void set (struct ev_loop *loop)
268 int active = is_active ();
270 ev_embed_set (static_cast<ev_embed *>(this), loop);
271 if (active) start ();
274 void start (struct ev_loop *embedded_loop)
282 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
284 EV_END_WATCHER (embed, embed)
289 #undef EV_BEGIN_WATCHER
290 #undef EV_END_WATCHER