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 schema_util | 8 import schema_util |
9 | 9 |
10 class HGenerator(object): | 10 class HGenerator(object): |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 | 246 |
247 return c | 247 return c |
248 | 248 |
249 def _GenerateFunctionParams(self, function): | 249 def _GenerateFunctionParams(self, function): |
250 """Generates the struct for passing parameters from JSON to a function. | 250 """Generates the struct for passing parameters from JSON to a function. |
251 """ | 251 """ |
252 c = Code() | 252 c = Code() |
253 | 253 |
254 if function.params: | 254 if function.params: |
255 (c.Sblock('struct Params {') | 255 (c.Sblock('struct Params {') |
256 .Concat(self._GeneratePropertyStructures(function.params)) | 256 .Concat(self._GeneratePropertyStructures(function.params, |
257 from_function=True)) | |
257 .Concat(self._GenerateFields(function.params)) | 258 .Concat(self._GenerateFields(function.params)) |
258 .Append('~Params();') | 259 .Append('~Params();') |
259 .Append() | 260 .Append() |
260 .Append('static scoped_ptr<Params> Create(' | 261 .Append('static scoped_ptr<Params> Create(' |
261 'const base::ListValue& args);') | 262 'const base::ListValue& args);') |
262 .Eblock() | 263 .Eblock() |
263 .Sblock(' private:') | 264 .Sblock(' private:') |
264 .Append('Params();') | 265 .Append('Params();') |
265 .Append() | 266 .Append() |
266 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') | 267 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') |
267 .Eblock('};') | 268 .Eblock('};') |
268 ) | 269 ) |
269 | 270 |
270 return c | 271 return c |
271 | 272 |
272 def _GeneratePropertyStructures(self, props): | 273 def _GeneratePropertyStructures(self, props, from_function=False): |
273 """Generate the structures required by a property such as OBJECT classes | 274 """Generate the structures required by a property such as OBJECT classes |
274 and enums. | 275 and enums. |
276 If from_function is True if these properties belong to an API Function. | |
not at google - send to devlin
2012/07/18 01:21:35
You'll want to use from_client not from_function h
chebert
2012/07/19 00:56:01
Done.
| |
275 """ | 277 """ |
276 c = Code() | 278 c = Code() |
277 for prop in props: | 279 for prop in props: |
278 if prop.type_ == PropertyType.OBJECT: | 280 if prop.type_ == PropertyType.OBJECT: |
279 c.Concat(self._GenerateType(prop)) | 281 c.Concat(self._GenerateType(prop)) |
280 c.Append() | 282 c.Append() |
281 elif prop.type_ == PropertyType.ARRAY: | 283 elif prop.type_ == PropertyType.ARRAY: |
282 c.Concat(self._GeneratePropertyStructures([prop.item_type])) | 284 c.Concat(self._GeneratePropertyStructures([prop.item_type])) |
283 c.Append() | 285 c.Append() |
284 elif prop.type_ == PropertyType.CHOICES: | 286 elif prop.type_ == PropertyType.CHOICES: |
285 c.Concat(self._GenerateEnumDeclaration( | 287 c.Concat(self._GenerateEnumDeclaration( |
286 self._cpp_type_generator.GetChoicesEnumType(prop), | 288 self._cpp_type_generator.GetChoicesEnumType(prop), |
287 prop, | 289 prop, |
288 [choice.type_.name for choice in prop.choices.values()])) | 290 [choice.type_.name for choice in prop.choices.values()])) |
289 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) | 291 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) |
292 # We only need GetChoiceValue() if there is a ToValue() method. | |
293 if prop.from_json and not from_function: | |
not at google - send to devlin
2012/07/18 01:21:35
I think this should all be "prop.from_client"?
chebert
2012/07/19 00:56:01
Done.
| |
294 c.Append('scoped_ptr<base::Value> GetChoiceValue(%s) const;' % | |
not at google - send to devlin
2012/07/18 01:21:35
Btw this should be generated under private; should
chebert
2012/07/19 00:56:01
Turns out the CreateEnumValue move to private is n
not at google - send to devlin
2012/07/19 01:48:22
sure thing
| |
295 cpp_util.GetParameterDeclaration(prop, | |
296 self._cpp_type_generator.GetChoicesEnumType(prop))) | |
not at google - send to devlin
2012/07/18 01:21:35
There is some unfortunate whitespace thing going o
chebert
2012/07/19 00:56:01
Done.
| |
290 elif prop.type_ == PropertyType.ENUM: | 297 elif prop.type_ == PropertyType.ENUM: |
291 enum_name = self._cpp_type_generator.GetType(prop) | 298 enum_name = self._cpp_type_generator.GetType(prop) |
292 c.Concat(self._GenerateEnumDeclaration( | 299 c.Concat(self._GenerateEnumDeclaration( |
293 enum_name, | 300 enum_name, |
294 prop, | 301 prop, |
295 prop.enum_values)) | 302 prop.enum_values)) |
296 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' % | 303 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' % |
297 (enum_name, prop.unix_name)) | 304 (enum_name, prop.unix_name)) |
298 # If the property is from the UI then we're in a struct so this function | 305 # If the property is from the UI then we're in a struct so this function |
299 # should be static. If it's from the client, then we're just in a | 306 # should be static. If it's from the client, then we're just in a |
300 # namespace so we can't have the static keyword. | 307 # namespace so we can't have the static keyword. |
301 if prop.from_json: | 308 if prop.from_json: |
302 create_enum_value = 'static ' + create_enum_value | 309 create_enum_value = 'static ' + create_enum_value |
303 c.Append(create_enum_value) | 310 c.Append(create_enum_value) |
304 return c | 311 return c |
305 | 312 |
306 def _GenerateCreateCallbackArguments(self, function): | 313 def _GenerateCreateCallbackArguments(self, function): |
307 """Generates functions for passing paramaters to a callback. | 314 """Generates functions for passing paramaters to a callback. |
308 """ | 315 """ |
309 c = Code() | 316 c = Code() |
310 params = function.params | 317 params = function.params |
311 c.Concat(self._GeneratePropertyStructures(params)) | 318 c.Concat(self._GeneratePropertyStructures(params, from_function=True)) |
312 | 319 |
313 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) | 320 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) |
314 for param_list in param_lists: | 321 for param_list in param_lists: |
315 declaration_list = [] | 322 declaration_list = [] |
316 for param in param_list: | 323 for param in param_list: |
317 if param.description: | 324 if param.description: |
318 c.Comment(param.description) | 325 c.Comment(param.description) |
319 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration( | 326 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration( |
320 param, self._cpp_type_generator.GetType(param))) | 327 param, self._cpp_type_generator.GetType(param))) |
321 c.Append('scoped_ptr<base::ListValue> Create(%s);' % | 328 c.Append('scoped_ptr<base::ListValue> Create(%s);' % |
322 ', '.join(declaration_list)) | 329 ', '.join(declaration_list)) |
323 return c | 330 return c |
324 | 331 |
325 def _GenerateFunctionResults(self, callback): | 332 def _GenerateFunctionResults(self, callback): |
326 """Generates namespace for passing a function's result back. | 333 """Generates namespace for passing a function's result back. |
327 """ | 334 """ |
328 c = Code() | 335 c = Code() |
329 (c.Sblock('namespace Results {') | 336 (c.Sblock('namespace Results {') |
330 .Concat(self._GenerateCreateCallbackArguments(callback)) | 337 .Concat(self._GenerateCreateCallbackArguments(callback)) |
331 .Eblock('};') | 338 .Eblock('};') |
332 ) | 339 ) |
333 return c | 340 return c |
OLD | NEW |