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