| 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 | 10 |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 if underlying_type.property_type == PropertyType.ENUM: | 359 if underlying_type.property_type == PropertyType.ENUM: |
| 360 c.Sblock('if (%s != %s) {' % | 360 c.Sblock('if (%s != %s) {' % |
| 361 (prop_var, | 361 (prop_var, |
| 362 self._type_helper.GetEnumNoneValue(prop.type_))) | 362 self._type_helper.GetEnumNoneValue(prop.type_))) |
| 363 else: | 363 else: |
| 364 c.Sblock('if (%s.get()) {' % prop_var) | 364 c.Sblock('if (%s.get()) {' % prop_var) |
| 365 | 365 |
| 366 # ANY is a base::Value which is abstract and cannot be a direct member, so | 366 # ANY is a base::Value which is abstract and cannot be a direct member, so |
| 367 # it will always be a pointer. | 367 # it will always be a pointer. |
| 368 is_ptr = prop.optional or prop.type_.property_type == PropertyType.ANY | 368 is_ptr = prop.optional or prop.type_.property_type == PropertyType.ANY |
| 369 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( | 369 c.Cblock(self._CreateValueFromType( |
| 370 'value->SetWithoutPathExpansion("' + prop.name + '", %s);', |
| 370 prop.name, | 371 prop.name, |
| 371 self._CreateValueFromType(cpp_namespace, | 372 prop.type_, |
| 372 prop.type_, | 373 prop_var, |
| 373 prop_var, | 374 is_ptr=is_ptr)) |
| 374 is_ptr=is_ptr))) | |
| 375 | 375 |
| 376 if prop.optional: | 376 if prop.optional: |
| 377 c.Eblock('}') | 377 c.Eblock('}') |
| 378 | 378 |
| 379 if type_.additional_properties is not None: | 379 if type_.additional_properties is not None: |
| 380 if type_.additional_properties.property_type == PropertyType.ANY: | 380 if type_.additional_properties.property_type == PropertyType.ANY: |
| 381 c.Append('value->MergeDictionary(&additional_properties);') | 381 c.Append('value->MergeDictionary(&additional_properties);') |
| 382 else: | 382 else: |
| 383 # Non-copyable types will be wrapped in a linked_ptr for inclusion in | 383 # Non-copyable types will be wrapped in a linked_ptr for inclusion in |
| 384 # maps, so we need to unwrap them. | 384 # maps, so we need to unwrap them. |
| 385 needs_unwrap = ( | 385 needs_unwrap = ( |
| 386 not self._type_helper.IsCopyable(type_.additional_properties)) | 386 not self._type_helper.IsCopyable(type_.additional_properties)) |
| 387 cpp_type = self._type_helper.GetCppType(type_.additional_properties, | 387 cpp_type = self._type_helper.GetCppType(type_.additional_properties, |
| 388 is_in_container=True) | 388 is_in_container=True) |
| 389 (c.Sblock('for (std::map<std::string, %s>::const_iterator it =' % | 389 (c.Sblock('for (std::map<std::string, %s>::const_iterator it =' % |
| 390 cpp_util.PadForGenerics(cpp_type)) | 390 cpp_util.PadForGenerics(cpp_type)) |
| 391 .Append(' additional_properties.begin();') | 391 .Append(' additional_properties.begin();') |
| 392 .Append(' it != additional_properties.end(); ++it) {') | 392 .Append(' it != additional_properties.end(); ++it) {') |
| 393 .Append('value->SetWithoutPathExpansion(it->first, %s);' % | 393 .Cblock(self._CreateValueFromType( |
| 394 self._CreateValueFromType( | 394 'value->SetWithoutPathExpansion(it->first, %s);', |
| 395 cpp_namespace, | 395 type_.additional_properties.name, |
| 396 type_.additional_properties, | 396 type_.additional_properties, |
| 397 '%sit->second' % ('*' if needs_unwrap else ''))) | 397 '%sit->second' % ('*' if needs_unwrap else ''))) |
| 398 .Eblock('}') | 398 .Eblock('}') |
| 399 ) | 399 ) |
| 400 | 400 |
| 401 return (c.Append() | 401 return (c.Append() |
| 402 .Append('return value.Pass();') | 402 .Append('return value.Pass();') |
| 403 .Eblock('}')) | 403 .Eblock('}')) |
| 404 | 404 |
| 405 def _GenerateChoiceTypeToValue(self, cpp_namespace, type_): | 405 def _GenerateChoiceTypeToValue(self, cpp_namespace, type_): |
| 406 """Generates a function that serializes a choice-representing type | 406 """Generates a function that serializes a choice-representing type |
| 407 into a base::Value. | 407 into a base::Value. |
| 408 """ | 408 """ |
| 409 c = Code() | 409 c = Code() |
| 410 c.Sblock('scoped_ptr<base::Value> %s::ToValue() const {' % cpp_namespace) | 410 c.Sblock('scoped_ptr<base::Value> %s::ToValue() const {' % cpp_namespace) |
| 411 c.Append('scoped_ptr<base::Value> result;') | 411 c.Append('scoped_ptr<base::Value> result;') |
| 412 for choice in type_.choices: | 412 for choice in type_.choices: |
| 413 choice_var = 'as_%s' % choice.unix_name | 413 choice_var = 'as_%s' % choice.unix_name |
| 414 (c.Sblock('if (%s) {' % choice_var) | 414 (c.Sblock('if (%s) {' % choice_var) |
| 415 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % | 415 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % |
| 416 type_.unix_name) | 416 type_.unix_name) |
| 417 .Append('result.reset(%s);' % self._CreateValueFromType( | 417 .Cblock(self._CreateValueFromType('result.reset(%s);', |
| 418 cpp_namespace, | 418 choice.name, |
| 419 choice, | 419 choice, |
| 420 '*%s' % choice_var)) | 420 '*%s' % choice_var)) |
| 421 .Eblock('}') | 421 .Eblock('}') |
| 422 ) | 422 ) |
| 423 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % | 423 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % |
| 424 type_.unix_name) | 424 type_.unix_name) |
| 425 .Append('return result.Pass();') | 425 .Append('return result.Pass();') |
| 426 .Eblock('}') | 426 .Eblock('}') |
| 427 ) | 427 ) |
| 428 return c | 428 return c |
| 429 | 429 |
| 430 def _GenerateFunction(self, function): | 430 def _GenerateFunction(self, function): |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 (c.Append('namespace %s {' % event_namespace) | 467 (c.Append('namespace %s {' % event_namespace) |
| 468 .Append() | 468 .Append() |
| 469 .Cblock(self._GenerateEventNameConstant(None, event)) | 469 .Cblock(self._GenerateEventNameConstant(None, event)) |
| 470 .Cblock(self._GenerateCreateCallbackArguments(event_namespace, | 470 .Cblock(self._GenerateCreateCallbackArguments(event_namespace, |
| 471 None, | 471 None, |
| 472 event)) | 472 event)) |
| 473 .Append('} // namespace %s' % event_namespace) | 473 .Append('} // namespace %s' % event_namespace) |
| 474 ) | 474 ) |
| 475 return c | 475 return c |
| 476 | 476 |
| 477 def _CreateValueFromType(self, cpp_namespace, type_, var, is_ptr=False): | 477 def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False): |
| 478 """Creates a base::Value given a type. Generated code passes ownership | 478 """Creates a base::Value given a type. Generated code passes ownership |
| 479 to caller. | 479 to caller. |
| 480 | 480 |
| 481 var: variable or variable* | 481 var: variable or variable* |
| 482 | 482 |
| 483 E.g for std::string, generate new base::StringValue(var) | 483 E.g for std::string, generate new base::StringValue(var) |
| 484 """ | 484 """ |
| 485 c = Code() |
| 486 underlying_type = self._type_helper.FollowRef(type_) |
| 487 if underlying_type.property_type == PropertyType.ARRAY: |
| 488 # Enums are treated specially because C++ templating thinks that they're |
| 489 # ints, but really they're strings. |
| 490 underlying_item_type = self._type_helper.FollowRef( |
| 491 underlying_type.item_type) |
| 492 if underlying_item_type.property_type == PropertyType.ENUM: |
| 493 if is_ptr: |
| 494 vardot = '(%s)->' % var |
| 495 else: |
| 496 vardot = '(%s).' % var |
| 497 |
| 498 maybe_namespace = '' |
| 499 if type_.item_type.property_type == PropertyType.REF: |
| 500 maybe_namespace = '%s::' % underlying_item_type.namespace.unix_name |
| 501 |
| 502 enum_list_var = '%s_list' % prop_name |
| 503 (c.Append('std::vector<std::string> %s;' % enum_list_var) |
| 504 .Append('for (typename std::vector<%s>::const_iterator it = %sbegin();
' |
| 505 % (self._type_helper.GetCppType(underlying_item_type), vardot)) |
| 506 .Sblock(' it != %send(); ++it) {' % vardot) |
| 507 .Append('%s.push_back(%sToString(*it));' % (enum_list_var, |
| 508 maybe_namespace)) |
| 509 .Eblock('}')) |
| 510 |
| 511 if is_ptr: |
| 512 var = 'scoped_ptr<std::vector<std::string> >(&%s)' % enum_list_var |
| 513 else: |
| 514 var = enum_list_var |
| 515 |
| 516 c.Append(code % self._CreateValueFromTypeInternal(type_, var, is_ptr)) |
| 517 return c |
| 518 |
| 519 def _CreateValueFromTypeInternal(self, type_, var, is_ptr): |
| 485 underlying_type = self._type_helper.FollowRef(type_) | 520 underlying_type = self._type_helper.FollowRef(type_) |
| 486 if (underlying_type.property_type == PropertyType.CHOICES or | 521 if (underlying_type.property_type == PropertyType.CHOICES or |
| 487 underlying_type.property_type == PropertyType.OBJECT): | 522 underlying_type.property_type == PropertyType.OBJECT): |
| 488 if is_ptr: | 523 if is_ptr: |
| 489 return '(%s)->ToValue().release()' % var | 524 return '(%s)->ToValue().release()' % var |
| 490 else: | 525 else: |
| 491 return '(%s).ToValue().release()' % var | 526 return '(%s).ToValue().release()' % var |
| 492 elif (underlying_type.property_type == PropertyType.ANY or | 527 elif (underlying_type.property_type == PropertyType.ANY or |
| 493 underlying_type.property_type == PropertyType.FUNCTION): | 528 underlying_type.property_type == PropertyType.FUNCTION): |
| 494 if is_ptr: | 529 if is_ptr: |
| 495 vardot = '(%s)->' % var | 530 vardot = '(%s)->' % var |
| 496 else: | 531 else: |
| 497 vardot = '(%s).' % var | 532 vardot = '(%s).' % var |
| 498 return '%sDeepCopy()' % vardot | 533 return '%sDeepCopy()' % vardot |
| 499 elif underlying_type.property_type == PropertyType.ENUM: | 534 elif underlying_type.property_type == PropertyType.ENUM: |
| 500 classname = cpp_util.Classname(schema_util.StripNamespace( | 535 classname = cpp_util.Classname(schema_util.StripNamespace( |
| 501 underlying_type.name)) | 536 underlying_type.name)) |
| 502 return 'new base::StringValue(%sToString(%s))' % (classname, var) | 537 return 'new base::StringValue(%sToString(%s))' % (classname, var) |
| 503 elif underlying_type.property_type == PropertyType.BINARY: | 538 elif underlying_type.property_type == PropertyType.BINARY: |
| 504 if is_ptr: | 539 if is_ptr: |
| 505 vardot = var + '->' | 540 vardot = var + '->' |
| 506 else: | 541 else: |
| 507 vardot = var + '.' | 542 vardot = var + '.' |
| 508 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' % | 543 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' % |
| 509 (vardot, vardot)) | 544 (vardot, vardot)) |
| 510 elif underlying_type.property_type == PropertyType.ARRAY: | 545 elif underlying_type.property_type == PropertyType.ARRAY: |
| 511 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( | 546 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( |
| 512 cpp_namespace, | |
| 513 underlying_type, | |
| 514 var, | 547 var, |
| 515 is_ptr) | 548 is_ptr) |
| 516 elif underlying_type.property_type.is_fundamental: | 549 elif underlying_type.property_type.is_fundamental: |
| 517 if is_ptr: | 550 if is_ptr: |
| 518 var = '*%s' % var | 551 var = '*%s' % var |
| 519 if underlying_type.property_type == PropertyType.STRING: | 552 if underlying_type.property_type == PropertyType.STRING: |
| 520 return 'new base::StringValue(%s)' % var | 553 return 'new base::StringValue(%s)' % var |
| 521 else: | 554 else: |
| 522 return 'new base::FundamentalValue(%s)' % var | 555 return 'new base::FundamentalValue(%s)' % var |
| 523 else: | 556 else: |
| (...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 954 | 987 |
| 955 (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s' | 988 (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s' |
| 956 'Create(%(declaration_list)s) {') | 989 'Create(%(declaration_list)s) {') |
| 957 .Append('scoped_ptr<base::ListValue> create_results(' | 990 .Append('scoped_ptr<base::ListValue> create_results(' |
| 958 'new base::ListValue());') | 991 'new base::ListValue());') |
| 959 ) | 992 ) |
| 960 declaration_list = [] | 993 declaration_list = [] |
| 961 for param in params: | 994 for param in params: |
| 962 declaration_list.append(cpp_util.GetParameterDeclaration( | 995 declaration_list.append(cpp_util.GetParameterDeclaration( |
| 963 param, self._type_helper.GetCppType(param.type_))) | 996 param, self._type_helper.GetCppType(param.type_))) |
| 964 c.Append('create_results->Append(%s);' % self._CreateValueFromType( | 997 c.Cblock(self._CreateValueFromType('create_results->Append(%s);', |
| 965 cpp_namespace, | 998 param.name, |
| 966 param.type_, | 999 param.type_, |
| 967 param.unix_name)) | 1000 param.unix_name)) |
| 968 c.Append('return create_results.Pass();') | 1001 c.Append('return create_results.Pass();') |
| 969 c.Eblock('}') | 1002 c.Eblock('}') |
| 970 c.Substitute({ | 1003 c.Substitute({ |
| 971 'function_scope': ('%s::' % function_scope) if function_scope else '', | 1004 'function_scope': ('%s::' % function_scope) if function_scope else '', |
| 972 'declaration_list': ', '.join(declaration_list), | 1005 'declaration_list': ', '.join(declaration_list), |
| 973 'param_names': ', '.join(param.unix_name for param in params) | 1006 'param_names': ', '.join(param.unix_name for param in params) |
| 974 }) | 1007 }) |
| 975 return c | 1008 return c |
| 976 | 1009 |
| 977 def _GenerateEventNameConstant(self, function_scope, event): | 1010 def _GenerateEventNameConstant(self, function_scope, event): |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1018 if self._generate_error_messages: | 1051 if self._generate_error_messages: |
| 1019 params = list(params) + ['base::string16* error'] | 1052 params = list(params) + ['base::string16* error'] |
| 1020 return ', '.join(str(p) for p in params) | 1053 return ', '.join(str(p) for p in params) |
| 1021 | 1054 |
| 1022 def _GenerateArgs(self, args): | 1055 def _GenerateArgs(self, args): |
| 1023 """Builds the argument list for a function, given an array of arguments. | 1056 """Builds the argument list for a function, given an array of arguments. |
| 1024 """ | 1057 """ |
| 1025 if self._generate_error_messages: | 1058 if self._generate_error_messages: |
| 1026 args = list(args) + ['error'] | 1059 args = list(args) + ['error'] |
| 1027 return ', '.join(str(a) for a in args) | 1060 return ', '.join(str(a) for a in args) |
| OLD | NEW |