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