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