ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev++.h
Revision: 1.24
Committed: Fri Jan 18 18:13:21 2008 UTC (16 years, 3 months ago) by llucax
Content type: text/plain
Branch: MAIN
Changes since 1.23: +20 -19 lines
Log Message:
Move typedef and constant to the top.

File Contents

# User Rev Content
1 root 1.21 /*
2     * libev simple C++ wrapper classes
3     *
4     * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
5     * All rights reserved.
6     *
7     * Redistribution and use in source and binary forms, with or without modifica-
8     * tion, are permitted provided that the following conditions are met:
9     *
10     * 1. Redistributions of source code must retain the above copyright notice,
11     * this list of conditions and the following disclaimer.
12     *
13     * 2. Redistributions in binary form must reproduce the above copyright
14     * notice, this list of conditions and the following disclaimer in the
15     * documentation and/or other materials provided with the distribution.
16     *
17     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19     * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20     * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21     * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25     * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26     * OF THE POSSIBILITY OF SUCH DAMAGE.
27     *
28     * Alternatively, the contents of this file may be used under the terms of
29     * the GNU General Public License ("GPL") version 2 or any later version,
30     * in which case the provisions of the GPL are applicable instead of
31     * the above. If you wish to allow the use of your version of this file
32     * only under the terms of the GPL and not to allow others to use your
33     * version of this file under the BSD license, indicate your decision
34     * by deleting the provisions above and replace them with the notice
35     * and other provisions required by the GPL. If you do not delete the
36     * provisions above, a recipient may use your version of this file under
37     * either the BSD or the GPL.
38     */
39    
40 root 1.1 #ifndef EVPP_H__
41     #define EVPP_H__
42    
43 root 1.20 #ifdef EV_H
44     # include EV_H
45     #else
46 root 1.22 # include "ev.h"
47 root 1.20 #endif
48 root 1.1
49     namespace ev {
50    
51 llucax 1.24 typedef ev_tstamp tstamp;
52    
53     enum {
54     UNDEF = EV_UNDEF,
55     NONE = EV_NONE,
56     READ = EV_READ,
57     WRITE = EV_WRITE,
58     TIMEOUT = EV_TIMEOUT,
59     PERIODIC = EV_PERIODIC,
60     SIGNAL = EV_SIGNAL,
61     CHILD = EV_CHILD,
62     STAT = EV_STAT,
63     IDLE = EV_IDLE,
64     CHECK = EV_CHECK,
65     PREPARE = EV_PREPARE,
66     FORK = EV_FORK,
67     EMBED = EV_EMBED,
68     ERROR = EV_ERROR,
69     };
70    
71 root 1.13 template<class ev_watcher, class watcher>
72     struct base : ev_watcher
73 root 1.1 {
74 root 1.13 #if EV_MULTIPLICITY
75     EV_P;
76 root 1.1
77 root 1.13 void set (EV_P)
78     {
79     this->EV_A = EV_A;
80     }
81     #endif
82 root 1.1
83 root 1.13 base ()
84     {
85     ev_init (this, 0);
86     }
87    
88 root 1.18 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
89 root 1.13 {
90 root 1.17 this->data = data;
91 root 1.18 ev_set_cb (static_cast<ev_watcher *>(this), cb);
92 root 1.13 }
93    
94 root 1.19 // method callback
95 root 1.13 template<class K, void (K::*method)(watcher &w, int)>
96     void set (K *object)
97     {
98     set_ (object, method_thunk<K, method>);
99     }
100    
101     template<class K, void (K::*method)(watcher &w, int)>
102 root 1.18 static void method_thunk (EV_P_ ev_watcher *w, int revents)
103 root 1.13 {
104 root 1.17 K *obj = static_cast<K *>(w->data);
105 root 1.18 (obj->*method) (*static_cast<watcher *>(w), revents);
106 root 1.13 }
107    
108 root 1.19 // const method callback
109 root 1.13 template<class K, void (K::*method)(watcher &w, int) const>
110     void set (const K *object)
111     {
112     set_ (object, const_method_thunk<K, method>);
113     }
114    
115     template<class K, void (K::*method)(watcher &w, int) const>
116 root 1.18 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
117 root 1.13 {
118 root 1.17 K *obj = static_cast<K *>(w->data);
119 root 1.18 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
120 root 1.13 }
121    
122 root 1.19 // function callback
123 root 1.17 template<void (*function)(watcher &w, int)>
124     void set (void *data = 0)
125 root 1.13 {
126 root 1.16 set_ (data, function_thunk<function>);
127 root 1.13 }
128    
129     template<void (*function)(watcher &w, int)>
130 root 1.18 static void function_thunk (EV_P_ ev_watcher *w, int revents)
131 root 1.13 {
132 root 1.18 function (*static_cast<watcher *>(w), revents);
133 root 1.13 }
134    
135 root 1.19 // simple callback
136     template<class K, void (K::*method)()>
137     void set (K *object)
138     {
139     set_ (object, method_noargs_thunk<K, method>);
140     }
141    
142     template<class K, void (K::*method)()>
143     static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
144     {
145     K *obj = static_cast<K *>(w->data);
146     (obj->*method) ();
147     }
148    
149 root 1.13 void operator ()(int events = EV_UNDEF)
150     {
151 root 1.14 return ev_cb (static_cast<ev_watcher *>(this))
152     (static_cast<ev_watcher *>(this), events);
153 root 1.13 }
154    
155     bool is_active () const
156 root 1.1 {
157 root 1.13 return ev_is_active (static_cast<const ev_watcher *>(this));
158 root 1.1 }
159    
160 root 1.13 bool is_pending () const
161 root 1.1 {
162 root 1.13 return ev_is_pending (static_cast<const ev_watcher *>(this));
163 root 1.1 }
164     };
165    
166    
167     inline ev_tstamp now (EV_P)
168     {
169     return ev_now (EV_A);
170     }
171    
172     #if EV_MULTIPLICITY
173 root 1.13 #define EV_CONSTRUCT \
174     (EV_P = EV_DEFAULT) \
175 root 1.1 { \
176 root 1.13 set (EV_A); \
177     }
178 root 1.1 #else
179 root 1.13 #define EV_CONSTRUCT \
180     () \
181     { \
182     }
183 root 1.1 #endif
184    
185     /* using a template here would require quite a bit more lines,
186     * so a macro solution was chosen */
187 root 1.4 #define EV_BEGIN_WATCHER(cppstem,cstem) \
188 root 1.1 \
189 root 1.13 struct cppstem : base<ev_ ## cstem, cppstem> \
190 root 1.1 { \
191     void start () \
192     { \
193     ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
194     } \
195     \
196     void stop () \
197     { \
198     ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
199     } \
200     \
201 root 1.13 cppstem EV_CONSTRUCT \
202 root 1.1 \
203 root 1.3 ~cppstem () \
204     { \
205     stop (); \
206     } \
207     \
208 root 1.13 using base<ev_ ## cstem, cppstem>::set; \
209     \
210 root 1.1 private: \
211     \
212 llucax 1.23 cppstem (const cppstem &o); \
213 root 1.6 \
214 llucax 1.23 cppstem & operator =(const cppstem &o); \
215 root 1.1 \
216     public:
217    
218 root 1.4 #define EV_END_WATCHER(cppstem,cstem) \
219 root 1.6 };
220 root 1.4
221     EV_BEGIN_WATCHER (io, io)
222 root 1.1 void set (int fd, int events)
223     {
224     int active = is_active ();
225     if (active) stop ();
226     ev_io_set (static_cast<ev_io *>(this), fd, events);
227     if (active) start ();
228     }
229    
230     void set (int events)
231     {
232     int active = is_active ();
233     if (active) stop ();
234     ev_io_set (static_cast<ev_io *>(this), fd, events);
235     if (active) start ();
236     }
237    
238     void start (int fd, int events)
239     {
240     set (fd, events);
241     start ();
242     }
243 root 1.4 EV_END_WATCHER (io, io)
244 root 1.1
245 root 1.4 EV_BEGIN_WATCHER (timer, timer)
246 root 1.1 void set (ev_tstamp after, ev_tstamp repeat = 0.)
247     {
248     int active = is_active ();
249     if (active) stop ();
250     ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
251     if (active) start ();
252     }
253    
254     void start (ev_tstamp after, ev_tstamp repeat = 0.)
255     {
256     set (after, repeat);
257     start ();
258     }
259    
260     void again ()
261     {
262     ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
263     }
264 root 1.4 EV_END_WATCHER (timer, timer)
265 root 1.1
266 root 1.8 #if EV_PERIODIC_ENABLE
267 root 1.4 EV_BEGIN_WATCHER (periodic, periodic)
268 root 1.1 void set (ev_tstamp at, ev_tstamp interval = 0.)
269     {
270     int active = is_active ();
271     if (active) stop ();
272     ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
273     if (active) start ();
274     }
275    
276     void start (ev_tstamp at, ev_tstamp interval = 0.)
277     {
278     set (at, interval);
279     start ();
280     }
281    
282     void again ()
283     {
284     ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
285     }
286 root 1.4 EV_END_WATCHER (periodic, periodic)
287 root 1.3 #endif
288 root 1.1
289 root 1.4 EV_BEGIN_WATCHER (sig, signal)
290 root 1.1 void set (int signum)
291     {
292     int active = is_active ();
293     if (active) stop ();
294     ev_signal_set (static_cast<ev_signal *>(this), signum);
295     if (active) start ();
296     }
297    
298     void start (int signum)
299     {
300     set (signum);
301     start ();
302     }
303 root 1.4 EV_END_WATCHER (sig, signal)
304 root 1.1
305 root 1.4 EV_BEGIN_WATCHER (child, child)
306 root 1.1 void set (int pid)
307     {
308     int active = is_active ();
309     if (active) stop ();
310     ev_child_set (static_cast<ev_child *>(this), pid);
311     if (active) start ();
312     }
313    
314     void start (int pid)
315     {
316     set (pid);
317     start ();
318     }
319 root 1.4 EV_END_WATCHER (child, child)
320 root 1.1
321 root 1.8 #if EV_STAT_ENABLE
322     EV_BEGIN_WATCHER (stat, stat)
323     void set (const char *path, ev_tstamp interval = 0.)
324     {
325     int active = is_active ();
326     if (active) stop ();
327     ev_stat_set (static_cast<ev_stat *>(this), path, interval);
328     if (active) start ();
329     }
330    
331     void start (const char *path, ev_tstamp interval = 0.)
332     {
333 root 1.12 stop ();
334 root 1.8 set (path, interval);
335     start ();
336     }
337    
338     void update ()
339     {
340     ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
341     }
342     EV_END_WATCHER (stat, stat)
343     #endif
344    
345     EV_BEGIN_WATCHER (idle, idle)
346     void set () { }
347     EV_END_WATCHER (idle, idle)
348 root 1.7
349 root 1.8 EV_BEGIN_WATCHER (prepare, prepare)
350     void set () { }
351     EV_END_WATCHER (prepare, prepare)
352    
353     EV_BEGIN_WATCHER (check, check)
354     void set () { }
355     EV_END_WATCHER (check, check)
356    
357     #if EV_EMBED_ENABLE
358 root 1.7 EV_BEGIN_WATCHER (embed, embed)
359     void start (struct ev_loop *embedded_loop)
360     {
361 root 1.12 stop ();
362     ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
363 root 1.7 start ();
364     }
365    
366     void sweep ()
367     {
368     ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
369     }
370     EV_END_WATCHER (embed, embed)
371     #endif
372    
373 root 1.10 #if EV_FORK_ENABLE
374     EV_BEGIN_WATCHER (fork, fork)
375     void set () { }
376     EV_END_WATCHER (fork, fork)
377     #endif
378    
379 root 1.1 #undef EV_CONSTRUCT
380 root 1.4 #undef EV_BEGIN_WATCHER
381 root 1.7 #undef EV_END_WATCHER
382 root 1.1 }
383    
384     #endif
385