4 /* work in progress, don't use unless you know what you are doing */
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);
55 PERIODIC = EV_PERIODIC,
64 typedef ev_tstamp tstamp;
66 inline ev_tstamp now (EV_P)
73 #define EV_CONSTRUCT(cppstem) \
81 template<class O1, class O2> \
82 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \
83 : callback<cppstem> (object, method), EV_A (EV_A)
87 #define EV_CONSTRUCT(cppstem) \
88 template<class O1, class O2> \
89 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \
90 : callback<cppstem> (object, method)
94 /* using a template here would require quite a bit more lines,
95 * so a macro solution was chosen */
96 #define EV_BEGIN_WATCHER(cppstem,cstem) \
98 static void cb_ ## cppstem (EV_P_ struct ev_ ## cstem *w, int revents); \
100 struct cppstem : ev_ ## cstem, callback<cppstem> \
102 EV_CONSTRUCT (cppstem) \
104 ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \
107 bool is_active () const \
109 return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \
112 bool is_pending () const \
114 return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \
119 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
124 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
127 void operator ()(int events = EV_UNDEF) \
129 return call (this, events); \
139 cppstem (const cppstem &o) \
140 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
142 void operator =(const cppstem &o) { /* disabled */ } \
146 #define EV_END_WATCHER(cppstem,cstem) \
149 static void cb_ ## cppstem (EV_P_ struct ev_ ## cstem *w, int revents) \
151 (*static_cast<cppstem *>(w))(revents); \
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)
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 (idle, idle)
223 EV_END_WATCHER (idle, idle)
225 EV_BEGIN_WATCHER (prepare, prepare)
226 EV_END_WATCHER (prepare, prepare)
228 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)
264 #undef EV_BEGIN_WATCHER