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.1 by pcg, Wed Apr 2 05:14:59 2003 UTC vs.
Revision 1.13 by pcg, Wed Dec 5 18:19:50 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
12 Copyright (C) 2003-2007 Marc Lehmann <pcg\@goof.com>
13 13
14 This file is part of GVPE.
15
14 This program 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
16 the Free Software Foundation; either version 2 of the License, or 18 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version. 19 (at your option) any later version.
18 20
19 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,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of 22 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details. 24 GNU General Public License for more details.
23 25
24 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
25 along with this program; if not, write to the Free Software 27 along with gvpe; if not, write to the Free Software
26 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
27*/ 29*/
28 30
29#ifndef VPE_CALLBACK_H__ 31#ifndef CALLBACK_H__
30#define VPE_CALLBACK_H__ 32#define CALLBACK_H__
33
34#define CALLBACK_H_VERSION 3
35
36template<typename signature>
37struct callback;
31 38
32EOF 39EOF
33 40
34for my $a (0..7) { 41for my $a (0..10) {
35 my $CLASS = join "", map ", class A$_", 1..$a; 42 my $CLASS = join "", map ", class A$_", 1..$a;
36 my $TYPE = join ", ", map "A$_", 1..$a; 43 my $TYPE = join ", ", map "A$_", 1..$a;
37 my $ARG = join ", ", map "a$_", 1..$a; 44 my $ARG = join ", ", map "a$_", 1..$a;
38 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";
39 my $_ARG = $ARG ? ", $ARG" : ""; 48 my $_ARG = $ARG ? ", $ARG" : "";
49 my $_TYPE = $TYPE ? ", $TYPE" : "";
40 my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : ""; 50 my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : "";
51 my $_TTYPE = $a ? join "", map ", typename T::arg$_\_type", 1..$a : "";
41 52
42 print <<EOF; 53 print <<EOF;
43template<class R$CLASS> 54template<class R$CLASS>
44class callback$a { 55struct callback<R ($TYPE)>
45 struct object { }; 56{
57 typedef R (*ptr_type)(void *self$_TYPE);
46 58
47 void *obj; 59 template<class K, R (K::*method)($TYPE)>
48 R (object::*meth)($TYPE); 60 void set (K *object)
61 {
62 self = object;
63 func = thunk<K, method>;
64 }
49 65
50 /* a proxy is a kind of recipe on how to call a specific class method */
51 struct proxy_base {
52 virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) = 0;
53 };
54 template<class O1, class O2>
55 struct proxy : proxy_base {
56 virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG)
57 {
58 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)($TYPE)>(meth)))
59 ($ARG);
60 }
61 };
62
63 proxy_base *prxy;
64
65public:
66 template<class O1, class O2>
67 callback$a (O1 *object, R (O2::*method)($TYPE))
68 {
69 static proxy<O1,O2> p;
70 obj = reinterpret_cast<void *>(object);
71 meth = reinterpret_cast<R (object::*)($TYPE)>(method);
72 prxy = &p;
73 }
74
75 R call($TYPEARG) const 66 R call ($TYPEARG) const
76 { 67 {
77 return prxy->call (obj, meth$_ARG); 68 return func (self$_ARG);
78 } 69 }
79 70
80 R operator ()($TYPEARG) const 71 R operator ()($TYPEARG) const
81 { 72 {
82 return call ($ARG); 73 return call ($ARG);
83 } 74 }
75
76private:
77
78 void *self;
79 ptr_type func;
80
81 template<class klass, R (klass::*method)($TYPE)>
82 static R thunk (void *self$_TYPEARG)
83 {
84 klass *obj = static_cast<klass *>(self);
85 return (obj->*method) ($ARG);
86 }
84}; 87};
85 88
86EOF 89EOF
87} 90}
88 91
89print <<EOF 92print <<EOF
93
90#endif 94#endif
91EOF 95EOF
92 96

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines