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 #############################################################################
24 def __init__(self, name):
33 def dump(self, instance):
34 raise NotImplementedError
36 def wrap_instance(self, instance):
39 def unwrap_instance(self, instance):
46 Type.__init__(self, "void")
51 class Intrinsic(Type):
53 def __init__(self, name, format):
54 Type.__init__(self, name)
57 def dump(self, instance):
58 print ' g_pLog->TextF("%s", %s);' % (self.format, instance)
63 def __init__(self, type):
64 Type.__init__(self, 'C' + type.name)
67 def dump(self, instance):
68 self.type.dump(instance)
71 return "const " + str(self.type)
76 def __init__(self, type):
77 Type.__init__(self, 'P' + type.name)
81 return str(self.type) + " *"
83 def dump(self, instance):
84 print ' if(%s) {' % instance
86 self.type.dump("*" + instance)
87 except NotImplementedError:
88 print ' g_pLog->TextF("%%p", %s);' % instance
91 print ' g_pLog->Text("NULL");'
93 def wrap_instance(self, instance):
94 self.type.wrap_instance("*" + instance)
96 def unwrap_instance(self, instance):
97 self.type.wrap_instance("*" + instance)
100 class OutPointer(Pointer):
108 def __init__(self, name, values):
109 Type.__init__(self, name)
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
119 print ' g_pLog->TextF("%%i", %s);' % instance
126 def __init__(self, type, values):
127 Type.__init__(self, type.name)
133 def __init__(self, name, members):
134 Type.__init__(self, name)
135 self.members = members
137 def dump(self, instance):
138 print ' g_pLog->Text("{");'
140 for type, name in self.members:
144 print ' g_pLog->Text(", ");'
145 type.dump('(%s).%s' % (instance, name))
146 print ' g_pLog->Text("}");'
151 def __init__(self, name, type):
152 Type.__init__(self, name)
155 def dump(self, instance):
156 self.type.dump(instance)
161 def __init__(self, type, name, args, call = '__stdcall'):
167 def prototype(self, name=None):
174 s = self.call + ' ' + s
175 if name.startswith('*'):
177 s = str(self.type) + ' ' + s
180 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
187 class Interface(Type):
189 def __init__(self, name, base=None):
190 Type.__init__(self, name)
194 def itermethods(self):
195 if self.base is not None:
196 for method in self.base.itermethods():
198 for method in self.methods:
203 return "Wrap" + self.name
205 def wrap_pre_decl(self):
206 print "class %s;" % self.wrap_name()
209 print "class %s : public %s " % (self.wrap_name(), self.name)
212 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
213 print " virtual ~%s();" % self.wrap_name()
215 for method in self.itermethods():
216 print " " + method.prototype() + ";"
219 print " %s * m_pInstance;" % (self.name,)
224 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
225 print ' m_pInstance = pInstance;'
228 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
231 for method in self.itermethods():
232 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
233 if method.type is Void:
236 print ' %s result;' % method.type
238 print ' g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
239 print ' g_pLog->BeginParam("this", "%s *");' % self.name
240 print ' g_pLog->TextF("%p", m_pInstance);'
241 print ' g_pLog->EndParam();'
242 for type, name in method.args:
243 if not type.isoutput():
244 type.unwrap_instance(name)
245 print ' g_pLog->BeginParam("%s", "%s");' % (name, type)
247 print ' g_pLog->EndParam();'
248 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
249 for type, name in method.args:
251 print ' g_pLog->BeginParam("%s", "%s");' % (name, type)
253 print ' g_pLog->EndParam();'
254 type.wrap_instance(name)
255 if method.type is not Void:
256 print ' g_pLog->BeginReturn("%s");' % method.type
257 method.type.dump("result")
258 print ' g_pLog->EndReturn();'
259 method.type.wrap_instance('result')
260 print ' g_pLog->EndCall();'
261 if method.name == 'QueryInterface':
262 print ' if(*ppvObj == m_pInstance)'
263 print ' *ppvObj = this;'
264 if method.name == 'Release':
265 assert method.type is not Void
267 print ' delete this;'
268 if method.type is not Void:
269 print ' return result;'
275 class Method(Function):
277 def __init__(self, type, name, args):
278 Function.__init__(self, type, name, args)
283 class WrapPointer(Pointer):
285 def __init__(self, type):
286 Pointer.__init__(self, type)
287 if type not in towrap:
290 def wrap_instance(self, instance):
291 print " if(%s)" % instance
292 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
294 def unwrap_instance(self, instance):
295 print " if(%s)" % instance
296 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
298 String = Intrinsic("char *", "%s")
299 Int = Intrinsic("int", "%i")
300 Long = Intrinsic("long", "%li")
301 Float = Intrinsic("float", "%f")