1 | #ifndef EVPP_H__ |
1 | #ifndef EVPP_H__ |
2 | #define EVPP_H__ |
2 | #define EVPP_H__ |
3 | |
3 | |
4 | /* work in progress, don't use unless you know what you are doing */ |
4 | #include "ev.h" |
5 | |
5 | |
6 | namespace ev { |
6 | namespace ev { |
7 | |
7 | |
8 | template<class watcher> |
8 | template<class watcher> |
9 | class callback |
9 | class callback |
… | |
… | |
41 | void call (watcher *w, int e) const |
41 | void call (watcher *w, int e) const |
42 | { |
42 | { |
43 | return prxy->call (obj, meth, *w, e); |
43 | return prxy->call (obj, meth, *w, e); |
44 | } |
44 | } |
45 | }; |
45 | }; |
46 | |
|
|
47 | #include "ev.h" |
|
|
48 | |
46 | |
49 | enum { |
47 | enum { |
50 | UNDEF = EV_UNDEF, |
48 | UNDEF = EV_UNDEF, |
51 | NONE = EV_NONE, |
49 | NONE = EV_NONE, |
52 | READ = EV_READ, |
50 | READ = EV_READ, |
… | |
… | |
91 | |
89 | |
92 | #endif |
90 | #endif |
93 | |
91 | |
94 | /* using a template here would require quite a bit more lines, |
92 | /* using a template here would require quite a bit more lines, |
95 | * so a macro solution was chosen */ |
93 | * so a macro solution was chosen */ |
96 | #define EV_DECLARE_WATCHER(cppstem,cstem) \ |
94 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
97 | \ |
|
|
98 | extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
|
|
99 | \ |
95 | \ |
100 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
96 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
101 | { \ |
97 | { \ |
102 | EV_CONSTRUCT (cppstem) \ |
98 | EV_CONSTRUCT (cppstem) \ |
103 | { \ |
99 | { \ |
104 | ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
100 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
105 | } \ |
101 | } \ |
106 | \ |
102 | \ |
107 | bool is_active () const \ |
103 | bool is_active () const \ |
108 | { \ |
104 | { \ |
109 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
105 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
… | |
… | |
137 | private: \ |
133 | private: \ |
138 | \ |
134 | \ |
139 | cppstem (const cppstem &o) \ |
135 | cppstem (const cppstem &o) \ |
140 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
136 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
141 | { /* disabled */ } \ |
137 | { /* disabled */ } \ |
|
|
138 | \ |
142 | void operator =(const cppstem &o) { /* disabled */ } \ |
139 | void operator =(const cppstem &o) { /* disabled */ } \ |
143 | \ |
140 | \ |
|
|
141 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
142 | { \ |
|
|
143 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
144 | } \ |
|
|
145 | \ |
144 | public: |
146 | public: |
145 | |
147 | |
|
|
148 | #define EV_END_WATCHER(cppstem,cstem) \ |
|
|
149 | }; |
|
|
150 | |
146 | EV_DECLARE_WATCHER (io, io) |
151 | EV_BEGIN_WATCHER (io, io) |
147 | void set (int fd, int events) |
152 | void set (int fd, int events) |
148 | { |
153 | { |
149 | int active = is_active (); |
154 | int active = is_active (); |
150 | if (active) stop (); |
155 | if (active) stop (); |
151 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
156 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
… | |
… | |
163 | void start (int fd, int events) |
168 | void start (int fd, int events) |
164 | { |
169 | { |
165 | set (fd, events); |
170 | set (fd, events); |
166 | start (); |
171 | start (); |
167 | } |
172 | } |
168 | }; |
173 | EV_END_WATCHER (io, io) |
169 | |
174 | |
170 | EV_DECLARE_WATCHER (timer, timer) |
175 | EV_BEGIN_WATCHER (timer, timer) |
171 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
176 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
172 | { |
177 | { |
173 | int active = is_active (); |
178 | int active = is_active (); |
174 | if (active) stop (); |
179 | if (active) stop (); |
175 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
180 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
… | |
… | |
184 | |
189 | |
185 | void again () |
190 | void again () |
186 | { |
191 | { |
187 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
192 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
188 | } |
193 | } |
189 | }; |
194 | EV_END_WATCHER (timer, timer) |
190 | |
195 | |
191 | #if EV_PERIODICS |
196 | #if EV_PERIODICS |
192 | EV_DECLARE_WATCHER (periodic, periodic) |
197 | EV_BEGIN_WATCHER (periodic, periodic) |
193 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
198 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
194 | { |
199 | { |
195 | int active = is_active (); |
200 | int active = is_active (); |
196 | if (active) stop (); |
201 | if (active) stop (); |
197 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
202 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
… | |
… | |
206 | |
211 | |
207 | void again () |
212 | void again () |
208 | { |
213 | { |
209 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
214 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
210 | } |
215 | } |
211 | }; |
216 | EV_END_WATCHER (periodic, periodic) |
212 | #endif |
217 | #endif |
213 | |
218 | |
|
|
219 | EV_BEGIN_WATCHER (idle, idle) |
|
|
220 | void set () { } |
214 | EV_DECLARE_WATCHER (idle, idle) |
221 | EV_END_WATCHER (idle, idle) |
215 | }; |
|
|
216 | |
222 | |
|
|
223 | EV_BEGIN_WATCHER (prepare, prepare) |
|
|
224 | void set () { } |
217 | EV_DECLARE_WATCHER (prepare, prepare) |
225 | EV_END_WATCHER (prepare, prepare) |
218 | }; |
|
|
219 | |
226 | |
|
|
227 | EV_BEGIN_WATCHER (check, check) |
|
|
228 | void set () { } |
220 | EV_DECLARE_WATCHER (check, check) |
229 | EV_END_WATCHER (check, check) |
221 | }; |
|
|
222 | |
230 | |
223 | EV_DECLARE_WATCHER (sig, signal) |
231 | EV_BEGIN_WATCHER (sig, signal) |
224 | void set (int signum) |
232 | void set (int signum) |
225 | { |
233 | { |
226 | int active = is_active (); |
234 | int active = is_active (); |
227 | if (active) stop (); |
235 | if (active) stop (); |
228 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
236 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
232 | void start (int signum) |
240 | void start (int signum) |
233 | { |
241 | { |
234 | set (signum); |
242 | set (signum); |
235 | start (); |
243 | start (); |
236 | } |
244 | } |
237 | }; |
245 | EV_END_WATCHER (sig, signal) |
238 | |
246 | |
239 | EV_DECLARE_WATCHER (child, child) |
247 | EV_BEGIN_WATCHER (child, child) |
240 | void set (int pid) |
248 | void set (int pid) |
241 | { |
249 | { |
242 | int active = is_active (); |
250 | int active = is_active (); |
243 | if (active) stop (); |
251 | if (active) stop (); |
244 | ev_child_set (static_cast<ev_child *>(this), pid); |
252 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
248 | void start (int pid) |
256 | void start (int pid) |
249 | { |
257 | { |
250 | set (pid); |
258 | set (pid); |
251 | start (); |
259 | start (); |
252 | } |
260 | } |
253 | }; |
261 | EV_END_WATCHER (child, child) |
|
|
262 | |
|
|
263 | #if EV_MULTIPLICITY |
|
|
264 | |
|
|
265 | EV_BEGIN_WATCHER (embed, embed) |
|
|
266 | void set (struct ev_loop *loop) |
|
|
267 | { |
|
|
268 | int active = is_active (); |
|
|
269 | if (active) stop (); |
|
|
270 | ev_embed_set (static_cast<ev_embed *>(this), loop); |
|
|
271 | if (active) start (); |
|
|
272 | } |
|
|
273 | |
|
|
274 | void start (struct ev_loop *embedded_loop) |
|
|
275 | { |
|
|
276 | set (embedded_loop); |
|
|
277 | start (); |
|
|
278 | } |
|
|
279 | |
|
|
280 | void sweep () |
|
|
281 | { |
|
|
282 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
|
|
283 | } |
|
|
284 | EV_END_WATCHER (embed, embed) |
|
|
285 | |
|
|
286 | #endif |
254 | |
287 | |
255 | #undef EV_CONSTRUCT |
288 | #undef EV_CONSTRUCT |
|
|
289 | #undef EV_BEGIN_WATCHER |
256 | #undef EV_DECLARE_WATCHER |
290 | #undef EV_END_WATCHER |
257 | } |
291 | } |
258 | |
292 | |
259 | #endif |
293 | #endif |
260 | |
294 | |