1 | #ifndef EVPP_H__ |
1 | #ifndef EVPP_H__ |
2 | #define EVPP_H__ |
2 | #define EVPP_H__ |
3 | |
3 | |
|
|
4 | #ifdef EV_H |
|
|
5 | # include EV_H |
|
|
6 | #else |
4 | #include "ev.h" |
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 | |
106 | |
47 | enum { |
107 | enum { |
48 | UNDEF = EV_UNDEF, |
108 | UNDEF = EV_UNDEF, |
… | |
… | |
68 | { |
128 | { |
69 | return ev_now (EV_A); |
129 | return ev_now (EV_A); |
70 | } |
130 | } |
71 | |
131 | |
72 | #if EV_MULTIPLICITY |
132 | #if EV_MULTIPLICITY |
73 | |
|
|
74 | #define EV_CONSTRUCT(cppstem) \ |
|
|
75 | EV_P; \ |
133 | #define EV_CONSTRUCT \ |
76 | \ |
|
|
77 | void set (EV_P) \ |
134 | (EV_P = EV_DEFAULT) \ |
78 | { \ |
135 | { \ |
79 | this->EV_A = EV_A; \ |
|
|
80 | } \ |
|
|
81 | \ |
136 | set (EV_A); \ |
82 | template<class O1, class O2> \ |
137 | } |
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 |
138 | #else |
87 | |
139 | #define EV_CONSTRUCT \ |
88 | #define EV_CONSTRUCT(cppstem) \ |
140 | () \ |
89 | template<class O1, class O2> \ |
141 | { \ |
90 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
142 | } |
91 | : callback<cppstem> (object, method) |
|
|
92 | |
|
|
93 | #endif |
143 | #endif |
94 | |
144 | |
95 | /* using a template here would require quite a bit more lines, |
145 | /* using a template here would require quite a bit more lines, |
96 | * so a macro solution was chosen */ |
146 | * so a macro solution was chosen */ |
97 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
147 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
98 | \ |
148 | \ |
99 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
149 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
100 | { \ |
150 | { \ |
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 () \ |
151 | void start () \ |
117 | { \ |
152 | { \ |
118 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
153 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
119 | } \ |
154 | } \ |
120 | \ |
155 | \ |
121 | void stop () \ |
156 | void stop () \ |
122 | { \ |
157 | { \ |
123 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
158 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
124 | } \ |
159 | } \ |
125 | \ |
160 | \ |
126 | void operator ()(int events = EV_UNDEF) \ |
|
|
127 | { \ |
161 | cppstem EV_CONSTRUCT \ |
128 | return call (this, events); \ |
|
|
129 | } \ |
|
|
130 | \ |
162 | \ |
131 | ~cppstem () \ |
163 | ~cppstem () \ |
132 | { \ |
164 | { \ |
133 | stop (); \ |
165 | stop (); \ |
134 | } \ |
166 | } \ |
135 | \ |
167 | \ |
|
|
168 | using base<ev_ ## cstem, cppstem>::set; \ |
|
|
169 | \ |
136 | private: \ |
170 | private: \ |
137 | \ |
171 | \ |
138 | cppstem (const cppstem &o) \ |
172 | cppstem (const cppstem &o) \ |
139 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
|
|
140 | { /* disabled */ } \ |
173 | { /* disabled */ } \ |
141 | \ |
174 | \ |
142 | void operator =(const cppstem &o) { /* disabled */ } \ |
175 | void operator =(const cppstem &o) { /* disabled */ } \ |
143 | \ |
|
|
144 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
145 | { \ |
|
|
146 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
147 | } \ |
|
|
148 | \ |
176 | \ |
149 | public: |
177 | public: |
150 | |
178 | |
151 | #define EV_END_WATCHER(cppstem,cstem) \ |
179 | #define EV_END_WATCHER(cppstem,cstem) \ |
152 | }; |
180 | }; |
… | |
… | |
261 | if (active) start (); |
289 | if (active) start (); |
262 | } |
290 | } |
263 | |
291 | |
264 | void start (const char *path, ev_tstamp interval = 0.) |
292 | void start (const char *path, ev_tstamp interval = 0.) |
265 | { |
293 | { |
|
|
294 | stop (); |
266 | set (path, interval); |
295 | set (path, interval); |
267 | start (); |
296 | start (); |
268 | } |
297 | } |
269 | |
298 | |
270 | void update () |
299 | void update () |
… | |
… | |
286 | void set () { } |
315 | void set () { } |
287 | EV_END_WATCHER (check, check) |
316 | EV_END_WATCHER (check, check) |
288 | |
317 | |
289 | #if EV_EMBED_ENABLE |
318 | #if EV_EMBED_ENABLE |
290 | EV_BEGIN_WATCHER (embed, embed) |
319 | 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) |
320 | void start (struct ev_loop *embedded_loop) |
300 | { |
321 | { |
301 | set (embedded_loop); |
322 | stop (); |
|
|
323 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
302 | start (); |
324 | start (); |
303 | } |
325 | } |
304 | |
326 | |
305 | void sweep () |
327 | void sweep () |
306 | { |
328 | { |
307 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
329 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
308 | } |
330 | } |
309 | EV_END_WATCHER (embed, embed) |
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) |
310 | #endif |
338 | #endif |
311 | |
339 | |
312 | #undef EV_CONSTRUCT |
340 | #undef EV_CONSTRUCT |
313 | #undef EV_BEGIN_WATCHER |
341 | #undef EV_BEGIN_WATCHER |
314 | #undef EV_END_WATCHER |
342 | #undef EV_END_WATCHER |