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)); \ |
… | |
… | |
125 | } \ |
124 | } \ |
126 | \ |
125 | \ |
127 | void operator ()(int events = EV_UNDEF) \ |
126 | void operator ()(int events = EV_UNDEF) \ |
128 | { \ |
127 | { \ |
129 | return call (this, events); \ |
128 | return call (this, events); \ |
|
|
129 | } \ |
|
|
130 | \ |
|
|
131 | ~cppstem () \ |
|
|
132 | { \ |
|
|
133 | stop (); \ |
130 | } \ |
134 | } \ |
131 | \ |
135 | \ |
132 | private: \ |
136 | private: \ |
133 | \ |
137 | \ |
134 | cppstem (const cppstem &o) \ |
138 | cppstem (const cppstem &o) \ |
135 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
139 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
136 | { /* disabled */ } \ |
140 | { /* disabled */ } \ |
|
|
141 | \ |
137 | void operator =(const cppstem &o) { /* disabled */ } \ |
142 | void operator =(const cppstem &o) { /* disabled */ } \ |
138 | \ |
143 | \ |
|
|
144 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
145 | { \ |
|
|
146 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
147 | } \ |
|
|
148 | \ |
139 | public: |
149 | public: |
140 | |
150 | |
|
|
151 | #define EV_END_WATCHER(cppstem,cstem) \ |
|
|
152 | }; |
|
|
153 | |
141 | EV_DECLARE_WATCHER (io, io) |
154 | EV_BEGIN_WATCHER (io, io) |
142 | void set (int fd, int events) |
155 | void set (int fd, int events) |
143 | { |
156 | { |
144 | int active = is_active (); |
157 | int active = is_active (); |
145 | if (active) stop (); |
158 | if (active) stop (); |
146 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
159 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
… | |
… | |
158 | void start (int fd, int events) |
171 | void start (int fd, int events) |
159 | { |
172 | { |
160 | set (fd, events); |
173 | set (fd, events); |
161 | start (); |
174 | start (); |
162 | } |
175 | } |
163 | }; |
176 | EV_END_WATCHER (io, io) |
164 | |
177 | |
165 | EV_DECLARE_WATCHER (timer, timer) |
178 | EV_BEGIN_WATCHER (timer, timer) |
166 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
179 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
167 | { |
180 | { |
168 | int active = is_active (); |
181 | int active = is_active (); |
169 | if (active) stop (); |
182 | if (active) stop (); |
170 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
183 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
… | |
… | |
179 | |
192 | |
180 | void again () |
193 | void again () |
181 | { |
194 | { |
182 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
195 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
183 | } |
196 | } |
184 | }; |
197 | EV_END_WATCHER (timer, timer) |
185 | |
198 | |
|
|
199 | #if EV_PERIODIC_ENABLE |
186 | EV_DECLARE_WATCHER (periodic, periodic) |
200 | EV_BEGIN_WATCHER (periodic, periodic) |
187 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
201 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
188 | { |
202 | { |
189 | int active = is_active (); |
203 | int active = is_active (); |
190 | if (active) stop (); |
204 | if (active) stop (); |
191 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
205 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
… | |
… | |
200 | |
214 | |
201 | void again () |
215 | void again () |
202 | { |
216 | { |
203 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
217 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
204 | } |
218 | } |
205 | }; |
219 | EV_END_WATCHER (periodic, periodic) |
|
|
220 | #endif |
206 | |
221 | |
207 | EV_DECLARE_WATCHER (idle, idle) |
|
|
208 | }; |
|
|
209 | |
|
|
210 | EV_DECLARE_WATCHER (prepare, prepare) |
|
|
211 | }; |
|
|
212 | |
|
|
213 | EV_DECLARE_WATCHER (check, check) |
|
|
214 | }; |
|
|
215 | |
|
|
216 | EV_DECLARE_WATCHER (sig, signal) |
222 | EV_BEGIN_WATCHER (sig, signal) |
217 | void set (int signum) |
223 | void set (int signum) |
218 | { |
224 | { |
219 | int active = is_active (); |
225 | int active = is_active (); |
220 | if (active) stop (); |
226 | if (active) stop (); |
221 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
227 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
225 | void start (int signum) |
231 | void start (int signum) |
226 | { |
232 | { |
227 | set (signum); |
233 | set (signum); |
228 | start (); |
234 | start (); |
229 | } |
235 | } |
230 | }; |
236 | EV_END_WATCHER (sig, signal) |
231 | |
237 | |
232 | EV_DECLARE_WATCHER (child, child) |
238 | EV_BEGIN_WATCHER (child, child) |
233 | void set (int pid) |
239 | void set (int pid) |
234 | { |
240 | { |
235 | int active = is_active (); |
241 | int active = is_active (); |
236 | if (active) stop (); |
242 | if (active) stop (); |
237 | ev_child_set (static_cast<ev_child *>(this), pid); |
243 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
241 | void start (int pid) |
247 | void start (int pid) |
242 | { |
248 | { |
243 | set (pid); |
249 | set (pid); |
244 | start (); |
250 | start (); |
245 | } |
251 | } |
246 | }; |
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 |
247 | |
311 | |
248 | #undef EV_CONSTRUCT |
312 | #undef EV_CONSTRUCT |
|
|
313 | #undef EV_BEGIN_WATCHER |
249 | #undef EV_DECLARE_WATCHER |
314 | #undef EV_END_WATCHER |
250 | } |
315 | } |
251 | |
316 | |
252 | #endif |
317 | #endif |
253 | |
318 | |