| 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 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 .Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( | 711 .Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( |
| 712 ('*%(src_var)s', 'temp.get()'))) | 712 ('*%(src_var)s', 'temp.get()'))) |
| 713 .Append(' return %(failure_value)s;') | 713 .Append(' return %(failure_value)s;') |
| 714 .Append('%(dst_var)s = temp.Pass();') | 714 .Append('%(dst_var)s = temp.Pass();') |
| 715 ) | 715 ) |
| 716 else: | 716 else: |
| 717 (c.Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( | 717 (c.Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( |
| 718 ('*%(src_var)s', '&%(dst_var)s'))) | 718 ('*%(src_var)s', '&%(dst_var)s'))) |
| 719 .Append(' return %(failure_value)s;')) | 719 .Append(' return %(failure_value)s;')) |
| 720 elif underlying_type.property_type == PropertyType.ENUM: | 720 elif underlying_type.property_type == PropertyType.ENUM: |
| 721 c.Concat(self._GenerateStringToEnumConversion(type_, | 721 c.Concat(self._GenerateStringToEnumConversion(underlying_type, |
| 722 src_var, | 722 src_var, |
| 723 dst_var, | 723 dst_var, |
| 724 failure_value)) | 724 failure_value)) |
| 725 elif underlying_type.property_type == PropertyType.BINARY: | 725 elif underlying_type.property_type == PropertyType.BINARY: |
| 726 (c.Append('const base::BinaryValue* binary_value = NULL;') | 726 (c.Append('const base::BinaryValue* binary_value = NULL;') |
| 727 .Sblock('if (!%(src_var)s->IsType(base::Value::TYPE_BINARY)) {') | 727 .Sblock('if (!%(src_var)s->IsType(base::Value::TYPE_BINARY)) {') |
| 728 .Concat(self._GenerateError( | 728 .Concat(self._GenerateError( |
| 729 '"\'%%(key)s\': expected binary, got " + ' + | 729 '"\'%%(key)s\': expected binary, got " + ' + |
| 730 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) | 730 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 731 ) | 731 ) |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 if type_.property_type != PropertyType.ENUM: |
| 801 raise TypeError(type_) |
| 800 c = Code() | 802 c = Code() |
| 801 enum_as_string = '%s_as_string' % type_.unix_name | 803 enum_as_string = '%s_as_string' % type_.unix_name |
| 804 cpp_type_namespace = '' |
| 805 if type_.namespace != self._namespace: |
| 806 cpp_type_namespace = '%s::' % type_.namespace.unix_name |
| 807 cpp_type_name = self._type_helper.GetCppType(type_) |
| 802 (c.Append('std::string %s;' % enum_as_string) | 808 (c.Append('std::string %s;' % enum_as_string) |
| 803 .Sblock('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string)) | 809 .Sblock('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string)) |
| 804 .Concat(self._GenerateError( | 810 .Concat(self._GenerateError( |
| 805 '"\'%%(key)s\': expected string, got " + ' + | 811 '"\'%%(key)s\': expected string, got " + ' + |
| 806 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) | 812 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 807 .Append('return %s;' % failure_value) | 813 .Append('return %s;' % failure_value) |
| 808 .Eblock('}') | 814 .Eblock('}') |
| 809 .Append('%s = Parse%s(%s);' % (dst_var, | 815 .Append('%s = %sParse%s(%s);' % (dst_var, |
| 810 self._type_helper.GetCppType(type_), | 816 cpp_type_namespace, |
| 811 enum_as_string)) | 817 cpp_util.Classname(type_.name), |
| 812 .Sblock('if (%s == %s) {' % (dst_var, | 818 enum_as_string)) |
| 813 self._type_helper.GetEnumNoneValue(type_))) | 819 .Sblock('if (%s == %s%s) {' % (dst_var, |
| 820 cpp_type_namespace, |
| 821 self._type_helper.GetEnumNoneValue(type_))) |
| 814 .Concat(self._GenerateError( | 822 .Concat(self._GenerateError( |
| 815 '\"\'%%(key)s\': expected \\"' + | 823 '\"\'%%(key)s\': expected \\"' + |
| 816 '\\" or \\"'.join( | 824 '\\" or \\"'.join( |
| 817 enum_value.name | 825 enum_value.name |
| 818 for enum_value in self._type_helper.FollowRef(type_).enum_values) + | 826 for enum_value in self._type_helper.FollowRef(type_).enum_values) + |
| 819 '\\", got \\"" + %s + "\\""' % enum_as_string)) | 827 '\\", got \\"" + %s + "\\""' % enum_as_string)) |
| 820 .Append('return %s;' % failure_value) | 828 .Append('return %s;' % failure_value) |
| 821 .Eblock('}') | 829 .Eblock('}') |
| 822 .Substitute({'src_var': src_var, 'key': type_.name}) | 830 .Substitute({'src_var': src_var, 'key': type_.name}) |
| 823 ) | 831 ) |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 969 if self._generate_error_messages: | 977 if self._generate_error_messages: |
| 970 params = list(params) + ['base::string16* error'] | 978 params = list(params) + ['base::string16* error'] |
| 971 return ', '.join(str(p) for p in params) | 979 return ', '.join(str(p) for p in params) |
| 972 | 980 |
| 973 def _GenerateArgs(self, args): | 981 def _GenerateArgs(self, args): |
| 974 """Builds the argument list for a function, given an array of arguments. | 982 """Builds the argument list for a function, given an array of arguments. |
| 975 """ | 983 """ |
| 976 if self._generate_error_messages: | 984 if self._generate_error_messages: |
| 977 args = list(args) + ['error'] | 985 args = list(args) + ['error'] |
| 978 return ', '.join(str(a) for a in args) | 986 return ', '.join(str(a) for a in args) |
| OLD | NEW |