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);
49 typedef ev_tstamp tstamp;
51 inline ev_tstamp now (EV_P)
58 #define EV_CONSTRUCT(cppstem) \
66 template<class O1, class O2> \
67 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \
68 : callback<cppstem> (object, method), EV_A (EV_A)
72 #define EV_CONSTRUCT(cppstem) \
73 template<class O1, class O2> \
74 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \
75 : callback<cppstem> (object, method)
79 /* using a template here would require quite a bit more lines,
80 * so a macro solution was chosen */
81 #define EV_DECLARE_WATCHER(cppstem,cstem) \
83 extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \
85 struct cppstem : ev_ ## cstem, callback<cppstem> \
87 EV_CONSTRUCT (cppstem) \
89 ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \
92 bool is_active () const \
94 return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \
97 bool is_pending () const \
99 return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \
104 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
109 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
112 void operator ()(int events = EV_UNDEF) \
114 return call (this, events); \
119 cppstem (const cppstem &o) \
120 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
122 void operator =(const cppstem &o) { /* disabled */ } \
126 EV_DECLARE_WATCHER (io, io)
127 void set (int fd, int events)
129 int active = is_active ();
131 ev_io_set (static_cast<ev_io *>(this), fd, events);
132 if (active) start ();
135 void set (int events)
137 int active = is_active ();
139 ev_io_set (static_cast<ev_io *>(this), fd, events);
140 if (active) start ();
143 void start (int fd, int events)
150 EV_DECLARE_WATCHER (timer, timer)
151 void set (ev_tstamp after, ev_tstamp repeat = 0.)
153 int active = is_active ();
155 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
156 if (active) start ();
159 void start (ev_tstamp after, ev_tstamp repeat = 0.)
167 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
171 EV_DECLARE_WATCHER (periodic, periodic)
172 void set (ev_tstamp at, ev_tstamp interval = 0.)
174 int active = is_active ();
176 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
177 if (active) start ();
180 void start (ev_tstamp at, ev_tstamp interval = 0.)
188 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
192 EV_DECLARE_WATCHER (idle, idle)
195 EV_DECLARE_WATCHER (prepare, prepare)
198 EV_DECLARE_WATCHER (check, check)
201 EV_DECLARE_WATCHER (sig, signal)
202 void set (int signum)
204 int active = is_active ();
206 ev_signal_set (static_cast<ev_signal *>(this), signum);
207 if (active) start ();
210 void start (int signum)
217 EV_DECLARE_WATCHER (child, child)
220 int active = is_active ();
222 ev_child_set (static_cast<ev_child *>(this), pid);
223 if (active) start ();
234 #undef EV_DECLARE_WATCHER