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

Comparing libev/ev++.h (file contents):
Revision 1.17 by root, Sat Dec 8 14:27:38 2007 UTC vs.
Revision 1.21 by root, Tue Dec 25 07:05:45 2007 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 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 *data, void (*cb)(EV_P_ watcher *w, int revents)) 68 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
26 { 69 {
27 this->data = data; 70 this->data = data;
28 ev_set_cb (static_cast<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_P_ watcher *w, int revents) 82 static void method_thunk (EV_P_ ev_watcher *w, int revents)
39 { 83 {
40 K *obj = static_cast<K *>(w->data); 84 K *obj = static_cast<K *>(w->data);
41 (obj->*method) (*w, revents); 85 (obj->*method) (*static_cast<watcher *>(w), revents);
42 } 86 }
43 87
88 // const method callback
44 template<class K, void (K::*method)(watcher &w, int) const> 89 template<class K, void (K::*method)(watcher &w, int) const>
45 void set (const K *object) 90 void set (const K *object)
46 { 91 {
47 set_ (object, const_method_thunk<K, method>); 92 set_ (object, const_method_thunk<K, method>);
48 } 93 }
49 94
50 template<class K, void (K::*method)(watcher &w, int) const> 95 template<class K, void (K::*method)(watcher &w, int) const>
51 static void const_method_thunk (EV_P_ watcher *w, int revents) 96 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
52 { 97 {
53 K *obj = static_cast<K *>(w->data); 98 K *obj = static_cast<K *>(w->data);
54 (obj->*method) (*w, revents); 99 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
55 } 100 }
56 101
102 // function callback
57 template<void (*function)(watcher &w, int)> 103 template<void (*function)(watcher &w, int)>
58 void set (void *data = 0) 104 void set (void *data = 0)
59 { 105 {
60 set_ (data, function_thunk<function>); 106 set_ (data, function_thunk<function>);
61 } 107 }
62 108
63 template<void (*function)(watcher &w, int)> 109 template<void (*function)(watcher &w, int)>
64 static void function_thunk (EV_P_ watcher *w, int revents) 110 static void function_thunk (EV_P_ ev_watcher *w, int revents)
111 {
112 function (*static_cast<watcher *>(w), revents);
65 { 113 }
66 function (*w, revents); 114
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) ();
67 } 127 }
68 128
69 void operator ()(int events = EV_UNDEF) 129 void operator ()(int events = EV_UNDEF)
70 { 130 {
71 return ev_cb (static_cast<ev_watcher *>(this)) 131 return ev_cb (static_cast<ev_watcher *>(this))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines