1 #############################################################################
3 # Copyright 2008 Tungsten Graphics, Inc.
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.
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.
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/>.
18 #############################################################################
30 def __init__(self, name):
37 return self.name.replace(' ', '_')
48 def dump(self, instance):
49 raise NotImplementedError
51 def wrap_instance(self, instance):
54 def unwrap_instance(self, instance):
61 Type.__init__(self, "void")
68 def __init__(self, name):
70 assert char.isalnum() or char in '_ '
72 Type.__init__(self, name)
74 assert self.name not in all_types
75 if self.name not in all_types:
76 all_types[self.name] = self
79 print 'void Dump%s(const %s &value);' % (self.identifier(), str(self))
82 print 'void Dump%s(const %s &value) {' % (self.identifier(), str(self))
87 def _dump(self, instance):
88 raise NotImplementedError
90 def dump(self, instance):
91 print ' Dump%s(%s);' % (self.identifier(), instance)
94 class Intrinsic(Concrete):
96 def __init__(self, expr, format, name = None):
99 Concrete.__init__(self, name)
103 def _dump(self, instance):
104 print ' Log::TextF("%s", %s);' % (self.format, instance)
112 def __init__(self, type):
113 Type.__init__(self, 'C' + type.name)
116 def dump(self, instance):
117 self.type.dump(instance)
120 return "const " + str(self.type)
125 def __init__(self, type):
126 Type.__init__(self, 'P' + type.name)
130 return str(self.type) + " *"
132 def dump(self, instance):
133 print ' if(%s) {' % instance
134 print ' Log::BeginReference("%s", %s);' % (self.type, instance)
136 self.type.dump("*" + instance)
137 except NotImplementedError:
139 print ' Log::EndReference();'
142 print ' Log::Text("NULL");'
144 def wrap_instance(self, instance):
145 self.type.wrap_instance("*" + instance)
147 def unwrap_instance(self, instance):
148 self.type.wrap_instance("*" + instance)
151 def ConstPointer(type):
152 return Pointer(Const(type))
155 class OutPointer(Pointer):
161 class Enum(Concrete):
163 def __init__(self, name, values):
164 Concrete.__init__(self, name)
167 def _dump(self, instance):
168 print ' switch(%s) {' % instance
169 for value in self.values:
170 print ' case %s:' % value
171 print ' Log::Text("%s");' % value
174 print ' Log::TextF("%%i", %s);' % instance
179 class Flags(Concrete):
183 def __init__(self, type, values):
185 Concrete.__init__(self, type.name + str(Flags.__seq))
190 return str(self.type)
192 def _dump(self, instance):
193 print ' %s l_Value = %s;' % (self.type, instance)
194 for value in self.values:
195 print ' if((l_Value & %s) == %s) {' % (value, value)
196 print ' Log::Text("%s | ");' % value
197 print ' l_Value &= ~%s;' % value
199 self.type.dump("l_Value");
202 class Struct(Concrete):
204 def __init__(self, name, members):
205 Concrete.__init__(self, name)
206 self.members = members
208 def _dump(self, instance):
209 for type, name in self.members:
210 print ' Log::BeginElement("%s", "%s");' % (type, name)
211 type.dump('(%s).%s' % (instance, name))
212 print ' Log::EndElement();'
217 def __init__(self, name, type):
218 Type.__init__(self, name)
221 def dump(self, instance):
222 self.type.dump(instance)
227 def __init__(self, type, name, args, call = '__stdcall'):
233 def prototype(self, name=None):
240 s = self.call + ' ' + s
241 if name.startswith('*'):
243 s = str(self.type) + ' ' + s
246 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
253 class Interface(Type):
255 def __init__(self, name, base=None):
256 Type.__init__(self, name)
260 def itermethods(self):
261 if self.base is not None:
262 for method in self.base.itermethods():
264 for method in self.methods:
269 return "Wrap" + self.name
271 def wrap_pre_decl(self):
272 print "class %s;" % self.wrap_name()
275 print "class %s : public %s " % (self.wrap_name(), self.name)
278 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
279 print " virtual ~%s();" % self.wrap_name()
281 for method in self.itermethods():
282 print " " + method.prototype() + ";"
285 print " %s * m_pInstance;" % (self.name,)
290 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
291 print ' m_pInstance = pInstance;'
294 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
297 for method in self.itermethods():
298 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
299 if method.type is Void:
302 print ' %s result;' % method.type
304 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
305 print ' Log::BeginArg("%s *", "this");' % self.name
306 print ' Log::BeginReference("%s", m_pInstance);' % self.name
307 print ' Log::EndReference();'
308 print ' Log::EndArg();'
309 for type, name in method.args:
310 if not type.isoutput():
311 type.unwrap_instance(name)
312 print ' Log::BeginArg("%s", "%s");' % (type, name)
314 print ' Log::EndArg();'
315 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
316 for type, name in method.args:
318 print ' Log::BeginArg("%s", "%s");' % (type, name)
320 print ' Log::EndArg();'
321 type.wrap_instance(name)
322 if method.type is not Void:
323 print ' Log::BeginReturn("%s");' % method.type
324 method.type.dump("result")
325 print ' Log::EndReturn();'
326 method.type.wrap_instance('result')
327 print ' Log::EndCall();'
328 if method.name == 'QueryInterface':
329 print ' if(*ppvObj == m_pInstance)'
330 print ' *ppvObj = this;'
331 if method.name == 'Release':
332 assert method.type is not Void
334 print ' delete this;'
335 if method.type is not Void:
336 print ' return result;'
342 class Method(Function):
344 def __init__(self, type, name, args):
345 Function.__init__(self, type, name, args)
350 class WrapPointer(Pointer):
352 def __init__(self, type):
353 Pointer.__init__(self, type)
354 if type not in towrap:
357 def wrap_instance(self, instance):
358 print " if(%s)" % instance
359 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
361 def unwrap_instance(self, instance):
362 print " if(%s)" % instance
363 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
369 Type.__init__(self, "String")
374 def dump(self, instance):
375 print ' Log::DumpString(%s);' % instance
380 SChar = Intrinsic("signed char", "%i")
381 UChar = Intrinsic("unsigned char", "%u")
382 Short = Intrinsic("short", "%i")
383 Int = Intrinsic("int", "%i")
384 Long = Intrinsic("long", "%li")
385 UShort = Intrinsic("unsigned short", "%u")
386 UInt = Intrinsic("unsigned int", "%u")
387 ULong = Intrinsic("unsigned long", "%lu")
388 Float = Intrinsic("float", "%f")
389 Double = Intrinsic("double", "%f")
393 for type in all_types.itervalues():
396 for type in all_types.itervalues():