José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame^] | 1 | """C basic types""" |
| 2 | |
| 3 | class Type: |
| 4 | |
| 5 | def __init__(self, name): |
| 6 | self.name = name |
| 7 | |
| 8 | def __str__(self): |
| 9 | return self.name |
| 10 | |
| 11 | def isoutput(self): |
| 12 | return False |
| 13 | |
| 14 | def wrap_instance(self, instance): |
| 15 | pass |
| 16 | |
| 17 | |
| 18 | class Void(Type): |
| 19 | |
| 20 | def __init__(self): |
| 21 | Type.__init__(self, "void") |
| 22 | |
| 23 | Void = Void() |
| 24 | |
| 25 | |
| 26 | class Intrinsic(Type): |
| 27 | |
| 28 | def __init__(self, name, format): |
| 29 | Type.__init__(self, name) |
| 30 | self.format = format |
| 31 | |
| 32 | |
| 33 | class Const(Type): |
| 34 | |
| 35 | def __init__(self, type): |
| 36 | Type.__init__(self, 'C' + type.name) |
| 37 | self.type = type |
| 38 | |
| 39 | def __str__(self): |
| 40 | return "const " + str(self.type) |
| 41 | |
| 42 | |
| 43 | class Pointer(Type): |
| 44 | |
| 45 | def __init__(self, type): |
| 46 | Type.__init__(self, 'P' + type.name) |
| 47 | self.type = type |
| 48 | |
| 49 | def __str__(self): |
| 50 | return str(self.type) + " *" |
| 51 | |
| 52 | def wrap_instance(self, instance): |
| 53 | self.type.wrap_instance("*" + instance) |
| 54 | |
| 55 | |
| 56 | class Output(Type): |
| 57 | |
| 58 | def __init__(self, type): |
| 59 | Type.__init__(self, type.name) |
| 60 | self.type = type |
| 61 | |
| 62 | def __str__(self): |
| 63 | return str(self.type) |
| 64 | |
| 65 | def isoutput(self): |
| 66 | return True |
| 67 | |
| 68 | def wrap_instance(self, instance): |
| 69 | self.type.wrap_instance(instance) |
| 70 | |
| 71 | |
| 72 | class Enum(Type): |
| 73 | |
| 74 | def __init__(self, name, values): |
| 75 | Type.__init__(self, name) |
| 76 | self.values = values |
| 77 | |
| 78 | |
| 79 | class Flags(Type): |
| 80 | |
| 81 | def __init__(self, type, values): |
| 82 | Type.__init__(self, type.name) |
| 83 | self.values = values |
| 84 | |
| 85 | |
| 86 | class Struct(Type): |
| 87 | |
| 88 | def __init__(self, name, members): |
| 89 | Type.__init__(self, name) |
| 90 | self.members = members |
| 91 | |
| 92 | |
| 93 | class Alias(Type): |
| 94 | |
| 95 | def __init__(self, name, type): |
| 96 | Type.__init__(self, name) |
| 97 | self.type = type |
| 98 | |
| 99 | |
| 100 | class Function: |
| 101 | |
| 102 | def __init__(self, type, name, args, call = '__stdcall'): |
| 103 | self.type = type |
| 104 | self.name = name |
| 105 | self.args = args |
| 106 | self.call = call |
| 107 | |
| 108 | def prototype(self, name=None): |
| 109 | if name is not None: |
| 110 | name = name.strip() |
| 111 | else: |
| 112 | name = self.name |
| 113 | s = name |
| 114 | if self.call: |
| 115 | s = self.call + ' ' + s |
| 116 | if name.startswith('*'): |
| 117 | s = '(' + s + ')' |
| 118 | s = str(self.type) + ' ' + s |
| 119 | s += "(" |
| 120 | if self.args: |
| 121 | s += ", ".join(["%s %s" % (type, name) for type, name in self.args]) |
| 122 | else: |
| 123 | s += "void" |
| 124 | s += ")" |
| 125 | return s |
| 126 | |
| 127 | |
| 128 | class Interface(Type): |
| 129 | |
| 130 | def __init__(self, name, base=None): |
| 131 | Type.__init__(self, name) |
| 132 | self.base = base |
| 133 | self.methods = [] |
| 134 | |
| 135 | def itermethods(self): |
| 136 | if self.base is not None: |
| 137 | for method in self.base.itermethods(): |
| 138 | yield method |
| 139 | for method in self.methods: |
| 140 | yield method |
| 141 | raise StopIteration |
| 142 | |
| 143 | def wrap_name(self): |
| 144 | return "Wrap" + self.name |
| 145 | |
| 146 | def wrap_pre_decl(self): |
| 147 | print "class %s;" % self.wrap_name() |
| 148 | |
| 149 | def wrap_decl(self): |
| 150 | print "class %s : public %s " % (self.wrap_name(), self.name) |
| 151 | print "{" |
| 152 | print "public:" |
| 153 | print " %s(%s * pInstance);" % (self.wrap_name(), self.name) |
| 154 | print " virtual ~%s();" % self.wrap_name() |
| 155 | print |
| 156 | for method in self.itermethods(): |
| 157 | print " " + method.prototype() + ";" |
| 158 | print |
| 159 | print "private:" |
| 160 | print " Log *m_pLog;" |
| 161 | print " %s * m_pInstance;" % (self.name,) |
| 162 | print "};" |
| 163 | print |
| 164 | |
| 165 | def wrap_impl(self): |
| 166 | print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name) |
| 167 | print ' m_pLog = new Log("d3d8trace");' |
| 168 | print ' m_pInstance = pInstance;' |
| 169 | print '}' |
| 170 | print |
| 171 | print '%s::~%s() {' % (self.wrap_name(), self.wrap_name()) |
| 172 | print " delete m_pLog;" |
| 173 | print '}' |
| 174 | print |
| 175 | for method in self.itermethods(): |
| 176 | print method.prototype(self.wrap_name() + '::' + method.name) + ' {' |
| 177 | if method.type is Void: |
| 178 | result = '' |
| 179 | else: |
| 180 | print ' %s result;' % method.type |
| 181 | result = 'result = ' |
| 182 | print ' m_pLog->BeginCall("%s");' % (self.name + '::' + method.name) |
| 183 | print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args])) |
| 184 | for type, name in method.args: |
| 185 | if type.isoutput(): |
| 186 | type.wrap_instance(name) |
| 187 | if method.type is not Void: |
| 188 | method.type.wrap_instance('result') |
| 189 | print ' m_pLog->EndCall();' |
| 190 | if method.name == 'QueryInterface': |
| 191 | print ' if(*ppvObj == m_pInstance)' |
| 192 | print ' *ppvObj = this;' |
| 193 | if method.name == 'Release': |
| 194 | assert method.type is not Void |
| 195 | print ' if(!result)' |
| 196 | print ' delete this;' |
| 197 | if method.type is not Void: |
| 198 | print ' return result;' |
| 199 | print '}' |
| 200 | print |
| 201 | print |
| 202 | |
| 203 | |
| 204 | class Method(Function): |
| 205 | |
| 206 | def __init__(self, type, name, args): |
| 207 | Function.__init__(self, type, name, args) |
| 208 | |
| 209 | |
| 210 | towrap = [] |
| 211 | |
| 212 | class WrapPointer(Pointer): |
| 213 | |
| 214 | def __init__(self, type): |
| 215 | Pointer.__init__(self, type) |
| 216 | if type not in towrap: |
| 217 | towrap.append(type) |
| 218 | |
| 219 | def wrap_instance(self, instance): |
| 220 | print " if(%s)" % instance |
| 221 | print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance) |
| 222 | |
| 223 | String = Intrinsic("char *", "%s") |
| 224 | Int = Intrinsic("int", "%i") |
| 225 | Long = Intrinsic("long", "%li") |
| 226 | Float = Intrinsic("float", "%f") |
| 227 | |
| 228 | |
| 229 | def wrap(): |
| 230 | for type in towrap: |
| 231 | type.wrap_pre_decl() |
| 232 | print |
| 233 | for type in towrap: |
| 234 | type.wrap_decl() |
| 235 | print |
| 236 | for type in towrap: |
| 237 | type.wrap_impl() |
| 238 | print |