8 template<class watcher>
11 struct klass; // it is vital that this is never defined
14 void (klass::*m)(watcher &, int);
17 template<class O1, class O2>
18 explicit callback (O1 *object, void (O2::*method)(watcher &, int))
20 o = reinterpret_cast<klass *>(object);
21 m = reinterpret_cast<void (klass::*)(watcher &, int)>(method);
24 // this works because a standards-compliant C++ compiler
25 // basically can't help it: it doesn't have the knowledge
26 // required to miscompile (klass is not defined anywhere
27 // and nothing is known about the constructor arguments) :)
28 void call (watcher *w, int revents)
30 (o->*m) (*w, revents);
40 PERIODIC = EV_PERIODIC,
52 typedef ev_tstamp tstamp;
54 inline ev_tstamp now (EV_P)
61 #define EV_CONSTRUCT(cppstem) \
69 template<class O1, class O2> \
70 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \
71 : callback<cppstem> (object, method), EV_A (EV_A)
75 #define EV_CONSTRUCT(cppstem) \
76 template<class O1, class O2> \
77 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \
78 : callback<cppstem> (object, method)
82 /* using a template here would require quite a bit more lines,
83 * so a macro solution was chosen */
84 #define EV_BEGIN_WATCHER(cppstem,cstem) \
86 struct cppstem : ev_ ## cstem, callback<cppstem> \
88 EV_CONSTRUCT (cppstem) \
90 ev_init (static_cast<ev_ ## cstem *>(this), thunk); \
93 bool is_active () const \
95 return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \
98 bool is_pending () const \
100 return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \
105 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
110 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
113 void operator ()(int events = EV_UNDEF) \
115 return call (this, events); \
125 cppstem (const cppstem &o) \
126 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
129 void operator =(const cppstem &o) { /* disabled */ } \
131 static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \
133 (*static_cast<cppstem *>(w))(revents); \
138 #define EV_END_WATCHER(cppstem,cstem) \
141 EV_BEGIN_WATCHER (io, io)
142 void set (int fd, int events)
144 int active = is_active ();
146 ev_io_set (static_cast<ev_io *>(this), fd, events);
147 if (active) start ();
150 void set (int events)
152 int active = is_active ();
154 ev_io_set (static_cast<ev_io *>(this), fd, events);
155 if (active) start ();
158 void start (int fd, int events)
163 EV_END_WATCHER (io, io)
165 EV_BEGIN_WATCHER (timer, timer)
166 void set (ev_tstamp after, ev_tstamp repeat = 0.)
168 int active = is_active ();
170 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
171 if (active) start ();
174 void start (ev_tstamp after, ev_tstamp repeat = 0.)
182 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
184 EV_END_WATCHER (timer, timer)
186 #if EV_PERIODIC_ENABLE
187 EV_BEGIN_WATCHER (periodic, periodic)
188 void set (ev_tstamp at, ev_tstamp interval = 0.)
190 int active = is_active ();
192 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
193 if (active) start ();
196 void start (ev_tstamp at, ev_tstamp interval = 0.)
204 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
206 EV_END_WATCHER (periodic, periodic)
209 EV_BEGIN_WATCHER (sig, signal)
210 void set (int signum)
212 int active = is_active ();
214 ev_signal_set (static_cast<ev_signal *>(this), signum);
215 if (active) start ();
218 void start (int signum)
223 EV_END_WATCHER (sig, signal)
225 EV_BEGIN_WATCHER (child, child)
228 int active = is_active ();
230 ev_child_set (static_cast<ev_child *>(this), pid);
231 if (active) start ();
239 EV_END_WATCHER (child, child)
242 EV_BEGIN_WATCHER (stat, stat)
243 void set (const char *path, ev_tstamp interval = 0.)
245 int active = is_active ();
247 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
248 if (active) start ();
251 void start (const char *path, ev_tstamp interval = 0.)
254 set (path, interval);
260 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
262 EV_END_WATCHER (stat, stat)
265 EV_BEGIN_WATCHER (idle, idle)
267 EV_END_WATCHER (idle, idle)
269 EV_BEGIN_WATCHER (prepare, prepare)
271 EV_END_WATCHER (prepare, prepare)
273 EV_BEGIN_WATCHER (check, check)
275 EV_END_WATCHER (check, check)
278 EV_BEGIN_WATCHER (embed, embed)
279 void start (struct ev_loop *embedded_loop)
282 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
288 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
290 EV_END_WATCHER (embed, embed)
294 EV_BEGIN_WATCHER (fork, fork)
296 EV_END_WATCHER (fork, fork)
300 #undef EV_BEGIN_WATCHER
301 #undef EV_END_WATCHER