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 cpp_util | 7 import cpp_util |
8 import model | |
9 import os | |
10 import schema_util | 8 import schema_util |
11 | 9 |
12 class HGenerator(object): | 10 class HGenerator(object): |
13 """A .h generator for a namespace. | 11 """A .h generator for a namespace. |
14 """ | 12 """ |
15 def __init__(self, namespace, cpp_type_generator): | 13 def __init__(self, namespace, cpp_type_generator): |
16 self._cpp_type_generator = cpp_type_generator | 14 self._cpp_type_generator = cpp_type_generator |
17 self._namespace = namespace | 15 self._namespace = namespace |
18 self._target_namespace = ( | 16 self._target_namespace = ( |
19 self._cpp_type_generator.GetCppNamespaceName(self._namespace)) | 17 self._cpp_type_generator.GetCppNamespaceName(self._namespace)) |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 """Generates the list of types in the current namespace in an order in which | 102 """Generates the list of types in the current namespace in an order in which |
105 depended-upon types appear before types which depend on them. | 103 depended-upon types appear before types which depend on them. |
106 """ | 104 """ |
107 dependency_order = [] | 105 dependency_order = [] |
108 | 106 |
109 def ExpandType(path, type_): | 107 def ExpandType(path, type_): |
110 if type_ in path: | 108 if type_ in path: |
111 raise ValueError("Illegal circular dependency via cycle " + | 109 raise ValueError("Illegal circular dependency via cycle " + |
112 ", ".join(map(lambda x: x.name, path + [type_]))) | 110 ", ".join(map(lambda x: x.name, path + [type_]))) |
113 for prop in type_.properties.values(): | 111 for prop in type_.properties.values(): |
114 if not prop.optional and prop.type_ == PropertyType.REF: | 112 if prop.type_ == PropertyType.REF and ((not '.' in prop.ref_type) or |
113 prop.ref_type.startswith(self._namespace.name + '.')): | |
not at google - send to devlin
2012/06/04 04:16:49
Perhaps add GetSchemaNamespace to schema_util.py t
benjhayden
2012/06/04 20:33:15
Done.
| |
115 ExpandType(path + [type_], self._namespace.types[prop.ref_type]) | 114 ExpandType(path + [type_], self._namespace.types[prop.ref_type]) |
116 if not type_ in dependency_order: | 115 if not type_ in dependency_order: |
117 dependency_order.append(type_) | 116 dependency_order.append(type_) |
118 | 117 |
119 for type_ in self._namespace.types.values(): | 118 for type_ in self._namespace.types.values(): |
120 ExpandType([], type_) | 119 ExpandType([], type_) |
121 return dependency_order | 120 return dependency_order |
122 | 121 |
123 def _GenerateEnumDeclaration(self, enum_name, prop, values): | 122 def _GenerateEnumDeclaration(self, enum_name, prop, values): |
124 """Generate the declaration of a C++ enum for the given property and | 123 """Generate the declaration of a C++ enum for the given property and |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 if type_.description: | 170 if type_.description: |
172 c.Comment(type_.description) | 171 c.Comment(type_.description) |
173 c.Append('typedef std::vector<%(item_type)s> %(classname)s;') | 172 c.Append('typedef std::vector<%(item_type)s> %(classname)s;') |
174 c.Substitute({'classname': classname, 'item_type': | 173 c.Substitute({'classname': classname, 'item_type': |
175 self._cpp_type_generator.GetType(type_.item_type, | 174 self._cpp_type_generator.GetType(type_.item_type, |
176 wrap_optional=True)}) | 175 wrap_optional=True)}) |
177 elif type_.type_ == PropertyType.STRING: | 176 elif type_.type_ == PropertyType.STRING: |
178 if type_.description: | 177 if type_.description: |
179 c.Comment(type_.description) | 178 c.Comment(type_.description) |
180 c.Append('typedef std::string %(classname)s;') | 179 c.Append('typedef std::string %(classname)s;') |
181 c.Substitute({'classname': classname}) | 180 c.Substitute({'classname': classname}) |
not at google - send to devlin
2012/06/04 04:16:49
mind deleting the above line while you're here?
benjhayden
2012/06/04 20:33:15
Done.
| |
181 elif type_.type_ == PropertyType.INTEGER: | |
182 if type_.description: | |
183 c.Comment(type_.description) | |
184 # TODO: c.Sblock('enum %(classname)s {') ... .Eblock('};') | |
not at google - send to devlin
2012/06/04 04:16:49
Please expand on this TODO: add your name, and exp
benjhayden
2012/06/04 20:33:15
Done.
| |
185 c.Append('typedef int %(classname)s;') | |
182 else: | 186 else: |
183 if type_.description: | 187 if type_.description: |
184 c.Comment(type_.description) | 188 c.Comment(type_.description) |
185 (c.Sblock('struct %(classname)s {') | 189 (c.Sblock('struct %(classname)s {') |
186 .Append('~%(classname)s();') | 190 .Append('~%(classname)s();') |
187 .Append('%(classname)s();') | 191 .Append('%(classname)s();') |
188 .Append() | 192 .Append() |
189 .Concat(self._GeneratePropertyStructures(type_.properties.values())) | 193 .Concat(self._GeneratePropertyStructures(type_.properties.values())) |
190 .Concat(self._GenerateFields(type_.properties.values())) | 194 .Concat(self._GenerateFields(type_.properties.values())) |
191 ) | 195 ) |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 c.Comment(param.description) | 300 c.Comment(param.description) |
297 if param.type_ == PropertyType.ANY: | 301 if param.type_ == PropertyType.ANY: |
298 c.Comment("Value* Result::Create(Value*) not generated " | 302 c.Comment("Value* Result::Create(Value*) not generated " |
299 "because it's redundant.") | 303 "because it's redundant.") |
300 continue | 304 continue |
301 c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration( | 305 c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration( |
302 param, self._cpp_type_generator.GetType(param))) | 306 param, self._cpp_type_generator.GetType(param))) |
303 c.Eblock('};') | 307 c.Eblock('};') |
304 | 308 |
305 return c | 309 return c |
OLD | NEW |