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_PERIODICS |
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 | |
|
|
219 | EV_BEGIN_WATCHER (idle, idle) |
|
|
220 | void set () { } |
192 | EV_DECLARE_WATCHER (idle, idle) |
221 | EV_END_WATCHER (idle, idle) |
193 | }; |
|
|
194 | |
222 | |
|
|
223 | EV_BEGIN_WATCHER (prepare, prepare) |
|
|
224 | void set () { } |
195 | EV_DECLARE_WATCHER (prepare, prepare) |
225 | EV_END_WATCHER (prepare, prepare) |
196 | }; |
|
|
197 | |
226 | |
|
|
227 | EV_BEGIN_WATCHER (check, check) |
|
|
228 | void set () { } |
198 | EV_DECLARE_WATCHER (check, check) |
229 | EV_END_WATCHER (check, check) |
199 | }; |
|
|
200 | |
230 | |
201 | EV_DECLARE_WATCHER (sig, signal) |
231 | EV_BEGIN_WATCHER (sig, signal) |
202 | void set (int signum) |
232 | void set (int signum) |
203 | { |
233 | { |
204 | int active = is_active (); |
234 | int active = is_active (); |
205 | if (active) stop (); |
235 | if (active) stop (); |
206 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
236 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
210 | void start (int signum) |
240 | void start (int signum) |
211 | { |
241 | { |
212 | set (signum); |
242 | set (signum); |
213 | start (); |
243 | start (); |
214 | } |
244 | } |
215 | }; |
245 | EV_END_WATCHER (sig, signal) |
216 | |
246 | |
217 | EV_DECLARE_WATCHER (child, child) |
247 | EV_BEGIN_WATCHER (child, child) |
218 | void set (int pid) |
248 | void set (int pid) |
219 | { |
249 | { |
220 | int active = is_active (); |
250 | int active = is_active (); |
221 | if (active) stop (); |
251 | if (active) stop (); |
222 | ev_child_set (static_cast<ev_child *>(this), pid); |
252 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
226 | void start (int pid) |
256 | void start (int pid) |
227 | { |
257 | { |
228 | set (pid); |
258 | set (pid); |
229 | start (); |
259 | start (); |
230 | } |
260 | } |
231 | }; |
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 |
232 | |
287 | |
233 | #undef EV_CONSTRUCT |
288 | #undef EV_CONSTRUCT |
|
|
289 | #undef EV_BEGIN_WATCHER |
234 | #undef EV_DECLARE_WATCHER |
290 | #undef EV_END_WATCHER |
235 | } |
291 | } |
236 | |
292 | |
237 | #endif |
293 | #endif |
238 | |
294 | |