+#############################################################################
+#
+# Copyright 2008 Jose Fonseca
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#############################################################################
+
"""C basic types"""
class Type:
def isoutput(self):
return False
+ def dump(self, instance):
+ raise NotImplementedError
+
def wrap_instance(self, instance):
pass
+ def unwrap_instance(self, instance):
+ pass
+
class Void(Type):
Type.__init__(self, name)
self.format = format
+ def dump(self, instance):
+ print ' Log::TextF("%s", %s);' % (self.format, instance)
+
class Const(Type):
Type.__init__(self, 'C' + type.name)
self.type = type
+ def dump(self, instance):
+ self.type.dump(instance)
+
def __str__(self):
return "const " + str(self.type)
def __str__(self):
return str(self.type) + " *"
+
+ def dump(self, instance):
+ print ' if(%s) {' % instance
+ try:
+ self.type.dump("*" + instance)
+ except NotImplementedError:
+ print ' Log::TextF("%%p", %s);' % instance
+ print ' }'
+ print ' else'
+ print ' Log::Text("NULL");'
def wrap_instance(self, instance):
self.type.wrap_instance("*" + instance)
+ def unwrap_instance(self, instance):
+ self.type.wrap_instance("*" + instance)
-class Output(Type):
- def __init__(self, type):
- Type.__init__(self, type.name)
- self.type = type
+def ConstPointer(type):
+ return Pointer(Const(type))
- def __str__(self):
- return str(self.type)
+
+class OutPointer(Pointer):
def isoutput(self):
return True
- def wrap_instance(self, instance):
- self.type.wrap_instance(instance)
-
class Enum(Type):
def __init__(self, name, values):
Type.__init__(self, name)
self.values = values
+
+ def dump(self, instance):
+ print ' switch(%s) {' % instance
+ for value in self.values:
+ print ' case %s:' % value
+ print ' Log::Text("%s");' % value
+ print ' break;'
+ print ' default:'
+ print ' Log::TextF("%%i", %s);' % instance
+ print ' break;'
+ print ' }'
class Flags(Type):
def __init__(self, type, values):
Type.__init__(self, type.name)
+ self.type = type
self.values = values
+ def dump(self, instance):
+ print ' {'
+ print ' %s l_Value = %s;' % (self.type, instance)
+ for value in self.values:
+ print ' if((l_Value & %s) == %s) {' % (value, value)
+ print ' Log::Text("%s | ");' % value
+ print ' l_Value &= ~%s;' % value
+ print ' }'
+ self.type.dump("l_Value");
+ print ' }'
+
class Struct(Type):
Type.__init__(self, name)
self.members = members
+ def dump(self, instance):
+ print ' Log::Text("{");'
+ first = True
+ for type, name in self.members:
+ if first:
+ first = False
+ else:
+ print ' Log::Text(", ");'
+ type.dump('(%s).%s' % (instance, name))
+ print ' Log::Text("}");'
+
class Alias(Type):
Type.__init__(self, name)
self.type = type
+ def dump(self, instance):
+ self.type.dump(instance)
+
class Function:
for method in self.itermethods():
print " " + method.prototype() + ";"
print
- print "private:"
- print " Log *m_pLog;"
+ #print "private:"
print " %s * m_pInstance;" % (self.name,)
print "};"
print
else:
print ' %s result;' % method.type
result = 'result = '
- print ' g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
+ print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
+ print ' Log::BeginParam("this", "%s *");' % self.name
+ print ' Log::TextF("%p", m_pInstance);'
+ print ' Log::EndParam();'
+ for type, name in method.args:
+ if not type.isoutput():
+ type.unwrap_instance(name)
+ print ' Log::BeginParam("%s", "%s");' % (name, type)
+ type.dump(name)
+ print ' Log::EndParam();'
print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
- print ' g_pLog->EndCall();'
for type, name in method.args:
if type.isoutput():
+ print ' Log::BeginParam("%s", "%s");' % (name, type)
+ type.dump(name)
+ print ' Log::EndParam();'
type.wrap_instance(name)
if method.type is not Void:
+ print ' Log::BeginReturn("%s");' % method.type
+ method.type.dump("result")
+ print ' Log::EndReturn();'
method.type.wrap_instance('result')
+ print ' Log::EndCall();'
if method.name == 'QueryInterface':
print ' if(*ppvObj == m_pInstance)'
print ' *ppvObj = this;'
print " if(%s)" % instance
print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
+ def unwrap_instance(self, instance):
+ print " if(%s)" % instance
+ print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
+
String = Intrinsic("char *", "%s")
+Short = Intrinsic("short", "%i")
Int = Intrinsic("int", "%i")
Long = Intrinsic("long", "%li")
Float = Intrinsic("float", "%f")