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

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: Added more testing for event compilation. 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 (c.Append('//') 79 (c.Append('//')
80 .Append('// Functions') 80 .Append('// Functions')
81 .Append('//') 81 .Append('//')
82 .Append() 82 .Append()
83 ) 83 )
84 for function in self._namespace.functions.values(): 84 for function in self._namespace.functions.values():
85 (c.Concat(self._GenerateFunction( 85 (c.Concat(self._GenerateFunction(
86 cpp_util.Classname(function.name), function)) 86 cpp_util.Classname(function.name), function))
87 .Append() 87 .Append()
88 ) 88 )
89 if self._namespace.events:
90 (c.Append('//')
91 .Append('// Events')
92 .Append('//')
93 .Append()
94 )
95 for event in self._namespace.events.values():
96 (c.Concat(self._GenerateCallbackCreate(
97 cpp_util.Classname(event.name), event))
98 .Append()
99 )
89 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) 100 (c.Concat(self._cpp_type_generator.GetNamespaceEnd())
90 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) 101 .Concat(self._cpp_type_generator.GetRootNamespaceEnd())
91 .Append() 102 .Append()
92 ) 103 )
93 # TODO(calamity): Events
94 return c 104 return c
95 105
96 def _GenerateType(self, cpp_namespace, type_): 106 def _GenerateType(self, cpp_namespace, type_):
97 """Generates the function definitions for a type. 107 """Generates the function definitions for a type.
98 """ 108 """
99 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) 109 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name))
100 c = Code() 110 c = Code()
101 111
102 if type_.functions: 112 if type_.functions:
103 for function in type_.functions.values(): 113 for function in type_.functions.values():
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 function.params)) 283 function.params))
274 (c.Append('%(cpp_namespace)s::Params::Params() {}') 284 (c.Append('%(cpp_namespace)s::Params::Params() {}')
275 .Append('%(cpp_namespace)s::Params::~Params() {}') 285 .Append('%(cpp_namespace)s::Params::~Params() {}')
276 .Append() 286 .Append()
277 .Concat(self._GenerateFunctionParamsCreate(cpp_namespace, function)) 287 .Concat(self._GenerateFunctionParamsCreate(cpp_namespace, function))
278 .Append() 288 .Append()
279 ) 289 )
280 290
281 # Result::Create function 291 # Result::Create function
282 if function.callback: 292 if function.callback:
283 c.Concat(self._GenerateFunctionResultCreate(cpp_namespace, function)) 293 c.Concat(self._GenerateCallbackCreate(
294 "%s::Result" % cpp_namespace, function.callback))
284 295
285 c.Substitute({'cpp_namespace': cpp_namespace}) 296 c.Substitute({'cpp_namespace': cpp_namespace})
286 297
287 return c 298 return c
288 299
289 def _GenerateCreateEnumValue(self, cpp_namespace, prop): 300 def _GenerateCreateEnumValue(self, cpp_namespace, prop):
290 """Generates CreateEnumValue() that returns the |StringValue| 301 """Generates CreateEnumValue() that returns the |StringValue|
291 representation of an enum. 302 representation of an enum.
292 """ 303 """
293 c = Code() 304 c = Code()
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 c.Concat(self._GeneratePropertyFunctions( 591 c.Concat(self._GeneratePropertyFunctions(
581 param_namespace, [param.item_type])) 592 param_namespace, [param.item_type]))
582 elif param.type_ == PropertyType.CHOICES: 593 elif param.type_ == PropertyType.CHOICES:
583 c.Concat(self._GeneratePropertyFunctions( 594 c.Concat(self._GeneratePropertyFunctions(
584 param_namespace, param.choices.values())) 595 param_namespace, param.choices.values()))
585 elif param.type_ == PropertyType.ENUM: 596 elif param.type_ == PropertyType.ENUM:
586 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) 597 c.Concat(self._GenerateCreateEnumValue(param_namespace, param))
587 c.Append() 598 c.Append()
588 return c 599 return c
589 600
590 def _GenerateFunctionResultCreate(self, cpp_namespace, function): 601 def _GenerateCallbackCreate(self, function_scope, callback):
591 """Generate function to create a Result given the return value. 602 """Generate function to create Value parameters for a callback.
592 603
593 E.g for function "Bar", generate Bar::Result::Create 604 E.g for function "Bar", generate Bar::Result::Create
605 E.g for event "Gar", generate Gar::Create
594 """ 606 """
595 c = Code() 607 c = Code()
596 params = function.callback.params 608 params = callback.params
597 609
598 if not params: 610 if not params:
599 (c.Append('Value* %s::Result::Create() {' % cpp_namespace) 611 (c.Append('Value* %s::Create() {' % function_scope)
600 .Append(' return Value::CreateNullValue();') 612 .Append(' return Value::CreateNullValue();')
601 .Append('}') 613 .Append('}')
602 ) 614 )
603 else: 615 else:
604 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( 616 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams(
605 params) 617 params)
606 c.Concat(self._GeneratePropertyFunctions( 618 c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params))
607 cpp_namespace + '::Result', expanded_params))
608 619
609 # If there is a single parameter, this is straightforward. However, if 620 # If there is a single parameter, this is straightforward. However, if
610 # the callback parameter is of 'choices', this generates a Create method 621 # the callback parameter is of 'choices', this generates a Create method
611 # for each choice. This works because only 1 choice can be returned at a 622 # for each choice. This works because only 1 choice can be returned at a
612 # time. 623 # time.
613 for param in expanded_params: 624 for param in expanded_params:
614 if param.type_ == PropertyType.ANY: 625 if param.type_ == PropertyType.ANY:
615 # Generation of Value* Create(Value*) is redundant. 626 # Generation of Value* Create(Value*) is redundant.
616 continue 627 continue
617 # We treat this argument as 'required' to avoid wrapping it in a 628 # We treat this argument as 'required' to avoid wrapping it in a
618 # scoped_ptr if it's optional. 629 # scoped_ptr if it's optional.
619 param_copy = param.Copy() 630 param_copy = param.Copy()
620 param_copy.optional = False 631 param_copy.optional = False
621 c.Sblock('Value* %(cpp_namespace)s::Result::Create(const %(arg)s) {') 632 c.Sblock('Value* %(function_scope)s::Create(const %(arg)s) {')
622 c.Append('return %s;' % 633 c.Append('return %s;' %
623 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) 634 self._CreateValueFromProperty(param_copy, param_copy.unix_name))
624 c.Eblock('}') 635 c.Eblock('}')
625 c.Substitute({ 636 c.Substitute({
626 'cpp_namespace': cpp_namespace, 637 'function_scope': function_scope,
627 'arg': cpp_util.GetParameterDeclaration( 638 'arg': cpp_util.GetParameterDeclaration(
628 param_copy, self._cpp_type_generator.GetType(param_copy)) 639 param_copy, self._cpp_type_generator.GetType(param_copy))
629 }) 640 })
630 641
631 return c 642 return c
632 643
633 def _InitializePropertyToDefault(self, prop, dst): 644 def _InitializePropertyToDefault(self, prop, dst):
634 """Initialize a model.Property to its default value inside an object. 645 """Initialize a model.Property to its default value inside an object.
635 646
636 E.g for optional enum "state", generate dst->state = STATE_NONE; 647 E.g for optional enum "state", generate dst->state = STATE_NONE;
(...skipping 23 matching lines...) Expand all
660 """ 671 """
661 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == 672 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ ==
662 PropertyType.ARRAY) 673 PropertyType.ARRAY)
663 674
664 def _IsFundamentalOrFundamentalRef(self, prop): 675 def _IsFundamentalOrFundamentalRef(self, prop):
665 """Determines if this property is a Fundamental type or is a ref to a 676 """Determines if this property is a Fundamental type or is a ref to a
666 Fundamental type. 677 Fundamental type.
667 """ 678 """
668 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. 679 return (self._cpp_type_generator.GetReferencedProperty(prop).type_.
669 is_fundamental) 680 is_fundamental)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698