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