Chromium Code Reviews| 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 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 placeholders to take the property name. | 206 placeholders to take the property name. |
| 207 - |move_str| the string to use when moving a value; should have two | 207 - |move_str| the string to use when moving a value; should have two |
| 208 placeholders to take the property name. | 208 placeholders to take the property name. |
| 209 """ | 209 """ |
| 210 props = [] | 210 props = [] |
| 211 dicts = [] | 211 dicts = [] |
| 212 for prop in type_.properties.values(): | 212 for prop in type_.properties.values(): |
| 213 t = prop.type_ | 213 t = prop.type_ |
| 214 | 214 |
| 215 real_t = self._type_helper.FollowRef(t) | 215 real_t = self._type_helper.FollowRef(t) |
| 216 if (prop.optional or | 216 if (real_t.property_type != PropertyType.ENUM and # enums handled below |
|
Devlin
2016/03/23 21:41:01
drive-by - was seeing "std::move(other.enum)"
asargent_no_longer_on_chrome
2016/03/24 17:03:56
optional: I don't think you necessarily need the "
Devlin
2016/03/24 17:35:57
SGTM, done.
| |
| 217 t.property_type == PropertyType.ANY or | 217 (prop.optional or |
| 218 t.property_type == PropertyType.ARRAY or | 218 t.property_type == PropertyType.ANY or |
| 219 t.property_type == PropertyType.BINARY or | 219 t.property_type == PropertyType.ARRAY or |
| 220 t.property_type == PropertyType.CHOICES or | 220 t.property_type == PropertyType.BINARY or |
| 221 t.property_type == PropertyType.OBJECT or | 221 t.property_type == PropertyType.CHOICES or |
| 222 t.property_type == PropertyType.REF or | 222 t.property_type == PropertyType.OBJECT or |
| 223 t.property_type == PropertyType.STRING): | 223 t.property_type == PropertyType.REF or |
| 224 t.property_type == PropertyType.STRING)): | |
| 224 props.append(move_str % (prop.unix_name, prop.unix_name)) | 225 props.append(move_str % (prop.unix_name, prop.unix_name)) |
| 225 elif t.property_type == PropertyType.FUNCTION: | 226 elif t.property_type == PropertyType.FUNCTION: |
| 226 dicts.append(prop.unix_name) | 227 dicts.append(prop.unix_name) |
| 227 elif (real_t.property_type == PropertyType.ENUM or | 228 elif (real_t.property_type == PropertyType.ENUM or |
| 228 t.property_type == PropertyType.INTEGER or | 229 t.property_type == PropertyType.INTEGER or |
| 229 t.property_type == PropertyType.DOUBLE or | 230 t.property_type == PropertyType.DOUBLE or |
| 230 t.property_type == PropertyType.BOOLEAN): | 231 t.property_type == PropertyType.BOOLEAN): |
| 231 props.append(copy_str % (prop.unix_name, prop.unix_name)) | 232 props.append(copy_str % (prop.unix_name, prop.unix_name)) |
| 232 else: | 233 else: |
| 233 raise TypeError(t) | 234 raise TypeError(t) |
| 234 | 235 |
| 236 if (type_.property_type == PropertyType.OBJECT and | |
| 237 type_.additional_properties is not None): | |
| 238 if type_.additional_properties.property_type == PropertyType.ANY: | |
| 239 dicts.append('additional_properties') | |
| 240 else: | |
| 241 props.append(move_str % ('additional_properties', | |
| 242 'additional_properties')) | |
| 243 | |
| 235 return (props, dicts) | 244 return (props, dicts) |
| 236 | 245 |
| 237 def _GenerateMoveCtor(self, type_): | 246 def _GenerateMoveCtor(self, type_): |
| 238 props, dicts = self._GetMoveProps(type_, '%s(rhs.%s)', | 247 props, dicts = self._GetMoveProps(type_, '%s(rhs.%s)', |
| 239 '%s(std::move(rhs.%s))') | 248 '%s(std::move(rhs.%s))') |
| 240 s = '' | 249 s = '' |
| 241 if props: | 250 if props: |
| 242 s = s + ': %s' % (',\n'.join(props)) | 251 s = s + ': %s' % (',\n'.join(props)) |
| 243 s = s + '{' | 252 s = s + '{' |
| 244 for item in dicts: | 253 for item in dicts: |
| 245 s = s + ('\n%s.Swap(&rhs.%s);' % (item, item)) | 254 s = s + ('\n%s.Swap(&rhs.%s);' % (item, item)) |
| 246 s = s + '\n}' | 255 s = s + '\n}' |
| 247 | 256 |
| 248 return Code().Append(s) | 257 return Code().Append(s) |
| 249 | 258 |
| 250 def _GenerateMoveAssignOperator(self, type_): | 259 def _GenerateMoveAssignOperator(self, type_): |
| 251 props, dicts = self._GetMoveProps(type_, '%s = rhs.%s;', | 260 props, dicts = self._GetMoveProps(type_, '%s = rhs.%s;', |
| 252 '%s = std::move(rhs.%s);') | 261 '%s = std::move(rhs.%s);') |
| 253 s = '{\n' | 262 s = '{\n' |
| 254 if props: | 263 if props: |
| 255 s = s + '\n'.join(props) | 264 s = s + '\n'.join(props) |
| 256 for item in dicts: | 265 for item in dicts: |
| 257 s = s + ('\n%s.Swap(&rhs.%s);' % (item, item)) | 266 s = s + ('%s.Swap(&rhs.%s);' % (item, item)) |
| 258 s = s + '\nreturn *this;\n}' | 267 s = s + '\nreturn *this;\n}' |
| 259 | 268 |
| 260 return Code().Append(s) | 269 return Code().Append(s) |
| 261 | 270 |
| 262 def _GenerateTypePopulate(self, cpp_namespace, type_): | 271 def _GenerateTypePopulate(self, cpp_namespace, type_): |
| 263 """Generates the function for populating a type given a pointer to it. | 272 """Generates the function for populating a type given a pointer to it. |
| 264 | 273 |
| 265 E.g for type "Foo", generates Foo::Populate() | 274 E.g for type "Foo", generates Foo::Populate() |
| 266 """ | 275 """ |
| 267 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) | 276 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 465 if prop.optional: | 474 if prop.optional: |
| 466 c.Eblock('}') | 475 c.Eblock('}') |
| 467 | 476 |
| 468 if type_.additional_properties is not None: | 477 if type_.additional_properties is not None: |
| 469 if type_.additional_properties.property_type == PropertyType.ANY: | 478 if type_.additional_properties.property_type == PropertyType.ANY: |
| 470 c.Append('value->MergeDictionary(&additional_properties);') | 479 c.Append('value->MergeDictionary(&additional_properties);') |
| 471 else: | 480 else: |
| 472 # Non-copyable types will be wrapped in a linked_ptr for inclusion in | 481 # Non-copyable types will be wrapped in a linked_ptr for inclusion in |
| 473 # maps, so we need to unwrap them. | 482 # maps, so we need to unwrap them. |
| 474 needs_unwrap = ( | 483 needs_unwrap = ( |
| 484 not 'use_movable_types' in type_.namespace.compiler_options and | |
| 475 not self._type_helper.IsCopyable(type_.additional_properties)) | 485 not self._type_helper.IsCopyable(type_.additional_properties)) |
| 476 (c.Sblock('for (const auto& it : additional_properties) {') | 486 (c.Sblock('for (const auto& it : additional_properties) {') |
| 477 .Cblock(self._CreateValueFromType( | 487 .Cblock(self._CreateValueFromType( |
| 478 'value->SetWithoutPathExpansion(it.first, %s);', | 488 'value->SetWithoutPathExpansion(it.first, %s);', |
| 479 type_.additional_properties.name, | 489 type_.additional_properties.name, |
| 480 type_.additional_properties, | 490 type_.additional_properties, |
| 481 '%sit.second' % ('*' if needs_unwrap else ''))) | 491 '%sit.second' % ('*' if needs_unwrap else ''))) |
| 482 .Eblock('}') | 492 .Eblock('}') |
| 483 ) | 493 ) |
| 484 | 494 |
| (...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1146 if self._generate_error_messages: | 1156 if self._generate_error_messages: |
| 1147 params = list(params) + ['base::string16* error'] | 1157 params = list(params) + ['base::string16* error'] |
| 1148 return ', '.join(str(p) for p in params) | 1158 return ', '.join(str(p) for p in params) |
| 1149 | 1159 |
| 1150 def _GenerateArgs(self, args): | 1160 def _GenerateArgs(self, args): |
| 1151 """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. |
| 1152 """ | 1162 """ |
| 1153 if self._generate_error_messages: | 1163 if self._generate_error_messages: |
| 1154 args = list(args) + ['error'] | 1164 args = list(args) + ['error'] |
| 1155 return ', '.join(str(a) for a in args) | 1165 return ', '.join(str(a) for a in args) |
| OLD | NEW |