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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 return c | 207 return c |
208 | 208 |
209 def _GenerateTypePopulateProperty(self, prop, src, dst): | 209 def _GenerateTypePopulateProperty(self, prop, src, dst): |
210 """Generate the code to populate a single property in a type. | 210 """Generate the code to populate a single property in a type. |
211 | 211 |
212 src: base::DictionaryValue* | 212 src: base::DictionaryValue* |
213 dst: Type* | 213 dst: Type* |
214 """ | 214 """ |
215 c = Code() | 215 c = Code() |
216 value_var = prop.unix_name + '_value' | 216 value_var = prop.unix_name + '_value' |
217 c.Append('base::Value* %(value_var)s = NULL;') | 217 c.Append('const base::Value* %(value_var)s = NULL;') |
218 if prop.optional: | 218 if prop.optional: |
219 (c.Sblock( | 219 (c.Sblock( |
220 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {' | 220 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {' |
221 ) | 221 ) |
222 .Concat(self._GeneratePopulatePropertyFromValue( | 222 .Concat(self._GeneratePopulatePropertyFromValue( |
223 prop, value_var, dst, 'false')) | 223 prop, value_var, dst, 'false')) |
224 .Eblock('}') | 224 .Eblock('}') |
225 ) | 225 ) |
226 else: | 226 else: |
227 (c.Append( | 227 (c.Append( |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 else: | 453 else: |
454 (c.Append('if (!%s)' % | 454 (c.Append('if (!%s)' % |
455 cpp_util.GetAsFundamentalValue( | 455 cpp_util.GetAsFundamentalValue( |
456 self._cpp_type_generator.GetReferencedProperty(prop), | 456 self._cpp_type_generator.GetReferencedProperty(prop), |
457 value_var, | 457 value_var, |
458 '&%s->%s' % (dst, prop.unix_name))) | 458 '&%s->%s' % (dst, prop.unix_name))) |
459 .Append(' return %(failure_value)s;') | 459 .Append(' return %(failure_value)s;') |
460 ) | 460 ) |
461 elif self._IsObjectOrObjectRef(prop): | 461 elif self._IsObjectOrObjectRef(prop): |
462 if prop.optional: | 462 if prop.optional: |
463 (c.Append('base::DictionaryValue* dictionary = NULL;') | 463 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
464 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | 464 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
465 .Append(' return %(failure_value)s;') | 465 .Append(' return %(failure_value)s;') |
466 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') | 466 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') |
467 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') | 467 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') |
468 .Append(' return %(failure_value)s;') | 468 .Append(' return %(failure_value)s;') |
469 .Append('%(dst)s->%(name)s = temp.Pass();') | 469 .Append('%(dst)s->%(name)s = temp.Pass();') |
470 ) | 470 ) |
471 else: | 471 else: |
472 (c.Append('base::DictionaryValue* dictionary = NULL;') | 472 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
473 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | 473 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
474 .Append(' return %(failure_value)s;') | 474 .Append(' return %(failure_value)s;') |
475 .Append( | 475 .Append( |
476 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') | 476 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') |
477 .Append(' return %(failure_value)s;') | 477 .Append(' return %(failure_value)s;') |
478 ) | 478 ) |
479 elif prop.type_ == PropertyType.FUNCTION: | 479 elif prop.type_ == PropertyType.FUNCTION: |
480 if prop.optional: | 480 if prop.optional: |
481 c.Append('%(dst)s->%(name)s.reset(new base::DictionaryValue());') | 481 c.Append('%(dst)s->%(name)s.reset(new base::DictionaryValue());') |
482 elif prop.type_ == PropertyType.ANY: | 482 elif prop.type_ == PropertyType.ANY: |
483 if prop.optional: | 483 if prop.optional: |
484 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());') | 484 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());') |
485 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') | 485 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') |
486 elif self._IsArrayOrArrayRef(prop): | 486 elif self._IsArrayOrArrayRef(prop): |
487 # util_cc_helper deals with optional and required arrays | 487 # util_cc_helper deals with optional and required arrays |
488 (c.Append('base::ListValue* list = NULL;') | 488 (c.Append('const base::ListValue* list = NULL;') |
489 .Append('if (!%(value_var)s->GetAsList(&list))') | 489 .Append('if (!%(value_var)s->GetAsList(&list))') |
490 .Append(' return %(failure_value)s;')) | 490 .Append(' return %(failure_value)s;')) |
491 if prop.item_type.type_ == PropertyType.ENUM: | 491 if prop.item_type.type_ == PropertyType.ENUM: |
492 self._GenerateListValueToEnumArrayConversion(c, prop) | 492 self._GenerateListValueToEnumArrayConversion(c, prop) |
493 else: | 493 else: |
494 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 494 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( |
495 self._cpp_type_generator.GetReferencedProperty(prop), 'list', | 495 self._cpp_type_generator.GetReferencedProperty(prop), 'list', |
496 dst + '->' + prop.unix_name, prop.optional)) | 496 dst + '->' + prop.unix_name, prop.optional)) |
497 .Append(' return %(failure_value)s;') | 497 .Append(' return %(failure_value)s;') |
498 ) | 498 ) |
(...skipping 17 matching lines...) Expand all Loading... |
516 ) | 516 ) |
517 c.Eblock('}') | 517 c.Eblock('}') |
518 elif prop.type_ == PropertyType.ENUM: | 518 elif prop.type_ == PropertyType.ENUM: |
519 c.Sblock('{') | 519 c.Sblock('{') |
520 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp') | 520 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp') |
521 c.Append('%(dst)s->%(name)s = enum_temp;') | 521 c.Append('%(dst)s->%(name)s = enum_temp;') |
522 c.Eblock('}') | 522 c.Eblock('}') |
523 elif prop.type_ == PropertyType.BINARY: | 523 elif prop.type_ == PropertyType.BINARY: |
524 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') | 524 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') |
525 .Append(' return %(failure_value)s;') | 525 .Append(' return %(failure_value)s;') |
526 .Append('base::BinaryValue* binary_value =') | 526 .Append('const base::BinaryValue* binary_value =') |
527 .Append(' static_cast<base::BinaryValue*>(%(value_var)s);') | 527 .Append(' static_cast<const base::BinaryValue*>(%(value_var)s);') |
528 ) | 528 ) |
529 if prop.optional: | 529 if prop.optional: |
530 (c.Append('%(dst)s->%(name)s.reset(') | 530 (c.Append('%(dst)s->%(name)s.reset(') |
531 .Append(' new std::string(binary_value->GetBuffer(),') | 531 .Append(' new std::string(binary_value->GetBuffer(),') |
532 .Append(' binary_value->GetSize()));') | 532 .Append(' binary_value->GetSize()));') |
533 ) | 533 ) |
534 else: | 534 else: |
535 (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),') | 535 (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),') |
536 .Append(' binary_value->GetSize());') | 536 .Append(' binary_value->GetSize());') |
537 ) | 537 ) |
(...skipping 19 matching lines...) Expand all Loading... |
557 Leaves dst, name, and failure_value unsubstituted. | 557 Leaves dst, name, and failure_value unsubstituted. |
558 | 558 |
559 c: the Code object that is being appended to. | 559 c: the Code object that is being appended to. |
560 prop: the property that the code is populating. | 560 prop: the property that the code is populating. |
561 """ | 561 """ |
562 accessor = '.' | 562 accessor = '.' |
563 if prop.optional: | 563 if prop.optional: |
564 c.Append('%(dst)s->%(name)s.reset(new std::vector<' + ( | 564 c.Append('%(dst)s->%(name)s.reset(new std::vector<' + ( |
565 self._cpp_type_generator.GetType(prop.item_type) + '>);')) | 565 self._cpp_type_generator.GetType(prop.item_type) + '>);')) |
566 accessor = '->' | 566 accessor = '->' |
567 c.Sblock('for (ListValue::iterator it = list->begin(); ' | 567 c.Sblock('for (ListValue::const_iterator it = list->begin(); ' |
568 'it != list->end(); ++it) {') | 568 'it != list->end(); ++it) {') |
569 self._GenerateStringToEnumConversion(c, prop.item_type, | 569 self._GenerateStringToEnumConversion(c, prop.item_type, |
570 '(*it)', 'enum_temp') | 570 '(*it)', 'enum_temp') |
571 c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);') | 571 c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);') |
572 c.Eblock('}') | 572 c.Eblock('}') |
573 | 573 |
574 def _GenerateStringToEnumConversion(self, c, prop, value_var, enum_temp): | 574 def _GenerateStringToEnumConversion(self, c, prop, value_var, enum_temp): |
575 """Appends code that converts a string to an enum. | 575 """Appends code that converts a string to an enum. |
576 Leaves failure_value unsubstituded. | 576 Leaves failure_value unsubstituded. |
577 | 577 |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 """ | 761 """ |
762 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 762 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
763 PropertyType.ARRAY) | 763 PropertyType.ARRAY) |
764 | 764 |
765 def _IsFundamentalOrFundamentalRef(self, prop): | 765 def _IsFundamentalOrFundamentalRef(self, prop): |
766 """Determines if this property is a Fundamental type or is a ref to a | 766 """Determines if this property is a Fundamental type or is a ref to a |
767 Fundamental type. | 767 Fundamental type. |
768 """ | 768 """ |
769 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 769 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
770 is_fundamental) | 770 is_fundamental) |
OLD | NEW |