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