1 #############################################################################
3 # Copyright 2008 Jose Fonseca
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):
45 def dump(self, instance):
46 raise NotImplementedError
48 def wrap_instance(self, instance):
51 def unwrap_instance(self, instance):
58 Type.__init__(self, "void")
65 def __init__(self, name):
67 assert char.isalnum() or char == '_'
69 Type.__init__(self, name)
71 assert self.name not in all_types
72 if self.name not in all_types:
73 all_types[self.name] = self
76 print 'void Dump%s(const %s &value);' % (self.name, str(self))
79 print 'void Dump%s(const %s &value) {' % (self.name, str(self))
84 def _dump(self, instance):
85 raise NotImplementedError
87 def dump(self, instance):
88 print ' Dump%s(%s);' % (self.name, instance)
91 class Intrinsic(Concrete):
93 def __init__(self, expr, format, name = None):
96 Concrete.__init__(self, name)
100 def _dump(self, instance):
101 print ' Log::TextF("%s", %s);' % (self.format, instance)
109 def __init__(self, type):
110 Type.__init__(self, 'C' + type.name)
113 def dump(self, instance):
114 self.type.dump(instance)
117 return "const " + str(self.type)
122 def __init__(self, type):
123 Type.__init__(self, 'P' + type.name)
127 return str(self.type) + " *"
129 def dump(self, instance):
130 print ' if(%s) {' % instance
131 print ' Log::BeginReference("%s", %s);' % (self.type, instance)
133 self.type.dump("*" + instance)
134 except NotImplementedError:
136 print ' Log::EndReference();'
139 print ' Log::Text("NULL");'
141 def wrap_instance(self, instance):
142 self.type.wrap_instance("*" + instance)
144 def unwrap_instance(self, instance):
145 self.type.wrap_instance("*" + instance)
148 def ConstPointer(type):
149 return Pointer(Const(type))
152 class OutPointer(Pointer):
158 class Enum(Concrete):
160 def __init__(self, name, values):
161 Concrete.__init__(self, name)
164 def _dump(self, instance):
165 print ' switch(%s) {' % instance
166 for value in self.values:
167 print ' case %s:' % value
168 print ' Log::Text("%s");' % value
171 print ' Log::TextF("%%i", %s);' % instance
176 class Flags(Concrete):
180 def __init__(self, type, values):
182 Concrete.__init__(self, type.name + str(Flags.__seq))
187 return str(self.type)
189 def _dump(self, instance):
190 print ' %s l_Value = %s;' % (self.type, instance)
191 for value in self.values:
192 print ' if((l_Value & %s) == %s) {' % (value, value)
193 print ' Log::Text("%s | ");' % value
194 print ' l_Value &= ~%s;' % value
196 self.type.dump("l_Value");
199 class Struct(Concrete):
201 def __init__(self, name, members):
202 Concrete.__init__(self, name)
203 self.members = members
205 def _dump(self, instance):
206 for type, name in self.members:
207 print ' Log::BeginElement("%s", "%s");' % (type, name)
208 type.dump('(%s).%s' % (instance, name))
209 print ' Log::EndElement();'
214 def __init__(self, name, type):
215 Type.__init__(self, name)
218 def dump(self, instance):
219 self.type.dump(instance)
224 def __init__(self, type, name, args, call = '__stdcall'):
230 def prototype(self, name=None):
237 s = self.call + ' ' + s
238 if name.startswith('*'):
240 s = str(self.type) + ' ' + s
243 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
250 class Interface(Type):
252 def __init__(self, name, base=None):
253 Type.__init__(self, name)
257 def itermethods(self):
258 if self.base is not None:
259 for method in self.base.itermethods():
261 for method in self.methods:
266 return "Wrap" + self.name
268 def wrap_pre_decl(self):
269 print "class %s;" % self.wrap_name()
272 print "class %s : public %s " % (self.wrap_name(), self.name)
275 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
276 print " virtual ~%s();" % self.wrap_name()
278 for method in self.itermethods():
279 print " " + method.prototype() + ";"
282 print " %s * m_pInstance;" % (self.name,)
287 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
288 print ' m_pInstance = pInstance;'
291 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
294 for method in self.itermethods():
295 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
296 if method.type is Void:
299 print ' %s result;' % method.type
301 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
302 print ' Log::BeginArg("%s *", "this");' % self.name
303 print ' Log::TextF("%p", m_pInstance);'
304 print ' Log::EndArg();'
305 for type, name in method.args:
306 if not type.isoutput():
307 type.unwrap_instance(name)
308 print ' Log::BeginArg("%s", "%s");' % (type, name)
310 print ' Log::EndArg();'
311 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
312 for type, name in method.args:
314 print ' Log::BeginArg("%s", "%s");' % (type, name)
316 print ' Log::EndArg();'
317 type.wrap_instance(name)
318 if method.type is not Void:
319 print ' Log::BeginReturn("%s");' % method.type
320 method.type.dump("result")
321 print ' Log::EndReturn();'
322 method.type.wrap_instance('result')
323 print ' Log::EndCall();'
324 if method.name == 'QueryInterface':
325 print ' if(*ppvObj == m_pInstance)'
326 print ' *ppvObj = this;'
327 if method.name == 'Release':
328 assert method.type is not Void
330 print ' delete this;'
331 if method.type is not Void:
332 print ' return result;'
338 class Method(Function):
340 def __init__(self, type, name, args):
341 Function.__init__(self, type, name, args)
346 class WrapPointer(Pointer):
348 def __init__(self, type):
349 Pointer.__init__(self, type)
350 if type not in towrap:
353 def wrap_instance(self, instance):
354 print " if(%s)" % instance
355 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
357 def unwrap_instance(self, instance):
358 print " if(%s)" % instance
359 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
365 Type.__init__(self, "String")
370 def dump(self, instance):
371 print ' Log::DumpString(%s);' % instance
376 Short = Intrinsic("short", "%i")
377 Int = Intrinsic("int", "%i")
378 Long = Intrinsic("long", "%li")
379 Float = Intrinsic("float", "%f")
383 for type in all_types.itervalues():
386 for type in all_types.itervalues():