ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev++.h
(Generate patch)

Comparing libev/ev++.h (file contents):
Revision 1.19 by root, Fri Dec 14 17:47:52 2007 UTC vs.
Revision 1.24 by llucax, Fri Jan 18 18:13:21 2008 UTC

1/*
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
1#ifndef EVPP_H__ 40#ifndef EVPP_H__
2#define EVPP_H__ 41#define EVPP_H__
3 42
43#ifdef EV_H
44# include EV_H
45#else
4#include "ev.h" 46# include "ev.h"
47#endif
5 48
6namespace ev { 49namespace ev {
7 50
8 template<class ev_watcher, class watcher> 51 typedef ev_tstamp tstamp;
9 struct base : ev_watcher
10 {
11 #if EV_MULTIPLICITY
12 EV_P;
13
14 void set (EV_P)
15 {
16 this->EV_A = EV_A;
17 }
18 #endif
19
20 base ()
21 {
22 ev_init (this, 0);
23 }
24
25 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
26 {
27 this->data = data;
28 ev_set_cb (static_cast<ev_watcher *>(this), cb);
29 }
30
31 // method callback
32 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 static void method_thunk (EV_P_ ev_watcher *w, int revents)
40 {
41 K *obj = static_cast<K *>(w->data);
42 (obj->*method) (*static_cast<watcher *>(w), revents);
43 }
44
45 // const method callback
46 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 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
54 {
55 K *obj = static_cast<K *>(w->data);
56 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
57 }
58
59 // function callback
60 template<void (*function)(watcher &w, int)>
61 void set (void *data = 0)
62 {
63 set_ (data, function_thunk<function>);
64 }
65
66 template<void (*function)(watcher &w, int)>
67 static void function_thunk (EV_P_ ev_watcher *w, int revents)
68 {
69 function (*static_cast<watcher *>(w), revents);
70 }
71
72 // 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 void operator ()(int events = EV_UNDEF)
87 {
88 return ev_cb (static_cast<ev_watcher *>(this))
89 (static_cast<ev_watcher *>(this), events);
90 }
91
92 bool is_active () const
93 {
94 return ev_is_active (static_cast<const ev_watcher *>(this));
95 }
96
97 bool is_pending () const
98 {
99 return ev_is_pending (static_cast<const ev_watcher *>(this));
100 }
101 };
102 52
103 enum { 53 enum {
104 UNDEF = EV_UNDEF, 54 UNDEF = EV_UNDEF,
105 NONE = EV_NONE, 55 NONE = EV_NONE,
106 READ = EV_READ, 56 READ = EV_READ,
116 FORK = EV_FORK, 66 FORK = EV_FORK,
117 EMBED = EV_EMBED, 67 EMBED = EV_EMBED,
118 ERROR = EV_ERROR, 68 ERROR = EV_ERROR,
119 }; 69 };
120 70
121 typedef ev_tstamp tstamp; 71 template<class ev_watcher, class watcher>
72 struct base : ev_watcher
73 {
74 #if EV_MULTIPLICITY
75 EV_P;
76
77 void set (EV_P)
78 {
79 this->EV_A = EV_A;
80 }
81 #endif
82
83 base ()
84 {
85 ev_init (this, 0);
86 }
87
88 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
89 {
90 this->data = data;
91 ev_set_cb (static_cast<ev_watcher *>(this), cb);
92 }
93
94 // method callback
95 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 static void method_thunk (EV_P_ ev_watcher *w, int revents)
103 {
104 K *obj = static_cast<K *>(w->data);
105 (obj->*method) (*static_cast<watcher *>(w), revents);
106 }
107
108 // const method callback
109 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 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
117 {
118 K *obj = static_cast<K *>(w->data);
119 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
120 }
121
122 // function callback
123 template<void (*function)(watcher &w, int)>
124 void set (void *data = 0)
125 {
126 set_ (data, function_thunk<function>);
127 }
128
129 template<void (*function)(watcher &w, int)>
130 static void function_thunk (EV_P_ ev_watcher *w, int revents)
131 {
132 function (*static_cast<watcher *>(w), revents);
133 }
134
135 // 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 void operator ()(int events = EV_UNDEF)
150 {
151 return ev_cb (static_cast<ev_watcher *>(this))
152 (static_cast<ev_watcher *>(this), events);
153 }
154
155 bool is_active () const
156 {
157 return ev_is_active (static_cast<const ev_watcher *>(this));
158 }
159
160 bool is_pending () const
161 {
162 return ev_is_pending (static_cast<const ev_watcher *>(this));
163 }
164 };
165
122 166
123 inline ev_tstamp now (EV_P) 167 inline ev_tstamp now (EV_P)
124 { 168 {
125 return ev_now (EV_A); 169 return ev_now (EV_A);
126 } 170 }
163 \ 207 \
164 using base<ev_ ## cstem, cppstem>::set; \ 208 using base<ev_ ## cstem, cppstem>::set; \
165 \ 209 \
166 private: \ 210 private: \
167 \ 211 \
168 cppstem (const cppstem &o) \ 212 cppstem (const cppstem &o); \
169 { /* disabled */ } \
170 \ 213 \
171 void operator =(const cppstem &o) { /* disabled */ } \ 214 cppstem & operator =(const cppstem &o); \
172 \ 215 \
173 public: 216 public:
174 217
175 #define EV_END_WATCHER(cppstem,cstem) \ 218 #define EV_END_WATCHER(cppstem,cstem) \
176 }; 219 };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines