… | |
… | |
44 | } |
44 | } |
45 | }; |
45 | }; |
46 | |
46 | |
47 | #include "ev.h" |
47 | #include "ev.h" |
48 | |
48 | |
|
|
49 | enum { |
|
|
50 | UNDEF = EV_UNDEF, |
|
|
51 | NONE = EV_NONE, |
|
|
52 | READ = EV_READ, |
|
|
53 | WRITE = EV_WRITE, |
|
|
54 | TIMEOUT = EV_TIMEOUT, |
|
|
55 | PERIODIC = EV_PERIODIC, |
|
|
56 | SIGNAL = EV_SIGNAL, |
|
|
57 | IDLE = EV_IDLE, |
|
|
58 | CHECK = EV_CHECK, |
|
|
59 | PREPARE = EV_PREPARE, |
|
|
60 | CHILD = EV_CHILD, |
|
|
61 | ERROR = EV_ERROR, |
|
|
62 | }; |
|
|
63 | |
49 | typedef ev_tstamp tstamp; |
64 | typedef ev_tstamp tstamp; |
50 | |
65 | |
51 | inline ev_tstamp now (EV_P) |
66 | inline ev_tstamp now (EV_P) |
52 | { |
67 | { |
53 | return ev_now (EV_A); |
68 | return ev_now (EV_A); |
… | |
… | |
76 | |
91 | |
77 | #endif |
92 | #endif |
78 | |
93 | |
79 | /* using a template here would require quite a bit more lines, |
94 | /* using a template here would require quite a bit more lines, |
80 | * so a macro solution was chosen */ |
95 | * so a macro solution was chosen */ |
81 | #define EV_DECLARE_WATCHER(cppstem,cstem) \ |
96 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
82 | \ |
97 | \ |
83 | extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
98 | static void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
84 | \ |
99 | \ |
85 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
100 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
86 | { \ |
101 | { \ |
87 | EV_CONSTRUCT (cppstem) \ |
102 | EV_CONSTRUCT (cppstem) \ |
88 | { \ |
103 | { \ |
… | |
… | |
110 | } \ |
125 | } \ |
111 | \ |
126 | \ |
112 | void operator ()(int events = EV_UNDEF) \ |
127 | void operator ()(int events = EV_UNDEF) \ |
113 | { \ |
128 | { \ |
114 | return call (this, events); \ |
129 | return call (this, events); \ |
|
|
130 | } \ |
|
|
131 | \ |
|
|
132 | ~cppstem () \ |
|
|
133 | { \ |
|
|
134 | stop (); \ |
115 | } \ |
135 | } \ |
116 | \ |
136 | \ |
117 | private: \ |
137 | private: \ |
118 | \ |
138 | \ |
119 | cppstem (const cppstem &o) \ |
139 | cppstem (const cppstem &o) \ |
… | |
… | |
121 | { /* disabled */ } \ |
141 | { /* disabled */ } \ |
122 | void operator =(const cppstem &o) { /* disabled */ } \ |
142 | void operator =(const cppstem &o) { /* disabled */ } \ |
123 | \ |
143 | \ |
124 | public: |
144 | public: |
125 | |
145 | |
|
|
146 | #define EV_END_WATCHER(cppstem,cstem) \ |
|
|
147 | }; \ |
|
|
148 | \ |
|
|
149 | static void cb_ ## cppstem (struct ev_ ## cstem *w, int revents) \ |
|
|
150 | { \ |
|
|
151 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
152 | } |
|
|
153 | |
126 | EV_DECLARE_WATCHER (io, io) |
154 | EV_BEGIN_WATCHER (io, io) |
127 | void set (int fd, int events) |
155 | void set (int fd, int events) |
128 | { |
156 | { |
129 | int active = is_active (); |
157 | int active = is_active (); |
130 | if (active) stop (); |
158 | if (active) stop (); |
131 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
159 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
… | |
… | |
143 | void start (int fd, int events) |
171 | void start (int fd, int events) |
144 | { |
172 | { |
145 | set (fd, events); |
173 | set (fd, events); |
146 | start (); |
174 | start (); |
147 | } |
175 | } |
148 | }; |
176 | EV_END_WATCHER (io, io) |
149 | |
177 | |
150 | EV_DECLARE_WATCHER (timer, timer) |
178 | EV_BEGIN_WATCHER (timer, timer) |
151 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
179 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
152 | { |
180 | { |
153 | int active = is_active (); |
181 | int active = is_active (); |
154 | if (active) stop (); |
182 | if (active) stop (); |
155 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
183 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
… | |
… | |
164 | |
192 | |
165 | void again () |
193 | void again () |
166 | { |
194 | { |
167 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
195 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
168 | } |
196 | } |
169 | }; |
197 | EV_END_WATCHER (timer, timer) |
170 | |
198 | |
|
|
199 | #if EV_PERIODICS |
171 | EV_DECLARE_WATCHER (periodic, periodic) |
200 | EV_BEGIN_WATCHER (periodic, periodic) |
172 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
201 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
173 | { |
202 | { |
174 | int active = is_active (); |
203 | int active = is_active (); |
175 | if (active) stop (); |
204 | if (active) stop (); |
176 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
205 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
… | |
… | |
185 | |
214 | |
186 | void again () |
215 | void again () |
187 | { |
216 | { |
188 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
217 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
189 | } |
218 | } |
190 | }; |
219 | EV_END_WATCHER (periodic, periodic) |
|
|
220 | #endif |
191 | |
221 | |
|
|
222 | EV_BEGIN_WATCHER (idle, idle) |
192 | EV_DECLARE_WATCHER (idle, idle) |
223 | EV_END_WATCHER (idle, idle) |
193 | }; |
|
|
194 | |
224 | |
|
|
225 | EV_BEGIN_WATCHER (prepare, prepare) |
195 | EV_DECLARE_WATCHER (prepare, prepare) |
226 | EV_END_WATCHER (prepare, prepare) |
196 | }; |
|
|
197 | |
227 | |
|
|
228 | EV_BEGIN_WATCHER (check, check) |
198 | EV_DECLARE_WATCHER (check, check) |
229 | EV_END_WATCHER (check, check) |
199 | }; |
|
|
200 | |
230 | |
201 | EV_DECLARE_WATCHER (sig, signal) |
231 | EV_BEGIN_WATCHER (sig, signal) |
202 | void set (int signum) |
232 | void set (int signum) |
203 | { |
233 | { |
204 | int active = is_active (); |
234 | int active = is_active (); |
205 | if (active) stop (); |
235 | if (active) stop (); |
206 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
236 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
210 | void start (int signum) |
240 | void start (int signum) |
211 | { |
241 | { |
212 | set (signum); |
242 | set (signum); |
213 | start (); |
243 | start (); |
214 | } |
244 | } |
215 | }; |
245 | EV_END_WATCHER (sig, signal) |
216 | |
246 | |
217 | EV_DECLARE_WATCHER (child, child) |
247 | EV_BEGIN_WATCHER (child, child) |
218 | void set (int pid) |
248 | void set (int pid) |
219 | { |
249 | { |
220 | int active = is_active (); |
250 | int active = is_active (); |
221 | if (active) stop (); |
251 | if (active) stop (); |
222 | ev_child_set (static_cast<ev_child *>(this), pid); |
252 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
226 | void start (int pid) |
256 | void start (int pid) |
227 | { |
257 | { |
228 | set (pid); |
258 | set (pid); |
229 | start (); |
259 | start (); |
230 | } |
260 | } |
231 | }; |
261 | EV_END_WATCHER (child, child) |
232 | |
262 | |
233 | #undef EV_CONSTRUCT |
263 | #undef EV_CONSTRUCT |
234 | #undef EV_DECLARE_WATCHER |
264 | #undef EV_BEGIN_WATCHER |
235 | } |
265 | } |
236 | |
266 | |
237 | #endif |
267 | #endif |
238 | |
268 | |