1 |
#ifndef EVPP_H__ |
2 |
#define EVPP_H__ |
3 |
|
4 |
/* work in progress, don't use unless you know what you are doing */ |
5 |
|
6 |
namespace ev { |
7 |
|
8 |
template<class watcher> |
9 |
class callback |
10 |
{ |
11 |
struct object { }; |
12 |
|
13 |
void *obj; |
14 |
void (object::*meth)(watcher &, int); |
15 |
|
16 |
/* a proxy is a kind of recipe on how to call a specific class method */ |
17 |
struct proxy_base { |
18 |
virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int) const = 0; |
19 |
}; |
20 |
template<class O1, class O2> |
21 |
struct proxy : proxy_base { |
22 |
virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int e) const |
23 |
{ |
24 |
((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) |
25 |
(w, e); |
26 |
} |
27 |
}; |
28 |
|
29 |
proxy_base *prxy; |
30 |
|
31 |
public: |
32 |
template<class O1, class O2> |
33 |
explicit callback (O1 *object, void (O2::*method)(watcher &, int)) |
34 |
{ |
35 |
static proxy<O1,O2> p; |
36 |
obj = reinterpret_cast<void *>(object); |
37 |
meth = reinterpret_cast<void (object::*)(watcher &, int)>(method); |
38 |
prxy = &p; |
39 |
} |
40 |
|
41 |
void call (watcher *w, int e) const |
42 |
{ |
43 |
return prxy->call (obj, meth, *w, e); |
44 |
} |
45 |
}; |
46 |
|
47 |
#include "ev.h" |
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 |
|
64 |
typedef ev_tstamp tstamp; |
65 |
|
66 |
inline ev_tstamp now (EV_P) |
67 |
{ |
68 |
return ev_now (EV_A); |
69 |
} |
70 |
|
71 |
#if EV_MULTIPLICITY |
72 |
|
73 |
#define EV_CONSTRUCT(cppstem) \ |
74 |
EV_P; \ |
75 |
\ |
76 |
void set (EV_P) \ |
77 |
{ \ |
78 |
this->EV_A = EV_A; \ |
79 |
} \ |
80 |
\ |
81 |
template<class O1, class O2> \ |
82 |
explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \ |
83 |
: callback<cppstem> (object, method), EV_A (EV_A) |
84 |
|
85 |
#else |
86 |
|
87 |
#define EV_CONSTRUCT(cppstem) \ |
88 |
template<class O1, class O2> \ |
89 |
explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
90 |
: callback<cppstem> (object, method) |
91 |
|
92 |
#endif |
93 |
|
94 |
/* using a template here would require quite a bit more lines, |
95 |
* so a macro solution was chosen */ |
96 |
#define EV_DECLARE_WATCHER(cppstem,cstem) \ |
97 |
\ |
98 |
extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
99 |
\ |
100 |
struct cppstem : ev_ ## cstem, callback<cppstem> \ |
101 |
{ \ |
102 |
EV_CONSTRUCT (cppstem) \ |
103 |
{ \ |
104 |
ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
105 |
} \ |
106 |
\ |
107 |
bool is_active () const \ |
108 |
{ \ |
109 |
return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
110 |
} \ |
111 |
\ |
112 |
bool is_pending () const \ |
113 |
{ \ |
114 |
return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \ |
115 |
} \ |
116 |
\ |
117 |
void start () \ |
118 |
{ \ |
119 |
ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
120 |
} \ |
121 |
\ |
122 |
void stop () \ |
123 |
{ \ |
124 |
ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
125 |
} \ |
126 |
\ |
127 |
void operator ()(int events = EV_UNDEF) \ |
128 |
{ \ |
129 |
return call (this, events); \ |
130 |
} \ |
131 |
\ |
132 |
private: \ |
133 |
\ |
134 |
cppstem (const cppstem &o) \ |
135 |
: callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
136 |
{ /* disabled */ } \ |
137 |
void operator =(const cppstem &o) { /* disabled */ } \ |
138 |
\ |
139 |
public: |
140 |
|
141 |
EV_DECLARE_WATCHER (io, io) |
142 |
void set (int fd, int events) |
143 |
{ |
144 |
int active = is_active (); |
145 |
if (active) stop (); |
146 |
ev_io_set (static_cast<ev_io *>(this), fd, events); |
147 |
if (active) start (); |
148 |
} |
149 |
|
150 |
void set (int events) |
151 |
{ |
152 |
int active = is_active (); |
153 |
if (active) stop (); |
154 |
ev_io_set (static_cast<ev_io *>(this), fd, events); |
155 |
if (active) start (); |
156 |
} |
157 |
|
158 |
void start (int fd, int events) |
159 |
{ |
160 |
set (fd, events); |
161 |
start (); |
162 |
} |
163 |
}; |
164 |
|
165 |
EV_DECLARE_WATCHER (timer, timer) |
166 |
void set (ev_tstamp after, ev_tstamp repeat = 0.) |
167 |
{ |
168 |
int active = is_active (); |
169 |
if (active) stop (); |
170 |
ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
171 |
if (active) start (); |
172 |
} |
173 |
|
174 |
void start (ev_tstamp after, ev_tstamp repeat = 0.) |
175 |
{ |
176 |
set (after, repeat); |
177 |
start (); |
178 |
} |
179 |
|
180 |
void again () |
181 |
{ |
182 |
ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
183 |
} |
184 |
}; |
185 |
|
186 |
EV_DECLARE_WATCHER (periodic, periodic) |
187 |
void set (ev_tstamp at, ev_tstamp interval = 0.) |
188 |
{ |
189 |
int active = is_active (); |
190 |
if (active) stop (); |
191 |
ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
192 |
if (active) start (); |
193 |
} |
194 |
|
195 |
void start (ev_tstamp at, ev_tstamp interval = 0.) |
196 |
{ |
197 |
set (at, interval); |
198 |
start (); |
199 |
} |
200 |
|
201 |
void again () |
202 |
{ |
203 |
ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
204 |
} |
205 |
}; |
206 |
|
207 |
EV_DECLARE_WATCHER (idle, idle) |
208 |
}; |
209 |
|
210 |
EV_DECLARE_WATCHER (prepare, prepare) |
211 |
}; |
212 |
|
213 |
EV_DECLARE_WATCHER (check, check) |
214 |
}; |
215 |
|
216 |
EV_DECLARE_WATCHER (sig, signal) |
217 |
void set (int signum) |
218 |
{ |
219 |
int active = is_active (); |
220 |
if (active) stop (); |
221 |
ev_signal_set (static_cast<ev_signal *>(this), signum); |
222 |
if (active) start (); |
223 |
} |
224 |
|
225 |
void start (int signum) |
226 |
{ |
227 |
set (signum); |
228 |
start (); |
229 |
} |
230 |
}; |
231 |
|
232 |
EV_DECLARE_WATCHER (child, child) |
233 |
void set (int pid) |
234 |
{ |
235 |
int active = is_active (); |
236 |
if (active) stop (); |
237 |
ev_child_set (static_cast<ev_child *>(this), pid); |
238 |
if (active) start (); |
239 |
} |
240 |
|
241 |
void start (int pid) |
242 |
{ |
243 |
set (pid); |
244 |
start (); |
245 |
} |
246 |
}; |
247 |
|
248 |
#undef EV_CONSTRUCT |
249 |
#undef EV_DECLARE_WATCHER |
250 |
} |
251 |
|
252 |
#endif |
253 |
|