ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev++.h
Revision: 1.23
Committed: Fri Jan 18 18:12:42 2008 UTC (16 years, 3 months ago) by llucax
Content type: text/plain
Branch: MAIN
Changes since 1.22: +2 -3 lines
Log Message:
Remove private implementation of watcher's copy ctor and operator=.

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