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.3 by pcg, Sat Jan 17 01:18:36 2004 UTC vs.
Revision 1.12 by pcg, Tue Dec 4 17:17:19 2007 UTC

1#!/usr/bin/perl 1#!/usr/bin/perl
2 2
3use strict;
4
3print <<EOF; 5print <<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 CALLBACK_H__ 31#ifndef CALLBACK_H__
31#define CALLBACK_H__ 32#define CALLBACK_H__
32 33
34#define CALLBACK_H_VERSION 3
35
36template<typename signature>
37struct callback;
38
33EOF 39EOF
34 40
35for my $a (0..7) { 41for my $a (0..10) {
36 my $CLASS = join "", map ", class A$_", 1..$a; 42 my $CLASS = join "", map ", class A$_", 1..$a;
37 my $TYPE = join ", ", map "A$_", 1..$a; 43 my $TYPE = join ", ", map "A$_", 1..$a;
38 my $ARG = join ", ", map "a$_", 1..$a; 44 my $ARG = join ", ", map "a$_", 1..$a;
39 my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; 45 my $TYPEARG = join ", ", map "A$_ a$_", 1..$a;
46 my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a;
47 my $TYPEvoid = $TYPE ? $TYPE : "void";
40 my $_ARG = $ARG ? ", $ARG" : ""; 48 my $_ARG = $ARG ? ", $ARG" : "";
49 my $_TYPE = $TYPE ? ", $TYPE" : "";
41 my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : ""; 50 my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : "";
51 my $_TTYPE = $a ? join "", map ", typename T::arg$_\_type", 1..$a : "";
42 52
43 print <<EOF; 53 print <<EOF;
44template<class R$CLASS> 54template<class R$CLASS>
45class callback$a { 55struct callback<R ($TYPE)>
46 struct object { }; 56{
57 typedef R (*ptr_type)(void *self$_TYPE);
47 58
48 void *obj; 59private:
49 R (object::*meth)($TYPE);
50 60
51 /* a proxy is a kind of recipe on how to call a specific class method */ 61 void *self;
52 struct proxy_base { 62 ptr_type func;
53 virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) = 0; 63
54 }; 64protected:
65
66 template<typename method>
67 struct thunktype;
68
55 template<class O1, class O2> 69 template<class klass>
56 struct proxy : proxy_base { 70 struct thunktype<R (klass::*)>
57 virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) 71 {
58 { 72 typedef klass K;
59 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)($TYPE)>(meth)))
60 ($ARG);
61 }
62 }; 73 };
63 74
64 proxy_base *prxy; 75 template<class klass, R (klass::*method)($TYPE)>
76 static R thunk (void *self$_TYPEARG)
77 {
78 klass *obj = static_cast<klass *>(self);
79 return (obj->*method) ($ARG);
80 }
65 81
66public: 82public:
67 template<class O1, class O2> 83 template<class K, R (K::*method)($TYPE)>
68 callback$a (O1 *object, R (O2::*method)($TYPE)) 84 void set (K *object)
69 { 85 {
70 static proxy<O1,O2> p; 86 self = object;
71 obj = reinterpret_cast<void *>(object); 87 func = thunk<K, method>;
72 meth = reinterpret_cast<R (object::*)($TYPE)>(method);
73 prxy = &p;
74 } 88 }
75 89
76 R call($TYPEARG) const 90 R call ($TYPEARG) const
77 { 91 {
78 return prxy->call (obj, meth$_ARG); 92 return func (self$_ARG);
79 } 93 }
80 94
81 R operator ()($TYPEARG) const 95 R operator ()($TYPEARG) const
82 { 96 {
83 return call ($ARG); 97 return call ($ARG);
84 } 98 }
85}; 99};
86 100
87EOF 101EOF
88} 102}
89 103
90print <<EOF 104print <<EOF
105
91#endif 106#endif
92EOF 107EOF
93 108

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines