This repository has been archived by the owner on Aug 14, 2021. It is now read-only.
/
gir-cpp
executable file
·433 lines (391 loc) · 13.9 KB
/
gir-cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/python3
import sys
from gi.repository import Zeitgeist as importedgir
import gi
class Typeinfo:
def __init__(self, cstr, cppstr, c2cpp, cpp2c, cfree = ''):
self._cstr = cstr
self._cppstr = cppstr
self._cpp2c = cpp2c
self._c2cpp = c2cpp
self._cfree = cfree
def cstr (self):
return self._cstr
def cppstr (self):
return self._cppstr
def cpp2c (self, varname):
return self._cpp2c % (varname)
def c2cpp (self, varname):
return self._c2cpp % (varname)
def cfree (self, varname):
if self._cfree == '':
return ''
return self._cfree % (varname)
def marker (self):
return self._marker
class TypeUnknown(Typeinfo):
def __init__(self):
self._cstr = 'unknown'
class TypeBool(Typeinfo):
def __init__(self):
self._cstr = 'gboolean'
self._cppstr = 'bool'
self._c2cpp = '(%s == TRUE ? true : false)'
self._cpp2c = '(%s ? TRUE : FALSE)'
self._cfree = ''
class TypeInt(Typeinfo):
def __init__(self):
self._cstr = 'gint32'
self._cppstr = 'int'
self._c2cpp = '%s'
self._cpp2c = '%s'
self._cfree = ''
self._marker = '0'
class TypeUchar(Typeinfo):
def __init__(self):
self._cstr = 'guint8'
self._cppstr = 'unsigned char'
self._c2cpp = '%s'
self._cpp2c = '%s'
self._cfree = ''
self._marker = '0'
class TypeUshort(Typeinfo):
def __init__(self):
self._cstr = 'guint16'
self._cppstr = 'unsigned short'
self._c2cpp = '%s'
self._cpp2c = '%s'
self._cfree = ''
self._marker = '0'
class TypeUint(Typeinfo):
def __init__(self):
self._cstr = 'guint32'
self._cppstr = 'unsigned int'
self._c2cpp = '%s'
self._cpp2c = '%s'
self._cfree = ''
self._marker = '0'
class TypeInt64(Typeinfo):
def __init__(self):
self._cstr = 'gint64'
self._cppstr = 'long'
self._c2cpp = '%s'
self._cpp2c = '%s'
self._cfree = ''
self._marker = '0'
class TypeUint64(Typeinfo):
def __init__(self):
self._cstr = 'guint64'
self._cppstr = 'unsigned long'
self._c2cpp = '%s'
self._cpp2c = '%s'
self._cfree = ''
self._marker = '0'
class TypeDouble(Typeinfo):
def __init__(self):
self._cstr = 'gdouble'
self._cppstr = 'double'
self._c2cpp = '%s'
self._cpp2c = '%s'
self._cfree = ''
self._marker = '0.0f'
class TypeString(Typeinfo):
def __init__(self):
self._cstr = 'gchar *'
self._cppstr = 'std::string'
self._c2cpp = 'std::string(%s)'
self._cpp2c = 'g_strdup(%s.get_cstr())'
self._cfree = 'g_free(%s)'
self._marker = 'NULL'
class TypeInterface(Typeinfo):
def __init__(self, iinfo):
self._cstr = '%s *' % (iinfo.get_type_name())
self._cppstr = 'GirCpp::%s::%s' % (iinfo.get_namespace(), iinfo.get_name())
self._c2cpp = 'GirCpp::%s::%s(%s)' % (iinfo.get_namespace(), iinfo.get_name(), '%s')
self._cpp2c = 'g_object_ref(%s)'
self._cfree = 'g_object_unref(%s)'
self._marker = 'NULL'
class TypeArray(Typeinfo):
def __init__(self, insidetype):
self._insidetype = insidetype
def cstr (self):
cstr = self._insidetype.cstr()
cstr += '*'
return cstr
def cppstr (self):
return 'std::vector<%s>' % (self._insidetype.cppstr())
def c2cpp (self, varname):
cbuilder = ''
cbuilder += '[](%s input) => %s {\n' % (self.cstr(), self.cppstr())
cbuilder += '%s retval();\n' % self.cppstr()
cbuilder += 'int i = 0;\n'
cbuilder += 'for (i = 0; input[i] != %s; i++) {\n' % (self._insidetype.marker())
cbuilder += '%s cppitem = %s;\n' % (self._insidetype.cppstr(), self._insidetype.c2cpp('input[i]'))
cbuilder += 'retval.append(cppitem);\n'
cbuilder += '}\n'
cbuilder += 'return retval;\n'
cbuilder += '}(%s)' % varname
return cbuilder
def cpp2c (self, varname):
cbuilder = ''
cbuilder += '[](%s input) => %s {\n' % (self.cstr(), self.cppstr())
cbuilder += 'GArray * retarray = g_array_new(TRUE, TRUE, sizeof(%s));\n' % (self._insidetype.cstr())
cbuilder += 'std::for_each(input.begin(), input.end(), [retarray](%s item) {\n'
cbuilder += '%s item_cval = %s;\n' % (self._insidetype.cstr(), self._insidetype.cpp2c('item'))
cbuilder += 'g_array_append_val(retarray, item_cval);\n'
cbuilder += '});\n'
cbuilder += 'return (%s)g_array_free(retarray, FALSE);\n' % (self._insidetype.cstr())
cbuilder += '}(%s)' % varname
return cbuilder
def cfree (self, varname):
cbuilder = ''
if self._insidetype.cfree('foo') != '':
cbuilder += '[](%s input) {\n' % (self.cstr())
cbuilder += 'int i = 0;\n'
cbuilder += 'for (i = 0; input[i] != %s; i++) {\n' % (self._insidetype.marker())
cbuilder += self._insidetype.cfree('input[i]')
cbuilder += '\n}\n'
cbuilder += 'g_free(input);\n'
cbuilder += '}(%s)' % varname
else:
cbuilder += 'g_free(%s)' % (varname)
return cbuilder
class TypeGlist(Typeinfo):
def __init__(self, insidetype):
self._insidetype = insidetype
def cstr (self):
return 'GList *'
def cppstr (self):
return 'std::vector<%s>' % (self._insidetype.cppstr())
def c2cpp (self, varname):
cbuilder = ''
cbuilder += '[](%s input) => %s {\n' % (self.cstr(), self.cppstr())
cbuilder += '%s retval();\n' % self.cppstr()
cbuilder += 'GList * i = input;\n'
cbuilder += 'for (; i != nullptr; i = g_list_next(i)) {\n'
cbuilder += '%s item = (%s)i->data;\n' % (self._insidetype.cstr(), self._insidetype.cstr())
cbuilder += '%s cppitem = %s;\n' % (self._insidetype.cppstr(), self._insidetype.c2cpp('item'))
cbuilder += 'retval.append(cppitem);\n'
cbuilder += '}\n'
cbuilder += 'return retval;\n'
cbuilder += '}(%s)' % varname
return cbuilder
def cpp2c (self, varname):
cbuilder = ''
cbuilder += '[](%s input) => %s {\n' % (self.cstr(), self.cppstr())
cbuilder += 'GList * retarray = nullptr;\n'
cbuilder += 'std::for_each(input.begin(), input.end(), [&retarray](%s item) {'
cbuilder += '%s item_cval = %s;' % (self._insidetype.cstr(), self._insidetype.cpp2c('item'))
cbuilder += 'retarray = g_list_prepend(retarray, item_cval);'
cbuilder += '});\n'
cbuilder += 'return g_list_reverse(retarray);\n'
cbuilder += '}(%s)' % varname
return cbuilder
def cfree (self, varname):
cbuilder = ''
if self._insidetype.cfree('foo') != '':
cbuilder += '[](%s input) {\n' % (self.cstr())
cbuilder += 'GList * i = input;\n'
cbuilder += 'for (; i != nullptr; i = g_list_next(i)) {\n'
cbuilder += '%s item = (%s)i->data;\n' % (self._insidetype.cstr(), self._insidetype.cstr())
cbuilder += self._insidetype.cfree('item')
cbuilder += '}\n'
cbuilder += 'g_list_free(input);\n'
cbuilder += '}(%s)' % varname
else:
cbuilder += 'g_list_free(%s);' % (varname)
return cbuilder
class TypeGhash(Typeinfo):
def __init__(self, insidekeytype, insidevaluetype):
self._insidekeytype = insidekeytype
self._insidevaluetype = insidevaluetype
def cstr (self):
return 'GHashTable *'
def cppstr (self):
return 'std::map<%s, %s>' % (self._insidekeytype.cppstr(), self._insidevaluetype.cppstr())
def c2cpp (self, varname):
cbuilder = ''
cbuilder += '[](%s input) => %s {\n' % (self.cstr(), self.cppstr())
cbuilder += '%s retval();\n' % self.cppstr()
cbuilder += 'GHashTableIter iter;\n'
cbuilder += 'gpointer pkey, pvalue;\n\n'
cbuilder += 'g_hash_table_iter_init(&iter, input);\n'
cbuilder += 'while (g_hash_table_iter_next(&iter, &pkey, &pvalue)) {\n'
cbuilder += '%s key_ctype = (%s)pkey;\n' % (self._insidekeytype.cstr(), self._insidekeytype.cstr())
cbuilder += '%s value_ctype = (%s)pvalue;\n\n' % (self._insidevaluetype.cstr(), self._insidevaluetype.cstr())
cbuilder += '%s key_cpptype = %s;\n' % (self._insidekeytype.cppstr(), self._insidekeytype.c2cpp('key_ctype'))
cbuilder += '%s value_cpptype = %s;\n\n' % (self._insidevaluetype.cppstr(), self._insidevaluetype.c2cpp('value_ctype'))
cbuilder += 'retval.append(key_cpptype, value_cpptype);\n'
cbuilder += '}\n'
cbuilder += 'return retval;\n'
cbuilder += '}(%s)' % varname
return cbuilder
def cpp2c (self, varname):
cbuilder = ''
cbuilder += '[](%s input) => %s {\n' % (self.cppstr(), self.cstr())
cbuilder += 'GHashTable * rethash = g_hash_table_new_full(g_direct_hash, g_direct_equal,\n' # TODO add hash equal functions to all the types
cbuilder += '[](gpointer data) -> void {\n%s;\n},\n' % (self._insidekeytype.cfree('(%s)data' % self._insidekeytype.cstr()))
cbuilder += '[](gpointer data) -> void {\n%s;\n});\n' % (self._insidevaluetype.cfree('(%s)data' % self._insidevaluetype.cstr()))
cbuilder += 'std::for_each(input.begin(), input.end(), [rethash](std::pair<%s, %s> item) {\n'
cbuilder += '%s key_cval = %s;\n' % (self._insidekeytype.cstr(), self._insidekeytype.cpp2c('item.first'))
cbuilder += '%s value_cval = %s;\n' % (self._insidevaluetype.cstr(), self._insidevaluetype.cpp2c('item.second'))
cbuilder += 'g_hash_table_insert(rethash, key_cval, value_cval);\n'
cbuilder += '});\n'
cbuilder += 'return rethash;\n'
cbuilder += '}(%s)' % varname
return cbuilder
def cfree (self, varname):
return 'g_hash_table_unref(%s);' % (varname)
def typeParse (typeinfo):
if typeinfo.get_tag_as_string() == 'gboolean':
return TypeBool()
elif typeinfo.get_tag_as_string() == 'guint8':
return TypeUchar()
elif typeinfo.get_tag_as_string() == 'guint16':
return TypeUshort()
elif typeinfo.get_tag_as_string() == 'gint32':
return TypeInt()
elif typeinfo.get_tag_as_string() == 'guint32':
return TypeUint()
elif typeinfo.get_tag_as_string() == 'gint64':
return TypeInt64()
elif typeinfo.get_tag_as_string() == 'guint64':
return TypeUint64()
elif typeinfo.get_tag_as_string() == 'gdouble':
return TypeDouble()
elif typeinfo.get_tag_as_string() == 'utf8':
return TypeString()
elif typeinfo.get_tag_as_string() == 'interface':
iface = typeinfo.get_interface()
if iface.get_name() == 'ObjectPath':
return TypeString()
else:
return TypeInterface(iface)
elif typeinfo.get_tag_as_string() == 'array':
internaltype = typeParse(typeinfo.get_param_type(0))
if internaltype.cstr() == 'unknown':
return TypeUnknown()
return TypeArray(internaltype)
elif typeinfo.get_tag_as_string() == 'glist':
internaltype = typeParse(typeinfo.get_param_type(0))
if internaltype.cstr() == 'unknown':
return TypeUnknown()
return TypeGlist(internaltype)
elif typeinfo.get_tag_as_string() == 'ghash':
keytype = typeParse(typeinfo.get_param_type(0))
valtype = typeParse(typeinfo.get_param_type(1))
if keytype.cstr() == 'unknown' or valtype.cstr() == 'unknown':
return TypeUnknown()
return TypeGhash(keytype, valtype)
elif typeinfo.get_tag_as_string() == 'void':
return TypeUnknown()
else:
print("Unknown: %s" % (typeinfo.get_tag_as_string()))
return TypeUnknown()
def functionParse (func):
returntype = typeParse(func.get_return_type())
if returntype.cstr() == 'unknown' and not func.skip_return():
return ''
functiondef = ''
if not func.skip_return():
functiondef += returntype.cppstr()
else:
functiondef += 'void'
functiondef += ' '
functiondef += func.get_name()
functiondef += ' ( '
arglist = []
for arg in func.get_arguments():
argtype = typeParse (arg.get_type())
if argtype.cstr() == 'unknown':
return ''
argstr = ''
argstr += typeParse(arg.get_type()).cppstr()
argstr += ' '
argstr += arg.get_name()
arglist.append(argstr)
functiondef += ', '.join(arglist)
functiondef += ' ) {\n'
for arg in func.get_arguments():
argtype = typeParse (arg.get_type())
functiondef += argtype.cstr();
functiondef += ' %s_ctype = ' % (arg.get_name())
functiondef += argtype.cpp2c(arg.get_name())
functiondef += ';\n'
functiondef += '\n'
if not func.skip_return():
functiondef += returntype.cstr();
functiondef += ' retval_ctype = ';
functiondef += '%s (' % (func.get_symbol())
arglist = []
if func.is_method():
arglist.append('this')
for arg in func.get_arguments():
arglist.append('%s_ctype' % (arg.get_name()))
functiondef += ', '.join(arglist)
functiondef += ');\n'
if not func.skip_return():
functiondef += returntype.cppstr()
functiondef += ' retval = '
functiondef += returntype.c2cpp('retval_ctype')
functiondef += ';\n'
cfree = returntype.cfree('retval_ctype')
if cfree != '':
functiondef += '%s;\n' % (cfree)
functiondef += '\n'
for arg in func.get_arguments():
argtype = typeParse (arg.get_type())
cfree = argtype.cfree('%s_ctype' % (arg.get_name()))
if cfree != '':
functiondef += '%s;\n' % (cfree)
functiondef += '\n'
if not func.skip_return():
functiondef += 'return retval;\n'
else:
functiondef += 'return;\n'
functiondef += '}\n\n'
return functiondef
def objectParse (obj):
info = obj.__info__
cppout = '';
cppout += 'class %s {\n' % (obj.__name__)
cppout += 'private:\n'
cppout += 'std::shared_ptr<%s> internalObjectPtr;\n' % (info.get_type_name())
cppout += 'public:\n'
cppout += '/* constructors */\n'
cppout += '%s (%s * inobj) {\n' % (obj.__name__, info.get_type_name())
cppout += 'internalObjectPtr = std::make_shared<%s>(g_object_ref_sink(inobj), [](%s * deadobj){ g_clear_object(&deadobj); });' % (info.get_type_name(), info.get_type_name())
cppout += '}\n'
cppout += '%s (std::shared_ptr<%s> inobj) {\n' % (obj.__name__, info.get_type_name())
cppout += 'internalObjectPtr = inobj;\n'
cppout += '}\n'
cppout += '\n/* methods */\n'
for method in info.get_methods():
cppout += functionParse(method)
cppout += '\n/* casting */\n'
cppout += 'operator %s * () {\n' % (info.get_type_name())
cppout += 'return internalObjPtr.get();\n'
cppout += '}\n'
cppout += 'operator std::shared_ptr<%s> () {\n' % (info.get_type_name())
cppout += 'return std::copy(internalObjectPtr);\n'
cppout += '}\n'
cppout += 'operator GObject * () {\n'
cppout += 'return G_OBJECT(internalObjPtr.get());\n'
cppout += '}\n'
cppout += '}\n\n'
return cppout
def namespaceParse (obj):
cppout = ''
for item in obj.__dir__():
if item.startswith('_'):
continue
if str(obj.__getattr__(item).__class__) == "<class 'gi.FunctionInfo'>":
cppout += functionParse(obj.__getattr__(item))
elif str(obj.__getattr__(item).__class__) == "<class 'gi.types.GObjectMeta'>":
cppout += objectParse(obj.__getattr__(item))
return cppout
if __name__ == '__main__':
cpp = 'namespace GirCpp::%s {\n' % (importedgir._namespace)
cpp += namespaceParse(importedgir);
cpp += '}\n'
print(cpp);