1 |
#!/usr/bin/perl |
2 |
|
3 |
use strict; |
4 |
|
5 |
print <<EOF; |
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 |
8 |
// THIS IS A GENERATED FILE: distribution. |
9 |
|
10 |
/* |
11 |
callback.h -- C++ callback mechanism |
12 |
Copyright (C) 2003-2007 Marc Lehmann <pcg\@goof.com> |
13 |
|
14 |
This file is part of GVPE. |
15 |
|
16 |
GVPE is free software; you can redistribute it and/or modify |
17 |
it under the terms of the GNU General Public License as published by |
18 |
the Free Software Foundation; either version 2 of the License, or |
19 |
(at your option) any later version. |
20 |
|
21 |
This program is distributed in the hope that it will be useful, |
22 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24 |
GNU General Public License for more details. |
25 |
|
26 |
You should have received a copy of the GNU General Public License |
27 |
along with gvpe; if not, write to the Free Software |
28 |
Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
29 |
*/ |
30 |
|
31 |
#ifndef CALLBACK_H__ |
32 |
#define CALLBACK_H__ |
33 |
|
34 |
#define CALLBACK_H_VERSION 3 |
35 |
|
36 |
template<typename signature> |
37 |
struct callback; |
38 |
|
39 |
#define callback_set(callback,obj,klass,method) callback.set<klass, &klass::method> (obj) |
40 |
|
41 |
EOF |
42 |
|
43 |
for my $a (0..10) { |
44 |
my $CLASS = join "", map ", class A$_", 1..$a; |
45 |
my $TYPE = join ", ", map "A$_", 1..$a; |
46 |
my $ARG = join ", ", map "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"; |
50 |
my $_ARG = $ARG ? ", $ARG" : ""; |
51 |
my $_TYPE = $TYPE ? ", $TYPE" : ""; |
52 |
my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : ""; |
53 |
my $_TTYPE = $a ? join "", map ", typename T::arg$_\_type", 1..$a : ""; |
54 |
|
55 |
print <<EOF; |
56 |
template<class R$CLASS> |
57 |
struct callback<R ($TYPE)> |
58 |
{ |
59 |
typedef R (*ptr_type)(void *self$_TYPE); |
60 |
|
61 |
private: |
62 |
|
63 |
void *self; |
64 |
ptr_type func; |
65 |
|
66 |
protected: |
67 |
|
68 |
template<typename method> |
69 |
struct thunktype; |
70 |
|
71 |
template<class klass> |
72 |
struct thunktype<R (klass::*)> |
73 |
{ |
74 |
typedef klass K; |
75 |
}; |
76 |
|
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 |
} |
83 |
|
84 |
public: |
85 |
template<class K, R (K::*method)($TYPE)> |
86 |
void set (K *object) |
87 |
{ |
88 |
self = object; |
89 |
func = thunk<K, method>; |
90 |
} |
91 |
|
92 |
R call ($TYPEARG) const |
93 |
{ |
94 |
return func (self$_ARG); |
95 |
} |
96 |
|
97 |
R operator ()($TYPEARG) const |
98 |
{ |
99 |
return call ($ARG); |
100 |
} |
101 |
}; |
102 |
|
103 |
EOF |
104 |
} |
105 |
|
106 |
print <<EOF |
107 |
|
108 |
#endif |
109 |
EOF |
110 |
|