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

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: Reworked Create functions and lots of tests. 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
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 1a7f80c8bc4afbf0e0f76c015b3ae5667ac82137..5b4aa90ab1bc33ce55eac167f5d989fb67e8c449 100644
--- a/tools/json_schema_compiler/cc_generator.py
+++ b/tools/json_schema_compiler/cc_generator.py
@@ -86,11 +86,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_):
@@ -280,7 +290,8 @@ class CCGenerator(object):
# Result::Create function
if function.callback:
- c.Concat(self._GenerateFunctionResultCreate(cpp_namespace, function))
+ c.Concat(self._GenerateCreateCallbackArguments(
+ "%s::Result" % cpp_namespace, function.callback))
c.Substitute({'cpp_namespace': cpp_namespace})
@@ -510,7 +521,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.GetAllPossibleParameters([prop]):
(c.Sblock('case %s: {' % cpp_util.GetValueType(
self._cpp_type_generator.GetReferencedProperty(choice).type_))
.Concat(self._GeneratePopulatePropertyFromValue(
@@ -623,46 +634,42 @@ 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 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('Value* %s::Result::Create() {' % cpp_namespace)
- .Append(' return Value::CreateNullValue();')
- .Append('}')
- )
- 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 Value* Create(Value*) is redundant.
- continue
+ params = callback.params
+ all_params = self._cpp_type_generator.GetAllPossibleParameters(params)
+ c.Concat(self._GeneratePropertyFunctions(function_scope, all_params))
+
+ param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params)
+ for param_list in param_lists:
+ 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
+ c.Append('ListValue* create_results = new ListValue();')
+ 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('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;')
not at google - send to devlin 2012/07/11 07:22:06 .Pass()
+ c.Eblock('}')
+ c.Substitute({
+ 'function_scope': function_scope,
+ '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
+ })
return c

Powered by Google App Engine
This is Rietveld 408576698