|
|
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 | |
6 | namespace ev { |
49 | namespace ev { |
7 | |
50 | |
8 | template<class ev_watcher, class watcher> |
51 | template<class ev_watcher, class watcher> |
9 | struct base : ev_watcher |
52 | struct base : ev_watcher |
… | |
… | |
20 | base () |
63 | base () |
21 | { |
64 | { |
22 | ev_init (this, 0); |
65 | ev_init (this, 0); |
23 | } |
66 | } |
24 | |
67 | |
25 | void set_ (void *object, void (*cb)(ev_watcher *w, int revents)) |
68 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
26 | { |
69 | { |
27 | this->data = object; |
70 | this->data = data; |
28 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
71 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
29 | } |
72 | } |
30 | |
73 | |
|
|
74 | // method callback |
31 | template<class K, void (K::*method)(watcher &w, int)> |
75 | template<class K, void (K::*method)(watcher &w, int)> |
32 | void set (K *object) |
76 | void set (K *object) |
33 | { |
77 | { |
34 | set_ (object, method_thunk<K, method>); |
78 | set_ (object, method_thunk<K, method>); |
35 | } |
79 | } |
36 | |
80 | |
37 | template<class K, void (K::*method)(watcher &w, int)> |
81 | template<class K, void (K::*method)(watcher &w, int)> |
38 | static void method_thunk (ev_watcher *w, int revents) |
82 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
39 | { |
83 | { |
40 | watcher *self = static_cast<watcher *>(w); |
|
|
41 | K *obj = static_cast<K *>(self->data); |
84 | K *obj = static_cast<K *>(w->data); |
42 | (obj->*method) (*self, revents); |
85 | (obj->*method) (*static_cast<watcher *>(w), revents); |
43 | } |
86 | } |
44 | |
87 | |
|
|
88 | // const method callback |
45 | template<class K, void (K::*method)(watcher &w, int) const> |
89 | template<class K, void (K::*method)(watcher &w, int) const> |
46 | void set (const K *object) |
90 | void set (const K *object) |
47 | { |
91 | { |
48 | set_ (object, const_method_thunk<K, method>); |
92 | set_ (object, const_method_thunk<K, method>); |
49 | } |
93 | } |
50 | |
94 | |
51 | template<class K, void (K::*method)(watcher &w, int) const> |
95 | template<class K, void (K::*method)(watcher &w, int) const> |
52 | static void const_method_thunk (ev_watcher *w, int revents) |
96 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
53 | { |
97 | { |
54 | watcher *self = static_cast<watcher *>(w); |
|
|
55 | K *obj = static_cast<K *>(self->data); |
98 | K *obj = static_cast<K *>(w->data); |
56 | (obj->*method) (*self, revents); |
99 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
57 | } |
100 | } |
58 | |
101 | |
|
|
102 | // function callback |
59 | template<void (*function)(watcher &w, int)> |
103 | template<void (*function)(watcher &w, int)> |
60 | void set () |
104 | void set (void *data = 0) |
61 | { |
105 | { |
62 | set_ (0, function_thunk<function>); |
106 | set_ (data, function_thunk<function>); |
63 | } |
107 | } |
64 | |
108 | |
65 | template<void (*function)(watcher &w, int)> |
109 | template<void (*function)(watcher &w, int)> |
66 | static void function_thunk (ev_watcher *w, int revents) |
110 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
111 | { |
|
|
112 | function (*static_cast<watcher *>(w), revents); |
67 | { |
113 | } |
68 | watcher *self = static_cast<watcher *>(w); |
114 | |
69 | function (*self, revents); |
115 | // 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) (); |
70 | } |
127 | } |
71 | |
128 | |
72 | void operator ()(int events = EV_UNDEF) |
129 | void operator ()(int events = EV_UNDEF) |
73 | { |
130 | { |
74 | return e_cb (static_cast<ev_watcher *>(this)) (events); |
131 | return ev_cb (static_cast<ev_watcher *>(this)) |
|
|
132 | (static_cast<ev_watcher *>(this), events); |
75 | } |
133 | } |
76 | |
134 | |
77 | bool is_active () const |
135 | bool is_active () const |
78 | { |
136 | { |
79 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
137 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
… | |
… | |
148 | \ |
206 | \ |
149 | using base<ev_ ## cstem, cppstem>::set; \ |
207 | using base<ev_ ## cstem, cppstem>::set; \ |
150 | \ |
208 | \ |
151 | private: \ |
209 | private: \ |
152 | \ |
210 | \ |
153 | cppstem (const cppstem &o) \ |
211 | cppstem (const cppstem &o); \ |
154 | { /* disabled */ } \ |
|
|
155 | \ |
212 | \ |
156 | void operator =(const cppstem &o) { /* disabled */ } \ |
213 | cppstem & operator =(const cppstem &o); \ |
157 | \ |
214 | \ |
158 | public: |
215 | public: |
159 | |
216 | |
160 | #define EV_END_WATCHER(cppstem,cstem) \ |
217 | #define EV_END_WATCHER(cppstem,cstem) \ |
161 | }; |
218 | }; |