diff options
Diffstat (limited to 'tools/json_schema_compiler/cc_generator.py')
-rw-r--r-- | tools/json_schema_compiler/cc_generator.py | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py index 138f6bf..7f7cb56 100644 --- a/tools/json_schema_compiler/cc_generator.py +++ b/tools/json_schema_compiler/cc_generator.py @@ -2,9 +2,9 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +from code import Code from model import PropertyType import any_helper -import code import cpp_util import model import sys @@ -23,9 +23,9 @@ class CCGenerator(object): self._any_helper = any_helper.AnyHelper() def Generate(self): - """Generates a code.Code object with the .cc for a single namespace. + """Generates a Code object with the .cc for a single namespace. """ - c = code.Code() + c = Code() (c.Append(cpp_util.CHROMIUM_LICENSE) .Append() .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) @@ -50,6 +50,19 @@ class CCGenerator(object): .Concat(self._cpp_type_generator.GetNamespaceStart()) .Append() ) + if self._namespace.properties: + (c.Append('//') + .Append('// Properties') + .Append('//') + .Append() + ) + for property in self._namespace.properties.values(): + property_code = self._cpp_type_generator.GeneratePropertyValues( + property, + 'const %(type)s %(name)s = %(value)s;', + nodoc=True) + if property_code: + c.Concat(property_code).Append() if self._namespace.types: (c.Append('//') .Append('// Types') @@ -82,7 +95,7 @@ class CCGenerator(object): """Generates the function definitions for a type. """ classname = cpp_util.Classname(type_.name) - c = code.Code() + c = Code() if type_.functions: # Types with functions are not instantiable in C++ because they are @@ -151,7 +164,7 @@ class CCGenerator(object): else: s = '' s = s + ' {}' - return code.Code().Append(s) + return Code().Append(s) def _GenerateTypePopulate(self, cpp_namespace, type_): """Generates the function for populating a type given a pointer to it. @@ -159,7 +172,7 @@ class CCGenerator(object): E.g for type "Foo", generates Foo::Populate() """ classname = cpp_util.Classname(type_.name) - c = code.Code() + c = Code() (c.Append('// static') .Sblock('bool %(namespace)s::Populate' '(const Value& value, %(name)s* out) {') @@ -194,7 +207,7 @@ class CCGenerator(object): src: DictionaryValue* dst: Type* """ - c = code.Code() + c = Code() value_var = prop.unix_name + '_value' c.Append('Value* %(value_var)s = NULL;') if prop.optional: @@ -221,7 +234,7 @@ class CCGenerator(object): E.g. for type "Foo" generates Foo::ToValue() """ - c = code.Code() + c = Code() (c.Sblock('scoped_ptr<DictionaryValue> %s::ToValue() const {' % cpp_namespace) .Append('scoped_ptr<DictionaryValue> value(new DictionaryValue());') @@ -252,7 +265,7 @@ class CCGenerator(object): def _GenerateFunction(self, cpp_namespace, function): """Generates the definitions for function structs. """ - c = code.Code() + c = Code() # Params::Populate function if function.params: @@ -277,7 +290,7 @@ class CCGenerator(object): """Generates CreateEnumValue() that returns the |StringValue| representation of an enum. """ - c = code.Code() + c = Code() c.Append('// static') c.Sblock('scoped_ptr<Value> %(cpp_namespace)s::CreateEnumValue(%(arg)s) {') c.Sblock('switch (%s) {' % prop.unix_name) @@ -353,7 +366,7 @@ class CCGenerator(object): """Generates a check for the correct number of arguments when creating Params. """ - c = code.Code() + c = Code() num_required = 0 for param in function.params: if not param.optional: @@ -379,7 +392,7 @@ class CCGenerator(object): E.g for function "Bar", generate Bar::Params::Create() """ - c = code.Code() + c = Code() (c.Append('// static') .Sblock('scoped_ptr<%(cpp_namespace)s::Params> ' '%(cpp_namespace)s::Params::Create(const ListValue& args) {') @@ -434,7 +447,7 @@ class CCGenerator(object): |value_var| check_type: if true, will check if |value_var| is the correct Value::Type """ - c = code.Code() + c = Code() c.Sblock('{') if check_type and prop.type_ not in ( @@ -544,7 +557,7 @@ class CCGenerator(object): """Generate the functions for structures generated by a property such as CreateEnumValue for ENUMs and Populate/ToValue for Params/Result objects. """ - c = code.Code() + c = Code() for param in params: if param.type_ == PropertyType.OBJECT: c.Concat(self._GenerateType( @@ -564,7 +577,7 @@ class CCGenerator(object): E.g for function "Bar", generate Bar::Result::Create """ - c = code.Code() + c = Code() params = function.callback.params if not params: @@ -609,7 +622,7 @@ class CCGenerator(object): dst: Type* """ - c = code.Code() + c = Code() if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES): if prop.optional: prop_name = prop.unix_name |