| 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 any_helper | 7 import any_helper |
| 8 import cpp_util | 8 import cpp_util |
| 9 import model | 9 import model |
| 10 import schema_util | 10 import schema_util |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 (c.Append('//') | 73 (c.Append('//') |
| 74 .Append('// Functions') | 74 .Append('// Functions') |
| 75 .Append('//') | 75 .Append('//') |
| 76 .Append() | 76 .Append() |
| 77 ) | 77 ) |
| 78 for function in self._namespace.functions.values(): | 78 for function in self._namespace.functions.values(): |
| 79 (c.Concat(self._GenerateFunction( | 79 (c.Concat(self._GenerateFunction( |
| 80 cpp_util.Classname(function.name), function)) | 80 cpp_util.Classname(function.name), function)) |
| 81 .Append() | 81 .Append() |
| 82 ) | 82 ) |
| 83 if self._namespace.events: |
| 84 (c.Append('//') |
| 85 .Append('// Events') |
| 86 .Append('//') |
| 87 .Append() |
| 88 ) |
| 89 for event in self._namespace.events.values(): |
| 90 (c.Concat(self._GenerateCreateCallbackArguments( |
| 91 cpp_util.Classname(event.name), event)) |
| 92 .Append() |
| 93 ) |
| 83 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) | 94 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) |
| 84 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 95 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
| 85 .Append() | 96 .Append() |
| 86 ) | 97 ) |
| 87 # TODO(calamity): Events | |
| 88 return c | 98 return c |
| 89 | 99 |
| 90 def _GenerateType(self, cpp_namespace, type_): | 100 def _GenerateType(self, cpp_namespace, type_): |
| 91 """Generates the function definitions for a type. | 101 """Generates the function definitions for a type. |
| 92 """ | 102 """ |
| 93 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) | 103 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) |
| 94 c = Code() | 104 c = Code() |
| 95 | 105 |
| 96 if type_.functions: | 106 if type_.functions: |
| 97 for function in type_.functions.values(): | 107 for function in type_.functions.values(): |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 if function.params: | 277 if function.params: |
| 268 c.Concat(self._GeneratePropertyFunctions(cpp_namespace + '::Params', | 278 c.Concat(self._GeneratePropertyFunctions(cpp_namespace + '::Params', |
| 269 function.params)) | 279 function.params)) |
| 270 (c.Append('%(cpp_namespace)s::Params::Params() {}') | 280 (c.Append('%(cpp_namespace)s::Params::Params() {}') |
| 271 .Append('%(cpp_namespace)s::Params::~Params() {}') | 281 .Append('%(cpp_namespace)s::Params::~Params() {}') |
| 272 .Append() | 282 .Append() |
| 273 .Concat(self._GenerateFunctionParamsCreate(cpp_namespace, function)) | 283 .Concat(self._GenerateFunctionParamsCreate(cpp_namespace, function)) |
| 274 .Append() | 284 .Append() |
| 275 ) | 285 ) |
| 276 | 286 |
| 277 # Result::Create function | 287 # Results::Create function |
| 278 if function.callback: | 288 if function.callback: |
| 279 c.Concat(self._GenerateFunctionResultCreate(cpp_namespace, function)) | 289 c.Concat(self._GenerateCreateCallbackArguments( |
| 290 "%s::Results" % cpp_namespace, function.callback)) |
| 280 | 291 |
| 281 c.Substitute({'cpp_namespace': cpp_namespace}) | 292 c.Substitute({'cpp_namespace': cpp_namespace}) |
| 282 | 293 |
| 283 return c | 294 return c |
| 284 | 295 |
| 285 def _GenerateCreateEnumValue(self, cpp_namespace, prop): | 296 def _GenerateCreateEnumValue(self, cpp_namespace, prop): |
| 286 """Generates CreateEnumValue() that returns the |base::StringValue| | 297 """Generates CreateEnumValue() that returns the |base::StringValue| |
| 287 representation of an enum. | 298 representation of an enum. |
| 288 """ | 299 """ |
| 289 c = Code() | 300 c = Code() |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 self._GenerateListValueToEnumArrayConversion(c, prop) | 513 self._GenerateListValueToEnumArrayConversion(c, prop) |
| 503 else: | 514 else: |
| 504 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 515 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( |
| 505 self._cpp_type_generator.GetReferencedProperty(prop), 'list', | 516 self._cpp_type_generator.GetReferencedProperty(prop), 'list', |
| 506 dst + '->' + prop.unix_name, prop.optional)) | 517 dst + '->' + prop.unix_name, prop.optional)) |
| 507 .Append(' return %(failure_value)s;') | 518 .Append(' return %(failure_value)s;') |
| 508 ) | 519 ) |
| 509 elif prop.type_ == PropertyType.CHOICES: | 520 elif prop.type_ == PropertyType.CHOICES: |
| 510 type_var = '%(dst)s->%(name)s_type' | 521 type_var = '%(dst)s->%(name)s_type' |
| 511 c.Sblock('switch (%(value_var)s->GetType()) {') | 522 c.Sblock('switch (%(value_var)s->GetType()) {') |
| 512 for choice in self._cpp_type_generator.GetExpandedChoicesInParams([prop]): | 523 for choice in self._cpp_type_generator.ExpandParams([prop]): |
| 513 (c.Sblock('case %s: {' % cpp_util.GetValueType( | 524 (c.Sblock('case %s: {' % cpp_util.GetValueType( |
| 514 self._cpp_type_generator.GetReferencedProperty(choice).type_)) | 525 self._cpp_type_generator.GetReferencedProperty(choice).type_)) |
| 515 .Concat(self._GeneratePopulatePropertyFromValue( | 526 .Concat(self._GeneratePopulatePropertyFromValue( |
| 516 choice, value_var, dst, failure_value, check_type=False)) | 527 choice, value_var, dst, failure_value, check_type=False)) |
| 517 .Append('%s = %s;' % | 528 .Append('%s = %s;' % |
| 518 (type_var, | 529 (type_var, |
| 519 self._cpp_type_generator.GetEnumValue( | 530 self._cpp_type_generator.GetEnumValue( |
| 520 prop, choice.type_.name))) | 531 prop, choice.type_.name))) |
| 521 .Append('break;') | 532 .Append('break;') |
| 522 .Eblock('}') | 533 .Eblock('}') |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 '(enum_as_string == "%s")' % enum_value) | 607 '(enum_as_string == "%s")' % enum_value) |
| 597 .Append(' ' + enum_temp + ' = %s;' % ( | 608 .Append(' ' + enum_temp + ' = %s;' % ( |
| 598 self._cpp_type_generator.GetEnumValue(prop, enum_value))) | 609 self._cpp_type_generator.GetEnumValue(prop, enum_value))) |
| 599 ) | 610 ) |
| 600 (c.Append('else') | 611 (c.Append('else') |
| 601 .Append(' return %(failure_value)s;') | 612 .Append(' return %(failure_value)s;') |
| 602 ) | 613 ) |
| 603 | 614 |
| 604 def _GeneratePropertyFunctions(self, param_namespace, params): | 615 def _GeneratePropertyFunctions(self, param_namespace, params): |
| 605 """Generate the functions for structures generated by a property such as | 616 """Generate the functions for structures generated by a property such as |
| 606 CreateEnumValue for ENUMs and Populate/ToValue for Params/Result objects. | 617 CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects. |
| 607 """ | 618 """ |
| 608 c = Code() | 619 c = Code() |
| 609 for param in params: | 620 for param in params: |
| 610 if param.type_ == PropertyType.OBJECT: | 621 if param.type_ == PropertyType.OBJECT: |
| 611 c.Concat(self._GenerateType( | 622 c.Concat(self._GenerateType( |
| 612 param_namespace + '::' + cpp_util.Classname(param.name), | 623 param_namespace + '::' + cpp_util.Classname(param.name), |
| 613 param)) | 624 param)) |
| 614 c.Append() | 625 c.Append() |
| 615 elif param.type_ == PropertyType.ARRAY: | 626 elif param.type_ == PropertyType.ARRAY: |
| 616 c.Concat(self._GeneratePropertyFunctions( | 627 c.Concat(self._GeneratePropertyFunctions( |
| 617 param_namespace, [param.item_type])) | 628 param_namespace, [param.item_type])) |
| 618 elif param.type_ == PropertyType.CHOICES: | 629 elif param.type_ == PropertyType.CHOICES: |
| 619 c.Concat(self._GeneratePropertyFunctions( | 630 c.Concat(self._GeneratePropertyFunctions( |
| 620 param_namespace, param.choices.values())) | 631 param_namespace, param.choices.values())) |
| 621 elif param.type_ == PropertyType.ENUM: | 632 elif param.type_ == PropertyType.ENUM: |
| 622 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) | 633 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) |
| 623 c.Append() | 634 c.Append() |
| 624 return c | 635 return c |
| 625 | 636 |
| 626 def _GenerateFunctionResultCreate(self, cpp_namespace, function): | 637 def _GenerateCreateCallbackArguments(self, function_scope, callback): |
| 627 """Generate function to create a Result given the return value. | 638 """Generate all functions to create Value parameters for a callback. |
| 628 | 639 |
| 629 E.g for function "Bar", generate Bar::Result::Create | 640 E.g for function "Bar", generate Bar::Results::Create |
| 641 E.g for event "Baz", generate Baz::Create |
| 642 |
| 643 function_scope: the function scope path, e.g. Foo::Bar for the function |
| 644 Foo::Bar::Baz(). |
| 645 callback: the Function object we are creating callback arguments for. |
| 630 """ | 646 """ |
| 631 c = Code() | 647 c = Code() |
| 632 params = function.callback.params | 648 params = callback.params |
| 649 expanded_params = self._cpp_type_generator.ExpandParams(params) |
| 650 c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params)) |
| 633 | 651 |
| 634 if not params: | 652 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) |
| 635 (c.Append('base::Value* %s::Result::Create() {' % cpp_namespace) | 653 for param_list in param_lists: |
| 636 .Append(' return base::Value::CreateNullValue();') | 654 (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s::' |
| 637 .Append('}') | 655 'Create(%(declaration_list)s) {') |
| 656 .Append('scoped_ptr<base::ListValue> create_results(' |
| 657 'new base::ListValue());') |
| 638 ) | 658 ) |
| 639 else: | 659 declaration_list = [] |
| 640 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( | 660 for param in param_list: |
| 641 params) | |
| 642 c.Concat(self._GeneratePropertyFunctions( | |
| 643 cpp_namespace + '::Result', expanded_params)) | |
| 644 | |
| 645 # If there is a single parameter, this is straightforward. However, if | |
| 646 # the callback parameter is of 'choices', this generates a Create method | |
| 647 # for each choice. This works because only 1 choice can be returned at a | |
| 648 # time. | |
| 649 for param in expanded_params: | |
| 650 if param.type_ == PropertyType.ANY: | |
| 651 # Generation of base::Value* Create(base::Value*) is redundant. | |
| 652 continue | |
| 653 # We treat this argument as 'required' to avoid wrapping it in a | 661 # We treat this argument as 'required' to avoid wrapping it in a |
| 654 # scoped_ptr if it's optional. | 662 # scoped_ptr if it's optional. |
| 655 param_copy = param.Copy() | 663 param_copy = param.Copy() |
| 656 param_copy.optional = False | 664 param_copy.optional = False |
| 657 c.Sblock('base::Value* %(cpp_namespace)s::Result::Create(' | 665 c.Append('create_results->Append(%s);' % |
| 658 'const %(arg)s) {') | 666 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) |
| 659 c.Append('return %s;' % | 667 declaration_list.append("const %s" % cpp_util.GetParameterDeclaration( |
| 660 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) | 668 param_copy, self._cpp_type_generator.GetType(param_copy))) |
| 661 c.Eblock('}') | 669 |
| 662 c.Substitute({ | 670 c.Append('return create_results.Pass();') |
| 663 'cpp_namespace': cpp_namespace, | 671 c.Eblock('}') |
| 664 'arg': cpp_util.GetParameterDeclaration( | 672 c.Substitute({ |
| 665 param_copy, self._cpp_type_generator.GetType(param_copy)) | 673 'function_scope': function_scope, |
| 666 }) | 674 'declaration_list': ', '.join(declaration_list) |
| 675 }) |
| 667 | 676 |
| 668 return c | 677 return c |
| 669 | 678 |
| 670 def _InitializePropertyToDefault(self, prop, dst): | 679 def _InitializePropertyToDefault(self, prop, dst): |
| 671 """Initialize a model.Property to its default value inside an object. | 680 """Initialize a model.Property to its default value inside an object. |
| 672 | 681 |
| 673 E.g for optional enum "state", generate dst->state = STATE_NONE; | 682 E.g for optional enum "state", generate dst->state = STATE_NONE; |
| 674 | 683 |
| 675 dst: Type* | 684 dst: Type* |
| 676 """ | 685 """ |
| (...skipping 20 matching lines...) Expand all Loading... |
| 697 """ | 706 """ |
| 698 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 707 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
| 699 PropertyType.ARRAY) | 708 PropertyType.ARRAY) |
| 700 | 709 |
| 701 def _IsFundamentalOrFundamentalRef(self, prop): | 710 def _IsFundamentalOrFundamentalRef(self, prop): |
| 702 """Determines if this property is a Fundamental type or is a ref to a | 711 """Determines if this property is a Fundamental type or is a ref to a |
| 703 Fundamental type. | 712 Fundamental type. |
| 704 """ | 713 """ |
| 705 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 714 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
| 706 is_fundamental) | 715 is_fundamental) |
| OLD | NEW |