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.7 by pcg, Tue Dec 27 16:36:16 2005 UTC vs.
Revision 1.11 by pcg, Tue Dec 4 15:01:12 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines