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 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 .Append() | 247 .Append() |
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 elif prop.type_ == PropertyType.CHOICES: |
| 259 c.Sblock('if (%s_type != %s)' % |
| 260 (prop.unix_name, |
| 261 self._cpp_type_generator.GetEnumNoneValue(prop))) |
258 else: | 262 else: |
259 c.Sblock('if (%s.get())' % prop.unix_name) | 263 c.Sblock('if (%s.get())' % prop.unix_name) |
260 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( | 264 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( |
261 prop.name, | 265 prop.name, |
262 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name))) | 266 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name))) |
263 if prop.optional: | 267 if prop.optional: |
264 c.Eblock(); | 268 c.Eblock(); |
265 (c.Append() | 269 (c.Append() |
266 .Append('return value.Pass();') | 270 .Append('return value.Pass();') |
267 .Eblock('}') | 271 .Eblock('}') |
(...skipping 18 matching lines...) Expand all Loading... |
286 | 290 |
287 # Results::Create function | 291 # Results::Create function |
288 if function.callback: | 292 if function.callback: |
289 c.Concat(self._GenerateCreateCallbackArguments( | 293 c.Concat(self._GenerateCreateCallbackArguments( |
290 "%s::Results" % cpp_namespace, function.callback)) | 294 "%s::Results" % cpp_namespace, function.callback)) |
291 | 295 |
292 c.Substitute({'cpp_namespace': cpp_namespace}) | 296 c.Substitute({'cpp_namespace': cpp_namespace}) |
293 | 297 |
294 return c | 298 return c |
295 | 299 |
296 def _GenerateCreateEnumValue(self, cpp_namespace, prop): | |
297 """Generates CreateEnumValue() that returns the |base::StringValue| | |
298 representation of an enum. | |
299 """ | |
300 c = Code() | |
301 c.Append('// static') | |
302 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' | |
303 '%(arg)s) {') | |
304 c.Sblock('switch (%s) {' % prop.unix_name) | |
305 if prop.optional: | |
306 (c.Append('case %s: {' % self._cpp_type_generator.GetEnumNoneValue(prop)) | |
307 .Append(' return scoped_ptr<base::Value>();') | |
308 .Append('}') | |
309 ) | |
310 for enum_value in prop.enum_values: | |
311 (c.Append('case %s: {' % | |
312 self._cpp_type_generator.GetEnumValue(prop, enum_value)) | |
313 .Append(' return scoped_ptr<base::Value>(' | |
314 'base::Value::CreateStringValue("%s"));' % | |
315 enum_value) | |
316 .Append('}') | |
317 ) | |
318 (c.Append('default: {') | |
319 .Append(' return scoped_ptr<base::Value>();') | |
320 .Append('}') | |
321 ) | |
322 c.Eblock('}') | |
323 c.Eblock('}') | |
324 c.Substitute({ | |
325 'cpp_namespace': cpp_namespace, | |
326 'arg': cpp_util.GetParameterDeclaration( | |
327 prop, self._cpp_type_generator.GetType(prop)) | |
328 }) | |
329 return c | |
330 | |
331 def _CreateValueFromProperty(self, prop, var): | 300 def _CreateValueFromProperty(self, prop, var): |
332 """Creates a base::Value given a property. Generated code passes ownership | 301 """Creates a base::Value given a property. Generated code passes ownership |
333 to caller. | 302 to caller. |
334 | 303 |
335 var: variable or variable* | 304 var: variable or variable* |
336 | 305 |
337 E.g for std::string, generate base::Value::CreateStringValue(var) | 306 E.g for std::string, generate base::Value::CreateStringValue(var) |
338 """ | 307 """ |
339 if prop.type_ == PropertyType.CHOICES: | 308 if prop.type_ == PropertyType.CHOICES: |
340 # CHOICES conversion not implemented. If needed, write something to | 309 return 'Get%sChoiceValue().release()' % cpp_util.Classname(prop.name) |
341 # generate a function that returns a scoped_ptr<base::Value> and put it in | 310 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: | 311 if prop.optional: |
348 return '%s->ToValue().release()' % var | 312 return '%s->ToValue().release()' % var |
349 else: | 313 else: |
350 return '%s.ToValue().release()' % var | 314 return '%s.ToValue().release()' % var |
351 elif prop.type_ == PropertyType.ANY: | 315 elif prop.type_ == PropertyType.ANY: |
352 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) | 316 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) |
353 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 317 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
354 return '%s.DeepCopy()' % var | 318 return '%s.DeepCopy()' % var |
355 elif prop.type_ == PropertyType.ENUM: | 319 elif prop.type_ == PropertyType.ENUM: |
356 return 'CreateEnumValue(%s).release()' % var | 320 return 'CreateEnumValue(%s).release()' % var |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 c.Concat(self._GenerateType( | 586 c.Concat(self._GenerateType( |
623 param_namespace + '::' + cpp_util.Classname(param.name), | 587 param_namespace + '::' + cpp_util.Classname(param.name), |
624 param)) | 588 param)) |
625 c.Append() | 589 c.Append() |
626 elif param.type_ == PropertyType.ARRAY: | 590 elif param.type_ == PropertyType.ARRAY: |
627 c.Concat(self._GeneratePropertyFunctions( | 591 c.Concat(self._GeneratePropertyFunctions( |
628 param_namespace, [param.item_type])) | 592 param_namespace, [param.item_type])) |
629 elif param.type_ == PropertyType.CHOICES: | 593 elif param.type_ == PropertyType.CHOICES: |
630 c.Concat(self._GeneratePropertyFunctions( | 594 c.Concat(self._GeneratePropertyFunctions( |
631 param_namespace, param.choices.values())) | 595 param_namespace, param.choices.values())) |
| 596 if param.from_client: |
| 597 c.Concat(self._GenerateGetChoiceValue(param_namespace, param)) |
632 elif param.type_ == PropertyType.ENUM: | 598 elif param.type_ == PropertyType.ENUM: |
633 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) | 599 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) |
634 c.Append() | 600 c.Append() |
635 return c | 601 return c |
636 | 602 |
| 603 def _GenerateGetChoiceValue(self, cpp_namespace, prop): |
| 604 """Generates Get<Type>ChoiceValue() that returns a scoped_ptr<base::Value> |
| 605 representing the choice value. |
| 606 """ |
| 607 c = Code() |
| 608 (c.Sblock('scoped_ptr<base::Value> ' |
| 609 '%(cpp_namespace)s::Get%(choice)sChoiceValue() const {') |
| 610 .Sblock('switch (%s_type) {' % prop.unix_name) |
| 611 ) |
| 612 if prop.optional: |
| 613 c.Concat(self._GenerateReturnCase( |
| 614 self._cpp_type_generator.GetEnumNoneValue(prop), |
| 615 'scoped_ptr<base::Value>()')) |
| 616 for choice in self._cpp_type_generator.ExpandParams([prop]): |
| 617 c.Concat(self._GenerateReturnCase( |
| 618 self._cpp_type_generator.GetEnumValue(prop, choice.type_.name), |
| 619 'make_scoped_ptr<base::Value>(%s)' % |
| 620 self._CreateValueFromProperty(choice, choice.unix_name))) |
| 621 (c.Eblock('}') |
| 622 .Append('NOTREACHED();') |
| 623 .Append('return scoped_ptr<base::Value>();') |
| 624 .Eblock('}') |
| 625 .Append() |
| 626 .Substitute({ |
| 627 'cpp_namespace': cpp_namespace, |
| 628 'choice': cpp_util.Classname(prop.name) |
| 629 }) |
| 630 ) |
| 631 return c |
| 632 |
| 633 def _GenerateCreateEnumValue(self, cpp_namespace, prop): |
| 634 """Generates CreateEnumValue() that returns the base::StringValue |
| 635 representation of an enum. |
| 636 """ |
| 637 c = Code() |
| 638 c.Append('// static') |
| 639 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' |
| 640 '%(arg)s) {') |
| 641 c.Sblock('switch (%s) {' % prop.unix_name) |
| 642 if prop.optional: |
| 643 c.Concat(self._GenerateReturnCase( |
| 644 self._cpp_type_generator.GetEnumNoneValue(prop), |
| 645 'scoped_ptr<base::Value>()')) |
| 646 for enum_value in prop.enum_values: |
| 647 c.Concat(self._GenerateReturnCase( |
| 648 self._cpp_type_generator.GetEnumValue(prop, enum_value), |
| 649 'scoped_ptr<base::Value>(base::Value::CreateStringValue("%s"))' % |
| 650 enum_value)) |
| 651 (c.Eblock('}') |
| 652 .Append('NOTREACHED();') |
| 653 .Append('return scoped_ptr<base::Value>();') |
| 654 .Eblock('}') |
| 655 .Substitute({ |
| 656 'cpp_namespace': cpp_namespace, |
| 657 'arg': cpp_util.GetParameterDeclaration( |
| 658 prop, self._cpp_type_generator.GetType(prop)) |
| 659 }) |
| 660 ) |
| 661 return c |
| 662 |
| 663 def _GenerateReturnCase(self, case_value, return_value): |
| 664 """Generates a single return case for a switch block. |
| 665 """ |
| 666 c = Code() |
| 667 (c.Append('case %s:' % case_value) |
| 668 .Append(' return %s;' % return_value) |
| 669 ) |
| 670 return c |
| 671 |
637 def _GenerateCreateCallbackArguments(self, function_scope, callback): | 672 def _GenerateCreateCallbackArguments(self, function_scope, callback): |
638 """Generate all functions to create Value parameters for a callback. | 673 """Generate all functions to create Value parameters for a callback. |
639 | 674 |
640 E.g for function "Bar", generate Bar::Results::Create | 675 E.g for function "Bar", generate Bar::Results::Create |
641 E.g for event "Baz", generate Baz::Create | 676 E.g for event "Baz", generate Baz::Create |
642 | 677 |
643 function_scope: the function scope path, e.g. Foo::Bar for the function | 678 function_scope: the function scope path, e.g. Foo::Bar for the function |
644 Foo::Bar::Baz(). | 679 Foo::Bar::Baz(). |
645 callback: the Function object we are creating callback arguments for. | 680 callback: the Function object we are creating callback arguments for. |
646 """ | 681 """ |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 """ | 741 """ |
707 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 742 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
708 PropertyType.ARRAY) | 743 PropertyType.ARRAY) |
709 | 744 |
710 def _IsFundamentalOrFundamentalRef(self, prop): | 745 def _IsFundamentalOrFundamentalRef(self, prop): |
711 """Determines if this property is a Fundamental type or is a ref to a | 746 """Determines if this property is a Fundamental type or is a ref to a |
712 Fundamental type. | 747 Fundamental type. |
713 """ | 748 """ |
714 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 749 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
715 is_fundamental) | 750 is_fundamental) |
OLD | NEW |