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 def ConstPointer(type):
101 return Pointer(Const(type))
104 class OutPointer(Pointer):
112 def __init__(self, name, values):
113 Type.__init__(self, name)
116 def dump(self, instance):
117 print ' switch(%s) {' % instance
118 for value in self.values:
119 print ' case %s:' % value
120 print ' g_pLog->Text("%s");' % value
123 print ' g_pLog->TextF("%%i", %s);' % instance
130 def __init__(self, type, values):
131 Type.__init__(self, type.name)
135 def dump(self, instance):
137 print ' %s l_Value = %s;' % (self.type, instance)
138 for value in self.values:
139 print ' if((l_Value & %s) == %s) {' % (value, value)
140 print ' g_pLog->Text("%s | ");' % value
141 print ' l_Value &= ~%s;' % value
143 self.type.dump("l_Value");
149 def __init__(self, name, members):
150 Type.__init__(self, name)
151 self.members = members
153 def dump(self, instance):
154 print ' g_pLog->Text("{");'
156 for type, name in self.members:
160 print ' g_pLog->Text(", ");'
161 type.dump('(%s).%s' % (instance, name))
162 print ' g_pLog->Text("}");'
167 def __init__(self, name, type):
168 Type.__init__(self, name)
171 def dump(self, instance):
172 self.type.dump(instance)
177 def __init__(self, type, name, args, call = '__stdcall'):
183 def prototype(self, name=None):
190 s = self.call + ' ' + s
191 if name.startswith('*'):
193 s = str(self.type) + ' ' + s
196 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
203 class Interface(Type):
205 def __init__(self, name, base=None):
206 Type.__init__(self, name)
210 def itermethods(self):
211 if self.base is not None:
212 for method in self.base.itermethods():
214 for method in self.methods:
219 return "Wrap" + self.name
221 def wrap_pre_decl(self):
222 print "class %s;" % self.wrap_name()
225 print "class %s : public %s " % (self.wrap_name(), self.name)
228 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
229 print " virtual ~%s();" % self.wrap_name()
231 for method in self.itermethods():
232 print " " + method.prototype() + ";"
235 print " %s * m_pInstance;" % (self.name,)
240 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
241 print ' m_pInstance = pInstance;'
244 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
247 for method in self.itermethods():
248 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
249 if method.type is Void:
252 print ' %s result;' % method.type
254 print ' g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
255 print ' g_pLog->BeginParam("this", "%s *");' % self.name
256 print ' g_pLog->TextF("%p", m_pInstance);'
257 print ' g_pLog->EndParam();'
258 for type, name in method.args:
259 if not type.isoutput():
260 type.unwrap_instance(name)
261 print ' g_pLog->BeginParam("%s", "%s");' % (name, type)
263 print ' g_pLog->EndParam();'
264 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
265 for type, name in method.args:
267 print ' g_pLog->BeginParam("%s", "%s");' % (name, type)
269 print ' g_pLog->EndParam();'
270 type.wrap_instance(name)
271 if method.type is not Void:
272 print ' g_pLog->BeginReturn("%s");' % method.type
273 method.type.dump("result")
274 print ' g_pLog->EndReturn();'
275 method.type.wrap_instance('result')
276 print ' g_pLog->EndCall();'
277 if method.name == 'QueryInterface':
278 print ' if(*ppvObj == m_pInstance)'
279 print ' *ppvObj = this;'
280 if method.name == 'Release':
281 assert method.type is not Void
283 print ' delete this;'
284 if method.type is not Void:
285 print ' return result;'
291 class Method(Function):
293 def __init__(self, type, name, args):
294 Function.__init__(self, type, name, args)
299 class WrapPointer(Pointer):
301 def __init__(self, type):
302 Pointer.__init__(self, type)
303 if type not in towrap:
306 def wrap_instance(self, instance):
307 print " if(%s)" % instance
308 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
310 def unwrap_instance(self, instance):
311 print " if(%s)" % instance
312 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
314 String = Intrinsic("char *", "%s")
315 Short = Intrinsic("short", "%i")
316 Int = Intrinsic("int", "%i")
317 Long = Intrinsic("long", "%li")
318 Float = Intrinsic("float", "%f")