| OLD | NEW |
| 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, Type | 6 from model import PropertyType, Type |
| 7 import cpp_util | 7 import cpp_util |
| 8 import model | 8 import model |
| 9 import schema_util | 9 import schema_util |
| 10 import sys | 10 import sys |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 def Generate(self): | 24 def Generate(self): |
| 25 """Generates a Code object with the .cc for a single namespace. | 25 """Generates a Code object with the .cc for a single namespace. |
| 26 """ | 26 """ |
| 27 c = Code() | 27 c = Code() |
| 28 (c.Append(cpp_util.CHROMIUM_LICENSE) | 28 (c.Append(cpp_util.CHROMIUM_LICENSE) |
| 29 .Append() | 29 .Append() |
| 30 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 30 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
| 31 .Append() | 31 .Append() |
| 32 .Append(self._util_cc_helper.GetIncludePath()) | 32 .Append(self._util_cc_helper.GetIncludePath()) |
| 33 .Append('#include "base/json/json_writer.h"') |
| 34 .Append('#include "base/logging.h"') |
| 35 .Append('#include "base/string_number_conversions.h"') |
| 33 .Append('#include "%s/%s.h"' % | 36 .Append('#include "%s/%s.h"' % |
| 34 (self._namespace.source_file_dir, self._namespace.unix_name)) | 37 (self._namespace.source_file_dir, self._namespace.unix_name)) |
| 35 .Append('#include "base/logging.h"') | 38 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) |
| 36 .Cblock(self._type_helper.GenerateIncludes()) | |
| 37 .Concat(self._type_helper.GetRootNamespaceStart()) | 39 .Concat(self._type_helper.GetRootNamespaceStart()) |
| 38 .Cblock(self._type_helper.GetNamespaceStart()) | 40 .Cblock(self._type_helper.GetNamespaceStart()) |
| 39 ) | 41 ) |
| 40 if self._namespace.properties: | 42 if self._namespace.properties: |
| 41 (c.Append('//') | 43 (c.Append('//') |
| 42 .Append('// Properties') | 44 .Append('// Properties') |
| 43 .Append('//') | 45 .Append('//') |
| 44 .Append() | 46 .Append() |
| 45 ) | 47 ) |
| 46 for property in self._namespace.properties.values(): | 48 for property in self._namespace.properties.values(): |
| (...skipping 27 matching lines...) Expand all Loading... |
| 74 for event in self._namespace.events.values(): | 76 for event in self._namespace.events.values(): |
| 75 c.Cblock(self._GenerateEvent(event)) | 77 c.Cblock(self._GenerateEvent(event)) |
| 76 (c.Concat(self._type_helper.GetNamespaceEnd()) | 78 (c.Concat(self._type_helper.GetNamespaceEnd()) |
| 77 .Cblock(self._type_helper.GetRootNamespaceEnd()) | 79 .Cblock(self._type_helper.GetRootNamespaceEnd()) |
| 78 ) | 80 ) |
| 79 return c | 81 return c |
| 80 | 82 |
| 81 def _GenerateType(self, cpp_namespace, type_): | 83 def _GenerateType(self, cpp_namespace, type_): |
| 82 """Generates the function definitions for a type. | 84 """Generates the function definitions for a type. |
| 83 """ | 85 """ |
| 84 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) | 86 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
| 85 c = Code() | 87 c = Code() |
| 86 | 88 |
| 87 if type_.functions: | 89 if type_.functions: |
| 88 # Wrap functions within types in the type's namespace. | 90 # Wrap functions within types in the type's namespace. |
| 89 (c.Append('namespace %s {' % classname) | 91 (c.Append('namespace %s {' % classname) |
| 90 .Append()) | 92 .Append()) |
| 91 for function in type_.functions.values(): | 93 for function in type_.functions.values(): |
| 92 c.Cblock(self._GenerateFunction(function)) | 94 c.Cblock(self._GenerateFunction(function)) |
| 93 c.Append('} // namespace %s' % classname) | 95 c.Append('} // namespace %s' % classname) |
| 94 elif type_.property_type == PropertyType.ARRAY: | 96 elif type_.property_type == PropertyType.ARRAY: |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 else: | 159 else: |
| 158 s = '' | 160 s = '' |
| 159 s = s + ' {}' | 161 s = s + ' {}' |
| 160 return Code().Append(s) | 162 return Code().Append(s) |
| 161 | 163 |
| 162 def _GenerateTypePopulate(self, cpp_namespace, type_): | 164 def _GenerateTypePopulate(self, cpp_namespace, type_): |
| 163 """Generates the function for populating a type given a pointer to it. | 165 """Generates the function for populating a type given a pointer to it. |
| 164 | 166 |
| 165 E.g for type "Foo", generates Foo::Populate() | 167 E.g for type "Foo", generates Foo::Populate() |
| 166 """ | 168 """ |
| 167 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) | 169 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
| 168 c = Code() | 170 c = Code() |
| 169 (c.Append('// static') | 171 (c.Append('// static') |
| 170 .Append('bool %(namespace)s::Populate(') | 172 .Append('bool %(namespace)s::Populate(') |
| 171 .Sblock(' const base::Value& value, %(name)s* out) {') | 173 .Sblock(' const base::Value& value, %(name)s* out) {') |
| 172 ) | 174 ) |
| 173 if type_.property_type == PropertyType.CHOICES: | 175 if type_.property_type == PropertyType.CHOICES: |
| 174 for choice in type_.choices: | 176 for choice in type_.choices: |
| 175 value_type = cpp_util.GetValueType(self._type_helper.FollowRef(choice)) | 177 value_type = cpp_util.GetValueType(self._type_helper.FollowRef(choice)) |
| 176 (c.Sblock('if (value.IsType(%s)) {' % value_type) | 178 (c.Sblock('if (value.IsType(%s)) {' % value_type) |
| 177 .Concat(self._GeneratePopulateVariableFromValue( | 179 .Concat(self._GeneratePopulateVariableFromValue( |
| (...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 """ | 705 """ |
| 704 c = Code() | 706 c = Code() |
| 705 for type_ in types: | 707 for type_ in types: |
| 706 c.Cblock(self._GenerateType(namespace, type_)) | 708 c.Cblock(self._GenerateType(namespace, type_)) |
| 707 return c | 709 return c |
| 708 | 710 |
| 709 def _GenerateEnumToString(self, cpp_namespace, type_): | 711 def _GenerateEnumToString(self, cpp_namespace, type_): |
| 710 """Generates ToString() which gets the string representation of an enum. | 712 """Generates ToString() which gets the string representation of an enum. |
| 711 """ | 713 """ |
| 712 c = Code() | 714 c = Code() |
| 713 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) | 715 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
| 714 | 716 |
| 715 if cpp_namespace is not None: | 717 if cpp_namespace is not None: |
| 716 c.Append('// static') | 718 c.Append('// static') |
| 717 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace | 719 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace |
| 718 | 720 |
| 719 c.Sblock('std::string %sToString(%s enum_param) {' % | 721 c.Sblock('std::string %sToString(%s enum_param) {' % |
| 720 (maybe_namespace, classname)) | 722 (maybe_namespace, classname)) |
| 721 c.Sblock('switch (enum_param) {') | 723 c.Sblock('switch (enum_param) {') |
| 722 for enum_value in self._type_helper.FollowRef(type_).enum_values: | 724 for enum_value in self._type_helper.FollowRef(type_).enum_values: |
| 723 (c.Append('case %s: ' % self._type_helper.GetEnumValue(type_, enum_value)) | 725 (c.Append('case %s: ' % self._type_helper.GetEnumValue(type_, enum_value)) |
| 724 .Append(' return "%s";' % enum_value)) | 726 .Append(' return "%s";' % enum_value)) |
| 725 (c.Append('case %s:' % self._type_helper.GetEnumNoneValue(type_)) | 727 (c.Append('case %s:' % self._type_helper.GetEnumNoneValue(type_)) |
| 726 .Append(' return "";') | 728 .Append(' return "";') |
| 727 .Eblock('}') | 729 .Eblock('}') |
| 728 .Append('NOTREACHED();') | 730 .Append('NOTREACHED();') |
| 729 .Append('return "";') | 731 .Append('return "";') |
| 730 .Eblock('}') | 732 .Eblock('}') |
| 731 ) | 733 ) |
| 732 return c | 734 return c |
| 733 | 735 |
| 734 def _GenerateEnumFromString(self, cpp_namespace, type_): | 736 def _GenerateEnumFromString(self, cpp_namespace, type_): |
| 735 """Generates FromClassNameString() which gets an enum from its string | 737 """Generates FromClassNameString() which gets an enum from its string |
| 736 representation. | 738 representation. |
| 737 """ | 739 """ |
| 738 c = Code() | 740 c = Code() |
| 739 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) | 741 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
| 740 | 742 |
| 741 if cpp_namespace is not None: | 743 if cpp_namespace is not None: |
| 742 c.Append('// static') | 744 c.Append('// static') |
| 743 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace | 745 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace |
| 744 | 746 |
| 745 c.Sblock('%s%s %sParse%s(const std::string& enum_string) {' % | 747 c.Sblock('%s%s %sParse%s(const std::string& enum_string) {' % |
| 746 (maybe_namespace, classname, maybe_namespace, classname)) | 748 (maybe_namespace, classname, maybe_namespace, classname)) |
| 747 for i, enum_value in enumerate( | 749 for i, enum_value in enumerate( |
| 748 self._type_helper.FollowRef(type_).enum_values): | 750 self._type_helper.FollowRef(type_).enum_values): |
| 749 # This is broken up into all ifs with no else ifs because we get | 751 # This is broken up into all ifs with no else ifs because we get |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 """ | 802 """ |
| 801 c = Code() | 803 c = Code() |
| 802 underlying_type = self._type_helper.FollowRef(prop.type_) | 804 underlying_type = self._type_helper.FollowRef(prop.type_) |
| 803 if (underlying_type.property_type == PropertyType.ENUM and | 805 if (underlying_type.property_type == PropertyType.ENUM and |
| 804 prop.optional): | 806 prop.optional): |
| 805 c.Append('%s->%s = %s;' % ( | 807 c.Append('%s->%s = %s;' % ( |
| 806 dst, | 808 dst, |
| 807 prop.unix_name, | 809 prop.unix_name, |
| 808 self._type_helper.GetEnumNoneValue(prop.type_))) | 810 self._type_helper.GetEnumNoneValue(prop.type_))) |
| 809 return c | 811 return c |
| OLD | NEW |