1 |
root |
1.1 |
#ifndef EVPP_H__ |
2 |
|
|
#define EVPP_H__ |
3 |
|
|
|
4 |
root |
1.20 |
#ifdef EV_H |
5 |
|
|
# include EV_H |
6 |
|
|
#else |
7 |
|
|
# include <ev.h> |
8 |
|
|
#endif |
9 |
root |
1.1 |
|
10 |
|
|
namespace ev { |
11 |
|
|
|
12 |
root |
1.13 |
template<class ev_watcher, class watcher> |
13 |
|
|
struct base : ev_watcher |
14 |
root |
1.1 |
{ |
15 |
root |
1.13 |
#if EV_MULTIPLICITY |
16 |
|
|
EV_P; |
17 |
root |
1.1 |
|
18 |
root |
1.13 |
void set (EV_P) |
19 |
|
|
{ |
20 |
|
|
this->EV_A = EV_A; |
21 |
|
|
} |
22 |
|
|
#endif |
23 |
root |
1.1 |
|
24 |
root |
1.13 |
base () |
25 |
|
|
{ |
26 |
|
|
ev_init (this, 0); |
27 |
|
|
} |
28 |
|
|
|
29 |
root |
1.18 |
void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
30 |
root |
1.13 |
{ |
31 |
root |
1.17 |
this->data = data; |
32 |
root |
1.18 |
ev_set_cb (static_cast<ev_watcher *>(this), cb); |
33 |
root |
1.13 |
} |
34 |
|
|
|
35 |
root |
1.19 |
// method callback |
36 |
root |
1.13 |
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 |
root |
1.18 |
static void method_thunk (EV_P_ ev_watcher *w, int revents) |
44 |
root |
1.13 |
{ |
45 |
root |
1.17 |
K *obj = static_cast<K *>(w->data); |
46 |
root |
1.18 |
(obj->*method) (*static_cast<watcher *>(w), revents); |
47 |
root |
1.13 |
} |
48 |
|
|
|
49 |
root |
1.19 |
// const method callback |
50 |
root |
1.13 |
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 |
root |
1.18 |
static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
58 |
root |
1.13 |
{ |
59 |
root |
1.17 |
K *obj = static_cast<K *>(w->data); |
60 |
root |
1.18 |
(static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
61 |
root |
1.13 |
} |
62 |
|
|
|
63 |
root |
1.19 |
// function callback |
64 |
root |
1.17 |
template<void (*function)(watcher &w, int)> |
65 |
|
|
void set (void *data = 0) |
66 |
root |
1.13 |
{ |
67 |
root |
1.16 |
set_ (data, function_thunk<function>); |
68 |
root |
1.13 |
} |
69 |
|
|
|
70 |
|
|
template<void (*function)(watcher &w, int)> |
71 |
root |
1.18 |
static void function_thunk (EV_P_ ev_watcher *w, int revents) |
72 |
root |
1.13 |
{ |
73 |
root |
1.18 |
function (*static_cast<watcher *>(w), revents); |
74 |
root |
1.13 |
} |
75 |
|
|
|
76 |
root |
1.19 |
// 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 |
root |
1.13 |
void operator ()(int events = EV_UNDEF) |
91 |
|
|
{ |
92 |
root |
1.14 |
return ev_cb (static_cast<ev_watcher *>(this)) |
93 |
|
|
(static_cast<ev_watcher *>(this), events); |
94 |
root |
1.13 |
} |
95 |
|
|
|
96 |
|
|
bool is_active () const |
97 |
root |
1.1 |
{ |
98 |
root |
1.13 |
return ev_is_active (static_cast<const ev_watcher *>(this)); |
99 |
root |
1.1 |
} |
100 |
|
|
|
101 |
root |
1.13 |
bool is_pending () const |
102 |
root |
1.1 |
{ |
103 |
root |
1.13 |
return ev_is_pending (static_cast<const ev_watcher *>(this)); |
104 |
root |
1.1 |
} |
105 |
|
|
}; |
106 |
|
|
|
107 |
root |
1.2 |
enum { |
108 |
|
|
UNDEF = EV_UNDEF, |
109 |
|
|
NONE = EV_NONE, |
110 |
|
|
READ = EV_READ, |
111 |
|
|
WRITE = EV_WRITE, |
112 |
|
|
TIMEOUT = EV_TIMEOUT, |
113 |
|
|
PERIODIC = EV_PERIODIC, |
114 |
|
|
SIGNAL = EV_SIGNAL, |
115 |
root |
1.9 |
CHILD = EV_CHILD, |
116 |
|
|
STAT = EV_STAT, |
117 |
root |
1.2 |
IDLE = EV_IDLE, |
118 |
|
|
CHECK = EV_CHECK, |
119 |
|
|
PREPARE = EV_PREPARE, |
120 |
root |
1.9 |
FORK = EV_FORK, |
121 |
|
|
EMBED = EV_EMBED, |
122 |
root |
1.2 |
ERROR = EV_ERROR, |
123 |
|
|
}; |
124 |
|
|
|
125 |
root |
1.1 |
typedef ev_tstamp tstamp; |
126 |
|
|
|
127 |
|
|
inline ev_tstamp now (EV_P) |
128 |
|
|
{ |
129 |
|
|
return ev_now (EV_A); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
#if EV_MULTIPLICITY |
133 |
root |
1.13 |
#define EV_CONSTRUCT \ |
134 |
|
|
(EV_P = EV_DEFAULT) \ |
135 |
root |
1.1 |
{ \ |
136 |
root |
1.13 |
set (EV_A); \ |
137 |
|
|
} |
138 |
root |
1.1 |
#else |
139 |
root |
1.13 |
#define EV_CONSTRUCT \ |
140 |
|
|
() \ |
141 |
|
|
{ \ |
142 |
|
|
} |
143 |
root |
1.1 |
#endif |
144 |
|
|
|
145 |
|
|
/* using a template here would require quite a bit more lines, |
146 |
|
|
* so a macro solution was chosen */ |
147 |
root |
1.4 |
#define EV_BEGIN_WATCHER(cppstem,cstem) \ |
148 |
root |
1.1 |
\ |
149 |
root |
1.13 |
struct cppstem : base<ev_ ## cstem, cppstem> \ |
150 |
root |
1.1 |
{ \ |
151 |
|
|
void start () \ |
152 |
|
|
{ \ |
153 |
|
|
ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
154 |
|
|
} \ |
155 |
|
|
\ |
156 |
|
|
void stop () \ |
157 |
|
|
{ \ |
158 |
|
|
ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
159 |
|
|
} \ |
160 |
|
|
\ |
161 |
root |
1.13 |
cppstem EV_CONSTRUCT \ |
162 |
root |
1.1 |
\ |
163 |
root |
1.3 |
~cppstem () \ |
164 |
|
|
{ \ |
165 |
|
|
stop (); \ |
166 |
|
|
} \ |
167 |
|
|
\ |
168 |
root |
1.13 |
using base<ev_ ## cstem, cppstem>::set; \ |
169 |
|
|
\ |
170 |
root |
1.1 |
private: \ |
171 |
|
|
\ |
172 |
|
|
cppstem (const cppstem &o) \ |
173 |
|
|
{ /* disabled */ } \ |
174 |
root |
1.6 |
\ |
175 |
root |
1.1 |
void operator =(const cppstem &o) { /* disabled */ } \ |
176 |
|
|
\ |
177 |
|
|
public: |
178 |
|
|
|
179 |
root |
1.4 |
#define EV_END_WATCHER(cppstem,cstem) \ |
180 |
root |
1.6 |
}; |
181 |
root |
1.4 |
|
182 |
|
|
EV_BEGIN_WATCHER (io, io) |
183 |
root |
1.1 |
void set (int fd, int events) |
184 |
|
|
{ |
185 |
|
|
int active = is_active (); |
186 |
|
|
if (active) stop (); |
187 |
|
|
ev_io_set (static_cast<ev_io *>(this), fd, events); |
188 |
|
|
if (active) start (); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
void set (int events) |
192 |
|
|
{ |
193 |
|
|
int active = is_active (); |
194 |
|
|
if (active) stop (); |
195 |
|
|
ev_io_set (static_cast<ev_io *>(this), fd, events); |
196 |
|
|
if (active) start (); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
void start (int fd, int events) |
200 |
|
|
{ |
201 |
|
|
set (fd, events); |
202 |
|
|
start (); |
203 |
|
|
} |
204 |
root |
1.4 |
EV_END_WATCHER (io, io) |
205 |
root |
1.1 |
|
206 |
root |
1.4 |
EV_BEGIN_WATCHER (timer, timer) |
207 |
root |
1.1 |
void set (ev_tstamp after, ev_tstamp repeat = 0.) |
208 |
|
|
{ |
209 |
|
|
int active = is_active (); |
210 |
|
|
if (active) stop (); |
211 |
|
|
ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
212 |
|
|
if (active) start (); |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
void start (ev_tstamp after, ev_tstamp repeat = 0.) |
216 |
|
|
{ |
217 |
|
|
set (after, repeat); |
218 |
|
|
start (); |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
void again () |
222 |
|
|
{ |
223 |
|
|
ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
224 |
|
|
} |
225 |
root |
1.4 |
EV_END_WATCHER (timer, timer) |
226 |
root |
1.1 |
|
227 |
root |
1.8 |
#if EV_PERIODIC_ENABLE |
228 |
root |
1.4 |
EV_BEGIN_WATCHER (periodic, periodic) |
229 |
root |
1.1 |
void set (ev_tstamp at, ev_tstamp interval = 0.) |
230 |
|
|
{ |
231 |
|
|
int active = is_active (); |
232 |
|
|
if (active) stop (); |
233 |
|
|
ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
234 |
|
|
if (active) start (); |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
void start (ev_tstamp at, ev_tstamp interval = 0.) |
238 |
|
|
{ |
239 |
|
|
set (at, interval); |
240 |
|
|
start (); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
void again () |
244 |
|
|
{ |
245 |
|
|
ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
246 |
|
|
} |
247 |
root |
1.4 |
EV_END_WATCHER (periodic, periodic) |
248 |
root |
1.3 |
#endif |
249 |
root |
1.1 |
|
250 |
root |
1.4 |
EV_BEGIN_WATCHER (sig, signal) |
251 |
root |
1.1 |
void set (int signum) |
252 |
|
|
{ |
253 |
|
|
int active = is_active (); |
254 |
|
|
if (active) stop (); |
255 |
|
|
ev_signal_set (static_cast<ev_signal *>(this), signum); |
256 |
|
|
if (active) start (); |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
void start (int signum) |
260 |
|
|
{ |
261 |
|
|
set (signum); |
262 |
|
|
start (); |
263 |
|
|
} |
264 |
root |
1.4 |
EV_END_WATCHER (sig, signal) |
265 |
root |
1.1 |
|
266 |
root |
1.4 |
EV_BEGIN_WATCHER (child, child) |
267 |
root |
1.1 |
void set (int pid) |
268 |
|
|
{ |
269 |
|
|
int active = is_active (); |
270 |
|
|
if (active) stop (); |
271 |
|
|
ev_child_set (static_cast<ev_child *>(this), pid); |
272 |
|
|
if (active) start (); |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
void start (int pid) |
276 |
|
|
{ |
277 |
|
|
set (pid); |
278 |
|
|
start (); |
279 |
|
|
} |
280 |
root |
1.4 |
EV_END_WATCHER (child, child) |
281 |
root |
1.1 |
|
282 |
root |
1.8 |
#if EV_STAT_ENABLE |
283 |
|
|
EV_BEGIN_WATCHER (stat, stat) |
284 |
|
|
void set (const char *path, ev_tstamp interval = 0.) |
285 |
|
|
{ |
286 |
|
|
int active = is_active (); |
287 |
|
|
if (active) stop (); |
288 |
|
|
ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
289 |
|
|
if (active) start (); |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
void start (const char *path, ev_tstamp interval = 0.) |
293 |
|
|
{ |
294 |
root |
1.12 |
stop (); |
295 |
root |
1.8 |
set (path, interval); |
296 |
|
|
start (); |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
void update () |
300 |
|
|
{ |
301 |
|
|
ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
302 |
|
|
} |
303 |
|
|
EV_END_WATCHER (stat, stat) |
304 |
|
|
#endif |
305 |
|
|
|
306 |
|
|
EV_BEGIN_WATCHER (idle, idle) |
307 |
|
|
void set () { } |
308 |
|
|
EV_END_WATCHER (idle, idle) |
309 |
root |
1.7 |
|
310 |
root |
1.8 |
EV_BEGIN_WATCHER (prepare, prepare) |
311 |
|
|
void set () { } |
312 |
|
|
EV_END_WATCHER (prepare, prepare) |
313 |
|
|
|
314 |
|
|
EV_BEGIN_WATCHER (check, check) |
315 |
|
|
void set () { } |
316 |
|
|
EV_END_WATCHER (check, check) |
317 |
|
|
|
318 |
|
|
#if EV_EMBED_ENABLE |
319 |
root |
1.7 |
EV_BEGIN_WATCHER (embed, embed) |
320 |
|
|
void start (struct ev_loop *embedded_loop) |
321 |
|
|
{ |
322 |
root |
1.12 |
stop (); |
323 |
|
|
ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
324 |
root |
1.7 |
start (); |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
void sweep () |
328 |
|
|
{ |
329 |
|
|
ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
330 |
|
|
} |
331 |
|
|
EV_END_WATCHER (embed, embed) |
332 |
|
|
#endif |
333 |
|
|
|
334 |
root |
1.10 |
#if EV_FORK_ENABLE |
335 |
|
|
EV_BEGIN_WATCHER (fork, fork) |
336 |
|
|
void set () { } |
337 |
|
|
EV_END_WATCHER (fork, fork) |
338 |
|
|
#endif |
339 |
|
|
|
340 |
root |
1.1 |
#undef EV_CONSTRUCT |
341 |
root |
1.4 |
#undef EV_BEGIN_WATCHER |
342 |
root |
1.7 |
#undef EV_END_WATCHER |
343 |
root |
1.1 |
} |
344 |
|
|
|
345 |
|
|
#endif |
346 |
|
|
|