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 | #ifdef EV_H |
|
|
5 | # include EV_H |
|
|
6 | #else |
|
|
7 | # include <ev.h> |
|
|
8 | #endif |
5 | |
9 | |
6 | namespace ev { |
10 | namespace ev { |
7 | |
11 | |
8 | template<class watcher> |
12 | template<class ev_watcher, class watcher> |
9 | class callback |
13 | struct base : ev_watcher |
10 | { |
14 | { |
11 | struct object { }; |
15 | #if EV_MULTIPLICITY |
|
|
16 | EV_P; |
12 | |
17 | |
13 | void *obj; |
18 | void set (EV_P) |
14 | void (object::*meth)(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 | { |
19 | { |
24 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) |
20 | this->EV_A = EV_A; |
25 | (w, e); |
|
|
26 | } |
21 | } |
|
|
22 | #endif |
|
|
23 | |
|
|
24 | base () |
|
|
25 | { |
|
|
26 | ev_init (this, 0); |
27 | }; |
27 | } |
28 | |
28 | |
29 | proxy_base *prxy; |
29 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
30 | |
|
|
31 | public: |
|
|
32 | template<class O1, class O2> |
|
|
33 | explicit callback (O1 *object, void (O2::*method)(watcher &, int)) |
|
|
34 | { |
|
|
35 | static proxy<O1,O2> p; |
|
|
36 | obj = reinterpret_cast<void *>(object); |
|
|
37 | meth = reinterpret_cast<void (object::*)(watcher &, int)>(method); |
|
|
38 | prxy = &p; |
|
|
39 | } |
30 | { |
40 | |
31 | this->data = data; |
41 | void call (watcher *w, int e) const |
32 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
42 | { |
33 | } |
43 | return prxy->call (obj, meth, *w, e); |
34 | |
|
|
35 | // method callback |
|
|
36 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
37 | void set (K *object) |
|
|
38 | { |
|
|
39 | set_ (object, method_thunk<K, method>); |
|
|
40 | } |
|
|
41 | |
|
|
42 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
43 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
44 | { |
|
|
45 | K *obj = static_cast<K *>(w->data); |
|
|
46 | (obj->*method) (*static_cast<watcher *>(w), revents); |
|
|
47 | } |
|
|
48 | |
|
|
49 | // const method callback |
|
|
50 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
51 | void set (const K *object) |
|
|
52 | { |
|
|
53 | set_ (object, const_method_thunk<K, method>); |
|
|
54 | } |
|
|
55 | |
|
|
56 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
57 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
58 | { |
|
|
59 | K *obj = static_cast<K *>(w->data); |
|
|
60 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
|
|
61 | } |
|
|
62 | |
|
|
63 | // function callback |
|
|
64 | template<void (*function)(watcher &w, int)> |
|
|
65 | void set (void *data = 0) |
|
|
66 | { |
|
|
67 | set_ (data, function_thunk<function>); |
|
|
68 | } |
|
|
69 | |
|
|
70 | template<void (*function)(watcher &w, int)> |
|
|
71 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
72 | { |
|
|
73 | function (*static_cast<watcher *>(w), revents); |
|
|
74 | } |
|
|
75 | |
|
|
76 | // simple callback |
|
|
77 | template<class K, void (K::*method)()> |
|
|
78 | void set (K *object) |
|
|
79 | { |
|
|
80 | set_ (object, method_noargs_thunk<K, method>); |
|
|
81 | } |
|
|
82 | |
|
|
83 | template<class K, void (K::*method)()> |
|
|
84 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
85 | { |
|
|
86 | K *obj = static_cast<K *>(w->data); |
|
|
87 | (obj->*method) (); |
|
|
88 | } |
|
|
89 | |
|
|
90 | void operator ()(int events = EV_UNDEF) |
|
|
91 | { |
|
|
92 | return ev_cb (static_cast<ev_watcher *>(this)) |
|
|
93 | (static_cast<ev_watcher *>(this), events); |
|
|
94 | } |
|
|
95 | |
|
|
96 | bool is_active () const |
|
|
97 | { |
|
|
98 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
|
|
99 | } |
|
|
100 | |
|
|
101 | bool is_pending () const |
|
|
102 | { |
|
|
103 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
44 | } |
104 | } |
45 | }; |
105 | }; |
46 | |
|
|
47 | #include "ev.h" |
|
|
48 | |
106 | |
49 | enum { |
107 | enum { |
50 | UNDEF = EV_UNDEF, |
108 | UNDEF = EV_UNDEF, |
51 | NONE = EV_NONE, |
109 | NONE = EV_NONE, |
52 | READ = EV_READ, |
110 | READ = EV_READ, |
53 | WRITE = EV_WRITE, |
111 | WRITE = EV_WRITE, |
54 | TIMEOUT = EV_TIMEOUT, |
112 | TIMEOUT = EV_TIMEOUT, |
55 | PERIODIC = EV_PERIODIC, |
113 | PERIODIC = EV_PERIODIC, |
56 | SIGNAL = EV_SIGNAL, |
114 | SIGNAL = EV_SIGNAL, |
|
|
115 | CHILD = EV_CHILD, |
|
|
116 | STAT = EV_STAT, |
57 | IDLE = EV_IDLE, |
117 | IDLE = EV_IDLE, |
58 | CHECK = EV_CHECK, |
118 | CHECK = EV_CHECK, |
59 | PREPARE = EV_PREPARE, |
119 | PREPARE = EV_PREPARE, |
60 | CHILD = EV_CHILD, |
120 | FORK = EV_FORK, |
|
|
121 | EMBED = EV_EMBED, |
61 | ERROR = EV_ERROR, |
122 | ERROR = EV_ERROR, |
62 | }; |
123 | }; |
63 | |
124 | |
64 | typedef ev_tstamp tstamp; |
125 | typedef ev_tstamp tstamp; |
65 | |
126 | |
… | |
… | |
67 | { |
128 | { |
68 | return ev_now (EV_A); |
129 | return ev_now (EV_A); |
69 | } |
130 | } |
70 | |
131 | |
71 | #if EV_MULTIPLICITY |
132 | #if EV_MULTIPLICITY |
72 | |
|
|
73 | #define EV_CONSTRUCT(cppstem) \ |
|
|
74 | EV_P; \ |
133 | #define EV_CONSTRUCT \ |
75 | \ |
|
|
76 | void set (EV_P) \ |
134 | (EV_P = EV_DEFAULT) \ |
77 | { \ |
135 | { \ |
78 | this->EV_A = EV_A; \ |
|
|
79 | } \ |
|
|
80 | \ |
136 | set (EV_A); \ |
81 | template<class O1, class O2> \ |
137 | } |
82 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \ |
|
|
83 | : callback<cppstem> (object, method), EV_A (EV_A) |
|
|
84 | |
|
|
85 | #else |
138 | #else |
86 | |
139 | #define EV_CONSTRUCT \ |
87 | #define EV_CONSTRUCT(cppstem) \ |
140 | () \ |
88 | template<class O1, class O2> \ |
141 | { \ |
89 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
142 | } |
90 | : callback<cppstem> (object, method) |
|
|
91 | |
|
|
92 | #endif |
143 | #endif |
93 | |
144 | |
94 | /* using a template here would require quite a bit more lines, |
145 | /* using a template here would require quite a bit more lines, |
95 | * so a macro solution was chosen */ |
146 | * so a macro solution was chosen */ |
96 | #define EV_DECLARE_WATCHER(cppstem,cstem) \ |
147 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
97 | \ |
148 | \ |
98 | extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
|
|
99 | \ |
|
|
100 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
149 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
101 | { \ |
150 | { \ |
102 | EV_CONSTRUCT (cppstem) \ |
|
|
103 | { \ |
|
|
104 | ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
|
|
105 | } \ |
|
|
106 | \ |
|
|
107 | bool is_active () const \ |
|
|
108 | { \ |
|
|
109 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
110 | } \ |
|
|
111 | \ |
|
|
112 | bool is_pending () const \ |
|
|
113 | { \ |
|
|
114 | return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
115 | } \ |
|
|
116 | \ |
|
|
117 | void start () \ |
151 | void start () \ |
118 | { \ |
152 | { \ |
119 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
153 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
120 | } \ |
154 | } \ |
121 | \ |
155 | \ |
122 | void stop () \ |
156 | void stop () \ |
123 | { \ |
157 | { \ |
124 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
158 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
125 | } \ |
159 | } \ |
126 | \ |
160 | \ |
127 | void operator ()(int events = EV_UNDEF) \ |
161 | cppstem EV_CONSTRUCT \ |
|
|
162 | \ |
|
|
163 | ~cppstem () \ |
128 | { \ |
164 | { \ |
129 | return call (this, events); \ |
165 | stop (); \ |
130 | } \ |
166 | } \ |
131 | \ |
167 | \ |
|
|
168 | using base<ev_ ## cstem, cppstem>::set; \ |
|
|
169 | \ |
132 | private: \ |
170 | private: \ |
133 | \ |
171 | \ |
134 | cppstem (const cppstem &o) \ |
172 | cppstem (const cppstem &o) \ |
135 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
|
|
136 | { /* disabled */ } \ |
173 | { /* disabled */ } \ |
|
|
174 | \ |
137 | void operator =(const cppstem &o) { /* disabled */ } \ |
175 | void operator =(const cppstem &o) { /* disabled */ } \ |
138 | \ |
176 | \ |
139 | public: |
177 | public: |
140 | |
178 | |
|
|
179 | #define EV_END_WATCHER(cppstem,cstem) \ |
|
|
180 | }; |
|
|
181 | |
141 | EV_DECLARE_WATCHER (io, io) |
182 | EV_BEGIN_WATCHER (io, io) |
142 | void set (int fd, int events) |
183 | void set (int fd, int events) |
143 | { |
184 | { |
144 | int active = is_active (); |
185 | int active = is_active (); |
145 | if (active) stop (); |
186 | if (active) stop (); |
146 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
187 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
… | |
… | |
158 | void start (int fd, int events) |
199 | void start (int fd, int events) |
159 | { |
200 | { |
160 | set (fd, events); |
201 | set (fd, events); |
161 | start (); |
202 | start (); |
162 | } |
203 | } |
163 | }; |
204 | EV_END_WATCHER (io, io) |
164 | |
205 | |
165 | EV_DECLARE_WATCHER (timer, timer) |
206 | EV_BEGIN_WATCHER (timer, timer) |
166 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
207 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
167 | { |
208 | { |
168 | int active = is_active (); |
209 | int active = is_active (); |
169 | if (active) stop (); |
210 | if (active) stop (); |
170 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
211 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
… | |
… | |
179 | |
220 | |
180 | void again () |
221 | void again () |
181 | { |
222 | { |
182 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
223 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
183 | } |
224 | } |
184 | }; |
225 | EV_END_WATCHER (timer, timer) |
185 | |
226 | |
|
|
227 | #if EV_PERIODIC_ENABLE |
186 | EV_DECLARE_WATCHER (periodic, periodic) |
228 | EV_BEGIN_WATCHER (periodic, periodic) |
187 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
229 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
188 | { |
230 | { |
189 | int active = is_active (); |
231 | int active = is_active (); |
190 | if (active) stop (); |
232 | if (active) stop (); |
191 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
233 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
… | |
… | |
200 | |
242 | |
201 | void again () |
243 | void again () |
202 | { |
244 | { |
203 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
245 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
204 | } |
246 | } |
205 | }; |
247 | EV_END_WATCHER (periodic, periodic) |
|
|
248 | #endif |
206 | |
249 | |
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) |
250 | EV_BEGIN_WATCHER (sig, signal) |
217 | void set (int signum) |
251 | void set (int signum) |
218 | { |
252 | { |
219 | int active = is_active (); |
253 | int active = is_active (); |
220 | if (active) stop (); |
254 | if (active) stop (); |
221 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
255 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
225 | void start (int signum) |
259 | void start (int signum) |
226 | { |
260 | { |
227 | set (signum); |
261 | set (signum); |
228 | start (); |
262 | start (); |
229 | } |
263 | } |
230 | }; |
264 | EV_END_WATCHER (sig, signal) |
231 | |
265 | |
232 | EV_DECLARE_WATCHER (child, child) |
266 | EV_BEGIN_WATCHER (child, child) |
233 | void set (int pid) |
267 | void set (int pid) |
234 | { |
268 | { |
235 | int active = is_active (); |
269 | int active = is_active (); |
236 | if (active) stop (); |
270 | if (active) stop (); |
237 | ev_child_set (static_cast<ev_child *>(this), pid); |
271 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
241 | void start (int pid) |
275 | void start (int pid) |
242 | { |
276 | { |
243 | set (pid); |
277 | set (pid); |
244 | start (); |
278 | start (); |
245 | } |
279 | } |
246 | }; |
280 | EV_END_WATCHER (child, child) |
|
|
281 | |
|
|
282 | #if EV_STAT_ENABLE |
|
|
283 | EV_BEGIN_WATCHER (stat, stat) |
|
|
284 | void set (const char *path, ev_tstamp interval = 0.) |
|
|
285 | { |
|
|
286 | int active = is_active (); |
|
|
287 | if (active) stop (); |
|
|
288 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
|
|
289 | if (active) start (); |
|
|
290 | } |
|
|
291 | |
|
|
292 | void start (const char *path, ev_tstamp interval = 0.) |
|
|
293 | { |
|
|
294 | stop (); |
|
|
295 | set (path, interval); |
|
|
296 | start (); |
|
|
297 | } |
|
|
298 | |
|
|
299 | void update () |
|
|
300 | { |
|
|
301 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
|
|
302 | } |
|
|
303 | EV_END_WATCHER (stat, stat) |
|
|
304 | #endif |
|
|
305 | |
|
|
306 | EV_BEGIN_WATCHER (idle, idle) |
|
|
307 | void set () { } |
|
|
308 | EV_END_WATCHER (idle, idle) |
|
|
309 | |
|
|
310 | EV_BEGIN_WATCHER (prepare, prepare) |
|
|
311 | void set () { } |
|
|
312 | EV_END_WATCHER (prepare, prepare) |
|
|
313 | |
|
|
314 | EV_BEGIN_WATCHER (check, check) |
|
|
315 | void set () { } |
|
|
316 | EV_END_WATCHER (check, check) |
|
|
317 | |
|
|
318 | #if EV_EMBED_ENABLE |
|
|
319 | EV_BEGIN_WATCHER (embed, embed) |
|
|
320 | void start (struct ev_loop *embedded_loop) |
|
|
321 | { |
|
|
322 | stop (); |
|
|
323 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
324 | start (); |
|
|
325 | } |
|
|
326 | |
|
|
327 | void sweep () |
|
|
328 | { |
|
|
329 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
|
|
330 | } |
|
|
331 | EV_END_WATCHER (embed, embed) |
|
|
332 | #endif |
|
|
333 | |
|
|
334 | #if EV_FORK_ENABLE |
|
|
335 | EV_BEGIN_WATCHER (fork, fork) |
|
|
336 | void set () { } |
|
|
337 | EV_END_WATCHER (fork, fork) |
|
|
338 | #endif |
247 | |
339 | |
248 | #undef EV_CONSTRUCT |
340 | #undef EV_CONSTRUCT |
|
|
341 | #undef EV_BEGIN_WATCHER |
249 | #undef EV_DECLARE_WATCHER |
342 | #undef EV_END_WATCHER |
250 | } |
343 | } |
251 | |
344 | |
252 | #endif |
345 | #endif |
253 | |
346 | |