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" |
47 | enum { |
|
|
48 | UNDEF = EV_UNDEF, |
|
|
49 | NONE = EV_NONE, |
|
|
50 | READ = EV_READ, |
|
|
51 | WRITE = EV_WRITE, |
|
|
52 | TIMEOUT = EV_TIMEOUT, |
|
|
53 | PERIODIC = EV_PERIODIC, |
|
|
54 | SIGNAL = EV_SIGNAL, |
|
|
55 | IDLE = EV_IDLE, |
|
|
56 | CHECK = EV_CHECK, |
|
|
57 | PREPARE = EV_PREPARE, |
|
|
58 | CHILD = EV_CHILD, |
|
|
59 | ERROR = EV_ERROR, |
|
|
60 | }; |
48 | |
61 | |
49 | typedef ev_tstamp tstamp; |
62 | typedef ev_tstamp tstamp; |
50 | |
63 | |
51 | inline ev_tstamp now (EV_P) |
64 | inline ev_tstamp now (EV_P) |
52 | { |
65 | { |
… | |
… | |
76 | |
89 | |
77 | #endif |
90 | #endif |
78 | |
91 | |
79 | /* using a template here would require quite a bit more lines, |
92 | /* using a template here would require quite a bit more lines, |
80 | * so a macro solution was chosen */ |
93 | * so a macro solution was chosen */ |
81 | #define EV_DECLARE_WATCHER(cppstem,cstem) \ |
94 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
82 | \ |
|
|
83 | extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
|
|
84 | \ |
95 | \ |
85 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
96 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
86 | { \ |
97 | { \ |
87 | EV_CONSTRUCT (cppstem) \ |
98 | EV_CONSTRUCT (cppstem) \ |
88 | { \ |
99 | { \ |
89 | ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
100 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
90 | } \ |
101 | } \ |
91 | \ |
102 | \ |
92 | bool is_active () const \ |
103 | bool is_active () const \ |
93 | { \ |
104 | { \ |
94 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
105 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
… | |
… | |
110 | } \ |
121 | } \ |
111 | \ |
122 | \ |
112 | void operator ()(int events = EV_UNDEF) \ |
123 | void operator ()(int events = EV_UNDEF) \ |
113 | { \ |
124 | { \ |
114 | return call (this, events); \ |
125 | return call (this, events); \ |
|
|
126 | } \ |
|
|
127 | \ |
|
|
128 | ~cppstem () \ |
|
|
129 | { \ |
|
|
130 | stop (); \ |
115 | } \ |
131 | } \ |
116 | \ |
132 | \ |
117 | private: \ |
133 | private: \ |
118 | \ |
134 | \ |
119 | cppstem (const cppstem &o) \ |
135 | cppstem (const cppstem &o) \ |
120 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
136 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
121 | { /* disabled */ } \ |
137 | { /* disabled */ } \ |
|
|
138 | \ |
122 | void operator =(const cppstem &o) { /* disabled */ } \ |
139 | void operator =(const cppstem &o) { /* disabled */ } \ |
123 | \ |
140 | \ |
|
|
141 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
142 | { \ |
|
|
143 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
144 | } \ |
|
|
145 | \ |
124 | public: |
146 | public: |
125 | |
147 | |
|
|
148 | #define EV_END_WATCHER(cppstem,cstem) \ |
|
|
149 | }; |
|
|
150 | |
126 | EV_DECLARE_WATCHER (io, io) |
151 | EV_BEGIN_WATCHER (io, io) |
127 | void set (int fd, int events) |
152 | void set (int fd, int events) |
128 | { |
153 | { |
129 | int active = is_active (); |
154 | int active = is_active (); |
130 | if (active) stop (); |
155 | if (active) stop (); |
131 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
156 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
… | |
… | |
143 | void start (int fd, int events) |
168 | void start (int fd, int events) |
144 | { |
169 | { |
145 | set (fd, events); |
170 | set (fd, events); |
146 | start (); |
171 | start (); |
147 | } |
172 | } |
148 | }; |
173 | EV_END_WATCHER (io, io) |
149 | |
174 | |
150 | EV_DECLARE_WATCHER (timer, timer) |
175 | EV_BEGIN_WATCHER (timer, timer) |
151 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
176 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
152 | { |
177 | { |
153 | int active = is_active (); |
178 | int active = is_active (); |
154 | if (active) stop (); |
179 | if (active) stop (); |
155 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
180 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
… | |
… | |
164 | |
189 | |
165 | void again () |
190 | void again () |
166 | { |
191 | { |
167 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
192 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
168 | } |
193 | } |
169 | }; |
194 | EV_END_WATCHER (timer, timer) |
170 | |
195 | |
|
|
196 | #if EV_PERIODIC_ENABLE |
171 | EV_DECLARE_WATCHER (periodic, periodic) |
197 | EV_BEGIN_WATCHER (periodic, periodic) |
172 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
198 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
173 | { |
199 | { |
174 | int active = is_active (); |
200 | int active = is_active (); |
175 | if (active) stop (); |
201 | if (active) stop (); |
176 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
202 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
… | |
… | |
185 | |
211 | |
186 | void again () |
212 | void again () |
187 | { |
213 | { |
188 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
214 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
189 | } |
215 | } |
190 | }; |
216 | EV_END_WATCHER (periodic, periodic) |
|
|
217 | #endif |
191 | |
218 | |
192 | EV_DECLARE_WATCHER (idle, idle) |
|
|
193 | }; |
|
|
194 | |
|
|
195 | EV_DECLARE_WATCHER (prepare, prepare) |
|
|
196 | }; |
|
|
197 | |
|
|
198 | EV_DECLARE_WATCHER (check, check) |
|
|
199 | }; |
|
|
200 | |
|
|
201 | EV_DECLARE_WATCHER (sig, signal) |
219 | EV_BEGIN_WATCHER (sig, signal) |
202 | void set (int signum) |
220 | void set (int signum) |
203 | { |
221 | { |
204 | int active = is_active (); |
222 | int active = is_active (); |
205 | if (active) stop (); |
223 | if (active) stop (); |
206 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
224 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
210 | void start (int signum) |
228 | void start (int signum) |
211 | { |
229 | { |
212 | set (signum); |
230 | set (signum); |
213 | start (); |
231 | start (); |
214 | } |
232 | } |
215 | }; |
233 | EV_END_WATCHER (sig, signal) |
216 | |
234 | |
217 | EV_DECLARE_WATCHER (child, child) |
235 | EV_BEGIN_WATCHER (child, child) |
218 | void set (int pid) |
236 | void set (int pid) |
219 | { |
237 | { |
220 | int active = is_active (); |
238 | int active = is_active (); |
221 | if (active) stop (); |
239 | if (active) stop (); |
222 | ev_child_set (static_cast<ev_child *>(this), pid); |
240 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
226 | void start (int pid) |
244 | void start (int pid) |
227 | { |
245 | { |
228 | set (pid); |
246 | set (pid); |
229 | start (); |
247 | start (); |
230 | } |
248 | } |
231 | }; |
249 | EV_END_WATCHER (child, child) |
|
|
250 | |
|
|
251 | #if EV_STAT_ENABLE |
|
|
252 | EV_BEGIN_WATCHER (stat, stat) |
|
|
253 | void set (const char *path, ev_tstamp interval = 0.) |
|
|
254 | { |
|
|
255 | int active = is_active (); |
|
|
256 | if (active) stop (); |
|
|
257 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
|
|
258 | if (active) start (); |
|
|
259 | } |
|
|
260 | |
|
|
261 | void start (const char *path, ev_tstamp interval = 0.) |
|
|
262 | { |
|
|
263 | set (path, interval); |
|
|
264 | start (); |
|
|
265 | } |
|
|
266 | |
|
|
267 | void update () |
|
|
268 | { |
|
|
269 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
|
|
270 | } |
|
|
271 | EV_END_WATCHER (stat, stat) |
|
|
272 | #endif |
|
|
273 | |
|
|
274 | EV_BEGIN_WATCHER (idle, idle) |
|
|
275 | void set () { } |
|
|
276 | EV_END_WATCHER (idle, idle) |
|
|
277 | |
|
|
278 | EV_BEGIN_WATCHER (prepare, prepare) |
|
|
279 | void set () { } |
|
|
280 | EV_END_WATCHER (prepare, prepare) |
|
|
281 | |
|
|
282 | EV_BEGIN_WATCHER (check, check) |
|
|
283 | void set () { } |
|
|
284 | EV_END_WATCHER (check, check) |
|
|
285 | |
|
|
286 | #if EV_EMBED_ENABLE |
|
|
287 | EV_BEGIN_WATCHER (embed, embed) |
|
|
288 | void set (struct ev_loop *loop) |
|
|
289 | { |
|
|
290 | int active = is_active (); |
|
|
291 | if (active) stop (); |
|
|
292 | ev_embed_set (static_cast<ev_embed *>(this), loop); |
|
|
293 | if (active) start (); |
|
|
294 | } |
|
|
295 | |
|
|
296 | void start (struct ev_loop *embedded_loop) |
|
|
297 | { |
|
|
298 | set (embedded_loop); |
|
|
299 | start (); |
|
|
300 | } |
|
|
301 | |
|
|
302 | void sweep () |
|
|
303 | { |
|
|
304 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
|
|
305 | } |
|
|
306 | EV_END_WATCHER (embed, embed) |
|
|
307 | #endif |
232 | |
308 | |
233 | #undef EV_CONSTRUCT |
309 | #undef EV_CONSTRUCT |
|
|
310 | #undef EV_BEGIN_WATCHER |
234 | #undef EV_DECLARE_WATCHER |
311 | #undef EV_END_WATCHER |
235 | } |
312 | } |
236 | |
313 | |
237 | #endif |
314 | #endif |
238 | |
315 | |