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