|
|
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 | typedef ev_tstamp tstamp; |
9 | class callback |
|
|
10 | { |
|
|
11 | struct object { }; |
|
|
12 | |
|
|
13 | void *obj; |
|
|
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 | { |
|
|
24 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) |
|
|
25 | (w, e); |
|
|
26 | } |
|
|
27 | }; |
|
|
28 | |
|
|
29 | proxy_base *prxy; |
|
|
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 | } |
|
|
40 | |
|
|
41 | void call (watcher *w, int e) const |
|
|
42 | { |
|
|
43 | return prxy->call (obj, meth, *w, e); |
|
|
44 | } |
|
|
45 | }; |
|
|
46 | |
52 | |
47 | enum { |
53 | enum { |
48 | UNDEF = EV_UNDEF, |
54 | UNDEF = EV_UNDEF, |
49 | NONE = EV_NONE, |
55 | NONE = EV_NONE, |
50 | READ = EV_READ, |
56 | READ = EV_READ, |
… | |
… | |
60 | FORK = EV_FORK, |
66 | FORK = EV_FORK, |
61 | EMBED = EV_EMBED, |
67 | EMBED = EV_EMBED, |
62 | ERROR = EV_ERROR, |
68 | ERROR = EV_ERROR, |
63 | }; |
69 | }; |
64 | |
70 | |
65 | typedef ev_tstamp tstamp; |
71 | enum |
|
|
72 | { |
|
|
73 | AUTO = EVFLAG_AUTO, |
|
|
74 | NOENV = EVFLAG_NOENV, |
|
|
75 | FORKCHECK = EVFLAG_FORKCHECK, |
|
|
76 | SELECT = EVBACKEND_SELECT, |
|
|
77 | POLL = EVBACKEND_POLL, |
|
|
78 | EPOLL = EVBACKEND_EPOLL, |
|
|
79 | KQUEUE = EVBACKEND_KQUEUE, |
|
|
80 | DEVPOLL = EVBACKEND_DEVPOLL, |
|
|
81 | PORT = EVBACKEND_PORT |
|
|
82 | }; |
|
|
83 | |
|
|
84 | enum |
|
|
85 | { |
|
|
86 | NONBLOCK = EVLOOP_NONBLOCK, |
|
|
87 | ONESHOT = EVLOOP_ONESHOT |
|
|
88 | }; |
|
|
89 | |
|
|
90 | enum how_t |
|
|
91 | { |
|
|
92 | ONE = EVUNLOOP_ONE, |
|
|
93 | ALL = EVUNLOOP_ALL |
|
|
94 | }; |
|
|
95 | |
|
|
96 | template<class ev_watcher, class watcher> |
|
|
97 | struct base : ev_watcher |
|
|
98 | { |
|
|
99 | #if EV_MULTIPLICITY |
|
|
100 | EV_P; |
|
|
101 | |
|
|
102 | void set (EV_P) |
|
|
103 | { |
|
|
104 | this->EV_A = EV_A; |
|
|
105 | } |
|
|
106 | #endif |
|
|
107 | |
|
|
108 | base () |
|
|
109 | { |
|
|
110 | ev_init (this, 0); |
|
|
111 | } |
|
|
112 | |
|
|
113 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
|
|
114 | { |
|
|
115 | this->data = data; |
|
|
116 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
|
|
117 | } |
|
|
118 | |
|
|
119 | // method callback |
|
|
120 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
121 | void set (K *object) |
|
|
122 | { |
|
|
123 | set_ (object, method_thunk<K, method>); |
|
|
124 | } |
|
|
125 | |
|
|
126 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
127 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
128 | { |
|
|
129 | K *obj = static_cast<K *>(w->data); |
|
|
130 | (obj->*method) (*static_cast<watcher *>(w), revents); |
|
|
131 | } |
|
|
132 | |
|
|
133 | // const method callback |
|
|
134 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
135 | void set (const K *object) |
|
|
136 | { |
|
|
137 | set_ (object, const_method_thunk<K, method>); |
|
|
138 | } |
|
|
139 | |
|
|
140 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
141 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
142 | { |
|
|
143 | K *obj = static_cast<K *>(w->data); |
|
|
144 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
|
|
145 | } |
|
|
146 | |
|
|
147 | // function callback |
|
|
148 | template<void (*function)(watcher &w, int)> |
|
|
149 | void set (void *data = 0) |
|
|
150 | { |
|
|
151 | set_ (data, function_thunk<function>); |
|
|
152 | } |
|
|
153 | |
|
|
154 | template<void (*function)(watcher &w, int)> |
|
|
155 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
156 | { |
|
|
157 | function (*static_cast<watcher *>(w), revents); |
|
|
158 | } |
|
|
159 | |
|
|
160 | // simple callback |
|
|
161 | template<class K, void (K::*method)()> |
|
|
162 | void set (K *object) |
|
|
163 | { |
|
|
164 | set_ (object, method_noargs_thunk<K, method>); |
|
|
165 | } |
|
|
166 | |
|
|
167 | template<class K, void (K::*method)()> |
|
|
168 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
169 | { |
|
|
170 | K *obj = static_cast<K *>(w->data); |
|
|
171 | (obj->*method) (); |
|
|
172 | } |
|
|
173 | |
|
|
174 | void operator ()(int events = EV_UNDEF) |
|
|
175 | { |
|
|
176 | return ev_cb (static_cast<ev_watcher *>(this)) |
|
|
177 | (static_cast<ev_watcher *>(this), events); |
|
|
178 | } |
|
|
179 | |
|
|
180 | bool is_active () const |
|
|
181 | { |
|
|
182 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
|
|
183 | } |
|
|
184 | |
|
|
185 | bool is_pending () const |
|
|
186 | { |
|
|
187 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
|
|
188 | } |
|
|
189 | }; |
|
|
190 | |
|
|
191 | inline void delay (tstamp interval) |
|
|
192 | { |
|
|
193 | ev_sleep (interval); |
|
|
194 | } |
|
|
195 | |
|
|
196 | inline int version_major () |
|
|
197 | { |
|
|
198 | return ev_version_major (); |
|
|
199 | } |
|
|
200 | |
|
|
201 | inline int version_minor () |
|
|
202 | { |
|
|
203 | return ev_version_minor (); |
|
|
204 | } |
|
|
205 | |
|
|
206 | inline unsigned int supported_backends () |
|
|
207 | { |
|
|
208 | return ev_supported_backends (); |
|
|
209 | } |
|
|
210 | |
|
|
211 | inline unsigned int recommended_backends () |
|
|
212 | { |
|
|
213 | return ev_recommended_backends (); |
|
|
214 | } |
|
|
215 | |
|
|
216 | inline unsigned int embeddable_backends () |
|
|
217 | { |
|
|
218 | return ev_embeddable_backends (); |
|
|
219 | } |
|
|
220 | |
|
|
221 | inline void set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
222 | { |
|
|
223 | ev_set_allocator (cb); |
|
|
224 | } |
|
|
225 | |
|
|
226 | inline void set_syserr_cb (void (*cb)(const char *msg)) |
|
|
227 | { |
|
|
228 | ev_set_syserr_cb (cb); |
|
|
229 | } |
|
|
230 | |
66 | |
231 | |
67 | inline ev_tstamp now (EV_P) |
232 | inline ev_tstamp now (EV_P) |
68 | { |
233 | { |
69 | return ev_now (EV_A); |
234 | return ev_now (EV_A); |
70 | } |
235 | } |
71 | |
236 | |
72 | #if EV_MULTIPLICITY |
237 | #if EV_MULTIPLICITY |
73 | |
|
|
74 | #define EV_CONSTRUCT(cppstem) \ |
|
|
75 | EV_P; \ |
238 | #define EV_CONSTRUCT \ |
76 | \ |
|
|
77 | void set (EV_P) \ |
239 | (EV_P = EV_DEFAULT) \ |
78 | { \ |
240 | { \ |
79 | this->EV_A = EV_A; \ |
|
|
80 | } \ |
|
|
81 | \ |
241 | set (EV_A); \ |
82 | template<class O1, class O2> \ |
242 | } |
83 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \ |
|
|
84 | : callback<cppstem> (object, method), EV_A (EV_A) |
|
|
85 | |
|
|
86 | #else |
243 | #else |
87 | |
244 | #define EV_CONSTRUCT \ |
88 | #define EV_CONSTRUCT(cppstem) \ |
245 | () \ |
89 | template<class O1, class O2> \ |
246 | { \ |
90 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
247 | } |
91 | : callback<cppstem> (object, method) |
|
|
92 | |
|
|
93 | #endif |
248 | #endif |
94 | |
249 | |
95 | /* using a template here would require quite a bit more lines, |
250 | /* using a template here would require quite a bit more lines, |
96 | * so a macro solution was chosen */ |
251 | * so a macro solution was chosen */ |
97 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
252 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
98 | \ |
253 | \ |
99 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
254 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
100 | { \ |
255 | { \ |
101 | EV_CONSTRUCT (cppstem) \ |
|
|
102 | { \ |
|
|
103 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
|
|
104 | } \ |
|
|
105 | \ |
|
|
106 | bool is_active () const \ |
|
|
107 | { \ |
|
|
108 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
109 | } \ |
|
|
110 | \ |
|
|
111 | bool is_pending () const \ |
|
|
112 | { \ |
|
|
113 | return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
114 | } \ |
|
|
115 | \ |
|
|
116 | void start () \ |
256 | void start () \ |
117 | { \ |
257 | { \ |
118 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
258 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
119 | } \ |
259 | } \ |
120 | \ |
260 | \ |
121 | void stop () \ |
261 | void stop () \ |
122 | { \ |
262 | { \ |
123 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
263 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
124 | } \ |
264 | } \ |
125 | \ |
265 | \ |
126 | void operator ()(int events = EV_UNDEF) \ |
|
|
127 | { \ |
266 | cppstem EV_CONSTRUCT \ |
128 | return call (this, events); \ |
|
|
129 | } \ |
|
|
130 | \ |
267 | \ |
131 | ~cppstem () \ |
268 | ~cppstem () \ |
132 | { \ |
269 | { \ |
133 | stop (); \ |
270 | stop (); \ |
134 | } \ |
271 | } \ |
135 | \ |
272 | \ |
|
|
273 | using base<ev_ ## cstem, cppstem>::set; \ |
|
|
274 | \ |
136 | private: \ |
275 | private: \ |
137 | \ |
276 | \ |
138 | cppstem (const cppstem &o) \ |
277 | cppstem (const cppstem &o); \ |
139 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
|
|
140 | { /* disabled */ } \ |
|
|
141 | \ |
|
|
142 | void operator =(const cppstem &o) { /* disabled */ } \ |
|
|
143 | \ |
|
|
144 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
145 | { \ |
278 | \ |
146 | (*static_cast<cppstem *>(w))(revents); \ |
279 | cppstem & operator =(const cppstem &o); \ |
147 | } \ |
|
|
148 | \ |
280 | \ |
149 | public: |
281 | public: |
150 | |
282 | |
151 | #define EV_END_WATCHER(cppstem,cstem) \ |
283 | #define EV_END_WATCHER(cppstem,cstem) \ |
152 | }; |
284 | }; |
… | |
… | |
261 | if (active) start (); |
393 | if (active) start (); |
262 | } |
394 | } |
263 | |
395 | |
264 | void start (const char *path, ev_tstamp interval = 0.) |
396 | void start (const char *path, ev_tstamp interval = 0.) |
265 | { |
397 | { |
|
|
398 | stop (); |
266 | set (path, interval); |
399 | set (path, interval); |
267 | start (); |
400 | start (); |
268 | } |
401 | } |
269 | |
402 | |
270 | void update () |
403 | void update () |
… | |
… | |
286 | void set () { } |
419 | void set () { } |
287 | EV_END_WATCHER (check, check) |
420 | EV_END_WATCHER (check, check) |
288 | |
421 | |
289 | #if EV_EMBED_ENABLE |
422 | #if EV_EMBED_ENABLE |
290 | EV_BEGIN_WATCHER (embed, embed) |
423 | EV_BEGIN_WATCHER (embed, embed) |
291 | void set (struct ev_loop *loop) |
|
|
292 | { |
|
|
293 | int active = is_active (); |
|
|
294 | if (active) stop (); |
|
|
295 | ev_embed_set (static_cast<ev_embed *>(this), loop); |
|
|
296 | if (active) start (); |
|
|
297 | } |
|
|
298 | |
|
|
299 | void start (struct ev_loop *embedded_loop) |
424 | void start (struct ev_loop *embedded_loop) |
300 | { |
425 | { |
301 | set (embedded_loop); |
426 | stop (); |
|
|
427 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
302 | start (); |
428 | start (); |
303 | } |
429 | } |
304 | |
430 | |
305 | void sweep () |
431 | void sweep () |
306 | { |
432 | { |
307 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
433 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
308 | } |
434 | } |
309 | EV_END_WATCHER (embed, embed) |
435 | EV_END_WATCHER (embed, embed) |
|
|
436 | #endif |
|
|
437 | |
|
|
438 | #if EV_FORK_ENABLE |
|
|
439 | EV_BEGIN_WATCHER (fork, fork) |
|
|
440 | void set () { } |
|
|
441 | EV_END_WATCHER (fork, fork) |
310 | #endif |
442 | #endif |
311 | |
443 | |
312 | #undef EV_CONSTRUCT |
444 | #undef EV_CONSTRUCT |
313 | #undef EV_BEGIN_WATCHER |
445 | #undef EV_BEGIN_WATCHER |
314 | #undef EV_END_WATCHER |
446 | #undef EV_END_WATCHER |