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
132 self.type.dump("*" + instance)
133 except NotImplementedError:
134 print ' Log::TextF("%%p", %s);' % instance
137 print ' Log::Text("NULL");'
139 def wrap_instance(self, instance):
140 self.type.wrap_instance("*" + instance)
142 def unwrap_instance(self, instance):
143 self.type.wrap_instance("*" + instance)
146 def ConstPointer(type):
147 return Pointer(Const(type))
150 class OutPointer(Pointer):
156 class Enum(Concrete):
158 def __init__(self, name, values):
159 Concrete.__init__(self, name)
162 def _dump(self, instance):
163 print ' switch(%s) {' % instance
164 for value in self.values:
165 print ' case %s:' % value
166 print ' Log::Text("%s");' % value
169 print ' Log::TextF("%%i", %s);' % instance
174 class Flags(Concrete):
178 def __init__(self, type, values):
180 Concrete.__init__(self, type.name + str(Flags.__seq))
185 return str(self.type)
187 def _dump(self, instance):
188 print ' %s l_Value = %s;' % (self.type, instance)
189 for value in self.values:
190 print ' if((l_Value & %s) == %s) {' % (value, value)
191 print ' Log::Text("%s | ");' % value
192 print ' l_Value &= ~%s;' % value
194 self.type.dump("l_Value");
197 class Struct(Concrete):
199 def __init__(self, name, members):
200 Concrete.__init__(self, name)
201 self.members = members
203 def _dump(self, instance):
204 print ' Log::Text("{");'
206 for type, name in self.members:
210 print ' Log::Text(", ");'
211 type.dump('(%s).%s' % (instance, name))
212 print ' Log::Text("}");'
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::BeginParam("this", "%s *");' % self.name
306 print ' Log::TextF("%p", m_pInstance);'
307 print ' Log::EndParam();'
308 for type, name in method.args:
309 if not type.isoutput():
310 type.unwrap_instance(name)
311 print ' Log::BeginParam("%s", "%s");' % (name, type)
313 print ' Log::EndParam();'
314 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
315 for type, name in method.args:
317 print ' Log::BeginParam("%s", "%s");' % (name, type)
319 print ' Log::EndParam();'
320 type.wrap_instance(name)
321 if method.type is not Void:
322 print ' Log::BeginReturn("%s");' % method.type
323 method.type.dump("result")
324 print ' Log::EndReturn();'
325 method.type.wrap_instance('result')
326 print ' Log::EndCall();'
327 if method.name == 'QueryInterface':
328 print ' if(*ppvObj == m_pInstance)'
329 print ' *ppvObj = this;'
330 if method.name == 'Release':
331 assert method.type is not Void
333 print ' delete this;'
334 if method.type is not Void:
335 print ' return result;'
341 class Method(Function):
343 def __init__(self, type, name, args):
344 Function.__init__(self, type, name, args)
349 class WrapPointer(Pointer):
351 def __init__(self, type):
352 Pointer.__init__(self, type)
353 if type not in towrap:
356 def wrap_instance(self, instance):
357 print " if(%s)" % instance
358 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
360 def unwrap_instance(self, instance):
361 print " if(%s)" % instance
362 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
368 Type.__init__(self, "String")
373 def dump(self, instance):
374 print ' Log::DumpString(%s);' % instance
379 Short = Intrinsic("short", "%i")
380 Int = Intrinsic("int", "%i")
381 Long = Intrinsic("long", "%li")
382 Float = Intrinsic("float", "%f")
386 for type in all_types.itervalues():
389 for type in all_types.itervalues():