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

Comparing libev/ev++.h (file contents):
Revision 1.7 by root, Sat Nov 24 09:48:37 2007 UTC vs.
Revision 1.18 by root, Tue Dec 11 03:18:33 2007 UTC

3 3
4#include "ev.h" 4#include "ev.h"
5 5
6namespace ev { 6namespace ev {
7 7
8 template<class watcher> 8 template<class ev_watcher, class watcher>
9 class callback 9 struct base : ev_watcher
10 { 10 {
11 struct object { }; 11 #if EV_MULTIPLICITY
12 EV_P;
12 13
13 void *obj; 14 void set (EV_P)
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 { 15 {
24 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) 16 this->EV_A = EV_A;
25 (w, e);
26 } 17 }
18 #endif
19
20 base ()
21 {
22 ev_init (this, 0);
27 }; 23 }
28 24
29 proxy_base *prxy; 25 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
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 } 26 {
40 27 this->data = data;
41 void call (watcher *w, int e) const 28 ev_set_cb (static_cast<ev_watcher *>(this), cb);
42 { 29 }
43 return prxy->call (obj, meth, *w, e); 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_P_ ev_watcher *w, int revents)
39 {
40 K *obj = static_cast<K *>(w->data);
41 (obj->*method) (*static_cast<watcher *>(w), revents);
42 }
43
44 template<class K, void (K::*method)(watcher &w, int) const>
45 void set (const K *object)
46 {
47 set_ (object, const_method_thunk<K, method>);
48 }
49
50 template<class K, void (K::*method)(watcher &w, int) const>
51 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
52 {
53 K *obj = static_cast<K *>(w->data);
54 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
55 }
56
57 template<void (*function)(watcher &w, int)>
58 void set (void *data = 0)
59 {
60 set_ (data, function_thunk<function>);
61 }
62
63 template<void (*function)(watcher &w, int)>
64 static void function_thunk (EV_P_ ev_watcher *w, int revents)
65 {
66 function (*static_cast<watcher *>(w), revents);
67 }
68
69 void operator ()(int events = EV_UNDEF)
70 {
71 return ev_cb (static_cast<ev_watcher *>(this))
72 (static_cast<ev_watcher *>(this), events);
73 }
74
75 bool is_active () const
76 {
77 return ev_is_active (static_cast<const ev_watcher *>(this));
78 }
79
80 bool is_pending () const
81 {
82 return ev_is_pending (static_cast<const ev_watcher *>(this));
44 } 83 }
45 }; 84 };
46 85
47 enum { 86 enum {
48 UNDEF = EV_UNDEF, 87 UNDEF = EV_UNDEF,
50 READ = EV_READ, 89 READ = EV_READ,
51 WRITE = EV_WRITE, 90 WRITE = EV_WRITE,
52 TIMEOUT = EV_TIMEOUT, 91 TIMEOUT = EV_TIMEOUT,
53 PERIODIC = EV_PERIODIC, 92 PERIODIC = EV_PERIODIC,
54 SIGNAL = EV_SIGNAL, 93 SIGNAL = EV_SIGNAL,
94 CHILD = EV_CHILD,
95 STAT = EV_STAT,
55 IDLE = EV_IDLE, 96 IDLE = EV_IDLE,
56 CHECK = EV_CHECK, 97 CHECK = EV_CHECK,
57 PREPARE = EV_PREPARE, 98 PREPARE = EV_PREPARE,
58 CHILD = EV_CHILD, 99 FORK = EV_FORK,
100 EMBED = EV_EMBED,
59 ERROR = EV_ERROR, 101 ERROR = EV_ERROR,
60 }; 102 };
61 103
62 typedef ev_tstamp tstamp; 104 typedef ev_tstamp tstamp;
63 105
65 { 107 {
66 return ev_now (EV_A); 108 return ev_now (EV_A);
67 } 109 }
68 110
69 #if EV_MULTIPLICITY 111 #if EV_MULTIPLICITY
70
71 #define EV_CONSTRUCT(cppstem) \
72 EV_P; \ 112 #define EV_CONSTRUCT \
73 \
74 void set (EV_P) \ 113 (EV_P = EV_DEFAULT) \
75 { \ 114 { \
76 this->EV_A = EV_A; \
77 } \
78 \ 115 set (EV_A); \
79 template<class O1, class O2> \ 116 }
80 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \
81 : callback<cppstem> (object, method), EV_A (EV_A)
82
83 #else 117 #else
84 118 #define EV_CONSTRUCT \
85 #define EV_CONSTRUCT(cppstem) \ 119 () \
86 template<class O1, class O2> \ 120 { \
87 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ 121 }
88 : callback<cppstem> (object, method)
89
90 #endif 122 #endif
91 123
92 /* using a template here would require quite a bit more lines, 124 /* using a template here would require quite a bit more lines,
93 * so a macro solution was chosen */ 125 * so a macro solution was chosen */
94 #define EV_BEGIN_WATCHER(cppstem,cstem) \ 126 #define EV_BEGIN_WATCHER(cppstem,cstem) \
95 \ 127 \
96 struct cppstem : ev_ ## cstem, callback<cppstem> \ 128 struct cppstem : base<ev_ ## cstem, cppstem> \
97 { \ 129 { \
98 EV_CONSTRUCT (cppstem) \
99 { \
100 ev_init (static_cast<ev_ ## cstem *>(this), thunk); \
101 } \
102 \
103 bool is_active () const \
104 { \
105 return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \
106 } \
107 \
108 bool is_pending () const \
109 { \
110 return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \
111 } \
112 \
113 void start () \ 130 void start () \
114 { \ 131 { \
115 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 132 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
116 } \ 133 } \
117 \ 134 \
118 void stop () \ 135 void stop () \
119 { \ 136 { \
120 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 137 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
121 } \ 138 } \
122 \ 139 \
123 void operator ()(int events = EV_UNDEF) \
124 { \ 140 cppstem EV_CONSTRUCT \
125 return call (this, events); \
126 } \
127 \ 141 \
128 ~cppstem () \ 142 ~cppstem () \
129 { \ 143 { \
130 stop (); \ 144 stop (); \
131 } \ 145 } \
132 \ 146 \
147 using base<ev_ ## cstem, cppstem>::set; \
148 \
133 private: \ 149 private: \
134 \ 150 \
135 cppstem (const cppstem &o) \ 151 cppstem (const cppstem &o) \
136 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
137 { /* disabled */ } \ 152 { /* disabled */ } \
138 \ 153 \
139 void operator =(const cppstem &o) { /* disabled */ } \ 154 void operator =(const cppstem &o) { /* disabled */ } \
140 \
141 static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \
142 { \
143 (*static_cast<cppstem *>(w))(revents); \
144 } \
145 \ 155 \
146 public: 156 public:
147 157
148 #define EV_END_WATCHER(cppstem,cstem) \ 158 #define EV_END_WATCHER(cppstem,cstem) \
149 }; 159 };
191 { 201 {
192 ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 202 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
193 } 203 }
194 EV_END_WATCHER (timer, timer) 204 EV_END_WATCHER (timer, timer)
195 205
196 #if EV_PERIODICS 206 #if EV_PERIODIC_ENABLE
197 EV_BEGIN_WATCHER (periodic, periodic) 207 EV_BEGIN_WATCHER (periodic, periodic)
198 void set (ev_tstamp at, ev_tstamp interval = 0.) 208 void set (ev_tstamp at, ev_tstamp interval = 0.)
199 { 209 {
200 int active = is_active (); 210 int active = is_active ();
201 if (active) stop (); 211 if (active) stop ();
212 void again () 222 void again ()
213 { 223 {
214 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); 224 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
215 } 225 }
216 EV_END_WATCHER (periodic, periodic) 226 EV_END_WATCHER (periodic, periodic)
227 #endif
228
229 EV_BEGIN_WATCHER (sig, signal)
230 void set (int signum)
231 {
232 int active = is_active ();
233 if (active) stop ();
234 ev_signal_set (static_cast<ev_signal *>(this), signum);
235 if (active) start ();
236 }
237
238 void start (int signum)
239 {
240 set (signum);
241 start ();
242 }
243 EV_END_WATCHER (sig, signal)
244
245 EV_BEGIN_WATCHER (child, child)
246 void set (int pid)
247 {
248 int active = is_active ();
249 if (active) stop ();
250 ev_child_set (static_cast<ev_child *>(this), pid);
251 if (active) start ();
252 }
253
254 void start (int pid)
255 {
256 set (pid);
257 start ();
258 }
259 EV_END_WATCHER (child, child)
260
261 #if EV_STAT_ENABLE
262 EV_BEGIN_WATCHER (stat, stat)
263 void set (const char *path, ev_tstamp interval = 0.)
264 {
265 int active = is_active ();
266 if (active) stop ();
267 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
268 if (active) start ();
269 }
270
271 void start (const char *path, ev_tstamp interval = 0.)
272 {
273 stop ();
274 set (path, interval);
275 start ();
276 }
277
278 void update ()
279 {
280 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
281 }
282 EV_END_WATCHER (stat, stat)
217 #endif 283 #endif
218 284
219 EV_BEGIN_WATCHER (idle, idle) 285 EV_BEGIN_WATCHER (idle, idle)
220 void set () { } 286 void set () { }
221 EV_END_WATCHER (idle, idle) 287 EV_END_WATCHER (idle, idle)
226 292
227 EV_BEGIN_WATCHER (check, check) 293 EV_BEGIN_WATCHER (check, check)
228 void set () { } 294 void set () { }
229 EV_END_WATCHER (check, check) 295 EV_END_WATCHER (check, check)
230 296
231 EV_BEGIN_WATCHER (sig, signal) 297 #if EV_EMBED_ENABLE
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_MULTIPLICITY
264
265 EV_BEGIN_WATCHER (embed, embed) 298 EV_BEGIN_WATCHER (embed, embed)
266 void set (struct ev_loop *loop)
267 {
268 int active = is_active ();
269 if (active) stop ();
270 ev_embed_set (static_cast<ev_embed *>(this), loop);
271 if (active) start ();
272 }
273
274 void start (struct ev_loop *embedded_loop) 299 void start (struct ev_loop *embedded_loop)
275 { 300 {
276 set (embedded_loop); 301 stop ();
302 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
277 start (); 303 start ();
278 } 304 }
279 305
280 void sweep () 306 void sweep ()
281 { 307 {
282 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); 308 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
283 } 309 }
284 EV_END_WATCHER (embed, embed) 310 EV_END_WATCHER (embed, embed)
311 #endif
285 312
313 #if EV_FORK_ENABLE
314 EV_BEGIN_WATCHER (fork, fork)
315 void set () { }
316 EV_END_WATCHER (fork, fork)
286 #endif 317 #endif
287 318
288 #undef EV_CONSTRUCT 319 #undef EV_CONSTRUCT
289 #undef EV_BEGIN_WATCHER 320 #undef EV_BEGIN_WATCHER
290 #undef EV_END_WATCHER 321 #undef EV_END_WATCHER

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines