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 570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
672 | 672 |
673 def _GenerateReturnCase(self, case_value, return_value): | 673 def _GenerateReturnCase(self, case_value, return_value): |
674 """Generates a single return case for a switch block. | 674 """Generates a single return case for a switch block. |
675 """ | 675 """ |
676 c = Code() | 676 c = Code() |
677 (c.Append('case %s:' % case_value) | 677 (c.Append('case %s:' % case_value) |
678 .Append(' return %s;' % return_value) | 678 .Append(' return %s;' % return_value) |
679 ) | 679 ) |
680 return c | 680 return c |
681 | 681 |
682 def _GenerateCreateCallbackArguments(self, function_scope, callback): | 682 def _GenerateCreateCallbackArguments(self, |
683 function_scope, | |
684 callback, | |
685 generate_to_json=False): | |
683 """Generate all functions to create Value parameters for a callback. | 686 """Generate all functions to create Value parameters for a callback. |
684 | 687 |
685 E.g for function "Bar", generate Bar::Results::Create | 688 E.g for function "Bar", generate Bar::Results::Create |
686 E.g for event "Baz", generate Baz::Create | 689 E.g for event "Baz", generate Baz::Create |
687 | 690 |
688 function_scope: the function scope path, e.g. Foo::Bar for the function | 691 function_scope: the function scope path, e.g. Foo::Bar for the function |
689 Foo::Bar::Baz(). | 692 Foo::Bar::Baz(). |
690 callback: the Function object we are creating callback arguments for. | 693 callback: the Function object we are creating callback arguments for. |
694 generate_to_json: Generate a ToJson method. | |
691 """ | 695 """ |
692 c = Code() | 696 c = Code() |
693 params = callback.params | 697 params = callback.params |
694 expanded_params = self._cpp_type_generator.ExpandParams(params) | 698 expanded_params = self._cpp_type_generator.ExpandParams(params) |
695 c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params)) | 699 c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params)) |
696 | 700 |
697 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) | 701 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) |
698 for param_list in param_lists: | 702 for param_list in param_lists: |
699 (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s::' | 703 (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s::' |
700 'Create(%(declaration_list)s) {') | 704 'Create(%(declaration_list)s) {') |
701 .Append('scoped_ptr<base::ListValue> create_results(' | 705 .Append('scoped_ptr<base::ListValue> create_results(' |
702 'new base::ListValue());') | 706 'new base::ListValue());') |
703 ) | 707 ) |
704 declaration_list = [] | 708 declaration_list = [] |
705 for param in param_list: | 709 for param in param_list: |
706 # We treat this argument as 'required' to avoid wrapping it in a | 710 # We treat this argument as 'required' to avoid wrapping it in a |
707 # scoped_ptr if it's optional. | 711 # scoped_ptr if it's optional. |
708 param_copy = param.Copy() | 712 param_copy = param.Copy() |
709 param_copy.optional = False | 713 param_copy.optional = False |
710 c.Append('create_results->Append(%s);' % | 714 c.Append('create_results->Append(%s);' % |
711 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) | 715 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) |
712 declaration_list.append("const %s" % cpp_util.GetParameterDeclaration( | 716 declaration_list.append("const %s" % cpp_util.GetParameterDeclaration( |
713 param_copy, self._cpp_type_generator.GetType(param_copy))) | 717 param_copy, self._cpp_type_generator.GetType(param_copy))) |
714 | 718 |
715 c.Append('return create_results.Pass();') | 719 c.Append('return create_results.Pass();') |
716 c.Eblock('}') | 720 c.Eblock('}') |
721 if generate_to_json: | |
722 c.Append() | |
723 (c.Sblock('std::string %(function_scope)s::' | |
724 'ToJson(%(declaration_list)s) {') | |
725 .Append('scoped_ptr<base::ListValue> create_results = ' | |
726 'Create(%(param_list)s);') | |
727 .Append('std::string json;') | |
728 .Append('base::JSONWriter::Write(create_results.get(), &json);') | |
729 .Append('return json;') | |
730 ) | |
731 c.Eblock('}') | |
732 | |
717 c.Substitute({ | 733 c.Substitute({ |
718 'function_scope': function_scope, | 734 'function_scope': function_scope, |
719 'declaration_list': ', '.join(declaration_list) | 735 'declaration_list': ', '.join(declaration_list), |
736 'param_list': ', '.join([param.unix_name for param in param_list]) | |
not at google - send to devlin
2012/07/26 02:39:58
I think if you leave out the [] it becomes a gener
mitchellwrosen
2012/07/26 17:38:59
Done.
| |
720 }) | 737 }) |
721 | 738 |
722 return c | 739 return c |
723 | 740 |
724 def _InitializePropertyToDefault(self, prop, dst): | 741 def _InitializePropertyToDefault(self, prop, dst): |
725 """Initialize a model.Property to its default value inside an object. | 742 """Initialize a model.Property to its default value inside an object. |
726 | 743 |
727 E.g for optional enum "state", generate dst->state = STATE_NONE; | 744 E.g for optional enum "state", generate dst->state = STATE_NONE; |
728 | 745 |
729 dst: Type* | 746 dst: Type* |
(...skipping 21 matching lines...) Expand all Loading... | |
751 """ | 768 """ |
752 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 769 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
753 PropertyType.ARRAY) | 770 PropertyType.ARRAY) |
754 | 771 |
755 def _IsFundamentalOrFundamentalRef(self, prop): | 772 def _IsFundamentalOrFundamentalRef(self, prop): |
756 """Determines if this property is a Fundamental type or is a ref to a | 773 """Determines if this property is a Fundamental type or is a ref to a |
757 Fundamental type. | 774 Fundamental type. |
758 """ | 775 """ |
759 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 776 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
760 is_fundamental) | 777 is_fundamental) |
OLD | NEW |