ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/callback.pl
(Generate patch)

Comparing gvpe/src/callback.pl (file contents):
Revision 1.10 by pcg, Sun Dec 2 00:54:52 2007 UTC vs.
Revision 1.14 by pcg, Thu Aug 7 17:54:26 2008 UTC

6// THIS IS A GENERATED FILE: RUN callback.pl to regenerate it 6// THIS IS A GENERATED FILE: RUN callback.pl to regenerate it
7// 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
8// THIS IS A GENERATED FILE: distribution. 8// THIS IS A GENERATED FILE: distribution.
9 9
10/* 10/*
11 callback.h -- C++ callback mechanism 11 * callback.h -- C++ callback mechanism
12 Copyright (C) 2003-2007 Marc Lehmann <pcg\@goof.com> 12 * Copyright (C) 2003-2008 Marc Lehmann <pcg\@goof.com>
13 13 *
14 This file is part of GVPE. 14 * This file is part of GVPE.
15 15 *
16 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
17 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
18 the Free Software Foundation; either version 2 of the License, or 18 * Free Software Foundation; either version 3 of the License, or (at your
19 (at your option) any later version. 19 * option) any later version.
20 20 *
21 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
22 but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
24 GNU General Public License for more details. 24 * Public License for more details.
25 25 *
26 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
27 along with gvpe; if not, write to the Free Software 27 * with this program; if not, see <http://www.gnu.org/licenses/>.
28 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.
29*/ 39*/
30 40
31#ifndef CALLBACK_H__ 41#ifndef CALLBACK_H__
32#define CALLBACK_H__ 42#define CALLBACK_H__
33 43
34#define CALLBACK_H_VERSION 3 44#define CALLBACK_H_VERSION 3
35 45
36template<class signature> 46template<typename signature>
37struct callback_funtype_trait;
38
39template<int arity, class signature>
40struct callback_get_impl; 47struct callback;
41 48
42EOF 49EOF
43 50
44for my $a (0..10) { 51for my $a (0..10) {
45 my $CLASS = join "", map ", class A$_", 1..$a; 52 my $CLASS = join "", map ", class A$_", 1..$a;
46 my $TYPE = join ", ", map "A$_", 1..$a; 53 my $TYPE = join ", ", map "A$_", 1..$a;
47 my $ARG = join ", ", map "a$_", 1..$a; 54 my $ARG = join ", ", map "a$_", 1..$a;
48 my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; 55 my $TYPEARG = join ", ", map "A$_ a$_", 1..$a;
49 my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a; 56 my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a;
50 my $TYPEvoid = $TYPE ? $TYPE : "void"; 57 my $TYPEvoid = $TYPE ? $TYPE : "void";
58 my $_ARG = $ARG ? ", $ARG" : "";
51 my $_TYPE = $TYPE ? ", $TYPE" : ""; 59 my $_TYPE = $TYPE ? ", $TYPE" : "";
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;
55template<class R$CLASS> 64template<class R$CLASS>
56class callback$a 65struct callback<R ($TYPE)>
57{ 66{
58 struct klass; // it is vital that this is never defined 67 typedef R (*ptr_type)(void *self$_TYPE);
59 68
60 typedef R (klass::*ptr_type)($TYPE); 69 template<class K, R (K::*method)($TYPE)>
61 70 void set (K *object)
62 klass *o;
63 R (klass::*m)($TYPE);
64
65public:
66 template<class O1, class O2>
67 explicit callback$a (O1 *object, R (O2::*method)($TYPE))
68 { 71 {
69 o = reinterpret_cast<klass *>(object); 72 self = object;
70 m = reinterpret_cast<R (klass::*)($TYPE)>(method); 73 func = thunk<K, method>;
71 } 74 }
72 75
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) :)
77 R call($TYPEARG) const 76 R call ($TYPEARG) const
78 { 77 {
79 return (o->*m) ($ARG); 78 return func (self$_ARG);
80 } 79 }
81 80
82 R operator ()($TYPEARG) const 81 R operator ()($TYPEARG) const
83 { 82 {
84 return call ($ARG); 83 return call ($ARG);
85 } 84 }
85
86private:
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 }
86}; 97};
87 98
88template<class R$CLASS>
89struct 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
97template<class R$CLASS>
98struct callback_funtype_trait<R ($TYPE)> : callback_funtype_trait$a<R$_TYPE>
99{
100};
101
102template<class signature>
103struct 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
109EOF 99EOF
110} 100}
111 101
112print <<EOF 102print <<EOF
113 103
114template<class signature>
115struct 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
126#endif 104#endif
127EOF 105EOF
128 106

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines