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 sys | 10 import sys |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 prop: the property the code is populating. | 448 prop: the property the code is populating. |
449 value_var: a Value* that should represent |prop|. | 449 value_var: a Value* that should represent |prop|. |
450 dst: the object with |prop| as a member. | 450 dst: the object with |prop| as a member. |
451 failure_value: the value to return if |prop| cannot be extracted from | 451 failure_value: the value to return if |prop| cannot be extracted from |
452 |value_var| | 452 |value_var| |
453 check_type: if true, will check if |value_var| is the correct Value::Type | 453 check_type: if true, will check if |value_var| is the correct Value::Type |
454 """ | 454 """ |
455 c = Code() | 455 c = Code() |
456 c.Sblock('{') | 456 c.Sblock('{') |
457 | 457 |
458 if check_type and prop.type_ not in ( | |
459 PropertyType.CHOICES, PropertyType.ANY): | |
460 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') | |
461 .Append(' return %(failure_value)s;') | |
462 ) | |
463 | |
464 if self._IsFundamentalOrFundamentalRef(prop): | 458 if self._IsFundamentalOrFundamentalRef(prop): |
465 if prop.optional: | 459 if prop.optional: |
466 (c.Append('%(ctype)s temp;') | 460 (c.Append('%(ctype)s temp;') |
467 .Append('if (%s)' % | 461 .Append('if (!%s)' % |
468 cpp_util.GetAsFundamentalValue( | 462 cpp_util.GetAsFundamentalValue( |
469 self._cpp_type_generator.GetReferencedProperty(prop), | 463 self._cpp_type_generator.GetReferencedProperty(prop), |
470 value_var, | 464 value_var, |
471 '&temp')) | 465 '&temp')) |
472 .Append(' %(dst)s->%(name)s.reset(new %(ctype)s(temp));') | 466 .Append(' return %(failure_value)s;') |
| 467 .Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));') |
473 ) | 468 ) |
474 else: | 469 else: |
475 (c.Append('if (!%s)' % | 470 (c.Append('if (!%s)' % |
476 cpp_util.GetAsFundamentalValue( | 471 cpp_util.GetAsFundamentalValue( |
477 self._cpp_type_generator.GetReferencedProperty(prop), | 472 self._cpp_type_generator.GetReferencedProperty(prop), |
478 value_var, | 473 value_var, |
479 '&%s->%s' % (dst, prop.unix_name))) | 474 '&%s->%s' % (dst, prop.unix_name))) |
480 .Append('return %(failure_value)s;') | 475 .Append(' return %(failure_value)s;') |
481 ) | 476 ) |
482 elif self._IsObjectOrObjectRef(prop): | 477 elif self._IsObjectOrObjectRef(prop): |
483 if prop.optional: | 478 if prop.optional: |
484 (c.Append('DictionaryValue* dictionary = NULL;') | 479 (c.Append('DictionaryValue* dictionary = NULL;') |
485 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | 480 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
486 .Append(' return %(failure_value)s;') | 481 .Append(' return %(failure_value)s;') |
487 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') | 482 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') |
488 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') | 483 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') |
489 .Append(' return %(failure_value)s;') | 484 .Append(' return %(failure_value)s;') |
490 .Append('%(dst)s->%(name)s = temp.Pass();') | 485 .Append('%(dst)s->%(name)s = temp.Pass();') |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 prop.unix_name, | 539 prop.unix_name, |
545 self._cpp_type_generator.GetEnumValue(prop, enum_value))) | 540 self._cpp_type_generator.GetEnumValue(prop, enum_value))) |
546 ) | 541 ) |
547 (c.Append('else') | 542 (c.Append('else') |
548 .Append(' return %(failure_value)s;') | 543 .Append(' return %(failure_value)s;') |
549 ) | 544 ) |
550 elif prop.type_ == PropertyType.BINARY: | 545 elif prop.type_ == PropertyType.BINARY: |
551 # This is the same if the property is optional or not. We need a pointer | 546 # This is the same if the property is optional or not. We need a pointer |
552 # to the BinaryValue to be able to populate it, so a scoped_ptr is used | 547 # to the BinaryValue to be able to populate it, so a scoped_ptr is used |
553 # whether it is optional or required. | 548 # whether it is optional or required. |
554 (c.Append('%(dst)s->%(name)s.reset(') | 549 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') |
| 550 .Append(' return %(failure_value)s;') |
| 551 .Append('%(dst)s->%(name)s.reset(') |
555 .Append(' static_cast<BinaryValue*>(%(value_var)s)->DeepCopy());') | 552 .Append(' static_cast<BinaryValue*>(%(value_var)s)->DeepCopy());') |
556 ) | 553 ) |
557 else: | 554 else: |
558 raise NotImplementedError(prop.type_) | 555 raise NotImplementedError(prop.type_) |
559 c.Eblock('}') | 556 c.Eblock('}') |
560 sub = { | 557 sub = { |
561 'value_var': value_var, | 558 'value_var': value_var, |
562 'name': prop.unix_name, | 559 'name': prop.unix_name, |
563 'dst': dst, | 560 'dst': dst, |
564 'failure_value': failure_value, | 561 'failure_value': failure_value, |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 """ | 659 """ |
663 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 660 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
664 PropertyType.ARRAY) | 661 PropertyType.ARRAY) |
665 | 662 |
666 def _IsFundamentalOrFundamentalRef(self, prop): | 663 def _IsFundamentalOrFundamentalRef(self, prop): |
667 """Determines if this property is a Fundamental type or is a ref to a | 664 """Determines if this property is a Fundamental type or is a ref to a |
668 Fundamental type. | 665 Fundamental type. |
669 """ | 666 """ |
670 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 667 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
671 is_fundamental) | 668 is_fundamental) |
OLD | NEW |