Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Unified Diff: tools/json_schema_compiler/cc_generator.py

Issue 10701012: JSON Schema Compiler: Added event compilation. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Synced. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/common/extensions/api/storage.json ('k') | tools/json_schema_compiler/cpp_type_generator.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/json_schema_compiler/cc_generator.py
diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py
index 99e0eb3c16a324c6827499bef7f026cb3b3d8748..d21e160e6266a8362bb31ef99fab2794fef4e31a 100644
--- a/tools/json_schema_compiler/cc_generator.py
+++ b/tools/json_schema_compiler/cc_generator.py
@@ -80,11 +80,21 @@ class CCGenerator(object):
cpp_util.Classname(function.name), function))
.Append()
)
+ if self._namespace.events:
+ (c.Append('//')
+ .Append('// Events')
+ .Append('//')
+ .Append()
+ )
+ for event in self._namespace.events.values():
+ (c.Concat(self._GenerateCreateCallbackArguments(
+ cpp_util.Classname(event.name), event))
+ .Append()
+ )
(c.Concat(self._cpp_type_generator.GetNamespaceEnd())
.Concat(self._cpp_type_generator.GetRootNamespaceEnd())
.Append()
)
- # TODO(calamity): Events
return c
def _GenerateType(self, cpp_namespace, type_):
@@ -274,9 +284,10 @@ class CCGenerator(object):
.Append()
)
- # Result::Create function
+ # Results::Create function
if function.callback:
- c.Concat(self._GenerateFunctionResultCreate(cpp_namespace, function))
+ c.Concat(self._GenerateCreateCallbackArguments(
+ "%s::Results" % cpp_namespace, function.callback))
c.Substitute({'cpp_namespace': cpp_namespace})
@@ -509,7 +520,7 @@ class CCGenerator(object):
elif prop.type_ == PropertyType.CHOICES:
type_var = '%(dst)s->%(name)s_type'
c.Sblock('switch (%(value_var)s->GetType()) {')
- for choice in self._cpp_type_generator.GetExpandedChoicesInParams([prop]):
+ for choice in self._cpp_type_generator.ExpandParams([prop]):
(c.Sblock('case %s: {' % cpp_util.GetValueType(
self._cpp_type_generator.GetReferencedProperty(choice).type_))
.Concat(self._GeneratePopulatePropertyFromValue(
@@ -603,7 +614,7 @@ class CCGenerator(object):
def _GeneratePropertyFunctions(self, param_namespace, params):
"""Generate the functions for structures generated by a property such as
- CreateEnumValue for ENUMs and Populate/ToValue for Params/Result objects.
+ CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects.
"""
c = Code()
for param in params:
@@ -623,47 +634,45 @@ class CCGenerator(object):
c.Append()
return c
- def _GenerateFunctionResultCreate(self, cpp_namespace, function):
- """Generate function to create a Result given the return value.
+ def _GenerateCreateCallbackArguments(self, function_scope, callback):
+ """Generate all functions to create Value parameters for a callback.
- E.g for function "Bar", generate Bar::Result::Create
+ E.g for function "Bar", generate Bar::Results::Create
+ E.g for event "Baz", generate Baz::Create
+
+ function_scope: the function scope path, e.g. Foo::Bar for the function
+ Foo::Bar::Baz().
+ callback: the Function object we are creating callback arguments for.
"""
c = Code()
- params = function.callback.params
-
- if not params:
- (c.Append('base::Value* %s::Result::Create() {' % cpp_namespace)
- .Append(' return base::Value::CreateNullValue();')
- .Append('}')
+ params = callback.params
+ expanded_params = self._cpp_type_generator.ExpandParams(params)
+ c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params))
+
+ param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params)
+ for param_list in param_lists:
+ (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s::'
+ 'Create(%(declaration_list)s) {')
+ .Append('scoped_ptr<base::ListValue> create_results('
+ 'new base::ListValue());')
)
- else:
- expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams(
- params)
- c.Concat(self._GeneratePropertyFunctions(
- cpp_namespace + '::Result', expanded_params))
-
- # If there is a single parameter, this is straightforward. However, if
- # the callback parameter is of 'choices', this generates a Create method
- # for each choice. This works because only 1 choice can be returned at a
- # time.
- for param in expanded_params:
- if param.type_ == PropertyType.ANY:
- # Generation of base::Value* Create(base::Value*) is redundant.
- continue
+ declaration_list = []
+ for param in param_list:
# We treat this argument as 'required' to avoid wrapping it in a
# scoped_ptr if it's optional.
param_copy = param.Copy()
param_copy.optional = False
- c.Sblock('base::Value* %(cpp_namespace)s::Result::Create('
- 'const %(arg)s) {')
- c.Append('return %s;' %
- self._CreateValueFromProperty(param_copy, param_copy.unix_name))
- c.Eblock('}')
- c.Substitute({
- 'cpp_namespace': cpp_namespace,
- 'arg': cpp_util.GetParameterDeclaration(
- param_copy, self._cpp_type_generator.GetType(param_copy))
- })
+ c.Append('create_results->Append(%s);' %
+ self._CreateValueFromProperty(param_copy, param_copy.unix_name))
+ declaration_list.append("const %s" % cpp_util.GetParameterDeclaration(
+ param_copy, self._cpp_type_generator.GetType(param_copy)))
+
+ c.Append('return create_results.Pass();')
+ c.Eblock('}')
+ c.Substitute({
+ 'function_scope': function_scope,
+ 'declaration_list': ', '.join(declaration_list)
+ })
return c
« no previous file with comments | « chrome/common/extensions/api/storage.json ('k') | tools/json_schema_compiler/cpp_type_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698