|
|
1 | /* |
|
|
2 | * libev simple C++ wrapper classes |
|
|
3 | * |
|
|
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
5 | * All rights reserved. |
|
|
6 | * |
|
|
7 | * Redistribution and use in source and binary forms, with or without modifica- |
|
|
8 | * tion, are permitted provided that the following conditions are met: |
|
|
9 | * |
|
|
10 | * 1. Redistributions of source code must retain the above copyright notice, |
|
|
11 | * this list of conditions and the following disclaimer. |
|
|
12 | * |
|
|
13 | * 2. Redistributions in binary form must reproduce the above copyright |
|
|
14 | * notice, this list of conditions and the following disclaimer in the |
|
|
15 | * documentation and/or other materials provided with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
|
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
|
|
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
|
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
|
|
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
|
22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
|
23 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
|
24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
|
|
25 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|
|
26 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
27 | * |
|
|
28 | * Alternatively, the contents of this file may be used under the terms of |
|
|
29 | * the GNU General Public License ("GPL") version 2 or any later version, |
|
|
30 | * in which case the provisions of the GPL are applicable instead of |
|
|
31 | * the above. If you wish to allow the use of your version of this file |
|
|
32 | * only under the terms of the GPL and not to allow others to use your |
|
|
33 | * version of this file under the BSD license, indicate your decision |
|
|
34 | * by deleting the provisions above and replace them with the notice |
|
|
35 | * and other provisions required by the GPL. If you do not delete the |
|
|
36 | * provisions above, a recipient may use your version of this file under |
|
|
37 | * either the BSD or the GPL. |
|
|
38 | */ |
|
|
39 | |
1 | #ifndef EVPP_H__ |
40 | #ifndef EVPP_H__ |
2 | #define EVPP_H__ |
41 | #define EVPP_H__ |
3 | |
42 | |
|
|
43 | #ifdef EV_H |
|
|
44 | # include EV_H |
|
|
45 | #else |
4 | #include "ev.h" |
46 | # include "ev.h" |
|
|
47 | #endif |
5 | |
48 | |
6 | namespace ev { |
49 | namespace ev { |
7 | |
50 | |
8 | template<class watcher> |
51 | template<class ev_watcher, class watcher> |
9 | class callback |
52 | struct base : ev_watcher |
10 | { |
53 | { |
11 | struct object { }; |
54 | #if EV_MULTIPLICITY |
|
|
55 | EV_P; |
12 | |
56 | |
13 | void *obj; |
57 | 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 | { |
58 | { |
24 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) |
59 | this->EV_A = EV_A; |
25 | (w, e); |
|
|
26 | } |
60 | } |
|
|
61 | #endif |
|
|
62 | |
|
|
63 | base () |
|
|
64 | { |
|
|
65 | ev_init (this, 0); |
27 | }; |
66 | } |
28 | |
67 | |
29 | proxy_base *prxy; |
68 | 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 | } |
69 | { |
40 | |
70 | this->data = data; |
41 | void call (watcher *w, int e) const |
71 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
42 | { |
72 | } |
43 | return prxy->call (obj, meth, *w, e); |
73 | |
|
|
74 | // method callback |
|
|
75 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
76 | void set (K *object) |
|
|
77 | { |
|
|
78 | set_ (object, method_thunk<K, method>); |
|
|
79 | } |
|
|
80 | |
|
|
81 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
82 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
83 | { |
|
|
84 | K *obj = static_cast<K *>(w->data); |
|
|
85 | (obj->*method) (*static_cast<watcher *>(w), revents); |
|
|
86 | } |
|
|
87 | |
|
|
88 | // const method callback |
|
|
89 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
90 | void set (const K *object) |
|
|
91 | { |
|
|
92 | set_ (object, const_method_thunk<K, method>); |
|
|
93 | } |
|
|
94 | |
|
|
95 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
96 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
97 | { |
|
|
98 | K *obj = static_cast<K *>(w->data); |
|
|
99 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
|
|
100 | } |
|
|
101 | |
|
|
102 | // function callback |
|
|
103 | template<void (*function)(watcher &w, int)> |
|
|
104 | void set (void *data = 0) |
|
|
105 | { |
|
|
106 | set_ (data, function_thunk<function>); |
|
|
107 | } |
|
|
108 | |
|
|
109 | template<void (*function)(watcher &w, int)> |
|
|
110 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
111 | { |
|
|
112 | function (*static_cast<watcher *>(w), revents); |
|
|
113 | } |
|
|
114 | |
|
|
115 | // simple callback |
|
|
116 | template<class K, void (K::*method)()> |
|
|
117 | void set (K *object) |
|
|
118 | { |
|
|
119 | set_ (object, method_noargs_thunk<K, method>); |
|
|
120 | } |
|
|
121 | |
|
|
122 | template<class K, void (K::*method)()> |
|
|
123 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
124 | { |
|
|
125 | K *obj = static_cast<K *>(w->data); |
|
|
126 | (obj->*method) (); |
|
|
127 | } |
|
|
128 | |
|
|
129 | void operator ()(int events = EV_UNDEF) |
|
|
130 | { |
|
|
131 | return ev_cb (static_cast<ev_watcher *>(this)) |
|
|
132 | (static_cast<ev_watcher *>(this), events); |
|
|
133 | } |
|
|
134 | |
|
|
135 | bool is_active () const |
|
|
136 | { |
|
|
137 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
|
|
138 | } |
|
|
139 | |
|
|
140 | bool is_pending () const |
|
|
141 | { |
|
|
142 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
44 | } |
143 | } |
45 | }; |
144 | }; |
46 | |
145 | |
47 | enum { |
146 | enum { |
48 | UNDEF = EV_UNDEF, |
147 | UNDEF = EV_UNDEF, |
… | |
… | |
50 | READ = EV_READ, |
149 | READ = EV_READ, |
51 | WRITE = EV_WRITE, |
150 | WRITE = EV_WRITE, |
52 | TIMEOUT = EV_TIMEOUT, |
151 | TIMEOUT = EV_TIMEOUT, |
53 | PERIODIC = EV_PERIODIC, |
152 | PERIODIC = EV_PERIODIC, |
54 | SIGNAL = EV_SIGNAL, |
153 | SIGNAL = EV_SIGNAL, |
|
|
154 | CHILD = EV_CHILD, |
|
|
155 | STAT = EV_STAT, |
55 | IDLE = EV_IDLE, |
156 | IDLE = EV_IDLE, |
56 | CHECK = EV_CHECK, |
157 | CHECK = EV_CHECK, |
57 | PREPARE = EV_PREPARE, |
158 | PREPARE = EV_PREPARE, |
58 | CHILD = EV_CHILD, |
159 | FORK = EV_FORK, |
|
|
160 | EMBED = EV_EMBED, |
59 | ERROR = EV_ERROR, |
161 | ERROR = EV_ERROR, |
60 | }; |
162 | }; |
61 | |
163 | |
62 | typedef ev_tstamp tstamp; |
164 | typedef ev_tstamp tstamp; |
63 | |
165 | |
… | |
… | |
65 | { |
167 | { |
66 | return ev_now (EV_A); |
168 | return ev_now (EV_A); |
67 | } |
169 | } |
68 | |
170 | |
69 | #if EV_MULTIPLICITY |
171 | #if EV_MULTIPLICITY |
70 | |
|
|
71 | #define EV_CONSTRUCT(cppstem) \ |
|
|
72 | EV_P; \ |
172 | #define EV_CONSTRUCT \ |
73 | \ |
|
|
74 | void set (EV_P) \ |
173 | (EV_P = EV_DEFAULT) \ |
75 | { \ |
174 | { \ |
76 | this->EV_A = EV_A; \ |
|
|
77 | } \ |
|
|
78 | \ |
175 | set (EV_A); \ |
79 | template<class O1, class O2> \ |
176 | } |
80 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \ |
|
|
81 | : callback<cppstem> (object, method), EV_A (EV_A) |
|
|
82 | |
|
|
83 | #else |
177 | #else |
84 | |
178 | #define EV_CONSTRUCT \ |
85 | #define EV_CONSTRUCT(cppstem) \ |
179 | () \ |
86 | template<class O1, class O2> \ |
180 | { \ |
87 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
181 | } |
88 | : callback<cppstem> (object, method) |
|
|
89 | |
|
|
90 | #endif |
182 | #endif |
91 | |
183 | |
92 | /* using a template here would require quite a bit more lines, |
184 | /* using a template here would require quite a bit more lines, |
93 | * so a macro solution was chosen */ |
185 | * so a macro solution was chosen */ |
94 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
186 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
95 | \ |
187 | \ |
96 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
188 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
97 | { \ |
189 | { \ |
98 | EV_CONSTRUCT (cppstem) \ |
|
|
99 | { \ |
|
|
100 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
|
|
101 | } \ |
|
|
102 | \ |
|
|
103 | bool is_active () const \ |
|
|
104 | { \ |
|
|
105 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
106 | } \ |
|
|
107 | \ |
|
|
108 | bool is_pending () const \ |
|
|
109 | { \ |
|
|
110 | return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
111 | } \ |
|
|
112 | \ |
|
|
113 | void start () \ |
190 | void start () \ |
114 | { \ |
191 | { \ |
115 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
192 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
116 | } \ |
193 | } \ |
117 | \ |
194 | \ |
118 | void stop () \ |
195 | void stop () \ |
119 | { \ |
196 | { \ |
120 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
197 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
121 | } \ |
198 | } \ |
122 | \ |
199 | \ |
123 | void operator ()(int events = EV_UNDEF) \ |
|
|
124 | { \ |
200 | cppstem EV_CONSTRUCT \ |
125 | return call (this, events); \ |
|
|
126 | } \ |
|
|
127 | \ |
201 | \ |
128 | ~cppstem () \ |
202 | ~cppstem () \ |
129 | { \ |
203 | { \ |
130 | stop (); \ |
204 | stop (); \ |
131 | } \ |
205 | } \ |
132 | \ |
206 | \ |
|
|
207 | using base<ev_ ## cstem, cppstem>::set; \ |
|
|
208 | \ |
133 | private: \ |
209 | private: \ |
134 | \ |
210 | \ |
135 | cppstem (const cppstem &o) \ |
211 | cppstem (const cppstem &o); \ |
136 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
|
|
137 | { /* disabled */ } \ |
|
|
138 | \ |
|
|
139 | void operator =(const cppstem &o) { /* disabled */ } \ |
|
|
140 | \ |
|
|
141 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
142 | { \ |
212 | \ |
143 | (*static_cast<cppstem *>(w))(revents); \ |
213 | cppstem & operator =(const cppstem &o); \ |
144 | } \ |
|
|
145 | \ |
214 | \ |
146 | public: |
215 | public: |
147 | |
216 | |
148 | #define EV_END_WATCHER(cppstem,cstem) \ |
217 | #define EV_END_WATCHER(cppstem,cstem) \ |
149 | }; |
218 | }; |
… | |
… | |
191 | { |
260 | { |
192 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
261 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
193 | } |
262 | } |
194 | EV_END_WATCHER (timer, timer) |
263 | EV_END_WATCHER (timer, timer) |
195 | |
264 | |
196 | #if EV_PERIODICS |
265 | #if EV_PERIODIC_ENABLE |
197 | EV_BEGIN_WATCHER (periodic, periodic) |
266 | EV_BEGIN_WATCHER (periodic, periodic) |
198 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
267 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
199 | { |
268 | { |
200 | int active = is_active (); |
269 | int active = is_active (); |
201 | if (active) stop (); |
270 | if (active) stop (); |
… | |
… | |
212 | void again () |
281 | void again () |
213 | { |
282 | { |
214 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
283 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
215 | } |
284 | } |
216 | EV_END_WATCHER (periodic, periodic) |
285 | EV_END_WATCHER (periodic, periodic) |
|
|
286 | #endif |
|
|
287 | |
|
|
288 | EV_BEGIN_WATCHER (sig, signal) |
|
|
289 | void set (int signum) |
|
|
290 | { |
|
|
291 | int active = is_active (); |
|
|
292 | if (active) stop (); |
|
|
293 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
|
|
294 | if (active) start (); |
|
|
295 | } |
|
|
296 | |
|
|
297 | void start (int signum) |
|
|
298 | { |
|
|
299 | set (signum); |
|
|
300 | start (); |
|
|
301 | } |
|
|
302 | EV_END_WATCHER (sig, signal) |
|
|
303 | |
|
|
304 | EV_BEGIN_WATCHER (child, child) |
|
|
305 | void set (int pid) |
|
|
306 | { |
|
|
307 | int active = is_active (); |
|
|
308 | if (active) stop (); |
|
|
309 | ev_child_set (static_cast<ev_child *>(this), pid); |
|
|
310 | if (active) start (); |
|
|
311 | } |
|
|
312 | |
|
|
313 | void start (int pid) |
|
|
314 | { |
|
|
315 | set (pid); |
|
|
316 | start (); |
|
|
317 | } |
|
|
318 | EV_END_WATCHER (child, child) |
|
|
319 | |
|
|
320 | #if EV_STAT_ENABLE |
|
|
321 | EV_BEGIN_WATCHER (stat, stat) |
|
|
322 | void set (const char *path, ev_tstamp interval = 0.) |
|
|
323 | { |
|
|
324 | int active = is_active (); |
|
|
325 | if (active) stop (); |
|
|
326 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
|
|
327 | if (active) start (); |
|
|
328 | } |
|
|
329 | |
|
|
330 | void start (const char *path, ev_tstamp interval = 0.) |
|
|
331 | { |
|
|
332 | stop (); |
|
|
333 | set (path, interval); |
|
|
334 | start (); |
|
|
335 | } |
|
|
336 | |
|
|
337 | void update () |
|
|
338 | { |
|
|
339 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
|
|
340 | } |
|
|
341 | EV_END_WATCHER (stat, stat) |
217 | #endif |
342 | #endif |
218 | |
343 | |
219 | EV_BEGIN_WATCHER (idle, idle) |
344 | EV_BEGIN_WATCHER (idle, idle) |
220 | void set () { } |
345 | void set () { } |
221 | EV_END_WATCHER (idle, idle) |
346 | EV_END_WATCHER (idle, idle) |
… | |
… | |
226 | |
351 | |
227 | EV_BEGIN_WATCHER (check, check) |
352 | EV_BEGIN_WATCHER (check, check) |
228 | void set () { } |
353 | void set () { } |
229 | EV_END_WATCHER (check, check) |
354 | EV_END_WATCHER (check, check) |
230 | |
355 | |
231 | EV_BEGIN_WATCHER (sig, signal) |
356 | #if EV_EMBED_ENABLE |
232 | void set (int signum) |
|
|
233 | { |
|
|
234 | int active = is_active (); |
|
|
235 | if (active) stop (); |
|
|
236 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
|
|
237 | if (active) start (); |
|
|
238 | } |
|
|
239 | |
|
|
240 | void start (int signum) |
|
|
241 | { |
|
|
242 | set (signum); |
|
|
243 | start (); |
|
|
244 | } |
|
|
245 | EV_END_WATCHER (sig, signal) |
|
|
246 | |
|
|
247 | EV_BEGIN_WATCHER (child, child) |
|
|
248 | void set (int pid) |
|
|
249 | { |
|
|
250 | int active = is_active (); |
|
|
251 | if (active) stop (); |
|
|
252 | ev_child_set (static_cast<ev_child *>(this), pid); |
|
|
253 | if (active) start (); |
|
|
254 | } |
|
|
255 | |
|
|
256 | void start (int pid) |
|
|
257 | { |
|
|
258 | set (pid); |
|
|
259 | start (); |
|
|
260 | } |
|
|
261 | EV_END_WATCHER (child, child) |
|
|
262 | |
|
|
263 | #if EV_MULTIPLICITY |
|
|
264 | |
|
|
265 | EV_BEGIN_WATCHER (embed, embed) |
357 | 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) |
358 | void start (struct ev_loop *embedded_loop) |
275 | { |
359 | { |
276 | set (embedded_loop); |
360 | stop (); |
|
|
361 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
277 | start (); |
362 | start (); |
278 | } |
363 | } |
279 | |
364 | |
280 | void sweep () |
365 | void sweep () |
281 | { |
366 | { |
282 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
367 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
283 | } |
368 | } |
284 | EV_END_WATCHER (embed, embed) |
369 | EV_END_WATCHER (embed, embed) |
|
|
370 | #endif |
285 | |
371 | |
|
|
372 | #if EV_FORK_ENABLE |
|
|
373 | EV_BEGIN_WATCHER (fork, fork) |
|
|
374 | void set () { } |
|
|
375 | EV_END_WATCHER (fork, fork) |
286 | #endif |
376 | #endif |
287 | |
377 | |
288 | #undef EV_CONSTRUCT |
378 | #undef EV_CONSTRUCT |
289 | #undef EV_BEGIN_WATCHER |
379 | #undef EV_BEGIN_WATCHER |
290 | #undef EV_END_WATCHER |
380 | #undef EV_END_WATCHER |