blob: 889e279f3f081a4bb904d1cd51b3c42d9640a926 [file] [log] [blame]
José Fonsecad626cf42008-07-07 07:43:16 +09001"""C basic types"""
2
3class 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é Fonseca27cd25d2008-07-07 13:44:00 +090017 def unwrap_instance(self, instance):
18 pass
19
José Fonsecad626cf42008-07-07 07:43:16 +090020
21class Void(Type):
22
23 def __init__(self):
24 Type.__init__(self, "void")
25
26Void = Void()
27
28
29class Intrinsic(Type):
30
31 def __init__(self, name, format):
32 Type.__init__(self, name)
33 self.format = format
34
35
36class 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
46class 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é Fonseca27cd25d2008-07-07 13:44:00 +090058 def unwrap_instance(self, instance):
59 self.type.wrap_instance("*" + instance)
60
José Fonsecad626cf42008-07-07 07:43:16 +090061
62class 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é Fonseca27cd25d2008-07-07 13:44:00 +090077 def unwrap_instance(self, instance):
78 self.type.wrap_instance(instance)
79
80def OutPointer(type):
81 return Output(Pointer(type))
82
José Fonsecad626cf42008-07-07 07:43:16 +090083
84class Enum(Type):
85
86 def __init__(self, name, values):
87 Type.__init__(self, name)
88 self.values = values
89
90
91class Flags(Type):
92
93 def __init__(self, type, values):
94 Type.__init__(self, type.name)
95 self.values = values
96
97
98class Struct(Type):
99
100 def __init__(self, name, members):
101 Type.__init__(self, name)
102 self.members = members
103
104
105class Alias(Type):
106
107 def __init__(self, name, type):
108 Type.__init__(self, name)
109 self.type = type
110
111
112class 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
140class 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é Fonseca27cd25d2008-07-07 13:44:00 +0900171 #print "private:"
José Fonsecad626cf42008-07-07 07:43:16 +0900172 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é Fonsecad626cf42008-07-07 07:43:16 +0900178 print ' m_pInstance = pInstance;'
179 print '}'
180 print
181 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
José Fonsecad626cf42008-07-07 07:43:16 +0900182 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é Fonsecad7605892008-07-07 13:09:31 +0900191 print ' g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
José Fonseca27cd25d2008-07-07 13:44:00 +0900192 for type, name in method.args:
193 if not type.isoutput():
194 type.unwrap_instance(name)
José Fonsecad626cf42008-07-07 07:43:16 +0900195 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
José Fonsecad7605892008-07-07 13:09:31 +0900196 print ' g_pLog->EndCall();'
José Fonsecad626cf42008-07-07 07:43:16 +0900197 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é Fonsecad626cf42008-07-07 07:43:16 +0900202 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
216class Method(Function):
217
218 def __init__(self, type, name, args):
219 Function.__init__(self, type, name, args)
220
221
222towrap = []
223
224class 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é Fonseca27cd25d2008-07-07 13:44:00 +0900235 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é Fonsecad626cf42008-07-07 07:43:16 +0900239String = Intrinsic("char *", "%s")
240Int = Intrinsic("int", "%i")
241Long = Intrinsic("long", "%li")
242Float = Intrinsic("float", "%f")
243
244
245def 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