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)
131 def dump(self, instance):
133 print ' %s l_Value = %s;' % (self.type, instance)
134 for value in self.values:
135 print ' if((l_Value & %s) == %s) {' % (value, value)
136 print ' g_pLog->Text("%s | ");' % value
137 print ' l_Value &= ~%s;' % value
139 self.type.dump("l_Value");
145 def __init__(self, name, members):
146 Type.__init__(self, name)
147 self.members = members
149 def dump(self, instance):
150 print ' g_pLog->Text("{");'
152 for type, name in self.members:
156 print ' g_pLog->Text(", ");'
157 type.dump('(%s).%s' % (instance, name))
158 print ' g_pLog->Text("}");'
163 def __init__(self, name, type):
164 Type.__init__(self, name)
167 def dump(self, instance):
168 self.type.dump(instance)
173 def __init__(self, type, name, args, call = '__stdcall'):
179 def prototype(self, name=None):
186 s = self.call + ' ' + s
187 if name.startswith('*'):
189 s = str(self.type) + ' ' + s
192 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
199 class Interface(Type):
201 def __init__(self, name, base=None):
202 Type.__init__(self, name)
206 def itermethods(self):
207 if self.base is not None:
208 for method in self.base.itermethods():
210 for method in self.methods:
215 return "Wrap" + self.name
217 def wrap_pre_decl(self):
218 print "class %s;" % self.wrap_name()
221 print "class %s : public %s " % (self.wrap_name(), self.name)
224 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
225 print " virtual ~%s();" % self.wrap_name()
227 for method in self.itermethods():
228 print " " + method.prototype() + ";"
231 print " %s * m_pInstance;" % (self.name,)
236 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
237 print ' m_pInstance = pInstance;'
240 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
243 for method in self.itermethods():
244 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
245 if method.type is Void:
248 print ' %s result;' % method.type
250 print ' g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
251 print ' g_pLog->BeginParam("this", "%s *");' % self.name
252 print ' g_pLog->TextF("%p", m_pInstance);'
253 print ' g_pLog->EndParam();'
254 for type, name in method.args:
255 if not type.isoutput():
256 type.unwrap_instance(name)
257 print ' g_pLog->BeginParam("%s", "%s");' % (name, type)
259 print ' g_pLog->EndParam();'
260 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
261 for type, name in method.args:
263 print ' g_pLog->BeginParam("%s", "%s");' % (name, type)
265 print ' g_pLog->EndParam();'
266 type.wrap_instance(name)
267 if method.type is not Void:
268 print ' g_pLog->BeginReturn("%s");' % method.type
269 method.type.dump("result")
270 print ' g_pLog->EndReturn();'
271 method.type.wrap_instance('result')
272 print ' g_pLog->EndCall();'
273 if method.name == 'QueryInterface':
274 print ' if(*ppvObj == m_pInstance)'
275 print ' *ppvObj = this;'
276 if method.name == 'Release':
277 assert method.type is not Void
279 print ' delete this;'
280 if method.type is not Void:
281 print ' return result;'
287 class Method(Function):
289 def __init__(self, type, name, args):
290 Function.__init__(self, type, name, args)
295 class WrapPointer(Pointer):
297 def __init__(self, type):
298 Pointer.__init__(self, type)
299 if type not in towrap:
302 def wrap_instance(self, instance):
303 print " if(%s)" % instance
304 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
306 def unwrap_instance(self, instance):
307 print " if(%s)" % instance
308 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
310 String = Intrinsic("char *", "%s")
311 Short = Intrinsic("short", "%i")
312 Int = Intrinsic("int", "%i")
313 Long = Intrinsic("long", "%li")
314 Float = Intrinsic("float", "%f")