| 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 from cpp_namespace_environment import CppNamespaceEnvironment | 10 from cpp_namespace_environment import CppNamespaceEnvironment |
| (...skipping 928 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 939 if is_ptr: | 939 if is_ptr: |
| 940 accessor = '->' | 940 accessor = '->' |
| 941 cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True) | 941 cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True) |
| 942 c.Append('%s.reset(new std::vector<%s>);' % | 942 c.Append('%s.reset(new std::vector<%s>);' % |
| 943 (dst_var, cpp_type)) | 943 (dst_var, cpp_type)) |
| 944 (c.Sblock('for (const auto& it : *(%s)) {' % src_var) | 944 (c.Sblock('for (const auto& it : *(%s)) {' % src_var) |
| 945 .Append('%s tmp;' % self._type_helper.GetCppType(item_type)) | 945 .Append('%s tmp;' % self._type_helper.GetCppType(item_type)) |
| 946 .Concat(self._GenerateStringToEnumConversion(item_type, | 946 .Concat(self._GenerateStringToEnumConversion(item_type, |
| 947 '(it)', | 947 '(it)', |
| 948 'tmp', | 948 'tmp', |
| 949 failure_value)) | 949 failure_value, |
| 950 is_ptr=False)) |
| 950 .Append('%s%spush_back(tmp);' % (dst_var, accessor)) | 951 .Append('%s%spush_back(tmp);' % (dst_var, accessor)) |
| 951 .Eblock('}') | 952 .Eblock('}') |
| 952 ) | 953 ) |
| 953 return c | 954 return c |
| 954 | 955 |
| 955 def _GenerateStringToEnumConversion(self, | 956 def _GenerateStringToEnumConversion(self, |
| 956 type_, | 957 type_, |
| 957 src_var, | 958 src_var, |
| 958 dst_var, | 959 dst_var, |
| 959 failure_value): | 960 failure_value, |
| 961 is_ptr=True): |
| 960 """Returns Code that converts a string type in |src_var| to an enum with | 962 """Returns Code that converts a string type in |src_var| to an enum with |
| 961 type |type_| in |dst_var|. In the generated code, if |src_var| is not | 963 type |type_| in |dst_var|. In the generated code, if |src_var| is not |
| 962 a valid enum name then the function will return |failure_value|. | 964 a valid enum name then the function will return |failure_value|. |
| 963 """ | 965 """ |
| 964 if type_.property_type != PropertyType.ENUM: | 966 if type_.property_type != PropertyType.ENUM: |
| 965 raise TypeError(type_) | 967 raise TypeError(type_) |
| 966 c = Code() | 968 c = Code() |
| 967 enum_as_string = '%s_as_string' % type_.unix_name | 969 enum_as_string = '%s_as_string' % type_.unix_name |
| 968 cpp_type_namespace = '' | 970 cpp_type_namespace = '' |
| 969 if type_.namespace != self._namespace: | 971 if type_.namespace != self._namespace: |
| 970 cpp_type_namespace = '%s::' % type_.namespace.unix_name | 972 cpp_type_namespace = '%s::' % type_.namespace.unix_name |
| 973 accessor = '->' if is_ptr else '.' |
| 971 (c.Append('std::string %s;' % enum_as_string) | 974 (c.Append('std::string %s;' % enum_as_string) |
| 972 .Sblock('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string)) | 975 .Sblock('if (!%s%sGetAsString(&%s)) {' % (src_var, |
| 976 accessor, |
| 977 enum_as_string)) |
| 973 .Concat(self._GenerateError( | 978 .Concat(self._GenerateError( |
| 974 '"\'%%(key)s\': expected string, got " + ' + | 979 '"\'%%(key)s\': expected string, got " + ' + |
| 975 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) | 980 self._util_cc_helper.GetValueTypeString('%%(src_var)s', is_ptr))) |
| 976 .Append('return %s;' % failure_value) | 981 .Append('return %s;' % failure_value) |
| 977 .Eblock('}') | 982 .Eblock('}') |
| 978 .Append('%s = %sParse%s(%s);' % (dst_var, | 983 .Append('%s = %sParse%s(%s);' % (dst_var, |
| 979 cpp_type_namespace, | 984 cpp_type_namespace, |
| 980 cpp_util.Classname(type_.name), | 985 cpp_util.Classname(type_.name), |
| 981 enum_as_string)) | 986 enum_as_string)) |
| 982 .Sblock('if (%s == %s%s) {' % (dst_var, | 987 .Sblock('if (%s == %s%s) {' % (dst_var, |
| 983 cpp_type_namespace, | 988 cpp_type_namespace, |
| 984 self._type_helper.GetEnumNoneValue(type_))) | 989 self._type_helper.GetEnumNoneValue(type_))) |
| 985 .Concat(self._GenerateError( | 990 .Concat(self._GenerateError( |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1150 if self._generate_error_messages: | 1155 if self._generate_error_messages: |
| 1151 params = list(params) + ['base::string16* error'] | 1156 params = list(params) + ['base::string16* error'] |
| 1152 return ', '.join(str(p) for p in params) | 1157 return ', '.join(str(p) for p in params) |
| 1153 | 1158 |
| 1154 def _GenerateArgs(self, args): | 1159 def _GenerateArgs(self, args): |
| 1155 """Builds the argument list for a function, given an array of arguments. | 1160 """Builds the argument list for a function, given an array of arguments. |
| 1156 """ | 1161 """ |
| 1157 if self._generate_error_messages: | 1162 if self._generate_error_messages: |
| 1158 args = list(args) + ['error'] | 1163 args = list(args) + ['error'] |
| 1159 return ', '.join(str(a) for a in args) | 1164 return ', '.join(str(a) for a in args) |
| OLD | NEW |