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 | 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 sys | 11 import sys |
11 import util_cc_helper | 12 import util_cc_helper |
12 | 13 |
13 class CCGenerator(object): | 14 class CCGenerator(object): |
14 """A .cc generator for a namespace. | 15 """A .cc generator for a namespace. |
15 """ | 16 """ |
16 def __init__(self, namespace, cpp_type_generator): | 17 def __init__(self, namespace, cpp_type_generator): |
17 self._cpp_type_generator = cpp_type_generator | 18 self._cpp_type_generator = cpp_type_generator |
18 self._namespace = namespace | 19 self._namespace = namespace |
19 self._target_namespace = ( | 20 self._target_namespace = ( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 nodoc=True) | 65 nodoc=True) |
65 if property_code: | 66 if property_code: |
66 c.Concat(property_code).Append() | 67 c.Concat(property_code).Append() |
67 if self._namespace.types: | 68 if self._namespace.types: |
68 (c.Append('//') | 69 (c.Append('//') |
69 .Append('// Types') | 70 .Append('// Types') |
70 .Append('//') | 71 .Append('//') |
71 .Append() | 72 .Append() |
72 ) | 73 ) |
73 for type_ in self._namespace.types.values(): | 74 for type_ in self._namespace.types.values(): |
74 (c.Concat(self._GenerateType(type_.name, type_)) | 75 (c.Concat(self._GenerateType( |
75 .Append() | 76 schema_util.StripSchemaNamespace(type_.name), type_)).Append() |
76 ) | 77 ) |
77 if self._namespace.functions: | 78 if self._namespace.functions: |
78 (c.Append('//') | 79 (c.Append('//') |
79 .Append('// Functions') | 80 .Append('// Functions') |
80 .Append('//') | 81 .Append('//') |
81 .Append() | 82 .Append() |
82 ) | 83 ) |
83 for function in self._namespace.functions.values(): | 84 for function in self._namespace.functions.values(): |
84 (c.Concat(self._GenerateFunction( | 85 (c.Concat(self._GenerateFunction( |
85 cpp_util.Classname(function.name), function)) | 86 cpp_util.Classname(function.name), function)) |
86 .Append() | 87 .Append() |
87 ) | 88 ) |
88 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) | 89 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) |
89 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 90 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
90 .Append() | 91 .Append() |
91 ) | 92 ) |
92 # TODO(calamity): Events | 93 # TODO(calamity): Events |
93 return c | 94 return c |
94 | 95 |
95 def _GenerateType(self, cpp_namespace, type_): | 96 def _GenerateType(self, cpp_namespace, type_): |
96 """Generates the function definitions for a type. | 97 """Generates the function definitions for a type. |
97 """ | 98 """ |
98 classname = cpp_util.Classname(type_.name) | 99 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) |
99 c = Code() | 100 c = Code() |
100 | 101 |
101 if type_.functions: | 102 if type_.functions: |
102 # Types with functions are not instantiable in C++ because they are | 103 # Types with functions are not instantiable in C++ because they are |
103 # handled in pure Javascript and hence have no properties or | 104 # handled in pure Javascript and hence have no properties or |
104 # additionalProperties. | 105 # additionalProperties. |
105 if type_.properties: | 106 if type_.properties: |
106 raise NotImplementedError('\n'.join(model.GetModelHierarchy(type_)) + | 107 raise NotImplementedError('\n'.join(model.GetModelHierarchy(type_)) + |
107 '\nCannot generate both functions and properties on a type') | 108 '\nCannot generate both functions and properties on a type') |
108 for function in type_.functions.values(): | 109 for function in type_.functions.values(): |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 else: | 168 else: |
168 s = '' | 169 s = '' |
169 s = s + ' {}' | 170 s = s + ' {}' |
170 return Code().Append(s) | 171 return Code().Append(s) |
171 | 172 |
172 def _GenerateTypePopulate(self, cpp_namespace, type_): | 173 def _GenerateTypePopulate(self, cpp_namespace, type_): |
173 """Generates the function for populating a type given a pointer to it. | 174 """Generates the function for populating a type given a pointer to it. |
174 | 175 |
175 E.g for type "Foo", generates Foo::Populate() | 176 E.g for type "Foo", generates Foo::Populate() |
176 """ | 177 """ |
177 classname = cpp_util.Classname(type_.name) | 178 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) |
178 c = Code() | 179 c = Code() |
179 (c.Append('// static') | 180 (c.Append('// static') |
180 .Sblock('bool %(namespace)s::Populate' | 181 .Sblock('bool %(namespace)s::Populate' |
181 '(const Value& value, %(name)s* out) {') | 182 '(const Value& value, %(name)s* out) {') |
182 .Append('if (!value.IsType(Value::TYPE_DICTIONARY))') | 183 .Append('if (!value.IsType(Value::TYPE_DICTIONARY))') |
183 .Append(' return false;') | 184 .Append(' return false;') |
184 ) | 185 ) |
185 if type_.properties: | 186 if type_.properties: |
186 (c.Append('const DictionaryValue* dict = ' | 187 (c.Append('const DictionaryValue* dict = ' |
187 'static_cast<const DictionaryValue*>(&value);') | 188 'static_cast<const DictionaryValue*>(&value);') |
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 """ | 660 """ |
660 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 661 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
661 PropertyType.ARRAY) | 662 PropertyType.ARRAY) |
662 | 663 |
663 def _IsFundamentalOrFundamentalRef(self, prop): | 664 def _IsFundamentalOrFundamentalRef(self, prop): |
664 """Determines if this property is a Fundamental type or is a ref to a | 665 """Determines if this property is a Fundamental type or is a ref to a |
665 Fundamental type. | 666 Fundamental type. |
666 """ | 667 """ |
667 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 668 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
668 is_fundamental) | 669 is_fundamental) |
OLD | NEW |