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 |
… | |
… | |
42 | { |
42 | { |
43 | return prxy->call (obj, meth, *w, e); |
43 | return prxy->call (obj, meth, *w, e); |
44 | } |
44 | } |
45 | }; |
45 | }; |
46 | |
46 | |
47 | #include "ev.h" |
|
|
48 | |
|
|
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, |
53 | WRITE = EV_WRITE, |
51 | WRITE = EV_WRITE, |
54 | TIMEOUT = EV_TIMEOUT, |
52 | TIMEOUT = EV_TIMEOUT, |
55 | PERIODIC = EV_PERIODIC, |
53 | PERIODIC = EV_PERIODIC, |
56 | SIGNAL = EV_SIGNAL, |
54 | SIGNAL = EV_SIGNAL, |
|
|
55 | CHILD = EV_CHILD, |
|
|
56 | STAT = EV_STAT, |
57 | IDLE = EV_IDLE, |
57 | IDLE = EV_IDLE, |
58 | CHECK = EV_CHECK, |
58 | CHECK = EV_CHECK, |
59 | PREPARE = EV_PREPARE, |
59 | PREPARE = EV_PREPARE, |
60 | CHILD = EV_CHILD, |
60 | FORK = EV_FORK, |
|
|
61 | EMBED = EV_EMBED, |
61 | ERROR = EV_ERROR, |
62 | ERROR = EV_ERROR, |
62 | }; |
63 | }; |
63 | |
64 | |
64 | typedef ev_tstamp tstamp; |
65 | typedef ev_tstamp tstamp; |
65 | |
66 | |
… | |
… | |
91 | |
92 | |
92 | #endif |
93 | #endif |
93 | |
94 | |
94 | /* using a template here would require quite a bit more lines, |
95 | /* using a template here would require quite a bit more lines, |
95 | * so a macro solution was chosen */ |
96 | * so a macro solution was chosen */ |
96 | #define EV_DECLARE_WATCHER(cppstem,cstem) \ |
97 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
97 | \ |
|
|
98 | extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
|
|
99 | \ |
98 | \ |
100 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
99 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
101 | { \ |
100 | { \ |
102 | EV_CONSTRUCT (cppstem) \ |
101 | EV_CONSTRUCT (cppstem) \ |
103 | { \ |
102 | { \ |
104 | ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
103 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
105 | } \ |
104 | } \ |
106 | \ |
105 | \ |
107 | bool is_active () const \ |
106 | bool is_active () const \ |
108 | { \ |
107 | { \ |
109 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
108 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
… | |
… | |
137 | private: \ |
136 | private: \ |
138 | \ |
137 | \ |
139 | cppstem (const cppstem &o) \ |
138 | cppstem (const cppstem &o) \ |
140 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
139 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
141 | { /* disabled */ } \ |
140 | { /* disabled */ } \ |
|
|
141 | \ |
142 | void operator =(const cppstem &o) { /* disabled */ } \ |
142 | void operator =(const cppstem &o) { /* disabled */ } \ |
143 | \ |
143 | \ |
|
|
144 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
145 | { \ |
|
|
146 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
147 | } \ |
|
|
148 | \ |
144 | public: |
149 | public: |
145 | |
150 | |
|
|
151 | #define EV_END_WATCHER(cppstem,cstem) \ |
|
|
152 | }; |
|
|
153 | |
146 | EV_DECLARE_WATCHER (io, io) |
154 | EV_BEGIN_WATCHER (io, io) |
147 | void set (int fd, int events) |
155 | void set (int fd, int events) |
148 | { |
156 | { |
149 | int active = is_active (); |
157 | int active = is_active (); |
150 | if (active) stop (); |
158 | if (active) stop (); |
151 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
159 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
… | |
… | |
163 | void start (int fd, int events) |
171 | void start (int fd, int events) |
164 | { |
172 | { |
165 | set (fd, events); |
173 | set (fd, events); |
166 | start (); |
174 | start (); |
167 | } |
175 | } |
168 | }; |
176 | EV_END_WATCHER (io, io) |
169 | |
177 | |
170 | EV_DECLARE_WATCHER (timer, timer) |
178 | EV_BEGIN_WATCHER (timer, timer) |
171 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
179 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
172 | { |
180 | { |
173 | int active = is_active (); |
181 | int active = is_active (); |
174 | if (active) stop (); |
182 | if (active) stop (); |
175 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
183 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
… | |
… | |
184 | |
192 | |
185 | void again () |
193 | void again () |
186 | { |
194 | { |
187 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
195 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
188 | } |
196 | } |
189 | }; |
197 | EV_END_WATCHER (timer, timer) |
190 | |
198 | |
191 | #if EV_PERIODICS |
199 | #if EV_PERIODIC_ENABLE |
192 | EV_DECLARE_WATCHER (periodic, periodic) |
200 | EV_BEGIN_WATCHER (periodic, periodic) |
193 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
201 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
194 | { |
202 | { |
195 | int active = is_active (); |
203 | int active = is_active (); |
196 | if (active) stop (); |
204 | if (active) stop (); |
197 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
205 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
… | |
… | |
206 | |
214 | |
207 | void again () |
215 | void again () |
208 | { |
216 | { |
209 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
217 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
210 | } |
218 | } |
211 | }; |
219 | EV_END_WATCHER (periodic, periodic) |
212 | #endif |
220 | #endif |
213 | |
221 | |
214 | EV_DECLARE_WATCHER (idle, idle) |
|
|
215 | }; |
|
|
216 | |
|
|
217 | EV_DECLARE_WATCHER (prepare, prepare) |
|
|
218 | }; |
|
|
219 | |
|
|
220 | EV_DECLARE_WATCHER (check, check) |
|
|
221 | }; |
|
|
222 | |
|
|
223 | EV_DECLARE_WATCHER (sig, signal) |
222 | EV_BEGIN_WATCHER (sig, signal) |
224 | void set (int signum) |
223 | void set (int signum) |
225 | { |
224 | { |
226 | int active = is_active (); |
225 | int active = is_active (); |
227 | if (active) stop (); |
226 | if (active) stop (); |
228 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
227 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
232 | void start (int signum) |
231 | void start (int signum) |
233 | { |
232 | { |
234 | set (signum); |
233 | set (signum); |
235 | start (); |
234 | start (); |
236 | } |
235 | } |
237 | }; |
236 | EV_END_WATCHER (sig, signal) |
238 | |
237 | |
239 | EV_DECLARE_WATCHER (child, child) |
238 | EV_BEGIN_WATCHER (child, child) |
240 | void set (int pid) |
239 | void set (int pid) |
241 | { |
240 | { |
242 | int active = is_active (); |
241 | int active = is_active (); |
243 | if (active) stop (); |
242 | if (active) stop (); |
244 | ev_child_set (static_cast<ev_child *>(this), pid); |
243 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
248 | void start (int pid) |
247 | void start (int pid) |
249 | { |
248 | { |
250 | set (pid); |
249 | set (pid); |
251 | start (); |
250 | start (); |
252 | } |
251 | } |
253 | }; |
252 | EV_END_WATCHER (child, child) |
|
|
253 | |
|
|
254 | #if EV_STAT_ENABLE |
|
|
255 | EV_BEGIN_WATCHER (stat, stat) |
|
|
256 | void set (const char *path, ev_tstamp interval = 0.) |
|
|
257 | { |
|
|
258 | int active = is_active (); |
|
|
259 | if (active) stop (); |
|
|
260 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
|
|
261 | if (active) start (); |
|
|
262 | } |
|
|
263 | |
|
|
264 | void start (const char *path, ev_tstamp interval = 0.) |
|
|
265 | { |
|
|
266 | set (path, interval); |
|
|
267 | start (); |
|
|
268 | } |
|
|
269 | |
|
|
270 | void update () |
|
|
271 | { |
|
|
272 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
|
|
273 | } |
|
|
274 | EV_END_WATCHER (stat, stat) |
|
|
275 | #endif |
|
|
276 | |
|
|
277 | EV_BEGIN_WATCHER (idle, idle) |
|
|
278 | void set () { } |
|
|
279 | EV_END_WATCHER (idle, idle) |
|
|
280 | |
|
|
281 | EV_BEGIN_WATCHER (prepare, prepare) |
|
|
282 | void set () { } |
|
|
283 | EV_END_WATCHER (prepare, prepare) |
|
|
284 | |
|
|
285 | EV_BEGIN_WATCHER (check, check) |
|
|
286 | void set () { } |
|
|
287 | EV_END_WATCHER (check, check) |
|
|
288 | |
|
|
289 | #if EV_EMBED_ENABLE |
|
|
290 | EV_BEGIN_WATCHER (embed, embed) |
|
|
291 | void set (struct ev_loop *loop) |
|
|
292 | { |
|
|
293 | int active = is_active (); |
|
|
294 | if (active) stop (); |
|
|
295 | ev_embed_set (static_cast<ev_embed *>(this), loop); |
|
|
296 | if (active) start (); |
|
|
297 | } |
|
|
298 | |
|
|
299 | void start (struct ev_loop *embedded_loop) |
|
|
300 | { |
|
|
301 | set (embedded_loop); |
|
|
302 | start (); |
|
|
303 | } |
|
|
304 | |
|
|
305 | void sweep () |
|
|
306 | { |
|
|
307 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
|
|
308 | } |
|
|
309 | EV_END_WATCHER (embed, embed) |
|
|
310 | #endif |
254 | |
311 | |
255 | #undef EV_CONSTRUCT |
312 | #undef EV_CONSTRUCT |
|
|
313 | #undef EV_BEGIN_WATCHER |
256 | #undef EV_DECLARE_WATCHER |
314 | #undef EV_END_WATCHER |
257 | } |
315 | } |
258 | |
316 | |
259 | #endif |
317 | #endif |
260 | |
318 | |