1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
|
|
2 | |
|
|
3 | use strict; |
2 | |
4 | |
3 | print <<EOF; |
5 | print <<EOF; |
4 | // THIS IS A GENERATED FILE: RUN callback.pl to regenerate it |
6 | // THIS IS A GENERATED FILE: RUN callback.pl to regenerate it |
5 | // THIS IS A GENERATED FILE: callback.pl is part of the GVPE |
7 | // THIS IS A GENERATED FILE: callback.pl is part of the GVPE |
6 | // THIS IS A GENERATED FILE: distribution. |
8 | // THIS IS A GENERATED FILE: distribution. |
7 | |
9 | |
8 | /* |
10 | /* |
9 | callback.h -- C++ callback mechanism |
11 | * callback.h -- C++ callback mechanism |
10 | Copyright (C) 2003-2006 Marc Lehmann <pcg\@goof.com> |
12 | * Copyright (C) 2003-2008 Marc Lehmann <pcg\@goof.com> |
11 | |
13 | * |
12 | This file is part of GVPE. |
14 | * This file is part of GVPE. |
13 | |
15 | * |
14 | GVPE is free software; you can redistribute it and/or modify |
16 | * This program is free software; you can redistribute it and/or modify it |
15 | it under the terms of the GNU General Public License as published by |
17 | * under the terms of the GNU General Public License as published by the |
16 | the Free Software Foundation; either version 2 of the License, or |
18 | * Free Software Foundation; either version 3 of the License, or (at your |
17 | (at your option) any later version. |
19 | * option) any later version. |
18 | |
20 | * |
19 | This program is distributed in the hope that it will be useful, |
21 | * This program is distributed in the hope that it will be useful, but |
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
22 | GNU General Public License for more details. |
24 | * Public License for more details. |
23 | |
25 | * |
24 | You should have received a copy of the GNU General Public License |
26 | * You should have received a copy of the GNU General Public License along |
25 | along with gvpe; if not, write to the Free Software |
27 | * with this program; if not, see <http://www.gnu.org/licenses/>. |
26 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
28 | * |
|
|
29 | * Additional permission under GNU GPL version 3 section 7 |
|
|
30 | * |
|
|
31 | * If you modify this Program, or any covered work, by linking or |
|
|
32 | * combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
33 | * version of that library), containing parts covered by the terms of the |
|
|
34 | * OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
35 | * additional permission to convey the resulting work. Corresponding |
|
|
36 | * Source for a non-source form of such a combination shall include the |
|
|
37 | * source code for the parts of OpenSSL used as well as that of the |
|
|
38 | * covered work. |
27 | */ |
39 | */ |
28 | |
40 | |
29 | #ifndef CALLBACK_H__ |
41 | #ifndef CALLBACK_H__ |
30 | #define CALLBACK_H__ |
42 | #define CALLBACK_H__ |
31 | |
43 | |
32 | #define CALLBACK_H_VERSION 2 |
44 | #define CALLBACK_H_VERSION 3 |
33 | |
45 | |
34 | template<class signature> |
46 | template<typename signature> |
35 | struct callback_funtype_trait; |
|
|
36 | |
|
|
37 | template<int arity, class signature> |
|
|
38 | struct callback_get_impl; |
47 | struct callback; |
39 | |
48 | |
40 | EOF |
49 | EOF |
41 | |
50 | |
42 | for my $a (0..10) { |
51 | for my $a (0..10) { |
43 | my $CLASS = join "", map ", class A$_", 1..$a; |
52 | my $CLASS = join "", map ", class A$_", 1..$a; |
44 | my $TYPE = join ", ", map "A$_", 1..$a; |
53 | my $TYPE = join ", ", map "A$_", 1..$a; |
45 | my $ARG = join ", ", map "a$_", 1..$a; |
54 | my $ARG = join ", ", map "a$_", 1..$a; |
46 | my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; |
55 | my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; |
47 | my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a; |
56 | my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a; |
48 | my $TYPEvoid = $TYPE ? $TYPE : "void"; |
57 | my $TYPEvoid = $TYPE ? $TYPE : "void"; |
|
|
58 | my $_ARG = $ARG ? ", $ARG" : ""; |
49 | my $_TYPE = $TYPE ? ", $TYPE" : ""; |
59 | my $_TYPE = $TYPE ? ", $TYPE" : ""; |
50 | my $_ARG = $ARG ? ", $ARG" : ""; |
|
|
51 | my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : ""; |
60 | my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : ""; |
52 | my $_TTYPE = $a ? join "", map ", typename T::arg$_\_type", 1..$a : ""; |
61 | my $_TTYPE = $a ? join "", map ", typename T::arg$_\_type", 1..$a : ""; |
53 | |
62 | |
54 | print <<EOF; |
63 | print <<EOF; |
55 | template<class R$CLASS> |
64 | template<class R$CLASS> |
56 | class callback$a |
65 | struct callback<R ($TYPE)> |
57 | { |
66 | { |
58 | struct object { }; |
67 | typedef R (*ptr_type)(void *self$_TYPE); |
59 | |
68 | |
60 | typedef R (object::*ptr_type)($TYPE); |
69 | template<class K, R (K::*method)($TYPE)> |
|
|
70 | void set (K *object) |
|
|
71 | { |
|
|
72 | self = object; |
|
|
73 | func = thunk<K, method>; |
|
|
74 | } |
61 | |
75 | |
62 | void *obj; |
|
|
63 | R (object::*meth)($TYPE); |
|
|
64 | |
|
|
65 | /* a proxy is a kind of recipe on how to call a specific class method */ |
|
|
66 | struct proxy_base { |
|
|
67 | virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) const = 0; |
|
|
68 | }; |
|
|
69 | template<class O1, class O2> |
|
|
70 | struct proxy : proxy_base { |
|
|
71 | virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) const |
|
|
72 | { |
|
|
73 | return (R)((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)($TYPE)>(meth))) |
|
|
74 | ($ARG); |
|
|
75 | } |
|
|
76 | }; |
|
|
77 | |
|
|
78 | proxy_base *prxy; |
|
|
79 | |
|
|
80 | public: |
|
|
81 | template<class O1, class O2> |
|
|
82 | explicit callback$a (O1 *object, R (O2::*method)($TYPE)) |
|
|
83 | { |
|
|
84 | static proxy<O1,O2> p; |
|
|
85 | obj = reinterpret_cast<void *>(object); |
|
|
86 | meth = reinterpret_cast<R (object::*)($TYPE)>(method); |
|
|
87 | prxy = &p; |
|
|
88 | } |
|
|
89 | |
|
|
90 | R call($TYPEARG) const |
76 | R call ($TYPEARG) const |
91 | { |
77 | { |
92 | return prxy->call (obj, meth$_ARG); |
78 | return func (self$_ARG); |
93 | } |
79 | } |
94 | |
80 | |
95 | R operator ()($TYPEARG) const |
81 | R operator ()($TYPEARG) const |
96 | { |
82 | { |
97 | return call ($ARG); |
83 | return call ($ARG); |
98 | } |
84 | } |
|
|
85 | |
|
|
86 | private: |
|
|
87 | |
|
|
88 | void *self; |
|
|
89 | ptr_type func; |
|
|
90 | |
|
|
91 | template<class klass, R (klass::*method)($TYPE)> |
|
|
92 | static R thunk (void *self$_TYPEARG) |
|
|
93 | { |
|
|
94 | klass *obj = static_cast<klass *>(self); |
|
|
95 | return (obj->*method) ($ARG); |
|
|
96 | } |
99 | }; |
97 | }; |
100 | |
98 | |
101 | template<class R$CLASS> |
|
|
102 | struct callback_funtype_trait$a |
|
|
103 | { |
|
|
104 | static const int arity = $a; |
|
|
105 | typedef R type ($TYPEvoid); |
|
|
106 | typedef R result_type; |
|
|
107 | $TYPEDEFS |
|
|
108 | }; |
|
|
109 | |
|
|
110 | template<class R$CLASS> |
|
|
111 | struct callback_funtype_trait<R ($TYPE)> : callback_funtype_trait$a<R$_TYPE> |
|
|
112 | { |
|
|
113 | }; |
|
|
114 | |
|
|
115 | template<class signature> |
|
|
116 | struct callback_get_impl<$a, signature> |
|
|
117 | { |
|
|
118 | typedef callback_funtype_trait<signature> T; |
|
|
119 | typedef callback$a<typename T::result_type$_TTYPE> type; |
|
|
120 | }; |
|
|
121 | |
|
|
122 | EOF |
99 | EOF |
123 | } |
100 | } |
124 | |
101 | |
125 | print <<EOF |
102 | print <<EOF |
126 | |
103 | |
127 | template<class signature> |
|
|
128 | struct callback : callback_get_impl<callback_funtype_trait<signature>::arity, signature>::type |
|
|
129 | { |
|
|
130 | typedef typename callback_get_impl<callback_funtype_trait<signature>::arity, signature>::type base_type; |
|
|
131 | |
|
|
132 | template<class O, class M> |
|
|
133 | explicit callback (O object, M method) |
|
|
134 | : base_type (object, method) |
|
|
135 | { |
|
|
136 | } |
|
|
137 | }; |
|
|
138 | |
|
|
139 | #endif |
104 | #endif |
140 | EOF |
105 | EOF |
141 | |
106 | |