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 |
… | |
… | |
41 | void call (watcher *w, int e) const |
41 | void call (watcher *w, int e) const |
42 | { |
42 | { |
43 | return prxy->call (obj, meth, *w, e); |
43 | return prxy->call (obj, meth, *w, e); |
44 | } |
44 | } |
45 | }; |
45 | }; |
46 | |
|
|
47 | #include "ev.h" |
|
|
48 | |
46 | |
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, |
… | |
… | |
93 | |
91 | |
94 | /* using a template here would require quite a bit more lines, |
92 | /* using a template here would require quite a bit more lines, |
95 | * so a macro solution was chosen */ |
93 | * so a macro solution was chosen */ |
96 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
94 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
97 | \ |
95 | \ |
98 | static void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
|
|
99 | \ |
|
|
100 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
96 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
101 | { \ |
97 | { \ |
102 | EV_CONSTRUCT (cppstem) \ |
98 | EV_CONSTRUCT (cppstem) \ |
103 | { \ |
99 | { \ |
104 | ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
100 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
105 | } \ |
101 | } \ |
106 | \ |
102 | \ |
107 | bool is_active () const \ |
103 | bool is_active () const \ |
108 | { \ |
104 | { \ |
109 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
105 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
… | |
… | |
137 | private: \ |
133 | private: \ |
138 | \ |
134 | \ |
139 | cppstem (const cppstem &o) \ |
135 | cppstem (const cppstem &o) \ |
140 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
136 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
141 | { /* disabled */ } \ |
137 | { /* disabled */ } \ |
|
|
138 | \ |
142 | void operator =(const cppstem &o) { /* disabled */ } \ |
139 | void operator =(const cppstem &o) { /* disabled */ } \ |
143 | \ |
140 | \ |
|
|
141 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
142 | { \ |
|
|
143 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
144 | } \ |
|
|
145 | \ |
144 | public: |
146 | public: |
145 | |
147 | |
146 | #define EV_END_WATCHER(cppstem,cstem) \ |
148 | #define EV_END_WATCHER(cppstem,cstem) \ |
147 | }; \ |
|
|
148 | \ |
|
|
149 | static void cb_ ## cppstem (struct ev_ ## cstem *w, int revents) \ |
|
|
150 | { \ |
|
|
151 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
152 | } |
149 | }; |
153 | |
150 | |
154 | EV_BEGIN_WATCHER (io, io) |
151 | EV_BEGIN_WATCHER (io, io) |
155 | void set (int fd, int events) |
152 | void set (int fd, int events) |
156 | { |
153 | { |
157 | int active = is_active (); |
154 | int active = is_active (); |
… | |
… | |
218 | } |
215 | } |
219 | EV_END_WATCHER (periodic, periodic) |
216 | EV_END_WATCHER (periodic, periodic) |
220 | #endif |
217 | #endif |
221 | |
218 | |
222 | EV_BEGIN_WATCHER (idle, idle) |
219 | EV_BEGIN_WATCHER (idle, idle) |
|
|
220 | void set () { } |
223 | EV_END_WATCHER (idle, idle) |
221 | EV_END_WATCHER (idle, idle) |
224 | |
222 | |
225 | EV_BEGIN_WATCHER (prepare, prepare) |
223 | EV_BEGIN_WATCHER (prepare, prepare) |
|
|
224 | void set () { } |
226 | EV_END_WATCHER (prepare, prepare) |
225 | EV_END_WATCHER (prepare, prepare) |
227 | |
226 | |
228 | EV_BEGIN_WATCHER (check, check) |
227 | EV_BEGIN_WATCHER (check, check) |
|
|
228 | void set () { } |
229 | EV_END_WATCHER (check, check) |
229 | EV_END_WATCHER (check, check) |
230 | |
230 | |
231 | EV_BEGIN_WATCHER (sig, signal) |
231 | EV_BEGIN_WATCHER (sig, signal) |
232 | void set (int signum) |
232 | void set (int signum) |
233 | { |
233 | { |
… | |
… | |
258 | set (pid); |
258 | set (pid); |
259 | start (); |
259 | start (); |
260 | } |
260 | } |
261 | EV_END_WATCHER (child, child) |
261 | EV_END_WATCHER (child, child) |
262 | |
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 |
|
|
287 | |
263 | #undef EV_CONSTRUCT |
288 | #undef EV_CONSTRUCT |
264 | #undef EV_BEGIN_WATCHER |
289 | #undef EV_BEGIN_WATCHER |
|
|
290 | #undef EV_END_WATCHER |
265 | } |
291 | } |
266 | |
292 | |
267 | #endif |
293 | #endif |
268 | |
294 | |