|
|
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 klass; // it is vital that this is never defined |
54 | #if EV_MULTIPLICITY |
|
|
55 | EV_P; |
12 | |
56 | |
13 | klass *o; |
57 | void set (EV_P) |
14 | void (klass::*m)(watcher &, int); |
|
|
15 | |
|
|
16 | public: |
|
|
17 | template<class O1, class O2> |
|
|
18 | explicit callback (O1 *object, void (O2::*method)(watcher &, int)) |
|
|
19 | { |
58 | { |
20 | o = reinterpret_cast<klass *>(object); |
59 | this->EV_A = EV_A; |
21 | m = reinterpret_cast<void (klass::*)(watcher &, int)>(method); |
|
|
22 | } |
60 | } |
|
|
61 | #endif |
23 | |
62 | |
24 | // this works because a standards-compliant C++ compiler |
63 | base () |
25 | // basically can't help it: it doesn't have the knowledge |
64 | { |
26 | // required to miscompile (klass is not defined anywhere |
65 | ev_init (this, 0); |
27 | // and nothing is known about the constructor arguments) :) |
|
|
28 | void call (watcher *w, int revents) |
|
|
29 | { |
66 | } |
30 | (o->*m) (*w, revents); |
67 | |
|
|
68 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
|
|
69 | { |
|
|
70 | this->data = data; |
|
|
71 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
|
|
72 | } |
|
|
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)); |
31 | } |
143 | } |
32 | }; |
144 | }; |
33 | |
145 | |
34 | enum { |
146 | enum { |
35 | UNDEF = EV_UNDEF, |
147 | UNDEF = EV_UNDEF, |
… | |
… | |
55 | { |
167 | { |
56 | return ev_now (EV_A); |
168 | return ev_now (EV_A); |
57 | } |
169 | } |
58 | |
170 | |
59 | #if EV_MULTIPLICITY |
171 | #if EV_MULTIPLICITY |
60 | |
|
|
61 | #define EV_CONSTRUCT(cppstem) \ |
|
|
62 | EV_P; \ |
172 | #define EV_CONSTRUCT \ |
63 | \ |
|
|
64 | void set (EV_P) \ |
173 | (EV_P = EV_DEFAULT) \ |
65 | { \ |
174 | { \ |
66 | this->EV_A = EV_A; \ |
|
|
67 | } \ |
|
|
68 | \ |
175 | set (EV_A); \ |
69 | template<class O1, class O2> \ |
176 | } |
70 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \ |
|
|
71 | : callback<cppstem> (object, method), EV_A (EV_A) |
|
|
72 | |
|
|
73 | #else |
177 | #else |
74 | |
178 | #define EV_CONSTRUCT \ |
75 | #define EV_CONSTRUCT(cppstem) \ |
179 | () \ |
76 | template<class O1, class O2> \ |
180 | { \ |
77 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
181 | } |
78 | : callback<cppstem> (object, method) |
|
|
79 | |
|
|
80 | #endif |
182 | #endif |
81 | |
183 | |
82 | /* using a template here would require quite a bit more lines, |
184 | /* using a template here would require quite a bit more lines, |
83 | * so a macro solution was chosen */ |
185 | * so a macro solution was chosen */ |
84 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
186 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
85 | \ |
187 | \ |
86 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
188 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
87 | { \ |
189 | { \ |
88 | EV_CONSTRUCT (cppstem) \ |
|
|
89 | { \ |
|
|
90 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
|
|
91 | } \ |
|
|
92 | \ |
|
|
93 | bool is_active () const \ |
|
|
94 | { \ |
|
|
95 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
96 | } \ |
|
|
97 | \ |
|
|
98 | bool is_pending () const \ |
|
|
99 | { \ |
|
|
100 | return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \ |
|
|
101 | } \ |
|
|
102 | \ |
|
|
103 | void start () \ |
190 | void start () \ |
104 | { \ |
191 | { \ |
105 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
192 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
106 | } \ |
193 | } \ |
107 | \ |
194 | \ |
108 | void stop () \ |
195 | void stop () \ |
109 | { \ |
196 | { \ |
110 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
197 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
111 | } \ |
198 | } \ |
112 | \ |
199 | \ |
113 | void operator ()(int events = EV_UNDEF) \ |
|
|
114 | { \ |
200 | cppstem EV_CONSTRUCT \ |
115 | return call (this, events); \ |
|
|
116 | } \ |
|
|
117 | \ |
201 | \ |
118 | ~cppstem () \ |
202 | ~cppstem () \ |
119 | { \ |
203 | { \ |
120 | stop (); \ |
204 | stop (); \ |
121 | } \ |
205 | } \ |
122 | \ |
206 | \ |
|
|
207 | using base<ev_ ## cstem, cppstem>::set; \ |
|
|
208 | \ |
123 | private: \ |
209 | private: \ |
124 | \ |
210 | \ |
125 | cppstem (const cppstem &o) \ |
211 | cppstem (const cppstem &o) \ |
126 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
|
|
127 | { /* disabled */ } \ |
212 | { /* disabled */ } \ |
128 | \ |
213 | \ |
129 | void operator =(const cppstem &o) { /* disabled */ } \ |
214 | void operator =(const cppstem &o) { /* disabled */ } \ |
130 | \ |
|
|
131 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
132 | { \ |
|
|
133 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
134 | } \ |
|
|
135 | \ |
215 | \ |
136 | public: |
216 | public: |
137 | |
217 | |
138 | #define EV_END_WATCHER(cppstem,cstem) \ |
218 | #define EV_END_WATCHER(cppstem,cstem) \ |
139 | }; |
219 | }; |
… | |
… | |
248 | if (active) start (); |
328 | if (active) start (); |
249 | } |
329 | } |
250 | |
330 | |
251 | void start (const char *path, ev_tstamp interval = 0.) |
331 | void start (const char *path, ev_tstamp interval = 0.) |
252 | { |
332 | { |
|
|
333 | stop (); |
253 | set (path, interval); |
334 | set (path, interval); |
254 | start (); |
335 | start (); |
255 | } |
336 | } |
256 | |
337 | |
257 | void update () |
338 | void update () |
… | |
… | |
273 | void set () { } |
354 | void set () { } |
274 | EV_END_WATCHER (check, check) |
355 | EV_END_WATCHER (check, check) |
275 | |
356 | |
276 | #if EV_EMBED_ENABLE |
357 | #if EV_EMBED_ENABLE |
277 | EV_BEGIN_WATCHER (embed, embed) |
358 | EV_BEGIN_WATCHER (embed, embed) |
278 | void set (struct ev_loop *loop) |
|
|
279 | { |
|
|
280 | int active = is_active (); |
|
|
281 | if (active) stop (); |
|
|
282 | ev_embed_set (static_cast<ev_embed *>(this), loop); |
|
|
283 | if (active) start (); |
|
|
284 | } |
|
|
285 | |
|
|
286 | void start (struct ev_loop *embedded_loop) |
359 | void start (struct ev_loop *embedded_loop) |
287 | { |
360 | { |
288 | set (embedded_loop); |
361 | stop (); |
|
|
362 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
289 | start (); |
363 | start (); |
290 | } |
364 | } |
291 | |
365 | |
292 | void sweep () |
366 | void sweep () |
293 | { |
367 | { |