ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev++.h
Revision: 1.5
Committed: Wed Nov 14 04:45:07 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +2 -2 lines
Log Message:
fix ev++

File Contents

# User Rev Content
1 root 1.1 #ifndef EVPP_H__
2     #define EVPP_H__
3    
4     /* work in progress, don't use unless you know what you are doing */
5    
6     namespace ev {
7    
8     template<class watcher>
9     class callback
10     {
11     struct object { };
12    
13     void *obj;
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     {
24     ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth)))
25     (w, e);
26     }
27     };
28    
29     proxy_base *prxy;
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     }
40    
41     void call (watcher *w, int e) const
42     {
43     return prxy->call (obj, meth, *w, e);
44     }
45     };
46    
47     #include "ev.h"
48    
49 root 1.2 enum {
50     UNDEF = EV_UNDEF,
51     NONE = EV_NONE,
52     READ = EV_READ,
53     WRITE = EV_WRITE,
54     TIMEOUT = EV_TIMEOUT,
55     PERIODIC = EV_PERIODIC,
56     SIGNAL = EV_SIGNAL,
57     IDLE = EV_IDLE,
58     CHECK = EV_CHECK,
59     PREPARE = EV_PREPARE,
60     CHILD = EV_CHILD,
61     ERROR = EV_ERROR,
62     };
63    
64 root 1.1 typedef ev_tstamp tstamp;
65    
66     inline ev_tstamp now (EV_P)
67     {
68     return ev_now (EV_A);
69     }
70    
71     #if EV_MULTIPLICITY
72    
73     #define EV_CONSTRUCT(cppstem) \
74     EV_P; \
75     \
76     void set (EV_P) \
77     { \
78     this->EV_A = EV_A; \
79     } \
80     \
81     template<class O1, class O2> \
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
86    
87     #define EV_CONSTRUCT(cppstem) \
88     template<class O1, class O2> \
89     explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \
90     : callback<cppstem> (object, method)
91    
92     #endif
93    
94     /* using a template here would require quite a bit more lines,
95     * so a macro solution was chosen */
96 root 1.4 #define EV_BEGIN_WATCHER(cppstem,cstem) \
97 root 1.1 \
98 root 1.5 static void cb_ ## cppstem (EV_P_ struct ev_ ## cstem *w, int revents); \
99 root 1.1 \
100     struct cppstem : ev_ ## cstem, callback<cppstem> \
101     { \
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 () \
118     { \
119     ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
120     } \
121     \
122     void stop () \
123     { \
124     ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
125     } \
126     \
127     void operator ()(int events = EV_UNDEF) \
128     { \
129     return call (this, events); \
130     } \
131     \
132 root 1.3 ~cppstem () \
133     { \
134     stop (); \
135     } \
136     \
137 root 1.1 private: \
138     \
139     cppstem (const cppstem &o) \
140     : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
141     { /* disabled */ } \
142     void operator =(const cppstem &o) { /* disabled */ } \
143     \
144     public:
145    
146 root 1.4 #define EV_END_WATCHER(cppstem,cstem) \
147     }; \
148     \
149 root 1.5 static void cb_ ## cppstem (EV_P_ struct ev_ ## cstem *w, int revents) \
150 root 1.4 { \
151     (*static_cast<cppstem *>(w))(revents); \
152     }
153    
154     EV_BEGIN_WATCHER (io, io)
155 root 1.1 void set (int fd, int events)
156     {
157     int active = is_active ();
158     if (active) stop ();
159     ev_io_set (static_cast<ev_io *>(this), fd, events);
160     if (active) start ();
161     }
162    
163     void set (int events)
164     {
165     int active = is_active ();
166     if (active) stop ();
167     ev_io_set (static_cast<ev_io *>(this), fd, events);
168     if (active) start ();
169     }
170    
171     void start (int fd, int events)
172     {
173     set (fd, events);
174     start ();
175     }
176 root 1.4 EV_END_WATCHER (io, io)
177 root 1.1
178 root 1.4 EV_BEGIN_WATCHER (timer, timer)
179 root 1.1 void set (ev_tstamp after, ev_tstamp repeat = 0.)
180     {
181     int active = is_active ();
182     if (active) stop ();
183     ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
184     if (active) start ();
185     }
186    
187     void start (ev_tstamp after, ev_tstamp repeat = 0.)
188     {
189     set (after, repeat);
190     start ();
191     }
192    
193     void again ()
194     {
195     ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
196     }
197 root 1.4 EV_END_WATCHER (timer, timer)
198 root 1.1
199 root 1.3 #if EV_PERIODICS
200 root 1.4 EV_BEGIN_WATCHER (periodic, periodic)
201 root 1.1 void set (ev_tstamp at, ev_tstamp interval = 0.)
202     {
203     int active = is_active ();
204     if (active) stop ();
205     ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
206     if (active) start ();
207     }
208    
209     void start (ev_tstamp at, ev_tstamp interval = 0.)
210     {
211     set (at, interval);
212     start ();
213     }
214    
215     void again ()
216     {
217     ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
218     }
219 root 1.4 EV_END_WATCHER (periodic, periodic)
220 root 1.3 #endif
221 root 1.1
222 root 1.4 EV_BEGIN_WATCHER (idle, idle)
223     EV_END_WATCHER (idle, idle)
224 root 1.1
225 root 1.4 EV_BEGIN_WATCHER (prepare, prepare)
226     EV_END_WATCHER (prepare, prepare)
227 root 1.1
228 root 1.4 EV_BEGIN_WATCHER (check, check)
229     EV_END_WATCHER (check, check)
230 root 1.1
231 root 1.4 EV_BEGIN_WATCHER (sig, signal)
232 root 1.1 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 root 1.4 EV_END_WATCHER (sig, signal)
246 root 1.1
247 root 1.4 EV_BEGIN_WATCHER (child, child)
248 root 1.1 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 root 1.4 EV_END_WATCHER (child, child)
262 root 1.1
263     #undef EV_CONSTRUCT
264 root 1.4 #undef EV_BEGIN_WATCHER
265 root 1.1 }
266    
267     #endif
268