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

Comparing libev/ev++.h (file contents):
Revision 1.3 by root, Sun Nov 11 01:07:35 2007 UTC vs.
Revision 1.14 by root, Tue Dec 4 17:08:05 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines