… | |
… | |
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 | \ |
|
|
83 | extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
|
|
84 | \ |
97 | \ |
85 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
98 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
86 | { \ |
99 | { \ |
87 | EV_CONSTRUCT (cppstem) \ |
100 | EV_CONSTRUCT (cppstem) \ |
88 | { \ |
101 | { \ |
89 | ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
102 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
90 | } \ |
103 | } \ |
91 | \ |
104 | \ |
92 | bool is_active () const \ |
105 | bool is_active () const \ |
93 | { \ |
106 | { \ |
94 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
107 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
… | |
… | |
110 | } \ |
123 | } \ |
111 | \ |
124 | \ |
112 | void operator ()(int events = EV_UNDEF) \ |
125 | void operator ()(int events = EV_UNDEF) \ |
113 | { \ |
126 | { \ |
114 | return call (this, events); \ |
127 | return call (this, events); \ |
|
|
128 | } \ |
|
|
129 | \ |
|
|
130 | ~cppstem () \ |
|
|
131 | { \ |
|
|
132 | stop (); \ |
115 | } \ |
133 | } \ |
116 | \ |
134 | \ |
117 | private: \ |
135 | private: \ |
118 | \ |
136 | \ |
119 | cppstem (const cppstem &o) \ |
137 | cppstem (const cppstem &o) \ |
120 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
138 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
121 | { /* disabled */ } \ |
139 | { /* disabled */ } \ |
|
|
140 | \ |
122 | void operator =(const cppstem &o) { /* disabled */ } \ |
141 | void operator =(const cppstem &o) { /* disabled */ } \ |
123 | \ |
142 | \ |
|
|
143 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
144 | { \ |
|
|
145 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
146 | } \ |
|
|
147 | \ |
124 | public: |
148 | public: |
125 | |
149 | |
|
|
150 | #define EV_END_WATCHER(cppstem,cstem) \ |
|
|
151 | }; |
|
|
152 | |
126 | EV_DECLARE_WATCHER (io, io) |
153 | EV_BEGIN_WATCHER (io, io) |
127 | void set (int fd, int events) |
154 | void set (int fd, int events) |
128 | { |
155 | { |
129 | int active = is_active (); |
156 | int active = is_active (); |
130 | if (active) stop (); |
157 | if (active) stop (); |
131 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
158 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
… | |
… | |
143 | void start (int fd, int events) |
170 | void start (int fd, int events) |
144 | { |
171 | { |
145 | set (fd, events); |
172 | set (fd, events); |
146 | start (); |
173 | start (); |
147 | } |
174 | } |
148 | }; |
175 | EV_END_WATCHER (io, io) |
149 | |
176 | |
150 | EV_DECLARE_WATCHER (timer, timer) |
177 | EV_BEGIN_WATCHER (timer, timer) |
151 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
178 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
152 | { |
179 | { |
153 | int active = is_active (); |
180 | int active = is_active (); |
154 | if (active) stop (); |
181 | if (active) stop (); |
155 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
182 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
… | |
… | |
164 | |
191 | |
165 | void again () |
192 | void again () |
166 | { |
193 | { |
167 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
194 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
168 | } |
195 | } |
169 | }; |
196 | EV_END_WATCHER (timer, timer) |
170 | |
197 | |
|
|
198 | #if EV_PERIODICS |
171 | EV_DECLARE_WATCHER (periodic, periodic) |
199 | EV_BEGIN_WATCHER (periodic, periodic) |
172 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
200 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
173 | { |
201 | { |
174 | int active = is_active (); |
202 | int active = is_active (); |
175 | if (active) stop (); |
203 | if (active) stop (); |
176 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
204 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
… | |
… | |
185 | |
213 | |
186 | void again () |
214 | void again () |
187 | { |
215 | { |
188 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
216 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
189 | } |
217 | } |
190 | }; |
218 | EV_END_WATCHER (periodic, periodic) |
|
|
219 | #endif |
191 | |
220 | |
|
|
221 | EV_BEGIN_WATCHER (idle, idle) |
192 | EV_DECLARE_WATCHER (idle, idle) |
222 | EV_END_WATCHER (idle, idle) |
193 | }; |
|
|
194 | |
223 | |
|
|
224 | EV_BEGIN_WATCHER (prepare, prepare) |
195 | EV_DECLARE_WATCHER (prepare, prepare) |
225 | EV_END_WATCHER (prepare, prepare) |
196 | }; |
|
|
197 | |
226 | |
|
|
227 | EV_BEGIN_WATCHER (check, check) |
198 | EV_DECLARE_WATCHER (check, check) |
228 | EV_END_WATCHER (check, check) |
199 | }; |
|
|
200 | |
229 | |
201 | EV_DECLARE_WATCHER (sig, signal) |
230 | EV_BEGIN_WATCHER (sig, signal) |
202 | void set (int signum) |
231 | void set (int signum) |
203 | { |
232 | { |
204 | int active = is_active (); |
233 | int active = is_active (); |
205 | if (active) stop (); |
234 | if (active) stop (); |
206 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
235 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
210 | void start (int signum) |
239 | void start (int signum) |
211 | { |
240 | { |
212 | set (signum); |
241 | set (signum); |
213 | start (); |
242 | start (); |
214 | } |
243 | } |
215 | }; |
244 | EV_END_WATCHER (sig, signal) |
216 | |
245 | |
217 | EV_DECLARE_WATCHER (child, child) |
246 | EV_BEGIN_WATCHER (child, child) |
218 | void set (int pid) |
247 | void set (int pid) |
219 | { |
248 | { |
220 | int active = is_active (); |
249 | int active = is_active (); |
221 | if (active) stop (); |
250 | if (active) stop (); |
222 | ev_child_set (static_cast<ev_child *>(this), pid); |
251 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
226 | void start (int pid) |
255 | void start (int pid) |
227 | { |
256 | { |
228 | set (pid); |
257 | set (pid); |
229 | start (); |
258 | start (); |
230 | } |
259 | } |
231 | }; |
260 | EV_END_WATCHER (child, child) |
232 | |
261 | |
233 | #undef EV_CONSTRUCT |
262 | #undef EV_CONSTRUCT |
234 | #undef EV_DECLARE_WATCHER |
263 | #undef EV_BEGIN_WATCHER |
235 | } |
264 | } |
236 | |
265 | |
237 | #endif |
266 | #endif |
238 | |
267 | |