1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
2 | |
2 | |
3 | print <<EOF; |
3 | print <<EOF; |
4 | // THIS IS A GENERATED FILE, RUN callback.pl to regenerate it |
4 | // THIS IS A GENERATED FILE: RUN callback.pl to regenerate it |
5 | // THIS IS A GENERATED FILE, RUN callback.pl to regenerate it |
5 | // THIS IS A GENERATED FILE: callback.pl is part of the GVPE |
6 | // THIS IS A GENERATED FILE, RUN callback.pl to regenerate it |
6 | // THIS IS A GENERATED FILE: distribution. |
7 | // THIS IS A GENERATED FILE, RUN callback.pl to regenerate it |
|
|
8 | // THIS IS A GENERATED FILE, RUN callback.pl to regenerate it |
|
|
9 | // THIS IS A GENERATED FILE, RUN callback.pl to regenerate it |
|
|
10 | |
7 | |
11 | /* |
8 | /* |
12 | callback.h -- C++ callback mechanism |
9 | callback.h -- C++ callback mechanism |
13 | Copyright (C) 2003 Marc Lehmann <pcg\@goof.com> |
10 | Copyright (C) 2003-2006 Marc Lehmann <pcg\@goof.com> |
14 | |
11 | |
|
|
12 | This file is part of GVPE. |
|
|
13 | |
15 | This program is free software; you can redistribute it and/or modify |
14 | GVPE is free software; you can redistribute it and/or modify |
16 | it under the terms of the GNU General Public License as published by |
15 | it under the terms of the GNU General Public License as published by |
17 | the Free Software Foundation; either version 2 of the License, or |
16 | the Free Software Foundation; either version 2 of the License, or |
18 | (at your option) any later version. |
17 | (at your option) any later version. |
19 | |
18 | |
20 | This program is distributed in the hope that it will be useful, |
19 | This program is distributed in the hope that it will be useful, |
21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | GNU General Public License for more details. |
22 | GNU General Public License for more details. |
24 | |
23 | |
25 | You should have received a copy of the GNU General Public License |
24 | You should have received a copy of the GNU General Public License |
26 | along with this program; if not, write to the Free Software |
25 | along with gvpe; if not, write to the Free Software |
27 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
26 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
28 | */ |
27 | */ |
29 | |
28 | |
30 | #ifndef VPE_CALLBACK_H__ |
29 | #ifndef CALLBACK_H__ |
31 | #define VPE_CALLBACK_H__ |
30 | #define CALLBACK_H__ |
|
|
31 | |
|
|
32 | #define CALLBACK_H_VERSION 2 |
|
|
33 | |
|
|
34 | template<class signature> |
|
|
35 | struct callback_funtype_trait; |
|
|
36 | |
|
|
37 | template<int arity, class signature> |
|
|
38 | struct callback_get_impl; |
32 | |
39 | |
33 | EOF |
40 | EOF |
34 | |
41 | |
35 | for my $a (0..7) { |
42 | for my $a (0..10) { |
36 | my $CLASS = join "", map ", class A$_", 1..$a; |
43 | my $CLASS = join "", map ", class A$_", 1..$a; |
37 | my $TYPE = join ", ", map "A$_", 1..$a; |
44 | my $TYPE = join ", ", map "A$_", 1..$a; |
38 | my $ARG = join ", ", map "a$_", 1..$a; |
45 | my $ARG = join ", ", map "a$_", 1..$a; |
39 | my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; |
46 | my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; |
|
|
47 | my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a; |
|
|
48 | my $TYPEvoid = $TYPE ? $TYPE : "void"; |
|
|
49 | my $_TYPE = $TYPE ? ", $TYPE" : ""; |
40 | my $_ARG = $ARG ? ", $ARG" : ""; |
50 | my $_ARG = $ARG ? ", $ARG" : ""; |
41 | my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : ""; |
51 | my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : ""; |
|
|
52 | my $_TTYPE = $a ? join "", map ", typename T::arg$_\_type", 1..$a : ""; |
42 | |
53 | |
43 | print <<EOF; |
54 | print <<EOF; |
44 | template<class R$CLASS> |
55 | template<class R$CLASS> |
45 | class callback$a { |
56 | class callback$a |
|
|
57 | { |
46 | struct object { }; |
58 | struct object { }; |
|
|
59 | |
|
|
60 | typedef R (object::*ptr_type)($TYPE); |
47 | |
61 | |
48 | void *obj; |
62 | void *obj; |
49 | R (object::*meth)($TYPE); |
63 | R (object::*meth)($TYPE); |
50 | |
64 | |
51 | /* a proxy is a kind of recipe on how to call a specific class method */ |
65 | /* a proxy is a kind of recipe on how to call a specific class method */ |
52 | struct proxy_base { |
66 | struct proxy_base { |
53 | virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) = 0; |
67 | virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) const = 0; |
54 | }; |
68 | }; |
55 | template<class O1, class O2> |
69 | template<class O1, class O2> |
56 | struct proxy : proxy_base { |
70 | struct proxy : proxy_base { |
57 | virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) |
71 | virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) const |
58 | { |
72 | { |
59 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)($TYPE)>(meth))) |
73 | return (R)((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)($TYPE)>(meth))) |
60 | ($ARG); |
74 | ($ARG); |
61 | } |
75 | } |
62 | }; |
76 | }; |
63 | |
77 | |
64 | proxy_base *prxy; |
78 | proxy_base *prxy; |
65 | |
79 | |
66 | public: |
80 | public: |
67 | template<class O1, class O2> |
81 | template<class O1, class O2> |
68 | callback$a (O1 *object, R (O2::*method)($TYPE)) |
82 | explicit callback$a (O1 *object, R (O2::*method)($TYPE)) |
69 | { |
83 | { |
70 | static proxy<O1,O2> p; |
84 | static proxy<O1,O2> p; |
71 | obj = reinterpret_cast<void *>(object); |
85 | obj = reinterpret_cast<void *>(object); |
72 | meth = reinterpret_cast<R (object::*)($TYPE)>(method); |
86 | meth = reinterpret_cast<R (object::*)($TYPE)>(method); |
73 | prxy = &p; |
87 | prxy = &p; |
… | |
… | |
82 | { |
96 | { |
83 | return call ($ARG); |
97 | return call ($ARG); |
84 | } |
98 | } |
85 | }; |
99 | }; |
86 | |
100 | |
|
|
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 | |
87 | EOF |
122 | EOF |
88 | } |
123 | } |
89 | |
124 | |
90 | print <<EOF |
125 | print <<EOF |
|
|
126 | |
|
|
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 | |
91 | #endif |
139 | #endif |
92 | EOF |
140 | EOF |
93 | |
141 | |