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

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: 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 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
not at google - send to devlin 2012/07/03 14:21:39 hooray!
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(
not at google - send to devlin 2012/07/03 14:21:39 Some of the function names in json_schema_compiler
Matt Tytel 2012/07/11 02:08:50 Done.
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 322 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 _GenerateCallbackCreate(self, function_scope, callback):
not at google - send to devlin 2012/07/03 14:21:39 the name "function_scope" keeps tripping me up. ju
Matt Tytel 2012/07/11 02:08:50 In this meaning, for a function A::B::C(), A::B is
627 """Generate function to create a Result given the return value. 638 """Generate function 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 "Gar", generate Gar::Create
not at google - send to devlin 2012/07/03 14:21:39 ITYM "Baz".
Matt Tytel 2012/07/11 02:08:50 ITYR Done.
630 """ 642 """
631 c = Code() 643 c = Code()
632 params = function.callback.params 644 params = callback.params
633 645
634 if not params: 646 if not params:
635 (c.Append('Value* %s::Result::Create() {' % cpp_namespace) 647 (c.Append('Value* %s::Create() {' % function_scope)
636 .Append(' return Value::CreateNullValue();') 648 .Append(' return Value::CreateNullValue();')
637 .Append('}') 649 .Append('}')
638 ) 650 )
639 else: 651 else:
640 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( 652 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams(
641 params) 653 params)
642 c.Concat(self._GeneratePropertyFunctions( 654 c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params))
643 cpp_namespace + '::Result', expanded_params))
644 655
645 # If there is a single parameter, this is straightforward. However, if 656 # If there is a single parameter, this is straightforward. However, if
646 # the callback parameter is of 'choices', this generates a Create method 657 # 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 658 # for each choice. This works because only 1 choice can be returned at a
648 # time. 659 # time.
not at google - send to devlin 2012/07/03 14:21:39 This will, sadly, require some more reshuffling to
Matt Tytel 2012/07/11 02:08:50 Done.
649 for param in expanded_params: 660 for param in expanded_params:
650 if param.type_ == PropertyType.ANY: 661 if param.type_ == PropertyType.ANY:
651 # Generation of Value* Create(Value*) is redundant. 662 # Generation of Value* Create(Value*) is redundant.
652 continue 663 continue
653 # We treat this argument as 'required' to avoid wrapping it in a 664 # We treat this argument as 'required' to avoid wrapping it in a
654 # scoped_ptr if it's optional. 665 # scoped_ptr if it's optional.
655 param_copy = param.Copy() 666 param_copy = param.Copy()
656 param_copy.optional = False 667 param_copy.optional = False
657 c.Sblock('Value* %(cpp_namespace)s::Result::Create(const %(arg)s) {') 668 c.Sblock('Value* %(function_scope)s::Create(const %(arg)s) {')
not at google - send to devlin 2012/07/03 14:21:39 possible cleanup: make this return scoped_ptr<Valu
Matt Tytel 2012/07/11 02:08:50 I really like this, I'll file a separate issue if
658 c.Append('return %s;' % 669 c.Append('return %s;' %
659 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) 670 self._CreateValueFromProperty(param_copy, param_copy.unix_name))
660 c.Eblock('}') 671 c.Eblock('}')
661 c.Substitute({ 672 c.Substitute({
662 'cpp_namespace': cpp_namespace, 673 'function_scope': function_scope,
663 'arg': cpp_util.GetParameterDeclaration( 674 'arg': cpp_util.GetParameterDeclaration(
664 param_copy, self._cpp_type_generator.GetType(param_copy)) 675 param_copy, self._cpp_type_generator.GetType(param_copy))
665 }) 676 })
666 677
667 return c 678 return c
668 679
669 def _InitializePropertyToDefault(self, prop, dst): 680 def _InitializePropertyToDefault(self, prop, dst):
670 """Initialize a model.Property to its default value inside an object. 681 """Initialize a model.Property to its default value inside an object.
671 682
672 E.g for optional enum "state", generate dst->state = STATE_NONE; 683 E.g for optional enum "state", generate dst->state = STATE_NONE;
(...skipping 23 matching lines...) Expand all
696 """ 707 """
697 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == 708 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ ==
698 PropertyType.ARRAY) 709 PropertyType.ARRAY)
699 710
700 def _IsFundamentalOrFundamentalRef(self, prop): 711 def _IsFundamentalOrFundamentalRef(self, prop):
701 """Determines if this property is a Fundamental type or is a ref to a 712 """Determines if this property is a Fundamental type or is a ref to a
702 Fundamental type. 713 Fundamental type.
703 """ 714 """
704 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. 715 return (self._cpp_type_generator.GetReferencedProperty(prop).type_.
705 is_fundamental) 716 is_fundamental)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698