1 |
pcg |
1.1 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
pcg |
1.10 |
use strict; |
4 |
|
|
|
5 |
pcg |
1.1 |
print <<EOF; |
6 |
pcg |
1.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 |
pcg |
1.1 |
|
10 |
|
|
/* |
11 |
|
|
callback.h -- C++ callback mechanism |
12 |
pcg |
1.10 |
Copyright (C) 2003-2007 Marc Lehmann <pcg\@goof.com> |
13 |
pcg |
1.1 |
|
14 |
pcg |
1.4 |
This file is part of GVPE. |
15 |
|
|
|
16 |
|
|
GVPE is free software; you can redistribute it and/or modify |
17 |
pcg |
1.1 |
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 |
pcg |
1.4 |
along with gvpe; if not, write to the Free Software |
28 |
pcg |
1.5 |
Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
29 |
pcg |
1.1 |
*/ |
30 |
|
|
|
31 |
pcg |
1.3 |
#ifndef CALLBACK_H__ |
32 |
|
|
#define CALLBACK_H__ |
33 |
pcg |
1.1 |
|
34 |
pcg |
1.10 |
#define CALLBACK_H_VERSION 3 |
35 |
pcg |
1.9 |
|
36 |
|
|
template<class signature> |
37 |
|
|
struct callback_funtype_trait; |
38 |
|
|
|
39 |
|
|
template<int arity, class signature> |
40 |
|
|
struct callback_get_impl; |
41 |
|
|
|
42 |
pcg |
1.1 |
EOF |
43 |
|
|
|
44 |
pcg |
1.9 |
for my $a (0..10) { |
45 |
pcg |
1.1 |
my $CLASS = join "", map ", class A$_", 1..$a; |
46 |
|
|
my $TYPE = join ", ", map "A$_", 1..$a; |
47 |
|
|
my $ARG = join ", ", map "a$_", 1..$a; |
48 |
|
|
my $TYPEARG = join ", ", map "A$_ a$_", 1..$a; |
49 |
pcg |
1.9 |
my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a; |
50 |
|
|
my $TYPEvoid = $TYPE ? $TYPE : "void"; |
51 |
|
|
my $_TYPE = $TYPE ? ", $TYPE" : ""; |
52 |
|
|
my $_TTYPE = $a ? join "", map ", typename T::arg$_\_type", 1..$a : ""; |
53 |
pcg |
1.1 |
|
54 |
|
|
print <<EOF; |
55 |
|
|
template<class R$CLASS> |
56 |
pcg |
1.9 |
class callback$a |
57 |
|
|
{ |
58 |
pcg |
1.10 |
struct klass; // it is vital that this is never defined |
59 |
pcg |
1.9 |
|
60 |
pcg |
1.10 |
typedef R (klass::*ptr_type)($TYPE); |
61 |
pcg |
1.1 |
|
62 |
pcg |
1.10 |
klass *o; |
63 |
|
|
R (klass::*m)($TYPE); |
64 |
pcg |
1.1 |
|
65 |
|
|
public: |
66 |
|
|
template<class O1, class O2> |
67 |
pcg |
1.8 |
explicit callback$a (O1 *object, R (O2::*method)($TYPE)) |
68 |
pcg |
1.10 |
{ |
69 |
|
|
o = reinterpret_cast<klass *>(object); |
70 |
|
|
m = reinterpret_cast<R (klass::*)($TYPE)>(method); |
71 |
|
|
} |
72 |
|
|
|
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 |
pcg |
1.1 |
R call($TYPEARG) const |
78 |
pcg |
1.10 |
{ |
79 |
|
|
return (o->*m) ($ARG); |
80 |
|
|
} |
81 |
pcg |
1.1 |
|
82 |
|
|
R operator ()($TYPEARG) const |
83 |
pcg |
1.10 |
{ |
84 |
|
|
return call ($ARG); |
85 |
|
|
} |
86 |
pcg |
1.1 |
}; |
87 |
|
|
|
88 |
pcg |
1.9 |
template<class R$CLASS> |
89 |
|
|
struct 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 |
|
|
|
97 |
|
|
template<class R$CLASS> |
98 |
|
|
struct callback_funtype_trait<R ($TYPE)> : callback_funtype_trait$a<R$_TYPE> |
99 |
|
|
{ |
100 |
|
|
}; |
101 |
|
|
|
102 |
|
|
template<class signature> |
103 |
|
|
struct 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 |
|
|
|
109 |
pcg |
1.1 |
EOF |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
print <<EOF |
113 |
pcg |
1.9 |
|
114 |
|
|
template<class signature> |
115 |
|
|
struct 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 |
pcg |
1.10 |
: base_type (object, method) |
122 |
|
|
{ |
123 |
|
|
} |
124 |
pcg |
1.9 |
}; |
125 |
|
|
|
126 |
pcg |
1.1 |
#endif |
127 |
|
|
EOF |
128 |
|
|
|