| 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 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 failure_value: the value to return if |prop| cannot be extracted from | 427 failure_value: the value to return if |prop| cannot be extracted from |
| 428 |value_var| | 428 |value_var| |
| 429 check_type: if true, will check if |value_var| is the correct | 429 check_type: if true, will check if |value_var| is the correct |
| 430 base::Value::Type | 430 base::Value::Type |
| 431 """ | 431 """ |
| 432 c = Code() | 432 c = Code() |
| 433 c.Sblock('{') | 433 c.Sblock('{') |
| 434 | 434 |
| 435 if self._IsFundamentalOrFundamentalRef(prop): | 435 if self._IsFundamentalOrFundamentalRef(prop): |
| 436 if prop.optional: | 436 if prop.optional: |
| 437 (c.Append('%(ctype)s temp;') | 437 if prop.serialized_type is None: |
| 438 .Append('if (!%s)' % | 438 (c.Append('%(ctype)s temp;') |
| 439 cpp_util.GetAsFundamentalValue( | 439 .Append('if (!%s)' % |
| 440 self._cpp_type_generator.GetReferencedProperty(prop), | 440 cpp_util.GetAsFundamentalValue( |
| 441 value_var, | 441 self._cpp_type_generator.GetReferencedProperty(prop), |
| 442 '&temp')) | 442 value_var, |
| 443 .Append(' return %(failure_value)s;') | 443 '&temp')) |
| 444 .Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));') | 444 .Append(' return %(failure_value)s;') |
| 445 ) | 445 .Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));') |
| 446 ) |
| 447 else: |
| 448 (c.Append('%(ctype)s temp1;') |
| 449 .Append('%(serialized_ctype)s temp2;') |
| 450 .Append('if (!%s || !%s)' % |
| 451 (cpp_util.GetAsFundamentalValue( |
| 452 self._cpp_type_generator.GetReferencedProperty(prop), |
| 453 value_var, |
| 454 '&temp1'), |
| 455 cpp_util.DoConversion( |
| 456 self._cpp_type_generator.GetReferencedProperty(prop), |
| 457 'temp1', |
| 458 '&temp2'))) |
| 459 .Append(' return %(failure_value)s;') |
| 460 .Append('%(dst)s->%(name)s.reset(new %(serialized_ctype)s(temp2));') |
| 461 ) |
| 462 |
| 446 else: | 463 else: |
| 447 (c.Append('if (!%s)' % | 464 if prop.serialized_type is None: |
| 448 cpp_util.GetAsFundamentalValue( | 465 (c.Append('if (!%s)' % |
| 449 self._cpp_type_generator.GetReferencedProperty(prop), | 466 cpp_util.GetAsFundamentalValue( |
| 450 value_var, | 467 self._cpp_type_generator.GetReferencedProperty(prop), |
| 451 '&%s->%s' % (dst, prop.unix_name))) | 468 value_var, |
| 452 .Append(' return %(failure_value)s;') | 469 '&%s->%s' % (dst, prop.unix_name))) |
| 453 ) | 470 .Append(' return %(failure_value)s;') |
| 471 ) |
| 472 else: |
| 473 (c.Append('%(ctype)s temp;') |
| 474 .Append('if (!%s || !%s)' % |
| 475 (cpp_util.GetAsFundamentalValue( |
| 476 self._cpp_type_generator.GetReferencedProperty(prop), |
| 477 value_var, |
| 478 '&temp'), |
| 479 cpp_util.DoConversion( |
| 480 self._cpp_type_generator.GetReferencedProperty(prop), |
| 481 'temp', |
| 482 '&%s->%s' % (dst, prop.unix_name)))) |
| 483 .Append(' return %(failure_value)s;') |
| 484 ) |
| 485 |
| 454 elif self._IsObjectOrObjectRef(prop): | 486 elif self._IsObjectOrObjectRef(prop): |
| 455 if prop.optional: | 487 if prop.optional: |
| 456 (c.Append('base::DictionaryValue* dictionary = NULL;') | 488 (c.Append('base::DictionaryValue* dictionary = NULL;') |
| 457 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | 489 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
| 458 .Append(' return %(failure_value)s;') | 490 .Append(' return %(failure_value)s;') |
| 459 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') | 491 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') |
| 460 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') | 492 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') |
| 461 .Append(' return %(failure_value)s;') | 493 .Append(' return %(failure_value)s;') |
| 462 .Append('%(dst)s->%(name)s = temp.Pass();') | 494 .Append('%(dst)s->%(name)s = temp.Pass();') |
| 463 ) | 495 ) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 529 raise NotImplementedError(prop.type_) | 561 raise NotImplementedError(prop.type_) |
| 530 c.Eblock('}') | 562 c.Eblock('}') |
| 531 sub = { | 563 sub = { |
| 532 'value_var': value_var, | 564 'value_var': value_var, |
| 533 'name': prop.unix_name, | 565 'name': prop.unix_name, |
| 534 'dst': dst, | 566 'dst': dst, |
| 535 'failure_value': failure_value, | 567 'failure_value': failure_value, |
| 536 } | 568 } |
| 537 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY): | 569 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY): |
| 538 sub['ctype'] = self._cpp_type_generator.GetType(prop) | 570 sub['ctype'] = self._cpp_type_generator.GetType(prop) |
| 571 sub['serialized_ctype'] = self._cpp_type_generator.GetType( |
| 572 prop, serialized=True) |
| 539 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator | 573 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator |
| 540 .GetReferencedProperty(prop).type_) | 574 .GetReferencedProperty(prop).type_) |
| 541 c.Substitute(sub) | 575 c.Substitute(sub) |
| 542 return c | 576 return c |
| 543 | 577 |
| 544 def _GenerateListValueToEnumArrayConversion(self, c, prop): | 578 def _GenerateListValueToEnumArrayConversion(self, c, prop): |
| 545 """Appends code that converts a ListValue of string contstants to | 579 """Appends code that converts a ListValue of string contstants to |
| 546 an array of enums in dst. | 580 an array of enums in dst. |
| 547 Leaves dst, name, and failure_value unsubstituted. | 581 Leaves dst, name, and failure_value unsubstituted. |
| 548 | 582 |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 """ | 785 """ |
| 752 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 786 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
| 753 PropertyType.ARRAY) | 787 PropertyType.ARRAY) |
| 754 | 788 |
| 755 def _IsFundamentalOrFundamentalRef(self, prop): | 789 def _IsFundamentalOrFundamentalRef(self, prop): |
| 756 """Determines if this property is a Fundamental type or is a ref to a | 790 """Determines if this property is a Fundamental type or is a ref to a |
| 757 Fundamental type. | 791 Fundamental type. |
| 758 """ | 792 """ |
| 759 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 793 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
| 760 is_fundamental) | 794 is_fundamental) |
| OLD | NEW |