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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 .Append() | 81 .Append() |
82 ) | 82 ) |
83 if self._namespace.events: | 83 if self._namespace.events: |
84 (c.Append('//') | 84 (c.Append('//') |
85 .Append('// Events') | 85 .Append('// Events') |
86 .Append('//') | 86 .Append('//') |
87 .Append() | 87 .Append() |
88 ) | 88 ) |
89 for event in self._namespace.events.values(): | 89 for event in self._namespace.events.values(): |
90 (c.Concat(self._GenerateCreateCallbackArguments( | 90 (c.Concat(self._GenerateCreateCallbackArguments( |
91 cpp_util.Classname(event.name), event)) | 91 cpp_util.Classname(event.name), event, generate_to_json=True)) |
92 .Append() | 92 .Append() |
93 ) | 93 ) |
94 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) | 94 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) |
95 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 95 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
96 .Append() | 96 .Append() |
97 ) | 97 ) |
98 return c | 98 return c |
99 | 99 |
100 def _GenerateType(self, cpp_namespace, type_): | 100 def _GenerateType(self, cpp_namespace, type_): |
101 """Generates the function definitions for a type. | 101 """Generates the function definitions for a type. |
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 | 682 |
683 def _GenerateReturnCase(self, case_value, return_value): | 683 def _GenerateReturnCase(self, case_value, return_value): |
684 """Generates a single return case for a switch block. | 684 """Generates a single return case for a switch block. |
685 """ | 685 """ |
686 c = Code() | 686 c = Code() |
687 (c.Append('case %s:' % case_value) | 687 (c.Append('case %s:' % case_value) |
688 .Append(' return %s;' % return_value) | 688 .Append(' return %s;' % return_value) |
689 ) | 689 ) |
690 return c | 690 return c |
691 | 691 |
692 def _GenerateCreateCallbackArguments(self, function_scope, callback): | 692 def _GenerateCreateCallbackArguments(self, |
| 693 function_scope, |
| 694 callback, |
| 695 generate_to_json=False): |
693 """Generate all functions to create Value parameters for a callback. | 696 """Generate all functions to create Value parameters for a callback. |
694 | 697 |
695 E.g for function "Bar", generate Bar::Results::Create | 698 E.g for function "Bar", generate Bar::Results::Create |
696 E.g for event "Baz", generate Baz::Create | 699 E.g for event "Baz", generate Baz::Create |
697 | 700 |
698 function_scope: the function scope path, e.g. Foo::Bar for the function | 701 function_scope: the function scope path, e.g. Foo::Bar for the function |
699 Foo::Bar::Baz(). | 702 Foo::Bar::Baz(). |
700 callback: the Function object we are creating callback arguments for. | 703 callback: the Function object we are creating callback arguments for. |
| 704 generate_to_json: Generate a ToJson method. |
701 """ | 705 """ |
702 c = Code() | 706 c = Code() |
703 params = callback.params | 707 params = callback.params |
704 expanded_params = self._cpp_type_generator.ExpandParams(params) | 708 expanded_params = self._cpp_type_generator.ExpandParams(params) |
705 c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params)) | 709 c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params)) |
706 | 710 |
707 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) | 711 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) |
708 for param_list in param_lists: | 712 for param_list in param_lists: |
709 (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s::' | 713 (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s::' |
710 'Create(%(declaration_list)s) {') | 714 'Create(%(declaration_list)s) {') |
711 .Append('scoped_ptr<base::ListValue> create_results(' | 715 .Append('scoped_ptr<base::ListValue> create_results(' |
712 'new base::ListValue());') | 716 'new base::ListValue());') |
713 ) | 717 ) |
714 declaration_list = [] | 718 declaration_list = [] |
715 for param in param_list: | 719 for param in param_list: |
716 # We treat this argument as 'required' to avoid wrapping it in a | 720 # We treat this argument as 'required' to avoid wrapping it in a |
717 # scoped_ptr if it's optional. | 721 # scoped_ptr if it's optional. |
718 param_copy = param.Copy() | 722 param_copy = param.Copy() |
719 param_copy.optional = False | 723 param_copy.optional = False |
720 c.Append('create_results->Append(%s);' % | 724 c.Append('create_results->Append(%s);' % |
721 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) | 725 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) |
722 declaration_list.append("const %s" % cpp_util.GetParameterDeclaration( | 726 declaration_list.append("const %s" % cpp_util.GetParameterDeclaration( |
723 param_copy, self._cpp_type_generator.GetType(param_copy))) | 727 param_copy, self._cpp_type_generator.GetType(param_copy))) |
724 | 728 |
725 c.Append('return create_results.Pass();') | 729 c.Append('return create_results.Pass();') |
726 c.Eblock('}') | 730 c.Eblock('}') |
| 731 if generate_to_json: |
| 732 c.Append() |
| 733 (c.Sblock('std::string %(function_scope)s::' |
| 734 'ToJson(%(declaration_list)s) {') |
| 735 .Append('scoped_ptr<base::ListValue> create_results = ' |
| 736 '%(function_scope)s::Create(%(param_list)s);') |
| 737 .Append('std::string json;') |
| 738 .Append('base::JSONWriter::Write(create_results.get(), &json);') |
| 739 .Append('return json;') |
| 740 ) |
| 741 c.Eblock('}') |
| 742 |
727 c.Substitute({ | 743 c.Substitute({ |
728 'function_scope': function_scope, | 744 'function_scope': function_scope, |
729 'declaration_list': ', '.join(declaration_list) | 745 'declaration_list': ', '.join(declaration_list), |
| 746 'param_list': ', '.join(param.unix_name for param in param_list) |
730 }) | 747 }) |
731 | 748 |
732 return c | 749 return c |
733 | 750 |
734 def _InitializePropertyToDefault(self, prop, dst): | 751 def _InitializePropertyToDefault(self, prop, dst): |
735 """Initialize a model.Property to its default value inside an object. | 752 """Initialize a model.Property to its default value inside an object. |
736 | 753 |
737 E.g for optional enum "state", generate dst->state = STATE_NONE; | 754 E.g for optional enum "state", generate dst->state = STATE_NONE; |
738 | 755 |
739 dst: Type* | 756 dst: Type* |
(...skipping 21 matching lines...) Expand all Loading... |
761 """ | 778 """ |
762 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 779 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
763 PropertyType.ARRAY) | 780 PropertyType.ARRAY) |
764 | 781 |
765 def _IsFundamentalOrFundamentalRef(self, prop): | 782 def _IsFundamentalOrFundamentalRef(self, prop): |
766 """Determines if this property is a Fundamental type or is a ref to a | 783 """Determines if this property is a Fundamental type or is a ref to a |
767 Fundamental type. | 784 Fundamental type. |
768 """ | 785 """ |
769 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 786 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
770 is_fundamental) | 787 is_fundamental) |
OLD | NEW |