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-2007 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 | GVPE is free software; you can redistribute it and/or modify |
15 | 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 |
… | |
… | |
27 | */ |
29 | */ |
28 | |
30 | |
29 | #ifndef CALLBACK_H__ |
31 | #ifndef CALLBACK_H__ |
30 | #define CALLBACK_H__ |
32 | #define CALLBACK_H__ |
31 | |
33 | |
32 | #define CALLBACK_H_VERSION 2 |
34 | #define CALLBACK_H_VERSION 3 |
33 | |
35 | |
34 | template<class signature> |
36 | template<class signature> |
35 | struct callback_funtype_trait; |
37 | struct callback_funtype_trait; |
36 | |
38 | |
37 | template<int arity, class signature> |
39 | template<int arity, class signature> |
… | |
… | |
45 | my $ARG = join ", ", map "a$_", 1..$a; |
47 | my $ARG = join ", ", map "a$_", 1..$a; |
46 | my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; |
48 | my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; |
47 | my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a; |
49 | my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a; |
48 | my $TYPEvoid = $TYPE ? $TYPE : "void"; |
50 | my $TYPEvoid = $TYPE ? $TYPE : "void"; |
49 | my $_TYPE = $TYPE ? ", $TYPE" : ""; |
51 | my $_TYPE = $TYPE ? ", $TYPE" : ""; |
50 | my $_ARG = $ARG ? ", $ARG" : ""; |
|
|
51 | my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : ""; |
|
|
52 | my $_TTYPE = $a ? join "", map ", typename T::arg$_\_type", 1..$a : ""; |
52 | my $_TTYPE = $a ? join "", map ", typename T::arg$_\_type", 1..$a : ""; |
53 | |
53 | |
54 | print <<EOF; |
54 | print <<EOF; |
55 | template<class R$CLASS> |
55 | template<class R$CLASS> |
56 | class callback$a |
56 | class callback$a |
57 | { |
57 | { |
58 | struct object { }; |
58 | struct klass; // it is vital that this is never defined |
59 | |
59 | |
60 | typedef R (object::*ptr_type)($TYPE); |
60 | typedef R (klass::*ptr_type)($TYPE); |
61 | |
61 | |
62 | void *obj; |
62 | klass *o; |
63 | R (object::*meth)($TYPE); |
63 | R (klass::*m)($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 | |
64 | |
80 | public: |
65 | public: |
81 | template<class O1, class O2> |
66 | template<class O1, class O2> |
82 | explicit callback$a (O1 *object, R (O2::*method)($TYPE)) |
67 | explicit callback$a (O1 *object, R (O2::*method)($TYPE)) |
83 | { |
68 | { |
84 | static proxy<O1,O2> p; |
|
|
85 | obj = reinterpret_cast<void *>(object); |
69 | o = reinterpret_cast<klass *>(object); |
86 | meth = reinterpret_cast<R (object::*)($TYPE)>(method); |
70 | m = reinterpret_cast<R (klass::*)($TYPE)>(method); |
87 | prxy = &p; |
|
|
88 | } |
71 | } |
89 | |
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) :) |
90 | R call($TYPEARG) const |
77 | R call($TYPEARG) const |
91 | { |
78 | { |
92 | return prxy->call (obj, meth$_ARG); |
79 | return (o->*m) ($ARG); |
93 | } |
80 | } |
94 | |
81 | |
95 | R operator ()($TYPEARG) const |
82 | R operator ()($TYPEARG) const |
96 | { |
83 | { |
97 | return call ($ARG); |
84 | return call ($ARG); |
98 | } |
85 | } |
99 | }; |
86 | }; |
100 | |
87 | |
101 | template<class R$CLASS> |
88 | template<class R$CLASS> |
102 | struct callback_funtype_trait$a |
89 | struct callback_funtype_trait$a |
103 | { |
90 | { |
… | |
… | |
129 | { |
116 | { |
130 | typedef typename callback_get_impl<callback_funtype_trait<signature>::arity, signature>::type base_type; |
117 | typedef typename callback_get_impl<callback_funtype_trait<signature>::arity, signature>::type base_type; |
131 | |
118 | |
132 | template<class O, class M> |
119 | template<class O, class M> |
133 | explicit callback (O object, M method) |
120 | explicit callback (O object, M method) |
134 | : base_type (object, method) |
121 | : base_type (object, method) |
135 | { |
122 | { |
136 | } |
123 | } |
137 | }; |
124 | }; |
138 | |
125 | |
139 | #endif |
126 | #endif |
140 | EOF |
127 | EOF |
141 | |
128 | |