… | |
… | |
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 klass; // it is vital that this is never defined |
11 | #if EV_MULTIPLICITY |
|
|
12 | EV_P; |
12 | |
13 | |
13 | klass *o; |
14 | 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 | { |
15 | { |
20 | o = reinterpret_cast<klass *>(object); |
16 | this->EV_A = EV_A; |
21 | m = reinterpret_cast<void (klass::*)(watcher &, int)>(method); |
|
|
22 | } |
17 | } |
|
|
18 | #endif |
23 | |
19 | |
24 | // this works because a standards-compliant C++ compiler |
20 | base () |
25 | // basically can't help it: it doesn't have the knowledge |
21 | { |
26 | // required to miscompile (klass is not defined anywhere |
22 | ev_init (this, 0); |
27 | // and nothing is known about the constructor arguments) :) |
|
|
28 | void call (watcher *w, int revents) |
|
|
29 | { |
23 | } |
|
|
24 | |
|
|
25 | void set_ (void *object, void (*cb)(ev_watcher *w, int revents)) |
|
|
26 | { |
|
|
27 | this->data = object; |
|
|
28 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
|
|
29 | } |
|
|
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); |
30 | (o->*m) (*w, revents); |
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 ev_cb (static_cast<ev_watcher *>(this)) |
|
|
75 | (static_cast<ev_watcher *>(this), events); |
|
|
76 | } |
|
|
77 | |
|
|
78 | bool is_active () const |
|
|
79 | { |
|
|
80 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
|
|
81 | } |
|
|
82 | |
|
|
83 | bool is_pending () const |
|
|
84 | { |
|
|
85 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
31 | } |
86 | } |
32 | }; |
87 | }; |
33 | |
88 | |
34 | enum { |
89 | enum { |
35 | UNDEF = EV_UNDEF, |
90 | UNDEF = EV_UNDEF, |
… | |
… | |
55 | { |
110 | { |
56 | return ev_now (EV_A); |
111 | return ev_now (EV_A); |
57 | } |
112 | } |
58 | |
113 | |
59 | #if EV_MULTIPLICITY |
114 | #if EV_MULTIPLICITY |
60 | |
|
|
61 | #define EV_CONSTRUCT(cppstem) \ |
|
|
62 | EV_P; \ |
115 | #define EV_CONSTRUCT \ |
63 | \ |
|
|
64 | void set (EV_P) \ |
116 | (EV_P = EV_DEFAULT) \ |
65 | { \ |
117 | { \ |
66 | this->EV_A = EV_A; \ |
|
|
67 | } \ |
|
|
68 | \ |
118 | set (EV_A); \ |
69 | template<class O1, class O2> \ |
119 | } |
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 |
120 | #else |
74 | |
121 | #define EV_CONSTRUCT \ |
75 | #define EV_CONSTRUCT(cppstem) \ |
122 | () \ |
76 | template<class O1, class O2> \ |
123 | { \ |
77 | explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
124 | } |
78 | : callback<cppstem> (object, method) |
|
|
79 | |
|
|
80 | #endif |
125 | #endif |
81 | |
126 | |
82 | /* using a template here would require quite a bit more lines, |
127 | /* using a template here would require quite a bit more lines, |
83 | * so a macro solution was chosen */ |
128 | * so a macro solution was chosen */ |
84 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
129 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
85 | \ |
130 | \ |
86 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
131 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
87 | { \ |
132 | { \ |
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 () \ |
133 | void start () \ |
104 | { \ |
134 | { \ |
105 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
135 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
106 | } \ |
136 | } \ |
107 | \ |
137 | \ |
108 | void stop () \ |
138 | void stop () \ |
109 | { \ |
139 | { \ |
110 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
140 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
111 | } \ |
141 | } \ |
112 | \ |
142 | \ |
113 | void operator ()(int events = EV_UNDEF) \ |
|
|
114 | { \ |
143 | cppstem EV_CONSTRUCT \ |
115 | return call (this, events); \ |
|
|
116 | } \ |
|
|
117 | \ |
144 | \ |
118 | ~cppstem () \ |
145 | ~cppstem () \ |
119 | { \ |
146 | { \ |
120 | stop (); \ |
147 | stop (); \ |
121 | } \ |
148 | } \ |
122 | \ |
149 | \ |
|
|
150 | using base<ev_ ## cstem, cppstem>::set; \ |
|
|
151 | \ |
123 | private: \ |
152 | private: \ |
124 | \ |
153 | \ |
125 | cppstem (const cppstem &o) \ |
154 | cppstem (const cppstem &o) \ |
126 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
|
|
127 | { /* disabled */ } \ |
155 | { /* disabled */ } \ |
128 | \ |
156 | \ |
129 | void operator =(const cppstem &o) { /* disabled */ } \ |
157 | 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 | \ |
158 | \ |
136 | public: |
159 | public: |
137 | |
160 | |
138 | #define EV_END_WATCHER(cppstem,cstem) \ |
161 | #define EV_END_WATCHER(cppstem,cstem) \ |
139 | }; |
162 | }; |
… | |
… | |
248 | if (active) start (); |
271 | if (active) start (); |
249 | } |
272 | } |
250 | |
273 | |
251 | void start (const char *path, ev_tstamp interval = 0.) |
274 | void start (const char *path, ev_tstamp interval = 0.) |
252 | { |
275 | { |
|
|
276 | stop (); |
253 | set (path, interval); |
277 | set (path, interval); |
254 | start (); |
278 | start (); |
255 | } |
279 | } |
256 | |
280 | |
257 | void update () |
281 | void update () |
… | |
… | |
273 | void set () { } |
297 | void set () { } |
274 | EV_END_WATCHER (check, check) |
298 | EV_END_WATCHER (check, check) |
275 | |
299 | |
276 | #if EV_EMBED_ENABLE |
300 | #if EV_EMBED_ENABLE |
277 | EV_BEGIN_WATCHER (embed, embed) |
301 | 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) |
302 | void start (struct ev_loop *embedded_loop) |
287 | { |
303 | { |
288 | set (embedded_loop); |
304 | stop (); |
|
|
305 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
289 | start (); |
306 | start (); |
290 | } |
307 | } |
291 | |
308 | |
292 | void sweep () |
309 | void sweep () |
293 | { |
310 | { |