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 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 (c.Append('//') | 79 (c.Append('//') |
| 80 .Append('// Functions') | 80 .Append('// Functions') |
| 81 .Append('//') | 81 .Append('//') |
| 82 .Append() | 82 .Append() |
| 83 ) | 83 ) |
| 84 for function in self._namespace.functions.values(): | 84 for function in self._namespace.functions.values(): |
| 85 (c.Concat(self._GenerateFunction( | 85 (c.Concat(self._GenerateFunction( |
| 86 cpp_util.Classname(function.name), function)) | 86 cpp_util.Classname(function.name), function)) |
| 87 .Append() | 87 .Append() |
| 88 ) | 88 ) |
| 89 if self._namespace.events: | |
| 90 (c.Append('//') | |
| 91 .Append('// Events') | |
| 92 .Append('//') | |
| 93 .Append() | |
| 94 ) | |
| 95 for event in self._namespace.events.values(): | |
| 96 (c.Concat(self._GenerateCreateCallbackArguments( | |
| 97 cpp_util.Classname(event.name), event)) | |
| 98 .Append() | |
| 99 ) | |
| 89 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) | 100 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) |
| 90 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 101 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
| 91 .Append() | 102 .Append() |
| 92 ) | 103 ) |
| 93 # TODO(calamity): Events | |
| 94 return c | 104 return c |
| 95 | 105 |
| 96 def _GenerateType(self, cpp_namespace, type_): | 106 def _GenerateType(self, cpp_namespace, type_): |
| 97 """Generates the function definitions for a type. | 107 """Generates the function definitions for a type. |
| 98 """ | 108 """ |
| 99 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) | 109 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) |
| 100 c = Code() | 110 c = Code() |
| 101 | 111 |
| 102 if type_.functions: | 112 if type_.functions: |
| 103 for function in type_.functions.values(): | 113 for function in type_.functions.values(): |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 function.params)) | 283 function.params)) |
| 274 (c.Append('%(cpp_namespace)s::Params::Params() {}') | 284 (c.Append('%(cpp_namespace)s::Params::Params() {}') |
| 275 .Append('%(cpp_namespace)s::Params::~Params() {}') | 285 .Append('%(cpp_namespace)s::Params::~Params() {}') |
| 276 .Append() | 286 .Append() |
| 277 .Concat(self._GenerateFunctionParamsCreate(cpp_namespace, function)) | 287 .Concat(self._GenerateFunctionParamsCreate(cpp_namespace, function)) |
| 278 .Append() | 288 .Append() |
| 279 ) | 289 ) |
| 280 | 290 |
| 281 # Result::Create function | 291 # Result::Create function |
| 282 if function.callback: | 292 if function.callback: |
| 283 c.Concat(self._GenerateFunctionResultCreate(cpp_namespace, function)) | 293 c.Concat(self._GenerateCreateCallbackArguments( |
| 294 "%s::Result" % cpp_namespace, function.callback)) | |
| 284 | 295 |
| 285 c.Substitute({'cpp_namespace': cpp_namespace}) | 296 c.Substitute({'cpp_namespace': cpp_namespace}) |
| 286 | 297 |
| 287 return c | 298 return c |
| 288 | 299 |
| 289 def _GenerateCreateEnumValue(self, cpp_namespace, prop): | 300 def _GenerateCreateEnumValue(self, cpp_namespace, prop): |
| 290 """Generates CreateEnumValue() that returns the |StringValue| | 301 """Generates CreateEnumValue() that returns the |StringValue| |
| 291 representation of an enum. | 302 representation of an enum. |
| 292 """ | 303 """ |
| 293 c = Code() | 304 c = Code() |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 503 self._GenerateListValueToEnumArrayConversion(c, prop) | 514 self._GenerateListValueToEnumArrayConversion(c, prop) |
| 504 else: | 515 else: |
| 505 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 516 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( |
| 506 self._cpp_type_generator.GetReferencedProperty(prop), 'list', | 517 self._cpp_type_generator.GetReferencedProperty(prop), 'list', |
| 507 dst + '->' + prop.unix_name, prop.optional)) | 518 dst + '->' + prop.unix_name, prop.optional)) |
| 508 .Append(' return %(failure_value)s;') | 519 .Append(' return %(failure_value)s;') |
| 509 ) | 520 ) |
| 510 elif prop.type_ == PropertyType.CHOICES: | 521 elif prop.type_ == PropertyType.CHOICES: |
| 511 type_var = '%(dst)s->%(name)s_type' | 522 type_var = '%(dst)s->%(name)s_type' |
| 512 c.Sblock('switch (%(value_var)s->GetType()) {') | 523 c.Sblock('switch (%(value_var)s->GetType()) {') |
| 513 for choice in self._cpp_type_generator.GetExpandedChoicesInParams([prop]): | 524 for choice in self._cpp_type_generator.GetAllPossibleParameters([prop]): |
| 514 (c.Sblock('case %s: {' % cpp_util.GetValueType( | 525 (c.Sblock('case %s: {' % cpp_util.GetValueType( |
| 515 self._cpp_type_generator.GetReferencedProperty(choice).type_)) | 526 self._cpp_type_generator.GetReferencedProperty(choice).type_)) |
| 516 .Concat(self._GeneratePopulatePropertyFromValue( | 527 .Concat(self._GeneratePopulatePropertyFromValue( |
| 517 choice, value_var, dst, failure_value, check_type=False)) | 528 choice, value_var, dst, failure_value, check_type=False)) |
| 518 .Append('%s = %s;' % | 529 .Append('%s = %s;' % |
| 519 (type_var, | 530 (type_var, |
| 520 self._cpp_type_generator.GetEnumValue( | 531 self._cpp_type_generator.GetEnumValue( |
| 521 prop, choice.type_.name))) | 532 prop, choice.type_.name))) |
| 522 .Append('break;') | 533 .Append('break;') |
| 523 .Eblock('}') | 534 .Eblock('}') |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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::Result::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 all_params = self._cpp_type_generator.GetAllPossibleParameters(params) | |
| 650 c.Concat(self._GeneratePropertyFunctions(function_scope, all_params)) | |
| 633 | 651 |
| 634 if not params: | 652 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) |
| 635 (c.Append('Value* %s::Result::Create() {' % cpp_namespace) | 653 for param_list in param_lists: |
| 636 .Append(' return Value::CreateNullValue();') | 654 c.Sblock('ListValue* %(function_scope)s::Create(%(declaration_list)s) {') |
|
not at google - send to devlin
2012/07/11 07:22:06
return a scoped_ptr<ListValue> rather than a ListV
Matt Tytel
2012/07/12 03:07:56
This CL is getting a bit heavy. Can I do this sepa
not at google - send to devlin
2012/07/12 03:36:49
sounds good.
Matt Tytel
2012/07/13 02:34:43
Zoop. Might as well do it in this CL since I chang
not at google - send to devlin
2012/07/13 02:40:32
Cool sg
| |
| 637 .Append('}') | 655 c.Append('ListValue* create_results = new ListValue();') |
| 638 ) | 656 declaration_list = [] |
| 639 else: | 657 for param in param_list: |
| 640 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( | |
| 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 Value* Create(Value*) is redundant. | |
| 652 continue | |
| 653 # We treat this argument as 'required' to avoid wrapping it in a | 658 # We treat this argument as 'required' to avoid wrapping it in a |
| 654 # scoped_ptr if it's optional. | 659 # scoped_ptr if it's optional. |
| 655 param_copy = param.Copy() | 660 param_copy = param.Copy() |
| 656 param_copy.optional = False | 661 param_copy.optional = False |
| 657 c.Sblock('Value* %(cpp_namespace)s::Result::Create(const %(arg)s) {') | 662 c.Append('create_results->Append(%s);' % |
| 658 c.Append('return %s;' % | 663 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) |
| 659 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) | 664 declaration_list.append("const %s" % cpp_util.GetParameterDeclaration( |
| 660 c.Eblock('}') | 665 param_copy, self._cpp_type_generator.GetType(param_copy))) |
| 661 c.Substitute({ | 666 |
| 662 'cpp_namespace': cpp_namespace, | 667 c.Append('return create_results;') |
|
not at google - send to devlin
2012/07/11 07:22:06
.Pass()
| |
| 663 'arg': cpp_util.GetParameterDeclaration( | 668 c.Eblock('}') |
| 664 param_copy, self._cpp_type_generator.GetType(param_copy)) | 669 c.Substitute({ |
| 665 }) | 670 'function_scope': function_scope, |
| 671 'declaration_list': '%s' % ', '.join(declaration_list) | |
|
not at google - send to devlin
2012/07/11 07:22:06
Is this some kind of Python idiom/requirement I'm
Matt Tytel
2012/07/12 03:07:56
... % '%s' % '%s' % '%s' % ', '.join(declaration_l
| |
| 672 }) | |
| 666 | 673 |
| 667 return c | 674 return c |
| 668 | 675 |
| 669 def _InitializePropertyToDefault(self, prop, dst): | 676 def _InitializePropertyToDefault(self, prop, dst): |
| 670 """Initialize a model.Property to its default value inside an object. | 677 """Initialize a model.Property to its default value inside an object. |
| 671 | 678 |
| 672 E.g for optional enum "state", generate dst->state = STATE_NONE; | 679 E.g for optional enum "state", generate dst->state = STATE_NONE; |
| 673 | 680 |
| 674 dst: Type* | 681 dst: Type* |
| 675 """ | 682 """ |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 696 """ | 703 """ |
| 697 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 704 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
| 698 PropertyType.ARRAY) | 705 PropertyType.ARRAY) |
| 699 | 706 |
| 700 def _IsFundamentalOrFundamentalRef(self, prop): | 707 def _IsFundamentalOrFundamentalRef(self, prop): |
| 701 """Determines if this property is a Fundamental type or is a ref to a | 708 """Determines if this property is a Fundamental type or is a ref to a |
| 702 Fundamental type. | 709 Fundamental type. |
| 703 """ | 710 """ |
| 704 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 711 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
| 705 is_fundamental) | 712 is_fundamental) |
| OLD | NEW |