ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev++.h
Revision: 1.13
Committed: Tue Dec 4 16:23:29 2007 UTC (16 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +82 -60 lines
Log Message:
complete c++ overhaul

File Contents

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