blob: ea0034200c47dbce729ce7836301ae8dd437d5bf [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
17
18class Void(Type):
19
20 def __init__(self):
21 Type.__init__(self, "void")
22
23Void = Void()
24
25
26class Intrinsic(Type):
27
28 def __init__(self, name, format):
29 Type.__init__(self, name)
30 self.format = format
31
32
33class 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
43class 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
56class 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
72class Enum(Type):
73
74 def __init__(self, name, values):
75 Type.__init__(self, name)
76 self.values = values
77
78
79class Flags(Type):
80
81 def __init__(self, type, values):
82 Type.__init__(self, type.name)
83 self.values = values
84
85
86class Struct(Type):
87
88 def __init__(self, name, members):
89 Type.__init__(self, name)
90 self.members = members
91
92
93class Alias(Type):
94
95 def __init__(self, name, type):
96 Type.__init__(self, name)
97 self.type = type
98
99
100class 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
128class 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)
José Fonsecad626cf42008-07-07 07:43:16 +0900167 print ' m_pInstance = pInstance;'
168 print '}'
169 print
170 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
José Fonsecad626cf42008-07-07 07:43:16 +0900171 print '}'
172 print
173 for method in self.itermethods():
174 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
175 if method.type is Void:
176 result = ''
177 else:
178 print ' %s result;' % method.type
179 result = 'result = '
José Fonsecad7605892008-07-07 13:09:31 +0900180 print ' g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
José Fonsecad626cf42008-07-07 07:43:16 +0900181 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
José Fonsecad7605892008-07-07 13:09:31 +0900182 print ' g_pLog->EndCall();'
José Fonsecad626cf42008-07-07 07:43:16 +0900183 for type, name in method.args:
184 if type.isoutput():
185 type.wrap_instance(name)
186 if method.type is not Void:
187 method.type.wrap_instance('result')
José Fonsecad626cf42008-07-07 07:43:16 +0900188 if method.name == 'QueryInterface':
189 print ' if(*ppvObj == m_pInstance)'
190 print ' *ppvObj = this;'
191 if method.name == 'Release':
192 assert method.type is not Void
193 print ' if(!result)'
194 print ' delete this;'
195 if method.type is not Void:
196 print ' return result;'
197 print '}'
198 print
199 print
200
201
202class Method(Function):
203
204 def __init__(self, type, name, args):
205 Function.__init__(self, type, name, args)
206
207
208towrap = []
209
210class WrapPointer(Pointer):
211
212 def __init__(self, type):
213 Pointer.__init__(self, type)
214 if type not in towrap:
215 towrap.append(type)
216
217 def wrap_instance(self, instance):
218 print " if(%s)" % instance
219 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
220
221String = Intrinsic("char *", "%s")
222Int = Intrinsic("int", "%i")
223Long = Intrinsic("long", "%li")
224Float = Intrinsic("float", "%f")
225
226
227def wrap():
228 for type in towrap:
229 type.wrap_pre_decl()
230 print
231 for type in towrap:
232 type.wrap_decl()
233 print
234 for type in towrap:
235 type.wrap_impl()
236 print