ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/callback.pl
Revision: 1.9
Committed: Wed May 31 00:31:47 2006 UTC (17 years, 11 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_01
Changes since 1.8: +51 -2 lines
Log Message:
support callback<void (int, int)> syntax instead of callback2<void, int, int> in callback.h, made iom.h use it

File Contents

# User Rev Content
1 pcg 1.1 #!/usr/bin/perl
2    
3     print <<EOF;
4 pcg 1.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
6     // THIS IS A GENERATED FILE: distribution.
7 pcg 1.1
8     /*
9     callback.h -- C++ callback mechanism
10 pcg 1.8 Copyright (C) 2003-2006 Marc Lehmann <pcg\@goof.com>
11 pcg 1.1
12 pcg 1.4 This file is part of GVPE.
13    
14     GVPE is free software; you can redistribute it and/or modify
15 pcg 1.1 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
17     (at your option) any later version.
18    
19     This program is distributed in the hope that it will be useful,
20     but WITHOUT ANY WARRANTY; without even the implied warranty of
21     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22     GNU General Public License for more details.
23    
24     You should have received a copy of the GNU General Public License
25 pcg 1.4 along with gvpe; if not, write to the Free Software
26 pcg 1.5 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 pcg 1.1 */
28    
29 pcg 1.3 #ifndef CALLBACK_H__
30     #define CALLBACK_H__
31 pcg 1.1
32 pcg 1.9 #define CALLBACK_H_VERSION 2
33    
34     template<class signature>
35     struct callback_funtype_trait;
36    
37     template<int arity, class signature>
38     struct callback_get_impl;
39    
40 pcg 1.1 EOF
41    
42 pcg 1.9 for my $a (0..10) {
43 pcg 1.1 my $CLASS = join "", map ", class A$_", 1..$a;
44     my $TYPE = join ", ", map "A$_", 1..$a;
45     my $ARG = join ", ", map "a$_", 1..$a;
46     my $TYPEARG = join ", ", map "A$_ a$_", 1..$a;
47 pcg 1.9 my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a;
48     my $TYPEvoid = $TYPE ? $TYPE : "void";
49     my $_TYPE = $TYPE ? ", $TYPE" : "";
50 pcg 1.1 my $_ARG = $ARG ? ", $ARG" : "";
51     my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : "";
52 pcg 1.9 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.1 struct object { };
59    
60 pcg 1.9 typedef R (object::*ptr_type)($TYPE);
61    
62 pcg 1.1 void *obj;
63     R (object::*meth)($TYPE);
64    
65     /* a proxy is a kind of recipe on how to call a specific class method */
66     struct proxy_base {
67 pcg 1.8 virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) const = 0;
68 pcg 1.1 };
69     template<class O1, class O2>
70     struct proxy : proxy_base {
71 pcg 1.8 virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) const
72 pcg 1.1 {
73 pcg 1.7 return (R)((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)($TYPE)>(meth)))
74 pcg 1.1 ($ARG);
75     }
76     };
77    
78     proxy_base *prxy;
79    
80     public:
81     template<class O1, class O2>
82 pcg 1.8 explicit callback$a (O1 *object, R (O2::*method)($TYPE))
83 pcg 1.1 {
84     static proxy<O1,O2> p;
85     obj = reinterpret_cast<void *>(object);
86     meth = reinterpret_cast<R (object::*)($TYPE)>(method);
87     prxy = &p;
88     }
89    
90     R call($TYPEARG) const
91     {
92     return prxy->call (obj, meth$_ARG);
93     }
94    
95     R operator ()($TYPEARG) const
96     {
97     return call ($ARG);
98     }
99     };
100    
101 pcg 1.9 template<class R$CLASS>
102     struct callback_funtype_trait$a
103     {
104     static const int arity = $a;
105     typedef R type ($TYPEvoid);
106     typedef R result_type;
107     $TYPEDEFS
108     };
109    
110     template<class R$CLASS>
111     struct callback_funtype_trait<R ($TYPE)> : callback_funtype_trait$a<R$_TYPE>
112     {
113     };
114    
115     template<class signature>
116     struct callback_get_impl<$a, signature>
117     {
118     typedef callback_funtype_trait<signature> T;
119     typedef callback$a<typename T::result_type$_TTYPE> type;
120     };
121    
122 pcg 1.1 EOF
123     }
124    
125     print <<EOF
126 pcg 1.9
127     template<class signature>
128     struct callback : callback_get_impl<callback_funtype_trait<signature>::arity, signature>::type
129     {
130     typedef typename callback_get_impl<callback_funtype_trait<signature>::arity, signature>::type base_type;
131    
132     template<class O, class M>
133     explicit callback (O object, M method)
134     : base_type (object, method)
135     {
136     }
137     };
138    
139 pcg 1.1 #endif
140     EOF
141