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 |
10 | { |
10 | { |
11 | struct object { }; |
11 | struct klass; // it is vital that this is never defined |
12 | |
12 | |
13 | void *obj; |
13 | klass *o; |
14 | void (object::*meth)(watcher &, int); |
14 | void (klass::*m)(watcher &, int); |
15 | |
|
|
16 | /* a proxy is a kind of recipe on how to call a specific class method */ |
|
|
17 | struct proxy_base { |
|
|
18 | virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int) const = 0; |
|
|
19 | }; |
|
|
20 | template<class O1, class O2> |
|
|
21 | struct proxy : proxy_base { |
|
|
22 | virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int e) const |
|
|
23 | { |
|
|
24 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) |
|
|
25 | (w, e); |
|
|
26 | } |
|
|
27 | }; |
|
|
28 | |
|
|
29 | proxy_base *prxy; |
|
|
30 | |
15 | |
31 | public: |
16 | public: |
32 | template<class O1, class O2> |
17 | template<class O1, class O2> |
33 | explicit callback (O1 *object, void (O2::*method)(watcher &, int)) |
18 | explicit callback (O1 *object, void (O2::*method)(watcher &, int)) |
34 | { |
19 | { |
35 | static proxy<O1,O2> p; |
|
|
36 | obj = reinterpret_cast<void *>(object); |
20 | o = reinterpret_cast<klass *>(object); |
37 | meth = reinterpret_cast<void (object::*)(watcher &, int)>(method); |
21 | m = reinterpret_cast<void (klass::*)(watcher &, int)>(method); |
38 | prxy = &p; |
|
|
39 | } |
22 | } |
40 | |
23 | |
|
|
24 | // this works because a standards-compliant C++ compiler |
|
|
25 | // basically can't help it: it doesn't have the knowledge |
|
|
26 | // required to miscompile (klass is not defined anywhere |
|
|
27 | // and nothing is known about the constructor arguments) :) |
41 | void call (watcher *w, int e) const |
28 | void call (watcher *w, int revents) |
42 | { |
29 | { |
43 | return prxy->call (obj, meth, *w, e); |
30 | (o->*m) (*w, revents); |
44 | } |
31 | } |
45 | }; |
32 | }; |
46 | |
|
|
47 | #include "ev.h" |
|
|
48 | |
33 | |
49 | enum { |
34 | enum { |
50 | UNDEF = EV_UNDEF, |
35 | UNDEF = EV_UNDEF, |
51 | NONE = EV_NONE, |
36 | NONE = EV_NONE, |
52 | READ = EV_READ, |
37 | READ = EV_READ, |
53 | WRITE = EV_WRITE, |
38 | WRITE = EV_WRITE, |
54 | TIMEOUT = EV_TIMEOUT, |
39 | TIMEOUT = EV_TIMEOUT, |
55 | PERIODIC = EV_PERIODIC, |
40 | PERIODIC = EV_PERIODIC, |
56 | SIGNAL = EV_SIGNAL, |
41 | SIGNAL = EV_SIGNAL, |
|
|
42 | CHILD = EV_CHILD, |
|
|
43 | STAT = EV_STAT, |
57 | IDLE = EV_IDLE, |
44 | IDLE = EV_IDLE, |
58 | CHECK = EV_CHECK, |
45 | CHECK = EV_CHECK, |
59 | PREPARE = EV_PREPARE, |
46 | PREPARE = EV_PREPARE, |
60 | CHILD = EV_CHILD, |
47 | FORK = EV_FORK, |
|
|
48 | EMBED = EV_EMBED, |
61 | ERROR = EV_ERROR, |
49 | ERROR = EV_ERROR, |
62 | }; |
50 | }; |
63 | |
51 | |
64 | typedef ev_tstamp tstamp; |
52 | typedef ev_tstamp tstamp; |
65 | |
53 | |
… | |
… | |
91 | |
79 | |
92 | #endif |
80 | #endif |
93 | |
81 | |
94 | /* using a template here would require quite a bit more lines, |
82 | /* using a template here would require quite a bit more lines, |
95 | * so a macro solution was chosen */ |
83 | * so a macro solution was chosen */ |
96 | #define EV_DECLARE_WATCHER(cppstem,cstem) \ |
84 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
97 | \ |
|
|
98 | extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
|
|
99 | \ |
85 | \ |
100 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
86 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
101 | { \ |
87 | { \ |
102 | EV_CONSTRUCT (cppstem) \ |
88 | EV_CONSTRUCT (cppstem) \ |
103 | { \ |
89 | { \ |
104 | ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
90 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
105 | } \ |
91 | } \ |
106 | \ |
92 | \ |
107 | bool is_active () const \ |
93 | bool is_active () const \ |
108 | { \ |
94 | { \ |
109 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
95 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
… | |
… | |
125 | } \ |
111 | } \ |
126 | \ |
112 | \ |
127 | void operator ()(int events = EV_UNDEF) \ |
113 | void operator ()(int events = EV_UNDEF) \ |
128 | { \ |
114 | { \ |
129 | return call (this, events); \ |
115 | return call (this, events); \ |
|
|
116 | } \ |
|
|
117 | \ |
|
|
118 | ~cppstem () \ |
|
|
119 | { \ |
|
|
120 | stop (); \ |
130 | } \ |
121 | } \ |
131 | \ |
122 | \ |
132 | private: \ |
123 | private: \ |
133 | \ |
124 | \ |
134 | cppstem (const cppstem &o) \ |
125 | cppstem (const cppstem &o) \ |
135 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
126 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
136 | { /* disabled */ } \ |
127 | { /* disabled */ } \ |
|
|
128 | \ |
137 | void operator =(const cppstem &o) { /* disabled */ } \ |
129 | void operator =(const cppstem &o) { /* disabled */ } \ |
138 | \ |
130 | \ |
|
|
131 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
132 | { \ |
|
|
133 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
134 | } \ |
|
|
135 | \ |
139 | public: |
136 | public: |
140 | |
137 | |
|
|
138 | #define EV_END_WATCHER(cppstem,cstem) \ |
|
|
139 | }; |
|
|
140 | |
141 | EV_DECLARE_WATCHER (io, io) |
141 | EV_BEGIN_WATCHER (io, io) |
142 | void set (int fd, int events) |
142 | void set (int fd, int events) |
143 | { |
143 | { |
144 | int active = is_active (); |
144 | int active = is_active (); |
145 | if (active) stop (); |
145 | if (active) stop (); |
146 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
146 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
… | |
… | |
158 | void start (int fd, int events) |
158 | void start (int fd, int events) |
159 | { |
159 | { |
160 | set (fd, events); |
160 | set (fd, events); |
161 | start (); |
161 | start (); |
162 | } |
162 | } |
163 | }; |
163 | EV_END_WATCHER (io, io) |
164 | |
164 | |
165 | EV_DECLARE_WATCHER (timer, timer) |
165 | EV_BEGIN_WATCHER (timer, timer) |
166 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
166 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
167 | { |
167 | { |
168 | int active = is_active (); |
168 | int active = is_active (); |
169 | if (active) stop (); |
169 | if (active) stop (); |
170 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
170 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
… | |
… | |
179 | |
179 | |
180 | void again () |
180 | void again () |
181 | { |
181 | { |
182 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
182 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
183 | } |
183 | } |
184 | }; |
184 | EV_END_WATCHER (timer, timer) |
185 | |
185 | |
|
|
186 | #if EV_PERIODIC_ENABLE |
186 | EV_DECLARE_WATCHER (periodic, periodic) |
187 | EV_BEGIN_WATCHER (periodic, periodic) |
187 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
188 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
188 | { |
189 | { |
189 | int active = is_active (); |
190 | int active = is_active (); |
190 | if (active) stop (); |
191 | if (active) stop (); |
191 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
192 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
… | |
… | |
200 | |
201 | |
201 | void again () |
202 | void again () |
202 | { |
203 | { |
203 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
204 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
204 | } |
205 | } |
205 | }; |
206 | EV_END_WATCHER (periodic, periodic) |
|
|
207 | #endif |
206 | |
208 | |
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) |
209 | EV_BEGIN_WATCHER (sig, signal) |
217 | void set (int signum) |
210 | void set (int signum) |
218 | { |
211 | { |
219 | int active = is_active (); |
212 | int active = is_active (); |
220 | if (active) stop (); |
213 | if (active) stop (); |
221 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
214 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
225 | void start (int signum) |
218 | void start (int signum) |
226 | { |
219 | { |
227 | set (signum); |
220 | set (signum); |
228 | start (); |
221 | start (); |
229 | } |
222 | } |
230 | }; |
223 | EV_END_WATCHER (sig, signal) |
231 | |
224 | |
232 | EV_DECLARE_WATCHER (child, child) |
225 | EV_BEGIN_WATCHER (child, child) |
233 | void set (int pid) |
226 | void set (int pid) |
234 | { |
227 | { |
235 | int active = is_active (); |
228 | int active = is_active (); |
236 | if (active) stop (); |
229 | if (active) stop (); |
237 | ev_child_set (static_cast<ev_child *>(this), pid); |
230 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
241 | void start (int pid) |
234 | void start (int pid) |
242 | { |
235 | { |
243 | set (pid); |
236 | set (pid); |
244 | start (); |
237 | start (); |
245 | } |
238 | } |
246 | }; |
239 | EV_END_WATCHER (child, child) |
|
|
240 | |
|
|
241 | #if EV_STAT_ENABLE |
|
|
242 | EV_BEGIN_WATCHER (stat, stat) |
|
|
243 | void set (const char *path, ev_tstamp interval = 0.) |
|
|
244 | { |
|
|
245 | int active = is_active (); |
|
|
246 | if (active) stop (); |
|
|
247 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
|
|
248 | if (active) start (); |
|
|
249 | } |
|
|
250 | |
|
|
251 | void start (const char *path, ev_tstamp interval = 0.) |
|
|
252 | { |
|
|
253 | stop (); |
|
|
254 | set (path, interval); |
|
|
255 | start (); |
|
|
256 | } |
|
|
257 | |
|
|
258 | void update () |
|
|
259 | { |
|
|
260 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
|
|
261 | } |
|
|
262 | EV_END_WATCHER (stat, stat) |
|
|
263 | #endif |
|
|
264 | |
|
|
265 | EV_BEGIN_WATCHER (idle, idle) |
|
|
266 | void set () { } |
|
|
267 | EV_END_WATCHER (idle, idle) |
|
|
268 | |
|
|
269 | EV_BEGIN_WATCHER (prepare, prepare) |
|
|
270 | void set () { } |
|
|
271 | EV_END_WATCHER (prepare, prepare) |
|
|
272 | |
|
|
273 | EV_BEGIN_WATCHER (check, check) |
|
|
274 | void set () { } |
|
|
275 | EV_END_WATCHER (check, check) |
|
|
276 | |
|
|
277 | #if EV_EMBED_ENABLE |
|
|
278 | EV_BEGIN_WATCHER (embed, embed) |
|
|
279 | void start (struct ev_loop *embedded_loop) |
|
|
280 | { |
|
|
281 | stop (); |
|
|
282 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
283 | start (); |
|
|
284 | } |
|
|
285 | |
|
|
286 | void sweep () |
|
|
287 | { |
|
|
288 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
|
|
289 | } |
|
|
290 | EV_END_WATCHER (embed, embed) |
|
|
291 | #endif |
|
|
292 | |
|
|
293 | #if EV_FORK_ENABLE |
|
|
294 | EV_BEGIN_WATCHER (fork, fork) |
|
|
295 | void set () { } |
|
|
296 | EV_END_WATCHER (fork, fork) |
|
|
297 | #endif |
247 | |
298 | |
248 | #undef EV_CONSTRUCT |
299 | #undef EV_CONSTRUCT |
|
|
300 | #undef EV_BEGIN_WATCHER |
249 | #undef EV_DECLARE_WATCHER |
301 | #undef EV_END_WATCHER |
250 | } |
302 | } |
251 | |
303 | |
252 | #endif |
304 | #endif |
253 | |
305 | |