ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev++.h
Revision: 1.19
Committed: Fri Dec 14 17:47:52 2007 UTC (16 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_86, rel-1_85
Changes since 1.18: +17 -0 lines
Log Message:
*** empty log message ***

File Contents

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