Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: tools/json_schema_compiler/cc_generator.py

Issue 10825029: Added JSON schema compiler support for serialized types (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 from code import Code 5 from code import Code
6 from model import PropertyType 6 from model import PropertyType
7 import any_helper 7 import any_helper
8 import cpp_util 8 import cpp_util
9 import model 9 import model
10 import schema_util 10 import schema_util
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 441
442 if self._IsFundamentalOrFundamentalRef(prop): 442 if self._IsFundamentalOrFundamentalRef(prop):
443 if prop.optional: 443 if prop.optional:
444 (c.Append('%(ctype)s temp;') 444 (c.Append('%(ctype)s temp;')
445 .Append('if (!%s)' % 445 .Append('if (!%s)' %
446 cpp_util.GetAsFundamentalValue( 446 cpp_util.GetAsFundamentalValue(
447 self._cpp_type_generator.GetReferencedProperty(prop), 447 self._cpp_type_generator.GetReferencedProperty(prop),
448 value_var, 448 value_var,
449 '&temp')) 449 '&temp'))
450 .Append(' return %(failure_value)s;') 450 .Append(' return %(failure_value)s;')
451 .Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
452 ) 451 )
452 if prop.type_ != prop.compiled_type:
453 (c.Append('%(compiled_ctype)s temp2;')
454 .Append('if (!%s)' %
455 cpp_util.GenerateTypeConversionCall(
456 self._cpp_type_generator.GetReferencedProperty(prop),
457 'temp',
458 '&temp2'))
459 .Append(' return %(failure_value)s;')
460 .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));')
461 )
462 else:
463 c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
464
453 else: 465 else:
466 if prop.type_ != prop.compiled_type:
467 c.Append('%(ctype)s temp;')
454 (c.Append('if (!%s)' % 468 (c.Append('if (!%s)' %
455 cpp_util.GetAsFundamentalValue( 469 cpp_util.GetAsFundamentalValue(
456 self._cpp_type_generator.GetReferencedProperty(prop), 470 self._cpp_type_generator.GetReferencedProperty(prop),
457 value_var, 471 value_var,
458 '&%s->%s' % (dst, prop.unix_name))) 472 '%(fundamental_value_dst)s'))
not at google - send to devlin 2012/07/27 04:14:28 can you inline this value? It's only used once so
mitchellwrosen 2012/07/30 20:52:45 Done.
459 .Append(' return %(failure_value)s;') 473 .Append(' return %(failure_value)s;')
460 ) 474 )
475 if prop.type_ != prop.compiled_type:
476 (c.Append('if (!%s)' %
477 cpp_util.GenerateTypeConversionCall(
478 self._cpp_type_generator.GetReferencedProperty(prop),
479 'temp',
480 '&%s->%s' % (dst, prop.unix_name)))
481 .Append(' return %(failure_value)s;')
482 )
483
461 elif self._IsObjectOrObjectRef(prop): 484 elif self._IsObjectOrObjectRef(prop):
462 if prop.optional: 485 if prop.optional:
463 (c.Append('base::DictionaryValue* dictionary = NULL;') 486 (c.Append('base::DictionaryValue* dictionary = NULL;')
464 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') 487 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
465 .Append(' return %(failure_value)s;') 488 .Append(' return %(failure_value)s;')
466 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') 489 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());')
467 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') 490 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))')
468 .Append(' return %(failure_value)s;') 491 .Append(' return %(failure_value)s;')
469 .Append('%(dst)s->%(name)s = temp.Pass();') 492 .Append('%(dst)s->%(name)s = temp.Pass();')
470 ) 493 )
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 .Append(' binary_value->GetSize());') 559 .Append(' binary_value->GetSize());')
537 ) 560 )
538 else: 561 else:
539 raise NotImplementedError(prop.type_) 562 raise NotImplementedError(prop.type_)
540 c.Eblock('}') 563 c.Eblock('}')
541 sub = { 564 sub = {
542 'value_var': value_var, 565 'value_var': value_var,
543 'name': prop.unix_name, 566 'name': prop.unix_name,
544 'dst': dst, 567 'dst': dst,
545 'failure_value': failure_value, 568 'failure_value': failure_value,
569 'fundamental_value_dst': (
570 '&%s->%s' % (dst, prop.unix_name)
571 if prop.type_ == prop.compiled_type
572 else '&temp')
546 } 573 }
547 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY): 574 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY):
548 sub['ctype'] = self._cpp_type_generator.GetType(prop) 575 sub['ctype'] = self._cpp_type_generator.GetType(prop)
576 sub['compiled_ctype'] = self._cpp_type_generator.GetType(
577 prop, convert_type=True)
549 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator 578 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator
550 .GetReferencedProperty(prop).type_) 579 .GetReferencedProperty(prop).type_)
551 c.Substitute(sub) 580 c.Substitute(sub)
552 return c 581 return c
553 582
554 def _GenerateListValueToEnumArrayConversion(self, c, prop): 583 def _GenerateListValueToEnumArrayConversion(self, c, prop):
555 """Appends code that converts a ListValue of string contstants to 584 """Appends code that converts a ListValue of string contstants to
556 an array of enums in dst. 585 an array of enums in dst.
557 Leaves dst, name, and failure_value unsubstituted. 586 Leaves dst, name, and failure_value unsubstituted.
558 587
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 """ 790 """
762 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == 791 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ ==
763 PropertyType.ARRAY) 792 PropertyType.ARRAY)
764 793
765 def _IsFundamentalOrFundamentalRef(self, prop): 794 def _IsFundamentalOrFundamentalRef(self, prop):
766 """Determines if this property is a Fundamental type or is a ref to a 795 """Determines if this property is a Fundamental type or is a ref to a
767 Fundamental type. 796 Fundamental type.
768 """ 797 """
769 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. 798 return (self._cpp_type_generator.GetReferencedProperty(prop).type_.
770 is_fundamental) 799 is_fundamental)
OLDNEW
« no previous file with comments | « no previous file | tools/json_schema_compiler/cpp_type_generator.py » ('j') | tools/json_schema_compiler/cpp_type_generator.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698