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