]> git.cworth.org Git - apitrace/blob - base.py
Specify d3d9 output parameters.
[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 def ConstPointer(type):
101     return Pointer(Const(type))
102
103
104 class OutPointer(Pointer):
105
106     def isoutput(self):
107         return True
108
109
110 class Enum(Type):
111
112     def __init__(self, name, values):
113         Type.__init__(self, name)
114         self.values = values
115     
116     def dump(self, instance):
117         print '    switch(%s) {' % instance
118         for value in self.values:
119             print '    case %s:' % value
120             print '        g_pLog->Text("%s");' % value
121             print '        break;'
122         print '    default:'
123         print '        g_pLog->TextF("%%i", %s);' % instance
124         print '        break;'
125         print '    }'
126
127
128 class Flags(Type):
129
130     def __init__(self, type, values):
131         Type.__init__(self, type.name)
132         self.type = type
133         self.values = values
134
135     def dump(self, instance):
136         print '    {'
137         print '        %s l_Value = %s;' % (self.type, instance)
138         for value in self.values:
139             print '        if((l_Value & %s) == %s) {' % (value, value)
140             print '            g_pLog->Text("%s | ");' % value
141             print '            l_Value &= ~%s;' % value
142             print '        }'
143         self.type.dump("l_Value");
144         print '    }'
145
146
147 class Struct(Type):
148
149     def __init__(self, name, members):
150         Type.__init__(self, name)
151         self.members = members
152
153     def dump(self, instance):
154         print '    g_pLog->Text("{");'
155         first = True
156         for type, name in self.members:
157             if first:
158                 first = False
159             else:
160                 print '    g_pLog->Text(", ");'
161             type.dump('(%s).%s' % (instance, name))
162         print '    g_pLog->Text("}");'
163
164
165 class Alias(Type):
166
167     def __init__(self, name, type):
168         Type.__init__(self, name)
169         self.type = type
170
171     def dump(self, instance):
172         self.type.dump(instance)
173
174
175 class Function:
176
177     def __init__(self, type, name, args, call = '__stdcall'):
178         self.type = type
179         self.name = name
180         self.args = args
181         self.call = call
182
183     def prototype(self, name=None):
184         if name is not None:
185             name = name.strip()
186         else:
187             name = self.name
188         s = name
189         if self.call:
190             s = self.call + ' ' + s
191         if name.startswith('*'):
192             s = '(' + s + ')'
193         s = str(self.type) + ' ' + s
194         s += "("
195         if self.args:
196             s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
197         else:
198             s += "void"
199         s += ")"
200         return s
201
202
203 class Interface(Type):
204
205     def __init__(self, name, base=None):
206         Type.__init__(self, name)
207         self.base = base
208         self.methods = []
209
210     def itermethods(self):
211         if self.base is not None:
212             for method in self.base.itermethods():
213                 yield method
214         for method in self.methods:
215             yield method
216         raise StopIteration
217
218     def wrap_name(self):
219         return "Wrap" + self.name
220
221     def wrap_pre_decl(self):
222         print "class %s;" % self.wrap_name()
223
224     def wrap_decl(self):
225         print "class %s : public %s " % (self.wrap_name(), self.name)
226         print "{"
227         print "public:"
228         print "    %s(%s * pInstance);" % (self.wrap_name(), self.name)
229         print "    virtual ~%s();" % self.wrap_name()
230         print
231         for method in self.itermethods():
232             print "    " + method.prototype() + ";"
233         print
234         #print "private:"
235         print "    %s * m_pInstance;" % (self.name,)
236         print "};"
237         print
238
239     def wrap_impl(self):
240         print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
241         print '    m_pInstance = pInstance;'
242         print '}'
243         print
244         print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
245         print '}'
246         print
247         for method in self.itermethods():
248             print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
249             if method.type is Void:
250                 result = ''
251             else:
252                 print '    %s result;' % method.type
253                 result = 'result = '
254             print '    g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
255             print '    g_pLog->BeginParam("this", "%s *");' % self.name
256             print '    g_pLog->TextF("%p", m_pInstance);'
257             print '    g_pLog->EndParam();'
258             for type, name in method.args:
259                 if not type.isoutput():
260                     type.unwrap_instance(name)
261                     print '    g_pLog->BeginParam("%s", "%s");' % (name, type)
262                     type.dump(name)
263                     print '    g_pLog->EndParam();'
264             print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
265             for type, name in method.args:
266                 if type.isoutput():
267                     print '    g_pLog->BeginParam("%s", "%s");' % (name, type)
268                     type.dump(name)
269                     print '    g_pLog->EndParam();'
270                     type.wrap_instance(name)
271             if method.type is not Void:
272                 print '    g_pLog->BeginReturn("%s");' % method.type
273                 method.type.dump("result")
274                 print '    g_pLog->EndReturn();'
275                 method.type.wrap_instance('result')
276             print '    g_pLog->EndCall();'
277             if method.name == 'QueryInterface':
278                 print '    if(*ppvObj == m_pInstance)'
279                 print '        *ppvObj = this;'
280             if method.name == 'Release':
281                 assert method.type is not Void
282                 print '    if(!result)'
283                 print '        delete this;'
284             if method.type is not Void:
285                 print '    return result;'
286             print '}'
287             print
288         print
289
290
291 class Method(Function):
292
293     def __init__(self, type, name, args):
294         Function.__init__(self, type, name, args)
295
296
297 towrap = []
298
299 class WrapPointer(Pointer):
300
301     def __init__(self, type):
302         Pointer.__init__(self, type)
303         if type not in towrap:
304             towrap.append(type)
305
306     def wrap_instance(self, instance):
307         print "    if(%s)" % instance
308         print "        %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
309
310     def unwrap_instance(self, instance):
311         print "    if(%s)" % instance
312         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
313
314 String = Intrinsic("char *", "%s")
315 Short = Intrinsic("short", "%i")
316 Int = Intrinsic("int", "%i")
317 Long = Intrinsic("long", "%li")
318 Float = Intrinsic("float", "%f")
319
320
321 def wrap():
322     for type in towrap:
323         type.wrap_pre_decl()
324     print
325     for type in towrap:
326         type.wrap_decl()
327     print
328     for type in towrap:
329         type.wrap_impl()
330     print