1 |
root |
1.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 |
|
|
typedef ev_tstamp tstamp; |
50 |
|
|
|
51 |
|
|
inline ev_tstamp now (EV_P) |
52 |
|
|
{ |
53 |
|
|
return ev_now (EV_A); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
#if EV_MULTIPLICITY |
57 |
|
|
|
58 |
|
|
#define EV_CONSTRUCT(cppstem) \ |
59 |
|
|
EV_P; \ |
60 |
|
|
\ |
61 |
|
|
void set (EV_P) \ |
62 |
|
|
{ \ |
63 |
|
|
this->EV_A = EV_A; \ |
64 |
|
|
} \ |
65 |
|
|
\ |
66 |
|
|
template<class O1, class O2> \ |
67 |
|
|
explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \ |
68 |
|
|
: callback<cppstem> (object, method), EV_A (EV_A) |
69 |
|
|
|
70 |
|
|
#else |
71 |
|
|
|
72 |
|
|
#define EV_CONSTRUCT(cppstem) \ |
73 |
|
|
template<class O1, class O2> \ |
74 |
|
|
explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ |
75 |
|
|
: callback<cppstem> (object, method) |
76 |
|
|
|
77 |
|
|
#endif |
78 |
|
|
|
79 |
|
|
/* using a template here would require quite a bit more lines, |
80 |
|
|
* so a macro solution was chosen */ |
81 |
|
|
#define EV_DECLARE_WATCHER(cppstem,cstem) \ |
82 |
|
|
\ |
83 |
|
|
extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
84 |
|
|
\ |
85 |
|
|
struct cppstem : ev_ ## cstem, callback<cppstem> \ |
86 |
|
|
{ \ |
87 |
|
|
EV_CONSTRUCT (cppstem) \ |
88 |
|
|
{ \ |
89 |
|
|
ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
90 |
|
|
} \ |
91 |
|
|
\ |
92 |
|
|
bool is_active () const \ |
93 |
|
|
{ \ |
94 |
|
|
return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
95 |
|
|
} \ |
96 |
|
|
\ |
97 |
|
|
bool is_pending () const \ |
98 |
|
|
{ \ |
99 |
|
|
return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \ |
100 |
|
|
} \ |
101 |
|
|
\ |
102 |
|
|
void start () \ |
103 |
|
|
{ \ |
104 |
|
|
ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
105 |
|
|
} \ |
106 |
|
|
\ |
107 |
|
|
void stop () \ |
108 |
|
|
{ \ |
109 |
|
|
ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
110 |
|
|
} \ |
111 |
|
|
\ |
112 |
|
|
void operator ()(int events = EV_UNDEF) \ |
113 |
|
|
{ \ |
114 |
|
|
return call (this, events); \ |
115 |
|
|
} \ |
116 |
|
|
\ |
117 |
|
|
private: \ |
118 |
|
|
\ |
119 |
|
|
cppstem (const cppstem &o) \ |
120 |
|
|
: callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
121 |
|
|
{ /* disabled */ } \ |
122 |
|
|
void operator =(const cppstem &o) { /* disabled */ } \ |
123 |
|
|
\ |
124 |
|
|
public: |
125 |
|
|
|
126 |
|
|
EV_DECLARE_WATCHER (io, io) |
127 |
|
|
void set (int fd, int events) |
128 |
|
|
{ |
129 |
|
|
int active = is_active (); |
130 |
|
|
if (active) stop (); |
131 |
|
|
ev_io_set (static_cast<ev_io *>(this), fd, events); |
132 |
|
|
if (active) start (); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
void set (int events) |
136 |
|
|
{ |
137 |
|
|
int active = is_active (); |
138 |
|
|
if (active) stop (); |
139 |
|
|
ev_io_set (static_cast<ev_io *>(this), fd, events); |
140 |
|
|
if (active) start (); |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
void start (int fd, int events) |
144 |
|
|
{ |
145 |
|
|
set (fd, events); |
146 |
|
|
start (); |
147 |
|
|
} |
148 |
|
|
}; |
149 |
|
|
|
150 |
|
|
EV_DECLARE_WATCHER (timer, timer) |
151 |
|
|
void set (ev_tstamp after, ev_tstamp repeat = 0.) |
152 |
|
|
{ |
153 |
|
|
int active = is_active (); |
154 |
|
|
if (active) stop (); |
155 |
|
|
ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
156 |
|
|
if (active) start (); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
void start (ev_tstamp after, ev_tstamp repeat = 0.) |
160 |
|
|
{ |
161 |
|
|
set (after, repeat); |
162 |
|
|
start (); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
void again () |
166 |
|
|
{ |
167 |
|
|
ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
168 |
|
|
} |
169 |
|
|
}; |
170 |
|
|
|
171 |
|
|
EV_DECLARE_WATCHER (periodic, periodic) |
172 |
|
|
void set (ev_tstamp at, ev_tstamp interval = 0.) |
173 |
|
|
{ |
174 |
|
|
int active = is_active (); |
175 |
|
|
if (active) stop (); |
176 |
|
|
ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
177 |
|
|
if (active) start (); |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
void start (ev_tstamp at, ev_tstamp interval = 0.) |
181 |
|
|
{ |
182 |
|
|
set (at, interval); |
183 |
|
|
start (); |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
void again () |
187 |
|
|
{ |
188 |
|
|
ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
189 |
|
|
} |
190 |
|
|
}; |
191 |
|
|
|
192 |
|
|
EV_DECLARE_WATCHER (idle, idle) |
193 |
|
|
}; |
194 |
|
|
|
195 |
|
|
EV_DECLARE_WATCHER (prepare, prepare) |
196 |
|
|
}; |
197 |
|
|
|
198 |
|
|
EV_DECLARE_WATCHER (check, check) |
199 |
|
|
}; |
200 |
|
|
|
201 |
|
|
EV_DECLARE_WATCHER (sig, signal) |
202 |
|
|
void set (int signum) |
203 |
|
|
{ |
204 |
|
|
int active = is_active (); |
205 |
|
|
if (active) stop (); |
206 |
|
|
ev_signal_set (static_cast<ev_signal *>(this), signum); |
207 |
|
|
if (active) start (); |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
void start (int signum) |
211 |
|
|
{ |
212 |
|
|
set (signum); |
213 |
|
|
start (); |
214 |
|
|
} |
215 |
|
|
}; |
216 |
|
|
|
217 |
|
|
EV_DECLARE_WATCHER (child, child) |
218 |
|
|
void set (int pid) |
219 |
|
|
{ |
220 |
|
|
int active = is_active (); |
221 |
|
|
if (active) stop (); |
222 |
|
|
ev_child_set (static_cast<ev_child *>(this), pid); |
223 |
|
|
if (active) start (); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
void start (int pid) |
227 |
|
|
{ |
228 |
|
|
set (pid); |
229 |
|
|
start (); |
230 |
|
|
} |
231 |
|
|
}; |
232 |
|
|
|
233 |
|
|
#undef EV_CONSTRUCT |
234 |
|
|
#undef EV_DECLARE_WATCHER |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
#endif |
238 |
|
|
|