1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
2 | |
2 | |
|
|
3 | use strict; |
|
|
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, RUN callback.pl to regenerate it |
7 | // THIS IS A GENERATED FILE: callback.pl is part of the GVPE |
6 | // THIS IS A GENERATED FILE, RUN callback.pl to regenerate it |
8 | // 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 | |
9 | |
11 | /* |
10 | /* |
12 | callback.h -- C++ callback mechanism |
11 | callback.h -- C++ callback mechanism |
13 | Copyright (C) 2003 Marc Lehmann <pcg\@goof.com> |
12 | Copyright (C) 2003-2007 Marc Lehmann <pcg\@goof.com> |
14 | |
13 | |
|
|
14 | This file is part of GVPE. |
|
|
15 | |
15 | This program is free software; you can redistribute it and/or modify |
16 | 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 |
17 | 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 |
18 | the Free Software Foundation; either version 2 of the License, or |
18 | (at your option) any later version. |
19 | (at your option) any later version. |
19 | |
20 | |
20 | 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, |
21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | GNU General Public License for more details. |
24 | GNU General Public License for more details. |
24 | |
25 | |
25 | 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 |
26 | along with this program; if not, write to the Free Software |
27 | along with gvpe; if not, write to the Free Software |
27 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
28 | */ |
29 | */ |
29 | |
30 | |
30 | #ifndef VPE_CALLBACK_H__ |
31 | #ifndef CALLBACK_H__ |
31 | #define VPE_CALLBACK_H__ |
32 | #define CALLBACK_H__ |
|
|
33 | |
|
|
34 | #define CALLBACK_H_VERSION 3 |
|
|
35 | |
|
|
36 | template<class signature> |
|
|
37 | struct callback_funtype_trait; |
|
|
38 | |
|
|
39 | template<int arity, class signature> |
|
|
40 | struct callback_get_impl; |
32 | |
41 | |
33 | EOF |
42 | EOF |
34 | |
43 | |
35 | for my $a (0..7) { |
44 | for my $a (0..10) { |
36 | my $CLASS = join "", map ", class A$_", 1..$a; |
45 | my $CLASS = join "", map ", class A$_", 1..$a; |
37 | my $TYPE = join ", ", map "A$_", 1..$a; |
46 | my $TYPE = join ", ", map "A$_", 1..$a; |
38 | my $ARG = join ", ", map "a$_", 1..$a; |
47 | my $ARG = join ", ", map "a$_", 1..$a; |
39 | my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; |
48 | my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; |
40 | my $_ARG = $ARG ? ", $ARG" : ""; |
49 | my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a; |
|
|
50 | my $TYPEvoid = $TYPE ? $TYPE : "void"; |
41 | my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : ""; |
51 | my $_TYPE = $TYPE ? ", $TYPE" : ""; |
|
|
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 |
46 | struct object { }; |
57 | { |
|
|
58 | struct klass; // it is vital that this is never defined |
47 | |
59 | |
48 | void *obj; |
60 | typedef R (klass::*ptr_type)($TYPE); |
49 | R (object::*meth)($TYPE); |
|
|
50 | |
61 | |
51 | /* a proxy is a kind of recipe on how to call a specific class method */ |
62 | klass *o; |
52 | struct proxy_base { |
63 | R (klass::*m)($TYPE); |
53 | virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) = 0; |
|
|
54 | }; |
|
|
55 | template<class O1, class O2> |
|
|
56 | struct proxy : proxy_base { |
|
|
57 | virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) |
|
|
58 | { |
|
|
59 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)($TYPE)>(meth))) |
|
|
60 | ($ARG); |
|
|
61 | } |
|
|
62 | }; |
|
|
63 | |
|
|
64 | proxy_base *prxy; |
|
|
65 | |
64 | |
66 | public: |
65 | public: |
67 | template<class O1, class O2> |
66 | template<class O1, class O2> |
68 | callback$a (O1 *object, R (O2::*method)($TYPE)) |
67 | explicit callback$a (O1 *object, R (O2::*method)($TYPE)) |
69 | { |
68 | { |
70 | static proxy<O1,O2> p; |
|
|
71 | obj = reinterpret_cast<void *>(object); |
69 | o = reinterpret_cast<klass *>(object); |
72 | meth = reinterpret_cast<R (object::*)($TYPE)>(method); |
70 | m = reinterpret_cast<R (klass::*)($TYPE)>(method); |
73 | prxy = &p; |
|
|
74 | } |
71 | } |
75 | |
72 | |
|
|
73 | // this works because a standards-compliant C++ compiler |
|
|
74 | // basically can't help it: it doesn't have the knowledge |
|
|
75 | // required to miscompile (klass is not defined anywhere |
|
|
76 | // and nothing is known about the constructor arguments) :) |
76 | R call($TYPEARG) const |
77 | R call($TYPEARG) const |
77 | { |
78 | { |
78 | return prxy->call (obj, meth$_ARG); |
79 | return (o->*m) ($ARG); |
79 | } |
80 | } |
80 | |
81 | |
81 | R operator ()($TYPEARG) const |
82 | R operator ()($TYPEARG) const |
82 | { |
83 | { |
83 | return call ($ARG); |
84 | return call ($ARG); |
84 | } |
85 | } |
85 | }; |
86 | }; |
86 | |
87 | |
|
|
88 | template<class R$CLASS> |
|
|
89 | struct callback_funtype_trait$a |
|
|
90 | { |
|
|
91 | static const int arity = $a; |
|
|
92 | typedef R type ($TYPEvoid); |
|
|
93 | typedef R result_type; |
|
|
94 | $TYPEDEFS |
|
|
95 | }; |
|
|
96 | |
|
|
97 | template<class R$CLASS> |
|
|
98 | struct callback_funtype_trait<R ($TYPE)> : callback_funtype_trait$a<R$_TYPE> |
|
|
99 | { |
|
|
100 | }; |
|
|
101 | |
|
|
102 | template<class signature> |
|
|
103 | struct callback_get_impl<$a, signature> |
|
|
104 | { |
|
|
105 | typedef callback_funtype_trait<signature> T; |
|
|
106 | typedef callback$a<typename T::result_type$_TTYPE> type; |
|
|
107 | }; |
|
|
108 | |
87 | EOF |
109 | EOF |
88 | } |
110 | } |
89 | |
111 | |
90 | print <<EOF |
112 | print <<EOF |
|
|
113 | |
|
|
114 | template<class signature> |
|
|
115 | struct callback : callback_get_impl<callback_funtype_trait<signature>::arity, signature>::type |
|
|
116 | { |
|
|
117 | typedef typename callback_get_impl<callback_funtype_trait<signature>::arity, signature>::type base_type; |
|
|
118 | |
|
|
119 | template<class O, class M> |
|
|
120 | explicit callback (O object, M method) |
|
|
121 | : base_type (object, method) |
|
|
122 | { |
|
|
123 | } |
|
|
124 | }; |
|
|
125 | |
91 | #endif |
126 | #endif |
92 | EOF |
127 | EOF |
93 | |
128 | |