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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 141 |
142 return c | 142 return c |
143 | 143 |
144 def _GenerateInitializersAndBody(self, type_): | 144 def _GenerateInitializersAndBody(self, type_): |
145 items = [] | 145 items = [] |
146 for prop in type_.properties.values(): | 146 for prop in type_.properties.values(): |
147 t = prop.type_ | 147 t = prop.type_ |
148 | 148 |
149 real_t = self._type_helper.FollowRef(t) | 149 real_t = self._type_helper.FollowRef(t) |
150 if real_t.property_type == PropertyType.ENUM: | 150 if real_t.property_type == PropertyType.ENUM: |
151 items.append('%s(%s)' % ( | 151 namespace_prefix = ('%s::' % real_t.namespace.unix_name |
152 prop.unix_name, | 152 if real_t.namespace != self._namespace |
153 self._type_helper.GetEnumNoneValue(t))) | 153 else '') |
| 154 items.append('%s(%s%s)' % (prop.unix_name, |
| 155 namespace_prefix, |
| 156 self._type_helper.GetEnumNoneValue(t))) |
154 elif prop.optional: | 157 elif prop.optional: |
155 continue | 158 continue |
156 elif t.property_type == PropertyType.INTEGER: | 159 elif t.property_type == PropertyType.INTEGER: |
157 items.append('%s(0)' % prop.unix_name) | 160 items.append('%s(0)' % prop.unix_name) |
158 elif t.property_type == PropertyType.DOUBLE: | 161 elif t.property_type == PropertyType.DOUBLE: |
159 items.append('%s(0.0)' % prop.unix_name) | 162 items.append('%s(0.0)' % prop.unix_name) |
160 elif t.property_type == PropertyType.BOOLEAN: | 163 elif t.property_type == PropertyType.BOOLEAN: |
161 items.append('%s(false)' % prop.unix_name) | 164 items.append('%s(false)' % prop.unix_name) |
162 elif (t.property_type == PropertyType.ANY or | 165 elif (t.property_type == PropertyType.ANY or |
163 t.property_type == PropertyType.ARRAY or | 166 t.property_type == PropertyType.ARRAY or |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 c = Code() | 285 c = Code() |
283 value_var = prop.unix_name + '_value' | 286 value_var = prop.unix_name + '_value' |
284 c.Append('const base::Value* %(value_var)s = NULL;') | 287 c.Append('const base::Value* %(value_var)s = NULL;') |
285 if prop.optional: | 288 if prop.optional: |
286 (c.Sblock( | 289 (c.Sblock( |
287 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') | 290 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') |
288 .Concat(self._GeneratePopulatePropertyFromValue( | 291 .Concat(self._GeneratePopulatePropertyFromValue( |
289 prop, value_var, dst, 'false'))) | 292 prop, value_var, dst, 'false'))) |
290 underlying_type = self._type_helper.FollowRef(prop.type_) | 293 underlying_type = self._type_helper.FollowRef(prop.type_) |
291 if underlying_type.property_type == PropertyType.ENUM: | 294 if underlying_type.property_type == PropertyType.ENUM: |
| 295 namespace_prefix = ('%s::' % underlying_type.namespace.unix_name |
| 296 if underlying_type.namespace != self._namespace |
| 297 else '') |
292 (c.Append('} else {') | 298 (c.Append('} else {') |
293 .Append('%%(dst)s->%%(name)s = %s;' % | 299 .Append('%%(dst)s->%%(name)s = %s%s;' % |
294 self._type_helper.GetEnumNoneValue(prop.type_))) | 300 (namespace_prefix, |
| 301 self._type_helper.GetEnumNoneValue(prop.type_)))) |
295 c.Eblock('}') | 302 c.Eblock('}') |
296 else: | 303 else: |
297 (c.Sblock( | 304 (c.Sblock( |
298 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') | 305 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') |
299 .Concat(self._GenerateError('"\'%%(key)s\' is required"')) | 306 .Concat(self._GenerateError('"\'%%(key)s\' is required"')) |
300 .Append('return false;') | 307 .Append('return false;') |
301 .Eblock('}') | 308 .Eblock('}') |
302 .Concat(self._GeneratePopulatePropertyFromValue( | 309 .Concat(self._GeneratePopulatePropertyFromValue( |
303 prop, value_var, dst, 'false')) | 310 prop, value_var, dst, 'false')) |
304 ) | 311 ) |
(...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1020 """Initialize a model.Property to its default value inside an object. | 1027 """Initialize a model.Property to its default value inside an object. |
1021 | 1028 |
1022 E.g for optional enum "state", generate dst->state = STATE_NONE; | 1029 E.g for optional enum "state", generate dst->state = STATE_NONE; |
1023 | 1030 |
1024 dst: Type* | 1031 dst: Type* |
1025 """ | 1032 """ |
1026 c = Code() | 1033 c = Code() |
1027 underlying_type = self._type_helper.FollowRef(prop.type_) | 1034 underlying_type = self._type_helper.FollowRef(prop.type_) |
1028 if (underlying_type.property_type == PropertyType.ENUM and | 1035 if (underlying_type.property_type == PropertyType.ENUM and |
1029 prop.optional): | 1036 prop.optional): |
1030 c.Append('%s->%s = %s;' % ( | 1037 namespace_prefix = ('%s::' % underlying_type.namespace.unix_name |
| 1038 if underlying_type.namespace != self._namespace |
| 1039 else '') |
| 1040 c.Append('%s->%s = %s%s;' % ( |
1031 dst, | 1041 dst, |
1032 prop.unix_name, | 1042 prop.unix_name, |
| 1043 namespace_prefix, |
1033 self._type_helper.GetEnumNoneValue(prop.type_))) | 1044 self._type_helper.GetEnumNoneValue(prop.type_))) |
1034 return c | 1045 return c |
1035 | 1046 |
1036 def _GenerateError(self, body): | 1047 def _GenerateError(self, body): |
1037 """Generates an error message pertaining to population failure. | 1048 """Generates an error message pertaining to population failure. |
1038 | 1049 |
1039 E.g 'expected bool, got int' | 1050 E.g 'expected bool, got int' |
1040 """ | 1051 """ |
1041 c = Code() | 1052 c = Code() |
1042 if not self._generate_error_messages: | 1053 if not self._generate_error_messages: |
1043 return c | 1054 return c |
1044 (c.Append('if (error->length())') | 1055 (c.Append('if (error->length())') |
1045 .Append(' error->append(UTF8ToUTF16("; "));') | 1056 .Append(' error->append(UTF8ToUTF16("; "));') |
1046 .Append('error->append(UTF8ToUTF16(%s));' % body)) | 1057 .Append('error->append(UTF8ToUTF16(%s));' % body)) |
1047 return c | 1058 return c |
1048 | 1059 |
1049 def _GenerateParams(self, params): | 1060 def _GenerateParams(self, params): |
1050 """Builds the parameter list for a function, given an array of parameters. | 1061 """Builds the parameter list for a function, given an array of parameters. |
1051 """ | 1062 """ |
1052 if self._generate_error_messages: | 1063 if self._generate_error_messages: |
1053 params = list(params) + ['base::string16* error'] | 1064 params = list(params) + ['base::string16* error'] |
1054 return ', '.join(str(p) for p in params) | 1065 return ', '.join(str(p) for p in params) |
1055 | 1066 |
1056 def _GenerateArgs(self, args): | 1067 def _GenerateArgs(self, args): |
1057 """Builds the argument list for a function, given an array of arguments. | 1068 """Builds the argument list for a function, given an array of arguments. |
1058 """ | 1069 """ |
1059 if self._generate_error_messages: | 1070 if self._generate_error_messages: |
1060 args = list(args) + ['error'] | 1071 args = list(args) + ['error'] |
1061 return ', '.join(str(a) for a in args) | 1072 return ', '.join(str(a) for a in args) |
OLD | NEW |