]> git.cworth.org Git - apitrace/blob - base.py
Add copyright headers.
[apitrace] / base.py
1 #############################################################################
2 #
3 # Copyright 2008 Jose Fonseca
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published
7 # by the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 #############################################################################
19
20 """C basic types"""
21
22 class Type:
23
24     def __init__(self, name):
25         self.name = name
26
27     def __str__(self):
28         return self.name
29
30     def isoutput(self):
31         return False
32
33     def dump(self, instance):
34         raise NotImplementedError
35     
36     def wrap_instance(self, instance):
37         pass
38
39     def unwrap_instance(self, instance):
40         pass
41
42
43 class Void(Type):
44
45     def __init__(self):
46         Type.__init__(self, "void")
47
48 Void = Void()
49
50
51 class Intrinsic(Type):
52
53     def __init__(self, name, format):
54         Type.__init__(self, name)
55         self.format = format
56
57     def dump(self, instance):
58         print '    g_pLog->TextF("%s", %s);' % (self.format, instance)
59
60
61 class Const(Type):
62
63     def __init__(self, type):
64         Type.__init__(self, 'C' + type.name)
65         self.type = type
66
67     def dump(self, instance):
68         self.type.dump(instance)
69
70     def __str__(self):
71         return "const " + str(self.type)
72
73
74 class Pointer(Type):
75
76     def __init__(self, type):
77         Type.__init__(self, 'P' + type.name)
78         self.type = type
79
80     def __str__(self):
81         return str(self.type) + " *"
82     
83     def dump(self, instance):
84         print '    if(%s) {' % instance
85         try:
86             self.type.dump("*" + instance)
87         except NotImplementedError:
88             print '        g_pLog->TextF("%%p", %s);' % instance
89         print '    }'
90         print '    else'
91         print '        g_pLog->Text("NULL");'
92
93     def wrap_instance(self, instance):
94         self.type.wrap_instance("*" + instance)
95
96     def unwrap_instance(self, instance):
97         self.type.wrap_instance("*" + instance)
98
99
100 class OutPointer(Pointer):
101
102     def isoutput(self):
103         return True
104
105
106 class Enum(Type):
107
108     def __init__(self, name, values):
109         Type.__init__(self, name)
110         self.values = values
111     
112     def dump(self, instance):
113         print '    switch(%s) {' % instance
114         for value in self.values:
115             print '    case %s:' % value
116             print '        g_pLog->Text("%s");' % value
117             print '        break;'
118         print '    default:'
119         print '        g_pLog->TextF("%%i", %s);' % instance
120         print '        break;'
121         print '    }'
122
123
124 class Flags(Type):
125
126     def __init__(self, type, values):
127         Type.__init__(self, type.name)
128         self.values = values
129
130
131 class Struct(Type):
132
133     def __init__(self, name, members):
134         Type.__init__(self, name)
135         self.members = members
136
137     def dump(self, instance):
138         print '    g_pLog->Text("{");'
139         first = True
140         for type, name in self.members:
141             if first:
142                 first = False
143             else:
144                 print '    g_pLog->Text(", ");'
145             type.dump('(%s).%s' % (instance, name))
146         print '    g_pLog->Text("}");'
147
148
149 class Alias(Type):
150
151     def __init__(self, name, type):
152         Type.__init__(self, name)
153         self.type = type
154
155     def dump(self, instance):
156         self.type.dump(instance)
157
158
159 class Function:
160
161     def __init__(self, type, name, args, call = '__stdcall'):
162         self.type = type
163         self.name = name
164         self.args = args
165         self.call = call
166
167     def prototype(self, name=None):
168         if name is not None:
169             name = name.strip()
170         else:
171             name = self.name
172         s = name
173         if self.call:
174             s = self.call + ' ' + s
175         if name.startswith('*'):
176             s = '(' + s + ')'
177         s = str(self.type) + ' ' + s
178         s += "("
179         if self.args:
180             s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
181         else:
182             s += "void"
183         s += ")"
184         return s
185
186
187 class Interface(Type):
188
189     def __init__(self, name, base=None):
190         Type.__init__(self, name)
191         self.base = base
192         self.methods = []
193
194     def itermethods(self):
195         if self.base is not None:
196             for method in self.base.itermethods():
197                 yield method
198         for method in self.methods:
199             yield method
200         raise StopIteration
201
202     def wrap_name(self):
203         return "Wrap" + self.name
204
205     def wrap_pre_decl(self):
206         print "class %s;" % self.wrap_name()
207
208     def wrap_decl(self):
209         print "class %s : public %s " % (self.wrap_name(), self.name)
210         print "{"
211         print "public:"
212         print "    %s(%s * pInstance);" % (self.wrap_name(), self.name)
213         print "    virtual ~%s();" % self.wrap_name()
214         print
215         for method in self.itermethods():
216             print "    " + method.prototype() + ";"
217         print
218         #print "private:"
219         print "    %s * m_pInstance;" % (self.name,)
220         print "};"
221         print
222
223     def wrap_impl(self):
224         print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
225         print '    m_pInstance = pInstance;'
226         print '}'
227         print
228         print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
229         print '}'
230         print
231         for method in self.itermethods():
232             print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
233             if method.type is Void:
234                 result = ''
235             else:
236                 print '    %s result;' % method.type
237                 result = 'result = '
238             print '    g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
239             print '    g_pLog->BeginParam("this", "%s *");' % self.name
240             print '    g_pLog->TextF("%p", m_pInstance);'
241             print '    g_pLog->EndParam();'
242             for type, name in method.args:
243                 if not type.isoutput():
244                     type.unwrap_instance(name)
245                     print '    g_pLog->BeginParam("%s", "%s");' % (name, type)
246                     type.dump(name)
247                     print '    g_pLog->EndParam();'
248             print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
249             for type, name in method.args:
250                 if type.isoutput():
251                     print '    g_pLog->BeginParam("%s", "%s");' % (name, type)
252                     type.dump(name)
253                     print '    g_pLog->EndParam();'
254                     type.wrap_instance(name)
255             if method.type is not Void:
256                 print '    g_pLog->BeginReturn("%s");' % method.type
257                 method.type.dump("result")
258                 print '    g_pLog->EndReturn();'
259                 method.type.wrap_instance('result')
260             print '    g_pLog->EndCall();'
261             if method.name == 'QueryInterface':
262                 print '    if(*ppvObj == m_pInstance)'
263                 print '        *ppvObj = this;'
264             if method.name == 'Release':
265                 assert method.type is not Void
266                 print '    if(!result)'
267                 print '        delete this;'
268             if method.type is not Void:
269                 print '    return result;'
270             print '}'
271             print
272         print
273
274
275 class Method(Function):
276
277     def __init__(self, type, name, args):
278         Function.__init__(self, type, name, args)
279
280
281 towrap = []
282
283 class WrapPointer(Pointer):
284
285     def __init__(self, type):
286         Pointer.__init__(self, type)
287         if type not in towrap:
288             towrap.append(type)
289
290     def wrap_instance(self, instance):
291         print "    if(%s)" % instance
292         print "        %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
293
294     def unwrap_instance(self, instance):
295         print "    if(%s)" % instance
296         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
297
298 String = Intrinsic("char *", "%s")
299 Int = Intrinsic("int", "%i")
300 Long = Intrinsic("long", "%li")
301 Float = Intrinsic("float", "%f")
302
303
304 def wrap():
305     for type in towrap:
306         type.wrap_pre_decl()
307     print
308     for type in towrap:
309         type.wrap_decl()
310     print
311     for type in towrap:
312         type.wrap_impl()
313     print