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

Side by Side 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: Tested GetAllPossibleParameterLists better. 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 unified diff | Download patch
OLDNEW
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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 (c.Append('//') 73 (c.Append('//')
74 .Append('// Functions') 74 .Append('// Functions')
75 .Append('//') 75 .Append('//')
76 .Append() 76 .Append()
77 ) 77 )
78 for function in self._namespace.functions.values(): 78 for function in self._namespace.functions.values():
79 (c.Concat(self._GenerateFunction( 79 (c.Concat(self._GenerateFunction(
80 cpp_util.Classname(function.name), function)) 80 cpp_util.Classname(function.name), function))
81 .Append() 81 .Append()
82 ) 82 )
83 if self._namespace.events:
84 (c.Append('//')
85 .Append('// Events')
86 .Append('//')
87 .Append()
88 )
89 for event in self._namespace.events.values():
90 (c.Concat(self._GenerateCreateCallbackArguments(
91 cpp_util.Classname(event.name), event))
92 .Append()
93 )
83 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) 94 (c.Concat(self._cpp_type_generator.GetNamespaceEnd())
84 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) 95 .Concat(self._cpp_type_generator.GetRootNamespaceEnd())
85 .Append() 96 .Append()
86 ) 97 )
87 # TODO(calamity): Events
88 return c 98 return c
89 99
90 def _GenerateType(self, cpp_namespace, type_): 100 def _GenerateType(self, cpp_namespace, type_):
91 """Generates the function definitions for a type. 101 """Generates the function definitions for a type.
92 """ 102 """
93 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) 103 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name))
94 c = Code() 104 c = Code()
95 105
96 if type_.functions: 106 if type_.functions:
97 for function in type_.functions.values(): 107 for function in type_.functions.values():
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 function.params)) 279 function.params))
270 (c.Append('%(cpp_namespace)s::Params::Params() {}') 280 (c.Append('%(cpp_namespace)s::Params::Params() {}')
271 .Append('%(cpp_namespace)s::Params::~Params() {}') 281 .Append('%(cpp_namespace)s::Params::~Params() {}')
272 .Append() 282 .Append()
273 .Concat(self._GenerateFunctionParamsCreate(cpp_namespace, function)) 283 .Concat(self._GenerateFunctionParamsCreate(cpp_namespace, function))
274 .Append() 284 .Append()
275 ) 285 )
276 286
277 # Result::Create function 287 # Result::Create function
278 if function.callback: 288 if function.callback:
279 c.Concat(self._GenerateFunctionResultCreate(cpp_namespace, function)) 289 c.Concat(self._GenerateCreateCallbackArguments(
290 "%s::Result" % cpp_namespace, function.callback))
280 291
281 c.Substitute({'cpp_namespace': cpp_namespace}) 292 c.Substitute({'cpp_namespace': cpp_namespace})
282 293
283 return c 294 return c
284 295
285 def _GenerateCreateEnumValue(self, cpp_namespace, prop): 296 def _GenerateCreateEnumValue(self, cpp_namespace, prop):
286 """Generates CreateEnumValue() that returns the |base::StringValue| 297 """Generates CreateEnumValue() that returns the |base::StringValue|
287 representation of an enum. 298 representation of an enum.
288 """ 299 """
289 c = Code() 300 c = Code()
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 self._GenerateListValueToEnumArrayConversion(c, prop) 513 self._GenerateListValueToEnumArrayConversion(c, prop)
503 else: 514 else:
504 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( 515 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList(
505 self._cpp_type_generator.GetReferencedProperty(prop), 'list', 516 self._cpp_type_generator.GetReferencedProperty(prop), 'list',
506 dst + '->' + prop.unix_name, prop.optional)) 517 dst + '->' + prop.unix_name, prop.optional))
507 .Append(' return %(failure_value)s;') 518 .Append(' return %(failure_value)s;')
508 ) 519 )
509 elif prop.type_ == PropertyType.CHOICES: 520 elif prop.type_ == PropertyType.CHOICES:
510 type_var = '%(dst)s->%(name)s_type' 521 type_var = '%(dst)s->%(name)s_type'
511 c.Sblock('switch (%(value_var)s->GetType()) {') 522 c.Sblock('switch (%(value_var)s->GetType()) {')
512 for choice in self._cpp_type_generator.GetExpandedChoicesInParams([prop]): 523 for choice in self._cpp_type_generator.ExpandParams([prop]):
513 (c.Sblock('case %s: {' % cpp_util.GetValueType( 524 (c.Sblock('case %s: {' % cpp_util.GetValueType(
514 self._cpp_type_generator.GetReferencedProperty(choice).type_)) 525 self._cpp_type_generator.GetReferencedProperty(choice).type_))
515 .Concat(self._GeneratePopulatePropertyFromValue( 526 .Concat(self._GeneratePopulatePropertyFromValue(
516 choice, value_var, dst, failure_value, check_type=False)) 527 choice, value_var, dst, failure_value, check_type=False))
517 .Append('%s = %s;' % 528 .Append('%s = %s;' %
518 (type_var, 529 (type_var,
519 self._cpp_type_generator.GetEnumValue( 530 self._cpp_type_generator.GetEnumValue(
520 prop, choice.type_.name))) 531 prop, choice.type_.name)))
521 .Append('break;') 532 .Append('break;')
522 .Eblock('}') 533 .Eblock('}')
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 expanded_params = self._cpp_type_generator.ExpandParams(params)
650 c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params))
633 651
634 if not params: 652 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params)
635 (c.Append('base::Value* %s::Result::Create() {' % cpp_namespace) 653 for param_list in param_lists:
636 .Append(' return base::Value::CreateNullValue();') 654 (c.Sblock(
637 .Append('}') 655 'base::ListValue* %(function_scope)s::Create(%(declaration_list)s) {')
656 .Append('base::ListValue* create_results = new base::ListValue();')
638 ) 657 )
639 else: 658 declaration_list = []
640 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( 659 for param in param_list:
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 base::Value* Create(base::Value*) is redundant.
652 continue
653 # We treat this argument as 'required' to avoid wrapping it in a 660 # We treat this argument as 'required' to avoid wrapping it in a
654 # scoped_ptr if it's optional. 661 # scoped_ptr if it's optional.
655 param_copy = param.Copy() 662 param_copy = param.Copy()
656 param_copy.optional = False 663 param_copy.optional = False
657 c.Sblock('base::Value* %(cpp_namespace)s::Result::Create(' 664 c.Append('create_results->Append(%s);' %
658 'const %(arg)s) {') 665 self._CreateValueFromProperty(param_copy, param_copy.unix_name))
659 c.Append('return %s;' % 666 declaration_list.append("const %s" % cpp_util.GetParameterDeclaration(
660 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) 667 param_copy, self._cpp_type_generator.GetType(param_copy)))
661 c.Eblock('}') 668
662 c.Substitute({ 669 c.Append('return create_results;')
663 'cpp_namespace': cpp_namespace, 670 c.Eblock('}')
664 'arg': cpp_util.GetParameterDeclaration( 671 c.Substitute({
665 param_copy, self._cpp_type_generator.GetType(param_copy)) 672 'function_scope': function_scope,
666 }) 673 'declaration_list': ', '.join(declaration_list)
674 })
667 675
668 return c 676 return c
669 677
670 def _InitializePropertyToDefault(self, prop, dst): 678 def _InitializePropertyToDefault(self, prop, dst):
671 """Initialize a model.Property to its default value inside an object. 679 """Initialize a model.Property to its default value inside an object.
672 680
673 E.g for optional enum "state", generate dst->state = STATE_NONE; 681 E.g for optional enum "state", generate dst->state = STATE_NONE;
674 682
675 dst: Type* 683 dst: Type*
676 """ 684 """
(...skipping 20 matching lines...) Expand all
697 """ 705 """
698 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == 706 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ ==
699 PropertyType.ARRAY) 707 PropertyType.ARRAY)
700 708
701 def _IsFundamentalOrFundamentalRef(self, prop): 709 def _IsFundamentalOrFundamentalRef(self, prop):
702 """Determines if this property is a Fundamental type or is a ref to a 710 """Determines if this property is a Fundamental type or is a ref to a
703 Fundamental type. 711 Fundamental type.
704 """ 712 """
705 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. 713 return (self._cpp_type_generator.GetReferencedProperty(prop).type_.
706 is_fundamental) 714 is_fundamental)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698