ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev++.h
(Generate patch)

Comparing libev/ev++.h (file contents):
Revision 1.27 by llucax, Fri Jan 18 18:14:23 2008 UTC vs.
Revision 1.28 by llucax, Fri Jan 18 18:14:41 2008 UTC

91 { 91 {
92 ONE = EVUNLOOP_ONE, 92 ONE = EVUNLOOP_ONE,
93 ALL = EVUNLOOP_ALL 93 ALL = EVUNLOOP_ALL
94 }; 94 };
95 95
96#ifdef EV_AX
97# undef EV_AX
98#endif
99
100#ifdef EV_AX_
101# undef EV_AX_
102#endif
103
104#if EV_MULTIPLICITY
105# define EV_AX raw_loop
106# define EV_AX_ raw_loop,
107#else
108# define EV_AX
109# define EV_AX_
110#endif
111
112 struct loop_ref
113 {
114
115 loop_ref (EV_P)
116#if EV_MULTIPLICITY
117 : EV_AX (EV_A)
118#endif
119 {
120 }
121
122 bool operator== (const loop_ref &other) const
123 {
124#if EV_MULTIPLICITY
125 return this->EV_AX == other.EV_AX;
126#else
127 return true;
128#endif
129 }
130
131 bool operator!= (const loop_ref &other) const
132 {
133#if EV_MULTIPLICITY
134 return ! (*this == other);
135#else
136 return false;
137#endif
138 }
139
140#if EV_MULTIPLICITY
141 bool operator== (struct ev_loop *other) const
142 {
143 return this->EV_AX == other;
144 }
145
146 bool operator!= (struct ev_loop *other) const
147 {
148 return ! (*this == other);
149 }
150
151 bool operator== (const struct ev_loop *other) const
152 {
153 return this->EV_AX == other;
154 }
155
156 bool operator!= (const struct ev_loop *other) const
157 {
158 return (*this == other);
159 }
160
161 operator struct ev_loop * () const
162 {
163 return EV_AX;
164 }
165
166 operator const struct ev_loop * () const
167 {
168 return EV_AX;
169 }
170
171 bool is_default () const
172 {
173 return EV_AX == ev_default_loop (0);
174 }
175#endif
176
177 void loop (int flags = 0)
178 {
179 ev_loop (EV_AX_ flags);
180 }
181
182 void unloop (how_t how = ONE)
183 {
184 ev_unloop (EV_AX_ how);
185 }
186
187 void post_fork ()
188 {
189#if EV_MULTIPLICITY
190 ev_loop_fork (EV_AX);
191#else
192 ev_default_fork ();
193#endif
194 }
195
196 unsigned int count () const
197 {
198 return ev_loop_count (EV_AX);
199 }
200
201 unsigned int backend () const
202 {
203 return ev_backend (EV_AX);
204 }
205
206 tstamp now () const
207 {
208 return ev_now (EV_AX);
209 }
210
211 void ref ()
212 {
213 ev_ref (EV_AX);
214 }
215
216 void unref ()
217 {
218 ev_unref (EV_AX);
219 }
220
221 void set_io_collect_interval (tstamp interval)
222 {
223 ev_set_io_collect_interval (EV_AX_ interval);
224 }
225
226 void set_timeout_collect_interval (tstamp interval)
227 {
228 ev_set_timeout_collect_interval (EV_AX_ interval);
229 }
230
231 // function callback
232 void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void* arg = 0)
233 {
234 ev_once (EV_AX_ fd, events, timeout, cb, arg);
235 }
236
237 // method callback
238 template<class K, void (K::*method)(int)>
239 void once (int fd, int events, tstamp timeout, K *object)
240 {
241 once (fd, events, timeout, method_thunk<K, method>, object);
242 }
243
244 template<class K, void (K::*method)(int)>
245 static void method_thunk (int revents, void* arg)
246 {
247 K *obj = static_cast<K *>(arg);
248 (obj->*method) (revents);
249 }
250
251 // const method callback
252 template<class K, void (K::*method)(int) const>
253 void once (int fd, int events, tstamp timeout, const K *object)
254 {
255 once (fd, events, timeout, const_method_thunk<K, method>, object);
256 }
257
258 template<class K, void (K::*method)(int) const>
259 static void const_method_thunk (int revents, void* arg)
260 {
261 K *obj = static_cast<K *>(arg);
262 (obj->*method) (revents);
263 }
264
265 // simple method callback
266 template<class K, void (K::*method)()>
267 void once (int fd, int events, tstamp timeout, K *object)
268 {
269 once (fd, events, timeout, method_noargs_thunk<K, method>, object);
270 }
271
272 template<class K, void (K::*method)()>
273 static void method_noargs_thunk (int revents, void* arg)
274 {
275 K *obj = static_cast<K *>(arg);
276 (obj->*method) ();
277 }
278
279 // simpler function callback
280 template<void (*cb)(int)>
281 void once (int fd, int events, tstamp timeout)
282 {
283 once (fd, events, timeout, simpler_func_thunk<cb>);
284 }
285
286 template<void (*cb)(int)>
287 static void simpler_func_thunk (int revents, void* arg)
288 {
289 (*cb) (revents);
290 }
291
292 // simplest function callback
293 template<void (*cb)()>
294 void once (int fd, int events, tstamp timeout)
295 {
296 once (fd, events, timeout, simplest_func_thunk<cb>);
297 }
298
299 template<void (*cb)()>
300 static void simplest_func_thunk (int revents, void* arg)
301 {
302 (*cb) ();
303 }
304
305 void feed_fd_event (int fd, int revents)
306 {
307 ev_feed_fd_event (EV_AX_ fd, revents);
308 }
309
310 void feed_signal_event (int signum)
311 {
312 ev_feed_signal_event (EV_AX_ signum);
313 }
314
315#if EV_MULTIPLICITY
316 struct ev_loop* EV_AX;
317#endif
318
319 };
320
321#if EV_MULTIPLICITY
322 struct dynamic_loop: loop_ref
323 {
324
325 dynamic_loop (unsigned int flags = AUTO)
326 : loop_ref (ev_loop_new (flags))
327 {
328 }
329
330 ~dynamic_loop ()
331 {
332 ev_loop_destroy (EV_AX);
333 EV_AX = 0;
334 }
335
336 private:
337
338 dynamic_loop (const dynamic_loop &);
339
340 dynamic_loop & operator= (const dynamic_loop &);
341
342 };
343#endif
344
345 struct default_loop: loop_ref
346 {
347
348 default_loop (unsigned int flags = AUTO)
349#if EV_MULTIPLICITY
350 : loop_ref (ev_default_loop (flags))
351 {
352 }
353#else
354 {
355 ev_default_loop (flags);
356 }
357#endif
358
359 ~default_loop ()
360 {
361 ev_default_destroy ();
362#if EV_MULTIPLICITY
363 EV_AX = 0;
364#endif
365 }
366
367 private:
368
369 default_loop (const default_loop &);
370
371 default_loop & operator= (const default_loop &);
372
373 };
374
375 inline loop_ref get_default_loop ()
376 {
377#if EV_MULTIPLICITY
378 return ev_default_loop (0);
379#else
380 return loop_ref ();
381#endif
382 }
383
384#undef EV_AX
385#undef EV_AX_
386
387#undef EV_PX
388#undef EV_PX_
389#if EV_MULTIPLICITY
390# define EV_PX loop_ref EV_A
391# define EV_PX_ loop_ref EV_A_
392#else
393# define EV_PX
394# define EV_PX_
395#endif
396
96 template<class ev_watcher, class watcher> 397 template<class ev_watcher, class watcher>
97 struct base : ev_watcher 398 struct base : ev_watcher
98 { 399 {
99 #if EV_MULTIPLICITY 400 #if EV_MULTIPLICITY
100 EV_P; 401 EV_PX;
101 402
102 void set (EV_P) 403 void set (EV_PX)
103 { 404 {
104 this->EV_A = EV_A; 405 this->EV_A = EV_A;
105 } 406 }
106 #endif 407 #endif
107 408
108 base () 409 base (EV_PX)
410 #if EV_MULTIPLICITY
411 : EV_A (EV_A)
412 #endif
109 { 413 {
110 ev_init (this, 0); 414 ev_init (this, 0);
111 } 415 }
112 416
113 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) 417 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
231 inline void set_syserr_cb (void (*cb)(const char *msg)) 535 inline void set_syserr_cb (void (*cb)(const char *msg))
232 { 536 {
233 ev_set_syserr_cb (cb); 537 ev_set_syserr_cb (cb);
234 } 538 }
235 539
236
237 inline ev_tstamp now (EV_P)
238 {
239 return ev_now (EV_A);
240 }
241
242 #if EV_MULTIPLICITY 540 #if EV_MULTIPLICITY
243 #define EV_CONSTRUCT \ 541 #define EV_CONSTRUCT(cppstem,cstem) \
244 (EV_P = EV_DEFAULT) \ 542 (EV_PX = get_default_loop ()) \
543 : base<ev_ ## cstem, cppstem> (EV_A) \
245 { \ 544 { \
246 set (EV_A); \
247 } 545 }
248 #else 546 #else
249 #define EV_CONSTRUCT \ 547 #define EV_CONSTRUCT(cppstem,cstem) \
250 () \ 548 () \
251 { \ 549 { \
252 } 550 }
253 #endif 551 #endif
254 552
266 void stop () \ 564 void stop () \
267 { \ 565 { \
268 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 566 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
269 } \ 567 } \
270 \ 568 \
271 cppstem EV_CONSTRUCT \ 569 cppstem EV_CONSTRUCT(cppstem,cstem) \
272 \ 570 \
273 ~cppstem () \ 571 ~cppstem () \
274 { \ 572 { \
275 stop (); \ 573 stop (); \
276 } \ 574 } \
444 EV_BEGIN_WATCHER (fork, fork) 742 EV_BEGIN_WATCHER (fork, fork)
445 void set () { } 743 void set () { }
446 EV_END_WATCHER (fork, fork) 744 EV_END_WATCHER (fork, fork)
447 #endif 745 #endif
448 746
747 #undef EV_PX
748 #undef EV_PX_
449 #undef EV_CONSTRUCT 749 #undef EV_CONSTRUCT
450 #undef EV_BEGIN_WATCHER 750 #undef EV_BEGIN_WATCHER
451 #undef EV_END_WATCHER 751 #undef EV_END_WATCHER
752
452} 753}
453 754
454#endif 755#endif
455 756

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines