Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 cpp_util | 7 import cpp_util |
| 8 import schema_util | 8 import schema_util |
| 9 import util_cc_helper | 9 import util_cc_helper |
| 10 | 10 |
| (...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 679 ) | 679 ) |
| 680 if is_ptr and self._generate_error_messages: | 680 if is_ptr and self._generate_error_messages: |
| 681 c.Append('%(dst_var)s.reset();') | 681 c.Append('%(dst_var)s.reset();') |
| 682 else: | 682 else: |
| 683 c.Append('return %(failure_value)s;') | 683 c.Append('return %(failure_value)s;') |
| 684 c.Eblock('}') | 684 c.Eblock('}') |
| 685 c.Sblock('else {') | 685 c.Sblock('else {') |
| 686 item_type = self._type_helper.FollowRef(underlying_type.item_type) | 686 item_type = self._type_helper.FollowRef(underlying_type.item_type) |
| 687 if item_type.property_type == PropertyType.ENUM: | 687 if item_type.property_type == PropertyType.ENUM: |
| 688 c.Concat(self._GenerateListValueToEnumArrayConversion( | 688 c.Concat(self._GenerateListValueToEnumArrayConversion( |
| 689 item_type, | 689 underlying_type.item_type, # Pass ref. |
|
not at google - send to devlin
2014/03/28 18:42:25
what does "Pass ref" mean?
mtomasz
2014/03/31 01:39:20
PropertyType of the passed type is equal to REF. R
| |
| 690 'list', | 690 'list', |
| 691 dst_var, | 691 dst_var, |
| 692 failure_value, | 692 failure_value, |
| 693 is_ptr=is_ptr)) | 693 is_ptr=is_ptr)) |
| 694 else: | 694 else: |
| 695 (c.Sblock('if (!%s) {' % self._util_cc_helper.PopulateArrayFromList( | 695 (c.Sblock('if (!%s) {' % self._util_cc_helper.PopulateArrayFromList( |
| 696 underlying_type, | 696 underlying_type, |
| 697 'list', | 697 'list', |
| 698 dst_var, | 698 dst_var, |
| 699 is_ptr))) | 699 is_ptr))) |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 772 c = Code() | 772 c = Code() |
| 773 accessor = '.' | 773 accessor = '.' |
| 774 if is_ptr: | 774 if is_ptr: |
| 775 accessor = '->' | 775 accessor = '->' |
| 776 cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True) | 776 cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True) |
| 777 c.Append('%s.reset(new std::vector<%s>);' % | 777 c.Append('%s.reset(new std::vector<%s>);' % |
| 778 (dst_var, cpp_util.PadForGenerics(cpp_type))) | 778 (dst_var, cpp_util.PadForGenerics(cpp_type))) |
| 779 (c.Sblock('for (base::ListValue::const_iterator it = %s->begin(); ' | 779 (c.Sblock('for (base::ListValue::const_iterator it = %s->begin(); ' |
| 780 'it != %s->end(); ++it) {' % (src_var, src_var)) | 780 'it != %s->end(); ++it) {' % (src_var, src_var)) |
| 781 .Append('%s tmp;' % self._type_helper.GetCppType(item_type)) | 781 .Append('%s tmp;' % self._type_helper.GetCppType(item_type)) |
| 782 .Concat(self._GenerateStringToEnumConversion(item_type, | 782 .Concat(self._GenerateStringToEnumConversion(item_type, # Pass ref. |
| 783 '(*it)', | 783 '(*it)', |
| 784 'tmp', | 784 'tmp', |
| 785 failure_value)) | 785 failure_value)) |
| 786 .Append('%s%spush_back(tmp);' % (dst_var, accessor)) | 786 .Append('%s%spush_back(tmp);' % (dst_var, accessor)) |
| 787 .Eblock('}') | 787 .Eblock('}') |
| 788 ) | 788 ) |
| 789 return c | 789 return c |
| 790 | 790 |
| 791 def _GenerateStringToEnumConversion(self, | 791 def _GenerateStringToEnumConversion(self, |
| 792 type_, | 792 type_, |
| 793 src_var, | 793 src_var, |
| 794 dst_var, | 794 dst_var, |
| 795 failure_value): | 795 failure_value): |
| 796 """Returns Code that converts a string type in |src_var| to an enum with | 796 """Returns Code that converts a string type in |src_var| to an enum with |
| 797 type |type_| in |dst_var|. In the generated code, if |src_var| is not | 797 type |type_| in |dst_var|. In the generated code, if |src_var| is not |
| 798 a valid enum name then the function will return |failure_value|. | 798 a valid enum name then the function will return |failure_value|. |
| 799 """ | 799 """ |
| 800 c = Code() | 800 c = Code() |
| 801 enum_as_string = '%s_as_string' % type_.unix_name | 801 enum_as_string = '%s_as_string' % type_.unix_name |
| 802 # The passed type may be either of REF type, or directly ENUM. Note, that | |
| 803 # the second case is used only by JSON and it is always in the default | |
| 804 # namespace. | |
|
not at google - send to devlin
2014/03/28 18:42:25
It surprises me that enums need special treatment
mtomasz
2014/03/29 04:22:53
For enums we need to generate C++ code like this:
mtomasz
2014/03/31 01:39:20
I simplified the code. That's correct, that we don
| |
| 805 cpp_type_fullname = self._type_helper.GetCppType(type_) | |
| 806 cpp_type_namespace = None | |
| 807 cpp_type_name = None | |
| 808 # Eg. for other_namespace::EnumType: | |
| 809 # cpp_type_fullname = other_namespace::EnumType | |
| 810 # cpp_type_namespace = other_namespace | |
| 811 # cpp_type_name = EnumType | |
| 812 if type_.property_type == PropertyType.REF: | |
| 813 enum_type = self._type_helper.FollowRef(type_) | |
| 814 cpp_type_namespace = enum_type.namespace.unix_name | |
| 815 cpp_type_name = self._type_helper.GetCppType(enum_type) | |
| 816 else: | |
| 817 cpp_type_name = cpp_type_fullname | |
| 802 (c.Append('std::string %s;' % enum_as_string) | 818 (c.Append('std::string %s;' % enum_as_string) |
| 803 .Sblock('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string)) | 819 .Sblock('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string)) |
| 804 .Concat(self._GenerateError( | 820 .Concat(self._GenerateError( |
| 805 '"\'%%(key)s\': expected string, got " + ' + | 821 '"\'%%(key)s\': expected string, got " + ' + |
| 806 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) | 822 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 807 .Append('return %s;' % failure_value) | 823 .Append('return %s;' % failure_value) |
| 808 .Eblock('}') | 824 .Eblock('}') |
| 809 .Append('%s = Parse%s(%s);' % (dst_var, | 825 .Append('%s = %sParse%s(%s);' % ( |
| 810 self._type_helper.GetCppType(type_), | 826 dst_var, |
| 811 enum_as_string)) | 827 cpp_type_namespace + '::' if cpp_type_namespace else '', |
| 812 .Sblock('if (%s == %s) {' % (dst_var, | 828 cpp_type_name, |
| 813 self._type_helper.GetEnumNoneValue(type_))) | 829 enum_as_string)) |
| 830 .Sblock('if (%s == %s%s) {' % ( | |
| 831 dst_var, | |
| 832 cpp_type_namespace + '::' if cpp_type_namespace else '', | |
| 833 self._type_helper.GetEnumNoneValue(type_))) | |
| 814 .Concat(self._GenerateError( | 834 .Concat(self._GenerateError( |
| 815 '\"\'%%(key)s\': expected \\"' + | 835 '\"\'%%(key)s\': expected \\"' + |
| 816 '\\" or \\"'.join( | 836 '\\" or \\"'.join( |
| 817 enum_value.name | 837 enum_value.name |
| 818 for enum_value in self._type_helper.FollowRef(type_).enum_values) + | 838 for enum_value in self._type_helper.FollowRef(type_).enum_values) + |
| 819 '\\", got \\"" + %s + "\\""' % enum_as_string)) | 839 '\\", got \\"" + %s + "\\""' % enum_as_string)) |
| 820 .Append('return %s;' % failure_value) | 840 .Append('return %s;' % failure_value) |
| 821 .Eblock('}') | 841 .Eblock('}') |
| 822 .Substitute({'src_var': src_var, 'key': type_.name}) | 842 .Substitute({'src_var': src_var, 'key': type_.name}) |
| 823 ) | 843 ) |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 969 if self._generate_error_messages: | 989 if self._generate_error_messages: |
| 970 params = list(params) + ['base::string16* error'] | 990 params = list(params) + ['base::string16* error'] |
| 971 return ', '.join(str(p) for p in params) | 991 return ', '.join(str(p) for p in params) |
| 972 | 992 |
| 973 def _GenerateArgs(self, args): | 993 def _GenerateArgs(self, args): |
| 974 """Builds the argument list for a function, given an array of arguments. | 994 """Builds the argument list for a function, given an array of arguments. |
| 975 """ | 995 """ |
| 976 if self._generate_error_messages: | 996 if self._generate_error_messages: |
| 977 args = list(args) + ['error'] | 997 args = list(args) + ['error'] |
| 978 return ', '.join(str(a) for a in args) | 998 return ', '.join(str(a) for a in args) |
| OLD | NEW |