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 | CHILD = EV_CHILD, |
|
|
56 | STAT = EV_STAT, |
|
|
57 | IDLE = EV_IDLE, |
|
|
58 | CHECK = EV_CHECK, |
|
|
59 | PREPARE = EV_PREPARE, |
|
|
60 | FORK = EV_FORK, |
|
|
61 | EMBED = EV_EMBED, |
|
|
62 | ERROR = EV_ERROR, |
|
|
63 | }; |
48 | |
64 | |
49 | typedef ev_tstamp tstamp; |
65 | typedef ev_tstamp tstamp; |
50 | |
66 | |
51 | inline ev_tstamp now (EV_P) |
67 | inline ev_tstamp now (EV_P) |
52 | { |
68 | { |
… | |
… | |
76 | |
92 | |
77 | #endif |
93 | #endif |
78 | |
94 | |
79 | /* using a template here would require quite a bit more lines, |
95 | /* using a template here would require quite a bit more lines, |
80 | * so a macro solution was chosen */ |
96 | * so a macro solution was chosen */ |
81 | #define EV_DECLARE_WATCHER(cppstem,cstem) \ |
97 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
82 | \ |
|
|
83 | extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
|
|
84 | \ |
98 | \ |
85 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
99 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
86 | { \ |
100 | { \ |
87 | EV_CONSTRUCT (cppstem) \ |
101 | EV_CONSTRUCT (cppstem) \ |
88 | { \ |
102 | { \ |
89 | ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
103 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
90 | } \ |
104 | } \ |
91 | \ |
105 | \ |
92 | bool is_active () const \ |
106 | bool is_active () const \ |
93 | { \ |
107 | { \ |
94 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
108 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
… | |
… | |
110 | } \ |
124 | } \ |
111 | \ |
125 | \ |
112 | void operator ()(int events = EV_UNDEF) \ |
126 | void operator ()(int events = EV_UNDEF) \ |
113 | { \ |
127 | { \ |
114 | return call (this, events); \ |
128 | return call (this, events); \ |
|
|
129 | } \ |
|
|
130 | \ |
|
|
131 | ~cppstem () \ |
|
|
132 | { \ |
|
|
133 | stop (); \ |
115 | } \ |
134 | } \ |
116 | \ |
135 | \ |
117 | private: \ |
136 | private: \ |
118 | \ |
137 | \ |
119 | cppstem (const cppstem &o) \ |
138 | cppstem (const cppstem &o) \ |
120 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
139 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
121 | { /* disabled */ } \ |
140 | { /* disabled */ } \ |
|
|
141 | \ |
122 | void operator =(const cppstem &o) { /* disabled */ } \ |
142 | void operator =(const cppstem &o) { /* disabled */ } \ |
123 | \ |
143 | \ |
|
|
144 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
145 | { \ |
|
|
146 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
147 | } \ |
|
|
148 | \ |
124 | public: |
149 | public: |
125 | |
150 | |
|
|
151 | #define EV_END_WATCHER(cppstem,cstem) \ |
|
|
152 | }; |
|
|
153 | |
126 | EV_DECLARE_WATCHER (io, io) |
154 | EV_BEGIN_WATCHER (io, io) |
127 | void set (int fd, int events) |
155 | void set (int fd, int events) |
128 | { |
156 | { |
129 | int active = is_active (); |
157 | int active = is_active (); |
130 | if (active) stop (); |
158 | if (active) stop (); |
131 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
159 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
… | |
… | |
143 | void start (int fd, int events) |
171 | void start (int fd, int events) |
144 | { |
172 | { |
145 | set (fd, events); |
173 | set (fd, events); |
146 | start (); |
174 | start (); |
147 | } |
175 | } |
148 | }; |
176 | EV_END_WATCHER (io, io) |
149 | |
177 | |
150 | EV_DECLARE_WATCHER (timer, timer) |
178 | EV_BEGIN_WATCHER (timer, timer) |
151 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
179 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
152 | { |
180 | { |
153 | int active = is_active (); |
181 | int active = is_active (); |
154 | if (active) stop (); |
182 | if (active) stop (); |
155 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
183 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
… | |
… | |
164 | |
192 | |
165 | void again () |
193 | void again () |
166 | { |
194 | { |
167 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
195 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
168 | } |
196 | } |
169 | }; |
197 | EV_END_WATCHER (timer, timer) |
170 | |
198 | |
|
|
199 | #if EV_PERIODIC_ENABLE |
171 | EV_DECLARE_WATCHER (periodic, periodic) |
200 | EV_BEGIN_WATCHER (periodic, periodic) |
172 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
201 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
173 | { |
202 | { |
174 | int active = is_active (); |
203 | int active = is_active (); |
175 | if (active) stop (); |
204 | if (active) stop (); |
176 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
205 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
… | |
… | |
185 | |
214 | |
186 | void again () |
215 | void again () |
187 | { |
216 | { |
188 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
217 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
189 | } |
218 | } |
190 | }; |
219 | EV_END_WATCHER (periodic, periodic) |
|
|
220 | #endif |
191 | |
221 | |
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) |
222 | EV_BEGIN_WATCHER (sig, signal) |
202 | void set (int signum) |
223 | void set (int signum) |
203 | { |
224 | { |
204 | int active = is_active (); |
225 | int active = is_active (); |
205 | if (active) stop (); |
226 | if (active) stop (); |
206 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
227 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
210 | void start (int signum) |
231 | void start (int signum) |
211 | { |
232 | { |
212 | set (signum); |
233 | set (signum); |
213 | start (); |
234 | start (); |
214 | } |
235 | } |
215 | }; |
236 | EV_END_WATCHER (sig, signal) |
216 | |
237 | |
217 | EV_DECLARE_WATCHER (child, child) |
238 | EV_BEGIN_WATCHER (child, child) |
218 | void set (int pid) |
239 | void set (int pid) |
219 | { |
240 | { |
220 | int active = is_active (); |
241 | int active = is_active (); |
221 | if (active) stop (); |
242 | if (active) stop (); |
222 | ev_child_set (static_cast<ev_child *>(this), pid); |
243 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
226 | void start (int pid) |
247 | void start (int pid) |
227 | { |
248 | { |
228 | set (pid); |
249 | set (pid); |
229 | start (); |
250 | start (); |
230 | } |
251 | } |
231 | }; |
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 |
232 | |
311 | |
233 | #undef EV_CONSTRUCT |
312 | #undef EV_CONSTRUCT |
|
|
313 | #undef EV_BEGIN_WATCHER |
234 | #undef EV_DECLARE_WATCHER |
314 | #undef EV_END_WATCHER |
235 | } |
315 | } |
236 | |
316 | |
237 | #endif |
317 | #endif |
238 | |
318 | |