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 schema_util |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 ) | 248 ) |
249 for prop in type_.properties.values(): | 249 for prop in type_.properties.values(): |
250 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 250 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
251 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) | 251 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) |
252 else: | 252 else: |
253 if prop.optional: | 253 if prop.optional: |
254 if prop.type_ == PropertyType.ENUM: | 254 if prop.type_ == PropertyType.ENUM: |
255 c.Sblock('if (%s != %s)' % | 255 c.Sblock('if (%s != %s)' % |
256 (prop.unix_name, | 256 (prop.unix_name, |
257 self._cpp_type_generator.GetEnumNoneValue(prop))) | 257 self._cpp_type_generator.GetEnumNoneValue(prop))) |
258 else: | 258 elif not prop.type_ == PropertyType.CHOICES: |
not at google - send to devlin
2012/07/18 01:21:35
Prefer to structure this like
if prop.type_ == Pr
chebert
2012/07/19 00:56:01
Done.
| |
259 c.Sblock('if (%s.get())' % prop.unix_name) | 259 c.Sblock('if (%s.get())' % prop.unix_name) |
260 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( | 260 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( |
261 prop.name, | 261 prop.name, |
262 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name))) | 262 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name))) |
263 if prop.optional: | 263 if prop.optional: |
264 c.Eblock(); | 264 c.Eblock(); |
265 (c.Append() | 265 (c.Append() |
266 .Append('return value.Pass();') | 266 .Append('return value.Pass();') |
267 .Eblock('}') | 267 .Eblock('}') |
268 ) | 268 ) |
269 return c | 269 return c |
270 | 270 |
271 def _GenerateFunction(self, cpp_namespace, function): | 271 def _GenerateFunction(self, cpp_namespace, function): |
272 """Generates the definitions for function structs. | 272 """Generates the definitions for function structs. |
273 """ | 273 """ |
274 c = Code() | 274 c = Code() |
275 | 275 |
276 # Params::Populate function | 276 # Params::Populate function |
277 if function.params: | 277 if function.params: |
278 c.Concat(self._GeneratePropertyFunctions(cpp_namespace + '::Params', | 278 c.Concat(self._GeneratePropertyFunctions(cpp_namespace + '::Params', |
279 function.params)) | 279 function.params, from_function=True)) |
not at google - send to devlin
2012/07/18 01:21:35
Like I said in the header, use from_client.
chebert
2012/07/19 00:56:01
Done.
| |
280 (c.Append('%(cpp_namespace)s::Params::Params() {}') | 280 (c.Append('%(cpp_namespace)s::Params::Params() {}') |
281 .Append('%(cpp_namespace)s::Params::~Params() {}') | 281 .Append('%(cpp_namespace)s::Params::~Params() {}') |
282 .Append() | 282 .Append() |
283 .Concat(self._GenerateFunctionParamsCreate(cpp_namespace, function)) | 283 .Concat(self._GenerateFunctionParamsCreate(cpp_namespace, function)) |
284 .Append() | 284 .Append() |
285 ) | 285 ) |
286 | 286 |
287 # Results::Create function | 287 # Results::Create function |
288 if function.callback: | 288 if function.callback: |
289 c.Concat(self._GenerateCreateCallbackArguments( | 289 c.Concat(self._GenerateCreateCallbackArguments( |
290 "%s::Results" % cpp_namespace, function.callback)) | 290 "%s::Results" % cpp_namespace, function.callback)) |
291 | 291 |
292 c.Substitute({'cpp_namespace': cpp_namespace}) | 292 c.Substitute({'cpp_namespace': cpp_namespace}) |
293 | 293 |
294 return c | 294 return c |
295 | 295 |
296 def _GenerateGetChoiceValue(self, cpp_namespace, prop): | |
not at google - send to devlin
2012/07/18 01:21:35
nit: define below the place that it's used.
chebert
2012/07/19 00:56:01
Done.
| |
297 """Generates GetChoiceValue() that returns a |scoped_ptr<base::Value>| | |
not at google - send to devlin
2012/07/18 01:21:35
usually |foo| is used for variables/parameters, no
chebert
2012/07/19 00:56:01
Done.
| |
298 representing the choice value. | |
299 """ | |
300 c = Code() | |
301 (c.Sblock('scoped_ptr<base::Value> ' | |
302 '%(cpp_namespace)s::GetChoiceValue(%(arg)s) const {') | |
303 .Sblock('switch(%s) {' % prop.unix_name) | |
304 ) | |
305 if prop.optional: | |
306 (c.Sblock('case %s: {' % | |
307 (self._cpp_type_generator.GetEnumNoneValue(prop))) | |
308 .Append('return scoped_ptr<base::Value>();') | |
309 .Eblock('}') | |
not at google - send to devlin
2012/07/18 01:21:35
The {} in each of these case statements is unneces
chebert
2012/07/19 00:56:01
Done.
| |
310 ) | |
311 for choice in self._cpp_type_generator.ExpandParams([prop]): | |
312 (c.Sblock('case %s: {' % | |
313 self._cpp_type_generator.GetEnumValue(prop, choice.type_.name)) | |
314 .Append('return scoped_ptr<base::Value>(%s);' % | |
not at google - send to devlin
2012/07/18 01:21:35
can do "make_scoped_ptr(%s)" here
chebert
2012/07/19 00:56:01
Done.
| |
315 self._CreateValueFromProperty(choice, choice.unix_name)) | |
316 .Eblock('}') | |
317 ) | |
318 (c.Eblock('}') | |
319 .Append('return scoped_ptr<base::Value>();') | |
320 .Eblock('}') | |
not at google - send to devlin
2012/07/18 01:21:35
also add a blank line
chebert
2012/07/19 00:56:01
Done.
| |
321 .Substitute({ | |
322 'cpp_namespace': cpp_namespace, | |
323 'arg': cpp_util.GetParameterDeclaration( | |
324 prop, self._cpp_type_generator.GetChoicesEnumType(prop)) | |
325 }) | |
326 ) | |
327 return c | |
328 | |
296 def _GenerateCreateEnumValue(self, cpp_namespace, prop): | 329 def _GenerateCreateEnumValue(self, cpp_namespace, prop): |
297 """Generates CreateEnumValue() that returns the |base::StringValue| | 330 """Generates CreateEnumValue() that returns the |base::StringValue| |
298 representation of an enum. | 331 representation of an enum. |
299 """ | 332 """ |
300 c = Code() | 333 c = Code() |
301 c.Append('// static') | 334 c.Append('// static') |
302 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' | 335 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' |
303 '%(arg)s) {') | 336 '%(arg)s) {') |
304 c.Sblock('switch (%s) {' % prop.unix_name) | 337 c.Sblock('switch (%s) {' % prop.unix_name) |
305 if prop.optional: | 338 if prop.optional: |
(...skipping 24 matching lines...) Expand all Loading... | |
330 | 363 |
331 def _CreateValueFromProperty(self, prop, var): | 364 def _CreateValueFromProperty(self, prop, var): |
332 """Creates a base::Value given a property. Generated code passes ownership | 365 """Creates a base::Value given a property. Generated code passes ownership |
333 to caller. | 366 to caller. |
334 | 367 |
335 var: variable or variable* | 368 var: variable or variable* |
336 | 369 |
337 E.g for std::string, generate base::Value::CreateStringValue(var) | 370 E.g for std::string, generate base::Value::CreateStringValue(var) |
338 """ | 371 """ |
339 if prop.type_ == PropertyType.CHOICES: | 372 if prop.type_ == PropertyType.CHOICES: |
340 # CHOICES conversion not implemented. If needed, write something to | 373 return 'GetChoiceValue(%s_type).release()' % var |
341 # generate a function that returns a scoped_ptr<base::Value> and put it in | 374 elif self._IsObjectOrObjectRef(prop): |
342 # _GeneratePropertyFunctions, then use it here. Look at CreateEnumValue() | |
343 # for reference. | |
344 raise NotImplementedError( | |
345 'Conversion of CHOICES to base::Value not implemented') | |
346 if self._IsObjectOrObjectRef(prop): | |
347 if prop.optional: | 375 if prop.optional: |
348 return '%s->ToValue().release()' % var | 376 return '%s->ToValue().release()' % var |
349 else: | 377 else: |
350 return '%s.ToValue().release()' % var | 378 return '%s.ToValue().release()' % var |
351 elif prop.type_ == PropertyType.ANY: | 379 elif prop.type_ == PropertyType.ANY: |
352 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) | 380 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) |
353 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 381 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
354 return '%s.DeepCopy()' % var | 382 return '%s.DeepCopy()' % var |
355 elif prop.type_ == PropertyType.ENUM: | 383 elif prop.type_ == PropertyType.ENUM: |
356 return 'CreateEnumValue(%s).release()' % var | 384 return 'CreateEnumValue(%s).release()' % var |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
605 (c.Append( | 633 (c.Append( |
606 ('if' if i == 0 else 'else if') + | 634 ('if' if i == 0 else 'else if') + |
607 '(enum_as_string == "%s")' % enum_value) | 635 '(enum_as_string == "%s")' % enum_value) |
608 .Append(' ' + enum_temp + ' = %s;' % ( | 636 .Append(' ' + enum_temp + ' = %s;' % ( |
609 self._cpp_type_generator.GetEnumValue(prop, enum_value))) | 637 self._cpp_type_generator.GetEnumValue(prop, enum_value))) |
610 ) | 638 ) |
611 (c.Append('else') | 639 (c.Append('else') |
612 .Append(' return %(failure_value)s;') | 640 .Append(' return %(failure_value)s;') |
613 ) | 641 ) |
614 | 642 |
615 def _GeneratePropertyFunctions(self, param_namespace, params): | 643 def _GeneratePropertyFunctions(self, param_namespace, params, |
644 from_function=False): | |
616 """Generate the functions for structures generated by a property such as | 645 """Generate the functions for structures generated by a property such as |
617 CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects. | 646 CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects. |
647 If from_function is True if these properties belong to an API Function. | |
618 """ | 648 """ |
619 c = Code() | 649 c = Code() |
620 for param in params: | 650 for param in params: |
621 if param.type_ == PropertyType.OBJECT: | 651 if param.type_ == PropertyType.OBJECT: |
622 c.Concat(self._GenerateType( | 652 c.Concat(self._GenerateType( |
623 param_namespace + '::' + cpp_util.Classname(param.name), | 653 param_namespace + '::' + cpp_util.Classname(param.name), |
624 param)) | 654 param)) |
625 c.Append() | 655 c.Append() |
626 elif param.type_ == PropertyType.ARRAY: | 656 elif param.type_ == PropertyType.ARRAY: |
627 c.Concat(self._GeneratePropertyFunctions( | 657 c.Concat(self._GeneratePropertyFunctions( |
628 param_namespace, [param.item_type])) | 658 param_namespace, [param.item_type])) |
629 elif param.type_ == PropertyType.CHOICES: | 659 elif param.type_ == PropertyType.CHOICES: |
630 c.Concat(self._GeneratePropertyFunctions( | 660 c.Concat(self._GeneratePropertyFunctions( |
631 param_namespace, param.choices.values())) | 661 param_namespace, param.choices.values())) |
662 if not from_function: | |
663 c.Concat(self._GenerateGetChoiceValue(param_namespace, param)) | |
632 elif param.type_ == PropertyType.ENUM: | 664 elif param.type_ == PropertyType.ENUM: |
633 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) | 665 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) |
634 c.Append() | 666 c.Append() |
635 return c | 667 return c |
636 | 668 |
637 def _GenerateCreateCallbackArguments(self, function_scope, callback): | 669 def _GenerateCreateCallbackArguments(self, function_scope, callback): |
638 """Generate all functions to create Value parameters for a callback. | 670 """Generate all functions to create Value parameters for a callback. |
639 | 671 |
640 E.g for function "Bar", generate Bar::Results::Create | 672 E.g for function "Bar", generate Bar::Results::Create |
641 E.g for event "Baz", generate Baz::Create | 673 E.g for event "Baz", generate Baz::Create |
642 | 674 |
643 function_scope: the function scope path, e.g. Foo::Bar for the function | 675 function_scope: the function scope path, e.g. Foo::Bar for the function |
644 Foo::Bar::Baz(). | 676 Foo::Bar::Baz(). |
645 callback: the Function object we are creating callback arguments for. | 677 callback: the Function object we are creating callback arguments for. |
646 """ | 678 """ |
647 c = Code() | 679 c = Code() |
648 params = callback.params | 680 params = callback.params |
649 expanded_params = self._cpp_type_generator.ExpandParams(params) | 681 expanded_params = self._cpp_type_generator.ExpandParams(params) |
650 c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params)) | 682 c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params, |
683 from_function=True)) | |
651 | 684 |
652 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) | 685 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) |
653 for param_list in param_lists: | 686 for param_list in param_lists: |
654 (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s::' | 687 (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s::' |
655 'Create(%(declaration_list)s) {') | 688 'Create(%(declaration_list)s) {') |
656 .Append('scoped_ptr<base::ListValue> create_results(' | 689 .Append('scoped_ptr<base::ListValue> create_results(' |
657 'new base::ListValue());') | 690 'new base::ListValue());') |
658 ) | 691 ) |
659 declaration_list = [] | 692 declaration_list = [] |
660 for param in param_list: | 693 for param in param_list: |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
706 """ | 739 """ |
707 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 740 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
708 PropertyType.ARRAY) | 741 PropertyType.ARRAY) |
709 | 742 |
710 def _IsFundamentalOrFundamentalRef(self, prop): | 743 def _IsFundamentalOrFundamentalRef(self, prop): |
711 """Determines if this property is a Fundamental type or is a ref to a | 744 """Determines if this property is a Fundamental type or is a ref to a |
712 Fundamental type. | 745 Fundamental type. |
713 """ | 746 """ |
714 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 747 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
715 is_fundamental) | 748 is_fundamental) |
OLD | NEW |