… | |
… | |
3 | |
3 | |
4 | #include "ev.h" |
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 *object, void (*cb)(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 = object; |
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 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
32 | void set (K *object) |
|
|
33 | { |
|
|
34 | set_ (object, method_thunk<K, method>); |
|
|
35 | } |
|
|
36 | |
|
|
37 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
38 | static void method_thunk (ev_watcher *w, int revents) |
|
|
39 | { |
|
|
40 | watcher *self = static_cast<watcher *>(w); |
|
|
41 | K *obj = static_cast<K *>(self->data); |
|
|
42 | (obj->*method) (*self, revents); |
|
|
43 | } |
|
|
44 | |
|
|
45 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
46 | void set (const K *object) |
|
|
47 | { |
|
|
48 | set_ (object, const_method_thunk<K, method>); |
|
|
49 | } |
|
|
50 | |
|
|
51 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
52 | static void const_method_thunk (ev_watcher *w, int revents) |
|
|
53 | { |
|
|
54 | watcher *self = static_cast<watcher *>(w); |
|
|
55 | K *obj = static_cast<K *>(self->data); |
|
|
56 | (obj->*method) (*self, revents); |
|
|
57 | } |
|
|
58 | |
|
|
59 | template<void (*function)(watcher &w, int)> |
|
|
60 | void set () |
|
|
61 | { |
|
|
62 | set_ (0, function_thunk<function>); |
|
|
63 | } |
|
|
64 | |
|
|
65 | template<void (*function)(watcher &w, int)> |
|
|
66 | static void function_thunk (ev_watcher *w, int revents) |
|
|
67 | { |
|
|
68 | watcher *self = static_cast<watcher *>(w); |
|
|
69 | function (*self, revents); |
|
|
70 | } |
|
|
71 | |
|
|
72 | void operator ()(int events = EV_UNDEF) |
|
|
73 | { |
|
|
74 | return e_cb (static_cast<ev_watcher *>(this)) (events); |
|
|
75 | } |
|
|
76 | |
|
|
77 | bool is_active () const |
|
|
78 | { |
|
|
79 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
|
|
80 | } |
|
|
81 | |
|
|
82 | bool is_pending () const |
|
|
83 | { |
|
|
84 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
44 | } |
85 | } |
45 | }; |
86 | }; |
46 | |
87 | |
47 | enum { |
88 | enum { |
48 | UNDEF = EV_UNDEF, |
89 | UNDEF = EV_UNDEF, |
… | |
… | |
50 | READ = EV_READ, |
91 | READ = EV_READ, |
51 | WRITE = EV_WRITE, |
92 | WRITE = EV_WRITE, |
52 | TIMEOUT = EV_TIMEOUT, |
93 | TIMEOUT = EV_TIMEOUT, |
53 | PERIODIC = EV_PERIODIC, |
94 | PERIODIC = EV_PERIODIC, |
54 | SIGNAL = EV_SIGNAL, |
95 | SIGNAL = EV_SIGNAL, |
|
|
96 | CHILD = EV_CHILD, |
|
|
97 | STAT = EV_STAT, |
55 | IDLE = EV_IDLE, |
98 | IDLE = EV_IDLE, |
56 | CHECK = EV_CHECK, |
99 | CHECK = EV_CHECK, |
57 | PREPARE = EV_PREPARE, |
100 | PREPARE = EV_PREPARE, |
58 | CHILD = EV_CHILD, |
101 | FORK = EV_FORK, |
|
|
102 | EMBED = EV_EMBED, |
59 | ERROR = EV_ERROR, |
103 | ERROR = EV_ERROR, |
60 | }; |
104 | }; |
61 | |
105 | |
62 | typedef ev_tstamp tstamp; |
106 | typedef ev_tstamp tstamp; |
63 | |
107 | |
… | |
… | |
65 | { |
109 | { |
66 | return ev_now (EV_A); |
110 | return ev_now (EV_A); |
67 | } |
111 | } |
68 | |
112 | |
69 | #if EV_MULTIPLICITY |
113 | #if EV_MULTIPLICITY |
70 | |
|
|
71 | #define EV_CONSTRUCT(cppstem) \ |
|
|
72 | EV_P; \ |
114 | #define EV_CONSTRUCT \ |
73 | \ |
|
|
74 | void set (EV_P) \ |
115 | (EV_P = EV_DEFAULT) \ |
75 | { \ |
116 | { \ |
76 | this->EV_A = EV_A; \ |
|
|
77 | } \ |
|
|
78 | \ |
117 | set (EV_A); \ |
79 | template<class O1, class O2> \ |
118 | } |
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 |
119 | #else |
84 | |
120 | #define EV_CONSTRUCT \ |
85 | #define EV_CONSTRUCT(cppstem) \ |
121 | () \ |
86 | template<class O1, class O2> \ |
122 | { \ |
87 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
123 | } |
88 | : callback<cppstem> (object, method) |
|
|
89 | |
|
|
90 | #endif |
124 | #endif |
91 | |
125 | |
92 | /* using a template here would require quite a bit more lines, |
126 | /* using a template here would require quite a bit more lines, |
93 | * so a macro solution was chosen */ |
127 | * so a macro solution was chosen */ |
94 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
128 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
95 | \ |
129 | \ |
96 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
130 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
97 | { \ |
131 | { \ |
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 () \ |
132 | void start () \ |
114 | { \ |
133 | { \ |
115 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
134 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
116 | } \ |
135 | } \ |
117 | \ |
136 | \ |
118 | void stop () \ |
137 | void stop () \ |
119 | { \ |
138 | { \ |
120 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
139 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
121 | } \ |
140 | } \ |
122 | \ |
141 | \ |
123 | void operator ()(int events = EV_UNDEF) \ |
|
|
124 | { \ |
142 | cppstem EV_CONSTRUCT \ |
125 | return call (this, events); \ |
|
|
126 | } \ |
|
|
127 | \ |
143 | \ |
128 | ~cppstem () \ |
144 | ~cppstem () \ |
129 | { \ |
145 | { \ |
130 | stop (); \ |
146 | stop (); \ |
131 | } \ |
147 | } \ |
132 | \ |
148 | \ |
|
|
149 | using base<ev_ ## cstem, cppstem>::set; \ |
|
|
150 | \ |
133 | private: \ |
151 | private: \ |
134 | \ |
152 | \ |
135 | cppstem (const cppstem &o) \ |
153 | cppstem (const cppstem &o) \ |
136 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
|
|
137 | { /* disabled */ } \ |
154 | { /* disabled */ } \ |
138 | \ |
155 | \ |
139 | void operator =(const cppstem &o) { /* disabled */ } \ |
156 | void operator =(const cppstem &o) { /* disabled */ } \ |
140 | \ |
|
|
141 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
142 | { \ |
|
|
143 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
144 | } \ |
|
|
145 | \ |
157 | \ |
146 | public: |
158 | public: |
147 | |
159 | |
148 | #define EV_END_WATCHER(cppstem,cstem) \ |
160 | #define EV_END_WATCHER(cppstem,cstem) \ |
149 | }; |
161 | }; |
… | |
… | |
258 | if (active) start (); |
270 | if (active) start (); |
259 | } |
271 | } |
260 | |
272 | |
261 | void start (const char *path, ev_tstamp interval = 0.) |
273 | void start (const char *path, ev_tstamp interval = 0.) |
262 | { |
274 | { |
|
|
275 | stop (); |
263 | set (path, interval); |
276 | set (path, interval); |
264 | start (); |
277 | start (); |
265 | } |
278 | } |
266 | |
279 | |
267 | void update () |
280 | void update () |
… | |
… | |
283 | void set () { } |
296 | void set () { } |
284 | EV_END_WATCHER (check, check) |
297 | EV_END_WATCHER (check, check) |
285 | |
298 | |
286 | #if EV_EMBED_ENABLE |
299 | #if EV_EMBED_ENABLE |
287 | EV_BEGIN_WATCHER (embed, embed) |
300 | EV_BEGIN_WATCHER (embed, embed) |
288 | void set (struct ev_loop *loop) |
|
|
289 | { |
|
|
290 | int active = is_active (); |
|
|
291 | if (active) stop (); |
|
|
292 | ev_embed_set (static_cast<ev_embed *>(this), loop); |
|
|
293 | if (active) start (); |
|
|
294 | } |
|
|
295 | |
|
|
296 | void start (struct ev_loop *embedded_loop) |
301 | void start (struct ev_loop *embedded_loop) |
297 | { |
302 | { |
298 | set (embedded_loop); |
303 | stop (); |
|
|
304 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
299 | start (); |
305 | start (); |
300 | } |
306 | } |
301 | |
307 | |
302 | void sweep () |
308 | void sweep () |
303 | { |
309 | { |
304 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
310 | ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
305 | } |
311 | } |
306 | EV_END_WATCHER (embed, embed) |
312 | EV_END_WATCHER (embed, embed) |
|
|
313 | #endif |
|
|
314 | |
|
|
315 | #if EV_FORK_ENABLE |
|
|
316 | EV_BEGIN_WATCHER (fork, fork) |
|
|
317 | void set () { } |
|
|
318 | EV_END_WATCHER (fork, fork) |
307 | #endif |
319 | #endif |
308 | |
320 | |
309 | #undef EV_CONSTRUCT |
321 | #undef EV_CONSTRUCT |
310 | #undef EV_BEGIN_WATCHER |
322 | #undef EV_BEGIN_WATCHER |
311 | #undef EV_END_WATCHER |
323 | #undef EV_END_WATCHER |