Changeset View
Changeset View
Standalone View
Standalone View
tools/qt-types-gen.py
Show First 20 Lines • Show All 110 Lines • ▼ Show 20 Line(s) | 109 | try: | |||
---|---|---|---|---|---|
111 | self.declfile = opts['--declfile'] | 111 | self.declfile = opts['--declfile'] | ||
112 | self.implfile = opts['--implfile'] | 112 | self.implfile = opts['--implfile'] | ||
113 | self.realinclude = opts['--realinclude'] | 113 | self.realinclude = opts['--realinclude'] | ||
114 | self.prettyinclude = opts.get('--prettyinclude', self.realinclude) | 114 | self.prettyinclude = opts.get('--prettyinclude', self.realinclude) | ||
115 | self.extraincludes = opts.get('--extraincludes', None) | 115 | self.extraincludes = opts.get('--extraincludes', None) | ||
116 | self.must_define = opts.get('--must-define', None) | 116 | self.must_define = opts.get('--must-define', None) | ||
117 | self.visibility = opts.get('--visibility', '') | 117 | self.visibility = opts.get('--visibility', '') | ||
118 | dom = xml.dom.minidom.parse(opts['--specxml']) | 118 | dom = xml.dom.minidom.parse(opts['--specxml']) | ||
119 | except KeyError, k: | 119 | except KeyError as k: | ||
120 | assert False, 'Missing required parameter %s' % k.args[0] | 120 | assert False, 'Missing required parameter %s' % k.args[0] | ||
121 | 121 | | |||
122 | self.decls = [] | 122 | self.decls = [] | ||
123 | self.impls = [] | 123 | self.impls = [] | ||
124 | self.spec = get_by_path(dom, "spec")[0] | 124 | self.spec = get_by_path(dom, "spec")[0] | ||
125 | self.externals = gather_externals(self.spec) | 125 | self.externals = gather_externals(self.spec) | ||
126 | self.custom_lists = gather_custom_lists(self.spec, self.namespace) | 126 | self.custom_lists = gather_custom_lists(self.spec, self.namespace) | ||
127 | self.required_custom = [] | 127 | self.required_custom = [] | ||
▲ Show 20 Lines • Show All 121 Lines • ▼ Show 20 Line(s) | 239 | """) | |||
249 | self.impl("""\ | 249 | self.impl("""\ | ||
250 | } | 250 | } | ||
251 | 251 | | |||
252 | } // namespace %s | 252 | } // namespace %s | ||
253 | """ % self.namespace) | 253 | """ % self.namespace) | ||
254 | 254 | | |||
255 | # Write output to files | 255 | # Write output to files | ||
256 | 256 | | |||
257 | open(self.declfile, 'w').write(''.join(self.decls).encode("utf-8")) | 257 | open(self.declfile, 'wb').write(''.join(self.decls).encode("utf-8")) | ||
258 | open(self.implfile, 'w').write(''.join(self.impls).encode("utf-8")) | 258 | open(self.implfile, 'wb').write(''.join(self.impls).encode("utf-8")) | ||
259 | 259 | | |||
260 | def decl(self, str): | 260 | def decl(self, str): | ||
261 | self.decls.append(str) | 261 | self.decls.append(str) | ||
262 | 262 | | |||
263 | def impl(self, str): | 263 | def impl(self, str): | ||
264 | self.impls.append(str) | 264 | self.impls.append(str) | ||
265 | 265 | | |||
266 | def both(self, str): | 266 | def both(self, str): | ||
Show All 30 Lines | |||||
297 | * | 297 | * | ||
298 | * Generic list type with %s elements. Convertible with | 298 | * Generic list type with %s elements. Convertible with | ||
299 | * %s, but needed to have a discrete type in the Qt type system. | 299 | * %s, but needed to have a discrete type in the Qt type system. | ||
300 | */ | 300 | */ | ||
301 | """ % (val, get_headerfile_cmd(self.realinclude, self.prettyinclude), array_of, real)) | 301 | """ % (val, get_headerfile_cmd(self.realinclude, self.prettyinclude), array_of, real)) | ||
302 | self.decl(self.faketype(val, real)) | 302 | self.decl(self.faketype(val, real)) | ||
303 | self.to_declare.append(self.namespace + '::' + val) | 303 | self.to_declare.append(self.namespace + '::' + val) | ||
304 | 304 | | |||
305 | self.both('%s QDBusArgument& operator<<(QDBusArgument& arg, const %s &list)' % | ||||
306 | (self.visibility, val)) | ||||
307 | self.decl(';\n') | ||||
308 | self.impl(""" | ||||
309 | { | ||||
310 | int id = qMetaTypeId<%s>(); | ||||
311 | arg.beginArray(id); | ||||
312 | for (int i = 0; i < list.count(); ++i) { | ||||
313 | arg << list.at(i); | ||||
314 | } | ||||
315 | arg.endArray(); | ||||
316 | return arg; | ||||
317 | } | ||||
318 | | ||||
319 | """ % (array_of)) | ||||
320 | | ||||
321 | self.both('%s const QDBusArgument& operator>>(const QDBusArgument& arg, %s &list)' % | ||||
322 | (self.visibility, val)) | ||||
323 | self.decl(';\n\n') | ||||
324 | self.impl(""" | ||||
325 | { | ||||
326 | arg.beginArray(); | ||||
327 | list.clear(); | ||||
328 | while (!arg.atEnd()) { | ||||
329 | %s item; | ||||
330 | arg >> item; | ||||
331 | list.append(item); | ||||
332 | } | ||||
333 | arg.endArray(); | ||||
334 | return arg; | ||||
335 | } | ||||
336 | | ||||
337 | """ % (array_of)) | ||||
338 | | ||||
305 | structs = self.spec.getElementsByTagNameNS(NS_TP, 'struct') | 339 | structs = self.spec.getElementsByTagNameNS(NS_TP, 'struct') | ||
306 | mappings = self.spec.getElementsByTagNameNS(NS_TP, 'mapping') | 340 | mappings = self.spec.getElementsByTagNameNS(NS_TP, 'mapping') | ||
307 | exts = self.spec.getElementsByTagNameNS(NS_TP, 'external-type') | 341 | exts = self.spec.getElementsByTagNameNS(NS_TP, 'external-type') | ||
308 | 342 | | |||
309 | for deptype in structs + mappings: | 343 | for deptype in structs + mappings: | ||
310 | info = DepInfo(deptype, self.externals, self.custom_lists) | 344 | info = DepInfo(deptype, self.externals, self.custom_lists) | ||
311 | self.depinfos[info.binding.val] = info | 345 | self.depinfos[info.binding.val] = info | ||
312 | 346 | | |||
313 | leaves = [] | 347 | leaves = [] | ||
314 | next_leaves = [] | 348 | next_leaves = [] | ||
315 | 349 | | |||
316 | for val, depinfo in self.depinfos.iteritems(): | 350 | for val, depinfo in self.depinfos.items(): | ||
317 | leaf = True | 351 | leaf = True | ||
318 | 352 | | |||
319 | for dep in depinfo.deps: | 353 | for dep in depinfo.deps: | ||
320 | if not self.depinfos.has_key(dep): | 354 | if dep not in self.depinfos: | ||
321 | raise UnresolvedDependency(val, dep) | 355 | raise UnresolvedDependency(val, dep) | ||
322 | 356 | | |||
323 | leaf = False | 357 | leaf = False | ||
324 | self.depinfos[dep].revdeps.append(val) | 358 | self.depinfos[dep].revdeps.append(val) | ||
325 | 359 | | |||
326 | if leaf: | 360 | if leaf: | ||
327 | next_leaves.append(val) | 361 | next_leaves.append(val) | ||
328 | 362 | | |||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Line(s) | |||||
395 | { | 429 | { | ||
396 | """ % { | 430 | """ % { | ||
397 | 'name' : depinfo.binding.val, | 431 | 'name' : depinfo.binding.val, | ||
398 | 'headercmd': get_headerfile_cmd(self.realinclude, self.prettyinclude), | 432 | 'headercmd': get_headerfile_cmd(self.realinclude, self.prettyinclude), | ||
399 | 'docstring' : format_docstring(depinfo.el, self.refs), | 433 | 'docstring' : format_docstring(depinfo.el, self.refs), | ||
400 | 'visibility': self.visibility, | 434 | 'visibility': self.visibility, | ||
401 | }) | 435 | }) | ||
402 | 436 | | |||
403 | for i in xrange(members): | 437 | for i in range(members): | ||
404 | self.decl("""\ | 438 | self.decl("""\ | ||
405 | %s\ | 439 | %s\ | ||
406 | %s %s; | 440 | %s %s; | ||
407 | """ % (docstrings[i], bindings[i].val, names[i])) | 441 | """ % (docstrings[i], bindings[i].val, names[i])) | ||
408 | 442 | | |||
409 | self.decl("""\ | 443 | self.decl("""\ | ||
410 | }; | 444 | }; | ||
411 | 445 | | |||
412 | """) | 446 | """) | ||
413 | 447 | | |||
414 | self.both('%s bool operator==(%s v1, %s v2)' % | 448 | self.both('%s bool operator==(%s v1, %s v2)' % | ||
415 | (self.visibility, | 449 | (self.visibility, | ||
416 | depinfo.binding.inarg, | 450 | depinfo.binding.inarg, | ||
417 | depinfo.binding.inarg)) | 451 | depinfo.binding.inarg)) | ||
418 | self.decl(';\n') | 452 | self.decl(';\n') | ||
419 | self.impl(""" | 453 | self.impl(""" | ||
420 | {""") | 454 | {""") | ||
421 | if (bindings[0].val != 'QDBusVariant'): | 455 | if (bindings[0].val != 'QDBusVariant'): | ||
422 | self.impl(""" | 456 | self.impl(""" | ||
423 | return ((v1.%s == v2.%s)""" % (names[0], names[0])) | 457 | return ((v1.%s == v2.%s)""" % (names[0], names[0])) | ||
424 | else: | 458 | else: | ||
425 | self.impl(""" | 459 | self.impl(""" | ||
426 | return ((v1.%s.variant() == v2.%s.variant())""" % (names[0], names[0])) | 460 | return ((v1.%s.variant() == v2.%s.variant())""" % (names[0], names[0])) | ||
427 | for i in xrange(1, members): | 461 | for i in range(1, members): | ||
428 | if (bindings[i].val != 'QDBusVariant'): | 462 | if (bindings[i].val != 'QDBusVariant'): | ||
429 | self.impl(""" | 463 | self.impl(""" | ||
430 | && (v1.%s == v2.%s)""" % (names[i], names[i])) | 464 | && (v1.%s == v2.%s)""" % (names[i], names[i])) | ||
431 | else: | 465 | else: | ||
432 | self.impl(""" | 466 | self.impl(""" | ||
433 | && (v1.%s.variant() == v2.%s.variant())""" % (names[i], names[i])) | 467 | && (v1.%s.variant() == v2.%s.variant())""" % (names[i], names[i])) | ||
434 | self.impl(""" | 468 | self.impl(""" | ||
435 | ); | 469 | ); | ||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Line(s) | |||||
520 | typedef QList<%s> %sList; | 554 | typedef QList<%s> %sList; | ||
521 | 555 | | |||
522 | """ % (get_headerfile_cmd(self.realinclude, self.prettyinclude), list_of, list_of, list_of)) | 556 | """ % (get_headerfile_cmd(self.realinclude, self.prettyinclude), list_of, list_of, list_of)) | ||
523 | 557 | | |||
524 | def faketype(self, fake, real): | 558 | def faketype(self, fake, real): | ||
525 | return """\ | 559 | return """\ | ||
526 | struct %(visibility)s %(fake)s : public %(real)s | 560 | struct %(visibility)s %(fake)s : public %(real)s | ||
527 | { | 561 | { | ||
528 | inline %(fake)s() : %(real)s() {} | 562 | %(fake)s() : %(real)s() {} | ||
529 | inline %(fake)s(const %(real)s& a) : %(real)s(a) {} | 563 | %(fake)s(const %(real)s& a) : %(real)s(a) {} | ||
530 | 564 | | |||
531 | inline %(fake)s& operator=(const %(real)s& a) | 565 | %(fake)s& operator=(const %(real)s& a) | ||
532 | { | 566 | { | ||
533 | *(static_cast<%(real)s*>(this)) = a; | 567 | *(static_cast<%(real)s*>(this)) = a; | ||
534 | return *this; | 568 | return *this; | ||
535 | } | 569 | } | ||
536 | }; | 570 | }; | ||
537 | 571 | | |||
538 | """ % {'fake' : fake, 'real' : real, 'visibility': self.visibility} | 572 | """ % {'fake' : fake, 'real' : real, 'visibility': self.visibility} | ||
539 | 573 | | |||
540 | if __name__ == '__main__': | 574 | if __name__ == '__main__': | ||
541 | options, argv = gnu_getopt(sys.argv[1:], '', | 575 | options, argv = gnu_getopt(sys.argv[1:], '', | ||
542 | ['declfile=', | 576 | ['declfile=', | ||
543 | 'implfile=', | 577 | 'implfile=', | ||
544 | 'realinclude=', | 578 | 'realinclude=', | ||
545 | 'prettyinclude=', | 579 | 'prettyinclude=', | ||
546 | 'extraincludes=', | 580 | 'extraincludes=', | ||
547 | 'must-define=', | 581 | 'must-define=', | ||
548 | 'namespace=', | 582 | 'namespace=', | ||
549 | 'specxml=', | 583 | 'specxml=', | ||
550 | 'visibility=', | 584 | 'visibility=', | ||
551 | ]) | 585 | ]) | ||
552 | 586 | | |||
553 | try: | 587 | try: | ||
554 | Generator(dict(options))() | 588 | Generator(dict(options))() | ||
555 | except BrokenSpecException as e: | 589 | except BrokenSpecException as e: | ||
556 | print >> sys.stderr, 'Your spec is broken, dear developer! %s' % e | 590 | print('Your spec is broken, dear developer! %s' % e, file=sys.stderr) | ||
557 | sys.exit(42) | 591 | sys.exit(42) |