ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev++.h
Revision: 1.17
Committed: Sat Dec 8 14:27:38 2007 UTC (16 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_72
Changes since 1.16: +13 -16 lines
Log Message:
fix c++ interface

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.17 void set_ (void *data, void (*cb)(EV_P_ watcher *w, int revents))
26 root 1.13 {
27 root 1.17 this->data = data;
28     ev_set_cb (static_cast<watcher *>(this), cb);
29 root 1.13 }
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 root 1.17 static void method_thunk (EV_P_ watcher *w, int revents)
39 root 1.13 {
40 root 1.17 K *obj = static_cast<K *>(w->data);
41     (obj->*method) (*w, revents);
42 root 1.13 }
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 root 1.17 static void const_method_thunk (EV_P_ watcher *w, int revents)
52 root 1.13 {
53 root 1.17 K *obj = static_cast<K *>(w->data);
54     (obj->*method) (*w, revents);
55 root 1.13 }
56    
57 root 1.17 template<void (*function)(watcher &w, int)>
58     void set (void *data = 0)
59 root 1.13 {
60 root 1.16 set_ (data, function_thunk<function>);
61 root 1.13 }
62    
63     template<void (*function)(watcher &w, int)>
64 root 1.17 static void function_thunk (EV_P_ watcher *w, int revents)
65 root 1.13 {
66 root 1.17 function (*w, revents);
67 root 1.13 }
68    
69     void operator ()(int events = EV_UNDEF)
70     {
71 root 1.14 return ev_cb (static_cast<ev_watcher *>(this))
72     (static_cast<ev_watcher *>(this), events);
73 root 1.13 }
74    
75     bool is_active () const
76 root 1.1 {
77 root 1.13 return ev_is_active (static_cast<const ev_watcher *>(this));
78 root 1.1 }
79    
80 root 1.13 bool is_pending () const
81 root 1.1 {
82 root 1.13 return ev_is_pending (static_cast<const ev_watcher *>(this));
83 root 1.1 }
84     };
85    
86 root 1.2 enum {
87     UNDEF = EV_UNDEF,
88     NONE = EV_NONE,
89     READ = EV_READ,
90     WRITE = EV_WRITE,
91     TIMEOUT = EV_TIMEOUT,
92     PERIODIC = EV_PERIODIC,
93     SIGNAL = EV_SIGNAL,
94 root 1.9 CHILD = EV_CHILD,
95     STAT = EV_STAT,
96 root 1.2 IDLE = EV_IDLE,
97     CHECK = EV_CHECK,
98     PREPARE = EV_PREPARE,
99 root 1.9 FORK = EV_FORK,
100     EMBED = EV_EMBED,
101 root 1.2 ERROR = EV_ERROR,
102     };
103    
104 root 1.1 typedef ev_tstamp tstamp;
105    
106     inline ev_tstamp now (EV_P)
107     {
108     return ev_now (EV_A);
109     }
110    
111     #if EV_MULTIPLICITY
112 root 1.13 #define EV_CONSTRUCT \
113     (EV_P = EV_DEFAULT) \
114 root 1.1 { \
115 root 1.13 set (EV_A); \
116     }
117 root 1.1 #else
118 root 1.13 #define EV_CONSTRUCT \
119     () \
120     { \
121     }
122 root 1.1 #endif
123    
124     /* using a template here would require quite a bit more lines,
125     * so a macro solution was chosen */
126 root 1.4 #define EV_BEGIN_WATCHER(cppstem,cstem) \
127 root 1.1 \
128 root 1.13 struct cppstem : base<ev_ ## cstem, cppstem> \
129 root 1.1 { \
130     void start () \
131     { \
132     ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
133     } \
134     \
135     void stop () \
136     { \
137     ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
138     } \
139     \
140 root 1.13 cppstem EV_CONSTRUCT \
141 root 1.1 \
142 root 1.3 ~cppstem () \
143     { \
144     stop (); \
145     } \
146     \
147 root 1.13 using base<ev_ ## cstem, cppstem>::set; \
148     \
149 root 1.1 private: \
150     \
151     cppstem (const cppstem &o) \
152     { /* disabled */ } \
153 root 1.6 \
154 root 1.1 void operator =(const cppstem &o) { /* disabled */ } \
155     \
156     public:
157    
158 root 1.4 #define EV_END_WATCHER(cppstem,cstem) \
159 root 1.6 };
160 root 1.4
161     EV_BEGIN_WATCHER (io, io)
162 root 1.1 void set (int fd, int events)
163     {
164     int active = is_active ();
165     if (active) stop ();
166     ev_io_set (static_cast<ev_io *>(this), fd, events);
167     if (active) start ();
168     }
169    
170     void set (int events)
171     {
172     int active = is_active ();
173     if (active) stop ();
174     ev_io_set (static_cast<ev_io *>(this), fd, events);
175     if (active) start ();
176     }
177    
178     void start (int fd, int events)
179     {
180     set (fd, events);
181     start ();
182     }
183 root 1.4 EV_END_WATCHER (io, io)
184 root 1.1
185 root 1.4 EV_BEGIN_WATCHER (timer, timer)
186 root 1.1 void set (ev_tstamp after, ev_tstamp repeat = 0.)
187     {
188     int active = is_active ();
189     if (active) stop ();
190     ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
191     if (active) start ();
192     }
193    
194     void start (ev_tstamp after, ev_tstamp repeat = 0.)
195     {
196     set (after, repeat);
197     start ();
198     }
199    
200     void again ()
201     {
202     ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
203     }
204 root 1.4 EV_END_WATCHER (timer, timer)
205 root 1.1
206 root 1.8 #if EV_PERIODIC_ENABLE
207 root 1.4 EV_BEGIN_WATCHER (periodic, periodic)
208 root 1.1 void set (ev_tstamp at, ev_tstamp interval = 0.)
209     {
210     int active = is_active ();
211     if (active) stop ();
212     ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
213     if (active) start ();
214     }
215    
216     void start (ev_tstamp at, ev_tstamp interval = 0.)
217     {
218     set (at, interval);
219     start ();
220     }
221    
222     void again ()
223     {
224     ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
225     }
226 root 1.4 EV_END_WATCHER (periodic, periodic)
227 root 1.3 #endif
228 root 1.1
229 root 1.4 EV_BEGIN_WATCHER (sig, signal)
230 root 1.1 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 root 1.4 EV_END_WATCHER (sig, signal)
244 root 1.1
245 root 1.4 EV_BEGIN_WATCHER (child, child)
246 root 1.1 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 root 1.4 EV_END_WATCHER (child, child)
260 root 1.1
261 root 1.8 #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 root 1.12 stop ();
274 root 1.8 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)
283     #endif
284    
285     EV_BEGIN_WATCHER (idle, idle)
286     void set () { }
287     EV_END_WATCHER (idle, idle)
288 root 1.7
289 root 1.8 EV_BEGIN_WATCHER (prepare, prepare)
290     void set () { }
291     EV_END_WATCHER (prepare, prepare)
292    
293     EV_BEGIN_WATCHER (check, check)
294     void set () { }
295     EV_END_WATCHER (check, check)
296    
297     #if EV_EMBED_ENABLE
298 root 1.7 EV_BEGIN_WATCHER (embed, embed)
299     void start (struct ev_loop *embedded_loop)
300     {
301 root 1.12 stop ();
302     ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
303 root 1.7 start ();
304     }
305    
306     void sweep ()
307     {
308     ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
309     }
310     EV_END_WATCHER (embed, embed)
311     #endif
312    
313 root 1.10 #if EV_FORK_ENABLE
314     EV_BEGIN_WATCHER (fork, fork)
315     void set () { }
316     EV_END_WATCHER (fork, fork)
317     #endif
318    
319 root 1.1 #undef EV_CONSTRUCT
320 root 1.4 #undef EV_BEGIN_WATCHER
321 root 1.7 #undef EV_END_WATCHER
322 root 1.1 }
323    
324     #endif
325