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, Type |
7 import cpp_util | 7 import cpp_util |
8 import schema_util | 8 import schema_util |
9 | 9 |
10 class HGenerator(object): | 10 class HGenerator(object): |
11 """A .h generator for a namespace. | 11 """A .h generator for a namespace. |
12 """ | 12 """ |
13 def __init__(self, namespace, cpp_type_generator): | 13 def __init__(self, namespace, cpp_type_generator): |
14 self._cpp_type_generator = cpp_type_generator | 14 self._type_helper = cpp_type_generator |
15 self._namespace = namespace | 15 self._namespace = namespace |
16 self._target_namespace = ( | 16 self._target_namespace = ( |
17 self._cpp_type_generator.GetCppNamespaceName(self._namespace)) | 17 self._type_helper.GetCppNamespaceName(self._namespace)) |
18 | 18 |
19 def Generate(self): | 19 def Generate(self): |
20 """Generates a Code object with the .h for a single namespace. | 20 """Generates a Code object with the .h for a single namespace. |
21 """ | 21 """ |
22 c = Code() | 22 c = Code() |
23 (c.Append(cpp_util.CHROMIUM_LICENSE) | 23 (c.Append(cpp_util.CHROMIUM_LICENSE) |
24 .Append() | 24 .Append() |
25 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 25 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
26 .Append() | 26 .Append() |
27 ) | 27 ) |
28 | 28 |
29 ifndef_name = cpp_util.GenerateIfndefName(self._namespace.source_file_dir, | 29 ifndef_name = cpp_util.GenerateIfndefName(self._namespace.source_file_dir, |
30 self._target_namespace) | 30 self._target_namespace) |
31 (c.Append('#ifndef %s' % ifndef_name) | 31 (c.Append('#ifndef %s' % ifndef_name) |
32 .Append('#define %s' % ifndef_name) | 32 .Append('#define %s' % ifndef_name) |
33 .Append() | 33 .Append() |
34 .Append('#include <map>') | |
34 .Append('#include <string>') | 35 .Append('#include <string>') |
35 .Append('#include <vector>') | 36 .Append('#include <vector>') |
36 .Append() | 37 .Append() |
37 .Append('#include "base/basictypes.h"') | 38 .Append('#include "base/basictypes.h"') |
38 .Append('#include "base/logging.h"') | 39 .Append('#include "base/logging.h"') |
39 .Append('#include "base/memory/linked_ptr.h"') | 40 .Append('#include "base/memory/linked_ptr.h"') |
40 .Append('#include "base/memory/scoped_ptr.h"') | 41 .Append('#include "base/memory/scoped_ptr.h"') |
41 .Append('#include "base/values.h"') | 42 .Append('#include "base/values.h"') |
42 .Append('#include "tools/json_schema_compiler/any.h"') | |
43 .Append() | 43 .Append() |
44 ) | 44 ) |
45 | 45 |
46 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | 46 c.Concat(self._type_helper.GetRootNamespaceStart()) |
47 # TODO(calamity): These forward declarations should be #includes to allow | 47 # TODO(calamity): These forward declarations should be #includes to allow |
48 # $ref types from other files to be used as required params. This requires | 48 # $ref types from other files to be used as required params. This requires |
49 # some detangling of windows and tabs which will currently lead to circular | 49 # some detangling of windows and tabs which will currently lead to circular |
50 # #includes. | 50 # #includes. |
51 forward_declarations = ( | 51 forward_declarations = ( |
52 self._cpp_type_generator.GenerateForwardDeclarations()) | 52 self._type_helper.GenerateForwardDeclarations()) |
53 if not forward_declarations.IsEmpty(): | 53 if not forward_declarations.IsEmpty(): |
54 (c.Append() | 54 (c.Append() |
55 .Concat(forward_declarations) | 55 .Cblock(forward_declarations) |
56 .Append() | |
57 ) | 56 ) |
58 | 57 |
59 c.Concat(self._cpp_type_generator.GetNamespaceStart()) | 58 c.Concat(self._type_helper.GetNamespaceStart()) |
60 c.Append() | 59 c.Append() |
61 if self._namespace.properties: | 60 if self._namespace.properties: |
62 (c.Append('//') | 61 (c.Append('//') |
63 .Append('// Properties') | 62 .Append('// Properties') |
64 .Append('//') | 63 .Append('//') |
65 .Append() | 64 .Append() |
66 ) | 65 ) |
67 for property in self._namespace.properties.values(): | 66 for property in self._namespace.properties.values(): |
68 property_code = self._cpp_type_generator.GeneratePropertyValues( | 67 property_code = self._type_helper.GeneratePropertyValues( |
69 property, | 68 property, |
70 'extern const %(type)s %(name)s;') | 69 'extern const %(type)s %(name)s;') |
71 if property_code: | 70 if property_code: |
72 c.Concat(property_code).Append() | 71 c.Cblock(property_code) |
73 if self._namespace.types: | 72 if self._namespace.types: |
74 (c.Append('//') | 73 (c.Append('//') |
75 .Append('// Types') | 74 .Append('// Types') |
76 .Append('//') | 75 .Append('//') |
77 .Append() | 76 .Append() |
77 .Cblock(self._GenerateTypes(self._FieldDependencyOrder(), | |
78 is_toplevel=True, | |
79 generate_typedefs=True)) | |
78 ) | 80 ) |
79 for type_ in self._FieldDependencyOrder(): | |
80 (c.Concat(self._GenerateType(type_)) | |
81 .Append() | |
82 ) | |
83 if self._namespace.functions: | 81 if self._namespace.functions: |
84 (c.Append('//') | 82 (c.Append('//') |
85 .Append('// Functions') | 83 .Append('// Functions') |
86 .Append('//') | 84 .Append('//') |
87 .Append() | 85 .Append() |
88 ) | 86 ) |
89 for function in self._namespace.functions.values(): | 87 for function in self._namespace.functions.values(): |
90 (c.Concat(self._GenerateFunction(function)) | 88 c.Cblock(self._GenerateFunction(function)) |
91 .Append() | |
92 ) | |
93 if self._namespace.events: | 89 if self._namespace.events: |
94 (c.Append('//') | 90 (c.Append('//') |
95 .Append('// Events') | 91 .Append('// Events') |
96 .Append('//') | 92 .Append('//') |
97 .Append() | 93 .Append() |
98 ) | 94 ) |
99 for event in self._namespace.events.values(): | 95 for event in self._namespace.events.values(): |
100 (c.Concat(self._GenerateEvent(event)) | 96 c.Cblock(self._GenerateEvent(event)) |
101 .Append() | 97 (c.Concat(self._type_helper.GetNamespaceEnd()) |
102 ) | 98 .Cblock(self._type_helper.GetRootNamespaceEnd()) |
103 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) | |
104 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | |
105 .Append() | |
106 .Append('#endif // %s' % ifndef_name) | 99 .Append('#endif // %s' % ifndef_name) |
107 .Append() | 100 .Append() |
108 ) | 101 ) |
109 return c | 102 return c |
110 | 103 |
111 def _FieldDependencyOrder(self): | 104 def _FieldDependencyOrder(self): |
112 """Generates the list of types in the current namespace in an order in which | 105 """Generates the list of types in the current namespace in an order in which |
113 depended-upon types appear before types which depend on them. | 106 depended-upon types appear before types which depend on them. |
114 """ | 107 """ |
115 dependency_order = [] | 108 dependency_order = [] |
116 | 109 |
117 def ExpandType(path, type_): | 110 def ExpandType(path, type_): |
118 if type_ in path: | 111 if type_ in path: |
119 raise ValueError("Illegal circular dependency via cycle " + | 112 raise ValueError("Illegal circular dependency via cycle " + |
120 ", ".join(map(lambda x: x.name, path + [type_]))) | 113 ", ".join(map(lambda x: x.name, path + [type_]))) |
121 for prop in type_.properties.values(): | 114 for prop in type_.properties.values(): |
122 if (prop.type_ == PropertyType.REF and | 115 if (prop.type_ == PropertyType.REF and |
123 schema_util.GetNamespace(prop.ref_type) == self._namespace.name): | 116 schema_util.GetNamespace(prop.ref_type) == self._namespace.name): |
124 ExpandType(path + [type_], self._namespace.types[prop.ref_type]) | 117 ExpandType(path + [type_], self._namespace.types[prop.ref_type]) |
125 if not type_ in dependency_order: | 118 if not type_ in dependency_order: |
126 dependency_order.append(type_) | 119 dependency_order.append(type_) |
127 | 120 |
128 for type_ in self._namespace.types.values(): | 121 for type_ in self._namespace.types.values(): |
129 ExpandType([], type_) | 122 ExpandType([], type_) |
130 return dependency_order | 123 return dependency_order |
131 | 124 |
132 def _GenerateEnumDeclaration(self, enum_name, prop, values): | 125 def _GenerateEnumDeclaration(self, enum_name, type_): |
133 """Generate the declaration of a C++ enum for the given property and | 126 """Generate the declaration of a C++ enum. |
134 values. | |
135 """ | 127 """ |
136 c = Code() | 128 c = Code() |
137 c.Sblock('enum %s {' % enum_name) | 129 c.Sblock('enum %s {' % enum_name) |
138 c.Append(self._cpp_type_generator.GetEnumNoneValue(prop) + ',') | 130 c.Append(self._type_helper.GetEnumNoneValue(type_) + ',') |
139 for value in values: | 131 for value in type_.enum_values: |
140 c.Append(self._cpp_type_generator.GetEnumValue(prop, value) + ',') | 132 c.Append(self._type_helper.GetEnumValue(type_, value) + ',') |
141 (c.Eblock('};') | 133 return c.Eblock('};') |
142 .Append() | |
143 ) | |
144 return c | |
145 | 134 |
146 def _GenerateFields(self, props): | 135 def _GenerateFields(self, props): |
147 """Generates the field declarations when declaring a type. | 136 """Generates the field declarations when declaring a type. |
148 """ | 137 """ |
149 c = Code() | 138 c = Code() |
150 # Generate the enums needed for any fields with "choices" | 139 needs_blank_line = False |
151 for prop in props: | 140 for prop in props: |
152 if prop.type_ == PropertyType.CHOICES: | 141 if needs_blank_line: |
153 enum_name = self._cpp_type_generator.GetChoicesEnumType(prop) | |
154 c.Append('%s %s_type;' % (enum_name, prop.unix_name)) | |
155 c.Append() | 142 c.Append() |
156 | 143 needs_blank_line = True |
157 for prop in self._cpp_type_generator.ExpandParams(props): | |
158 if prop.description: | 144 if prop.description: |
159 c.Comment(prop.description) | 145 c.Comment(prop.description) |
146 # ANY is a base::Value which is abstract and cannot be a direct member, so | |
147 # we always need to wrap it in a scoped_ptr. | |
148 is_optional = (prop.optional or | |
Yoyo Zhou
2013/01/15 01:49:25
Is is_optional still an appropriate name, or is it
not at google - send to devlin
2013/01/15 21:47:27
Done.
| |
149 prop.type_.property_type == PropertyType.ANY) | |
160 (c.Append('%s %s;' % ( | 150 (c.Append('%s %s;' % ( |
161 self._cpp_type_generator.GetCompiledType(prop, wrap_optional=True), | 151 self._type_helper.GetCppType(prop.type_, is_optional=is_optional), |
162 prop.unix_name)) | 152 prop.unix_name)) |
163 .Append() | |
164 ) | 153 ) |
165 return c | 154 return c |
166 | 155 |
167 def _GenerateType(self, type_): | 156 def _GenerateType(self, type_, is_toplevel=False, generate_typedefs=False): |
168 """Generates a struct for a type. | 157 """Generates a struct for a type. |
Yoyo Zhou
2013/01/15 01:49:25
Please document the optional arguments.
not at google - send to devlin
2013/01/15 21:47:27
Done.
| |
169 """ | 158 """ |
170 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) | 159 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) |
171 c = Code() | 160 c = Code() |
172 | 161 |
173 if type_.functions: | 162 if type_.functions: |
174 c.Sblock('namespace %(classname)s {') | 163 # Wrap functions within types in the type's namespace. |
164 (c.Append('namespace %s {' % classname) | |
165 .Append() | |
166 ) | |
175 for function in type_.functions.values(): | 167 for function in type_.functions.values(): |
176 (c.Concat(self._GenerateFunction(function)) | 168 c.Cblock(self._GenerateFunction(function)) |
177 .Append() | 169 c.Append('} // namespace %s' % classname) |
170 elif type_.property_type == PropertyType.ARRAY: | |
171 if generate_typedefs and type_.description: | |
172 c.Comment(type_.description) | |
173 c.Cblock(self._GenerateType(type_.item_type)) | |
174 if generate_typedefs: | |
175 (c.Append('typedef std::vector<%s > %s;' % ( | |
176 self._type_helper.GetCppType(type_.item_type), | |
177 classname)) | |
178 ) | 178 ) |
179 c.Eblock('}') | 179 elif type_.property_type == PropertyType.STRING: |
180 elif type_.type_ == PropertyType.ARRAY: | 180 if generate_typedefs: |
181 if type_.description: | 181 if type_.description: |
182 c.Comment(type_.description) | 182 c.Comment(type_.description) |
183 c.Append('typedef std::vector<%(item_type)s> %(classname)s;') | 183 c.Append('typedef std::string %(classname)s;') |
184 c.Substitute({'classname': classname, 'item_type': | 184 elif type_.property_type == PropertyType.ENUM: |
185 self._cpp_type_generator.GetCompiledType(type_.item_type, | |
186 wrap_optional=True)}) | |
187 elif type_.type_ == PropertyType.STRING: | |
188 if type_.description: | |
189 c.Comment(type_.description) | |
190 c.Append('typedef std::string %(classname)s;') | |
191 elif type_.type_ == PropertyType.ENUM: | |
192 if type_.description: | 185 if type_.description: |
193 c.Comment(type_.description) | 186 c.Comment(type_.description) |
194 c.Sblock('enum %(classname)s {') | 187 c.Sblock('enum %(classname)s {') |
195 c.Append('%s,' % self._cpp_type_generator.GetEnumNoneValue(type_)) | 188 c.Append('%s,' % self._type_helper.GetEnumNoneValue(type_)) |
196 for value in type_.enum_values: | 189 for value in type_.enum_values: |
197 c.Append('%s,' % self._cpp_type_generator.GetEnumValue(type_, value)) | 190 c.Append('%s,' % self._type_helper.GetEnumValue(type_, value)) |
191 # Top level enums are in a namespace scope so the methods shouldn't be | |
192 # static. On the other hand, those declared inline (e.g. in an object) do. | |
193 maybe_static = '' if is_toplevel else 'static ' | |
198 (c.Eblock('};') | 194 (c.Eblock('};') |
199 .Append() | 195 .Append() |
200 .Append('scoped_ptr<base::Value> CreateEnumValue(%s %s);' % | 196 .Append('%sstd::string ToString(%s as_enum);' % |
201 (classname, classname.lower())) | 197 (maybe_static, classname)) |
202 .Append('std::string ToString(%s enum_param);' % classname) | 198 .Append('%s%s Parse%s(const std::string& as_string);' % |
203 .Append('%s From%sString(const std::string& enum_string);' % | 199 (maybe_static, classname, classname)) |
204 (classname, classname)) | |
205 ) | 200 ) |
206 else: | 201 elif type_.property_type == PropertyType.OBJECT: |
207 if type_.description: | 202 if type_.description: |
208 c.Comment(type_.description) | 203 c.Comment(type_.description) |
209 (c.Sblock('struct %(classname)s {') | 204 (c.Sblock('struct %(classname)s {') |
205 .Append('%(classname)s();') | |
210 .Append('~%(classname)s();') | 206 .Append('~%(classname)s();') |
207 ) | |
208 if type_.origin.from_json: | |
209 (c.Append() | |
210 .Comment('Populates a %s object from a base::Value. Returns' | |
211 ' whether |out| was successfully populated.' % classname) | |
212 .Append('static bool Populate(const base::Value& value, ' | |
213 '%(classname)s* out);') | |
214 ) | |
215 if type_.origin.from_client: | |
216 (c.Append() | |
217 .Comment('Returns a new base::DictionaryValue representing the' | |
218 ' serialized form of this %s object.' % classname) | |
219 .Append('scoped_ptr<base::DictionaryValue> ToValue() const;') | |
220 ) | |
221 properties = type_.properties.values() | |
222 (c.Append() | |
223 .Cblock(self._GenerateTypes(p.type_ for p in properties)) | |
224 .Cblock(self._GenerateFields(properties))) | |
225 if type_.additional_properties is not None: | |
226 (c.Cblock(self._GenerateType(type_.additional_properties)) | |
227 .Append('std::map<std::string, %s> additional_properties;' % | |
228 self._type_helper.GetCppType(type_.additional_properties, | |
229 is_in_container=True)) | |
230 ) | |
231 (c.Eblock() | |
232 .Sblock(' private:') | |
233 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') | |
234 .Eblock('};') | |
235 ) | |
236 elif type_.property_type == PropertyType.CHOICES: | |
237 if type_.description: | |
238 c.Comment(type_.description) | |
239 # Choices are modelled with optional fields for each choice. Exactly one | |
240 # field of the choice is guaranteed to be set by the compiler. | |
241 (c.Sblock('struct %(classname)s {') | |
211 .Append('%(classname)s();') | 242 .Append('%(classname)s();') |
212 .Append() | 243 .Append('~%(classname)s();') |
213 .Concat(self._GeneratePropertyStructures(type_.properties.values())) | 244 .Append()) |
214 .Concat(self._GenerateFields(type_.properties.values())) | 245 c.Cblock(self._GenerateTypes(type_.choices)) |
215 ) | 246 if type_.origin.from_json: |
216 if type_.from_json: | |
217 (c.Comment('Populates a %s object from a base::Value. Returns' | 247 (c.Comment('Populates a %s object from a base::Value. Returns' |
218 ' whether |out| was successfully populated.' % classname) | 248 ' whether |out| was successfully populated.' % classname) |
219 .Append('static bool Populate(const base::Value& value, ' | 249 .Append('static bool Populate(const base::Value& value, ' |
220 '%(classname)s* out);') | 250 '%(classname)s* out);') |
221 .Append() | 251 .Append() |
222 ) | 252 ) |
223 | 253 if type_.origin.from_client: |
224 if type_.from_client: | 254 (c.Comment('Returns a new base::Value representing the' |
225 (c.Comment('Returns a new base::DictionaryValue representing the' | 255 ' serialized form of this %s object.' % classname) |
226 ' serialized form of this %s object. Passes ' | 256 .Append('scoped_ptr<base::Value> ToValue() const;') |
227 'ownership to caller.' % classname) | 257 .Append() |
228 .Append('scoped_ptr<base::DictionaryValue> ToValue() const;') | |
229 ) | 258 ) |
230 | 259 c.Append('// Choices:') |
231 (c.Eblock() | 260 for choice_type in type_.choices: |
232 .Sblock(' private:') | 261 c.Append('%s as_%s;' % ( |
233 .Concat(self._GeneratePrivatePropertyStructures( | 262 self._type_helper.GetCppType(choice_type, is_optional=True), |
234 type_.properties.values())) | 263 choice_type.unix_name)) |
235 .Append() | 264 c.Eblock('};') |
236 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') | |
237 .Eblock('};') | |
238 ) | |
239 c.Substitute({'classname': classname}) | 265 c.Substitute({'classname': classname}) |
240 return c | 266 return c |
241 | 267 |
242 def _GenerateEvent(self, event): | 268 def _GenerateEvent(self, event): |
243 """Generates the namespaces for an event. | 269 """Generates the namespaces for an event. |
244 """ | 270 """ |
245 c = Code() | 271 c = Code() |
246 (c.Sblock('namespace %s {' % cpp_util.Classname(event.name)) | 272 # TODO(kalman): use event.unix_name not Classname. |
247 .Concat(self._GenerateCreateCallbackArguments(event, | 273 event_namespace = cpp_util.Classname(event.name) |
248 generate_to_json=True)) | 274 (c.Append('namespace %s {' % event_namespace) |
249 .Eblock('};') | 275 .Append() |
276 .Concat(self._GenerateCreateCallbackArguments(event)) | |
277 .Eblock('} // namespace %s' % event_namespace) | |
250 ) | 278 ) |
251 return c | 279 return c |
252 | 280 |
253 def _GenerateFunction(self, function): | 281 def _GenerateFunction(self, function): |
254 """Generates the namespaces and structs for a function. | 282 """Generates the namespaces and structs for a function. |
255 """ | 283 """ |
256 c = Code() | 284 c = Code() |
257 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name)) | 285 # TODO(kalman): Use function.unix_name not Classname here. |
258 .Concat(self._GenerateFunctionParams(function)) | 286 function_namespace = cpp_util.Classname(function.name) |
259 .Append() | 287 (c.Append('namespace %s {' % function_namespace) |
288 .Append() | |
289 .Cblock(self._GenerateFunctionParams(function)) | |
260 ) | 290 ) |
261 if function.callback: | 291 if function.callback: |
262 (c.Concat(self._GenerateFunctionResults(function.callback)) | 292 c.Cblock(self._GenerateFunctionResults(function.callback)) |
263 .Append() | 293 c.Append('} // namespace %s' % function_namespace) |
264 ) | |
265 c.Eblock('};') | |
266 | |
267 return c | 294 return c |
268 | 295 |
269 def _GenerateFunctionParams(self, function): | 296 def _GenerateFunctionParams(self, function): |
270 """Generates the struct for passing parameters from JSON to a function. | 297 """Generates the struct for passing parameters from JSON to a function. |
271 """ | 298 """ |
299 if not function.params: | |
300 return Code() | |
301 | |
272 c = Code() | 302 c = Code() |
273 | 303 (c.Sblock('struct Params {') |
274 if function.params: | 304 .Append('static scoped_ptr<Params> Create(const base::ListValue& args);') |
275 (c.Sblock('struct Params {') | 305 .Append('~Params();') |
276 .Concat(self._GeneratePropertyStructures(function.params)) | 306 .Append() |
277 .Concat(self._GenerateFields(function.params)) | 307 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) |
278 .Append('~Params();') | 308 .Cblock(self._GenerateFields(function.params)) |
279 .Append() | 309 .Eblock() |
280 .Append('static scoped_ptr<Params> Create(' | 310 .Sblock(' private:') |
281 'const base::ListValue& args);') | 311 .Append('Params();') |
282 .Eblock() | 312 .Append() |
283 .Sblock(' private:') | 313 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') |
284 .Append('Params();') | 314 .Eblock('};') |
285 .Append() | 315 ) |
286 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') | |
287 .Eblock('};') | |
288 ) | |
289 | |
290 return c | 316 return c |
291 | 317 |
292 def _GeneratePropertyStructures(self, props): | 318 def _GenerateTypes(self, types, is_toplevel=False, generate_typedefs=False): |
293 """Generate the structures required by a property such as OBJECT classes | 319 """Generate the structures required by a property such as OBJECT classes |
294 and enums. | 320 and enums. |
295 """ | 321 """ |
296 c = Code() | 322 c = Code() |
297 for prop in props: | 323 for type_ in types: |
298 if prop.type_ == PropertyType.OBJECT: | 324 c.Cblock(self._GenerateType(type_, |
299 c.Concat(self._GenerateType(prop)) | 325 is_toplevel=is_toplevel, |
300 c.Append() | 326 generate_typedefs=generate_typedefs)) |
301 elif prop.type_ == PropertyType.ARRAY: | |
302 c.Concat(self._GeneratePropertyStructures([prop.item_type])) | |
303 c.Append() | |
304 elif prop.type_ == PropertyType.CHOICES: | |
305 c.Concat(self._GenerateEnumDeclaration( | |
306 self._cpp_type_generator.GetChoicesEnumType(prop), | |
307 prop, | |
308 [choice.type_.name for choice in prop.choices.values()])) | |
309 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) | |
310 elif prop.type_ == PropertyType.ENUM: | |
311 enum_name = self._cpp_type_generator.GetCompiledType(prop) | |
312 c.Concat(self._GenerateEnumDeclaration( | |
313 enum_name, | |
314 prop, | |
315 prop.enum_values)) | |
316 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' % | |
317 (enum_name, prop.unix_name)) | |
318 enum_to_string = 'std::string ToString(%s enum_param);' % enum_name | |
319 enum_from_string = ('%s From%sString(const std::string& enum_string);' % | |
320 (enum_name, enum_name)) | |
321 # If the property is from the UI then we're in a struct so this function | |
322 # should be static. If it's from the client, then we're just in a | |
323 # namespace so we can't have the static keyword. | |
324 if prop.from_json: | |
325 create_enum_value = 'static %s' % create_enum_value | |
326 enum_to_string = 'static %s' % enum_to_string | |
327 enum_from_string = 'static %s' % enum_from_string | |
328 (c.Append(create_enum_value) | |
329 .Append(enum_to_string) | |
330 .Append(enum_from_string)) | |
331 return c | 327 return c |
332 | 328 |
333 def _GeneratePrivatePropertyStructures(self, props): | 329 def _GenerateCreateCallbackArguments(self, function): |
334 """Generate the private structures required by a property such as OBJECT | |
335 classes and enums. | |
336 """ | |
337 c = Code() | |
338 for prop in props: | |
339 if prop.type_ == PropertyType.ARRAY: | |
340 c.Concat(self._GeneratePrivatePropertyStructures([prop.item_type])) | |
341 c.Append() | |
342 elif prop.type_ == PropertyType.CHOICES: | |
343 # We only need GetChoiceValue() if there is a ToValue() method. | |
344 if prop.from_client: | |
345 c.Append('scoped_ptr<base::Value> Get%sChoiceValue() const;' % ( | |
346 cpp_util.Classname(prop.name))) | |
347 return c | |
348 | |
349 def _GenerateCreateCallbackArguments(self, function, generate_to_json=False): | |
350 """Generates functions for passing paramaters to a callback. | 330 """Generates functions for passing paramaters to a callback. |
Yoyo Zhou
2013/01/15 01:49:25
typo: parameters
not at google - send to devlin
2013/01/15 21:47:27
Done.
| |
351 """ | 331 """ |
352 c = Code() | 332 c = Code() |
353 params = function.params | 333 params = function.params |
354 c.Concat(self._GeneratePropertyStructures(params)) | 334 c.Cblock(self._GenerateTypes((p.type_ for p in params), is_toplevel=True)) |
355 | 335 |
356 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) | 336 declaration_list = [] |
357 for param_list in param_lists: | 337 for param in params: |
358 declaration_list = [] | 338 if param.description: |
359 for param in param_list: | 339 c.Comment(param.description) |
360 if param.description: | 340 declaration_list.append(cpp_util.GetParameterDeclaration( |
361 c.Comment(param.description) | 341 param, self._type_helper.GetCppType(param.type_))) |
362 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration( | 342 c.Append('scoped_ptr<base::ListValue> Create(%s);' % |
363 param, self._cpp_type_generator.GetCompiledType(param))) | 343 ', '.join(declaration_list)) |
364 c.Append('scoped_ptr<base::ListValue> Create(%s);' % | |
365 ', '.join(declaration_list)) | |
366 if generate_to_json: | |
367 c.Append('std::string ToJson(%s);' % ', '.join(declaration_list)) | |
368 return c | 344 return c |
369 | 345 |
370 def _GenerateFunctionResults(self, callback): | 346 def _GenerateFunctionResults(self, callback): |
371 """Generates namespace for passing a function's result back. | 347 """Generates namespace for passing a function's result back. |
372 """ | 348 """ |
373 c = Code() | 349 c = Code() |
374 (c.Sblock('namespace Results {') | 350 (c.Append('namespace Results {') |
375 .Concat(self._GenerateCreateCallbackArguments(callback)) | 351 .Append() |
376 .Eblock('};') | 352 .Concat(self._GenerateCreateCallbackArguments(callback)) |
353 .Append('} // namespace Results') | |
377 ) | 354 ) |
378 return c | 355 return c |
OLD | NEW |