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 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 return c | 206 return c |
207 | 207 |
208 def _GenerateTypePopulateProperty(self, prop, src, dst): | 208 def _GenerateTypePopulateProperty(self, prop, src, dst): |
209 """Generate the code to populate a single property in a type. | 209 """Generate the code to populate a single property in a type. |
210 | 210 |
211 src: base::DictionaryValue* | 211 src: base::DictionaryValue* |
212 dst: Type* | 212 dst: Type* |
213 """ | 213 """ |
214 c = Code() | 214 c = Code() |
215 value_var = prop.unix_name + '_value' | 215 value_var = prop.unix_name + '_value' |
216 c.Append('base::Value* %(value_var)s = NULL;') | 216 c.Append('const base::Value* %(value_var)s = NULL;') |
217 if prop.optional: | 217 if prop.optional: |
218 (c.Sblock( | 218 (c.Sblock( |
219 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {' | 219 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {' |
220 ) | 220 ) |
221 .Concat(self._GeneratePopulatePropertyFromValue( | 221 .Concat(self._GeneratePopulatePropertyFromValue( |
222 prop, value_var, dst, 'false')) | 222 prop, value_var, dst, 'false')) |
223 .Eblock('}') | 223 .Eblock('}') |
224 ) | 224 ) |
225 else: | 225 else: |
226 (c.Append( | 226 (c.Append( |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 else: | 446 else: |
447 (c.Append('if (!%s)' % | 447 (c.Append('if (!%s)' % |
448 cpp_util.GetAsFundamentalValue( | 448 cpp_util.GetAsFundamentalValue( |
449 self._cpp_type_generator.GetReferencedProperty(prop), | 449 self._cpp_type_generator.GetReferencedProperty(prop), |
450 value_var, | 450 value_var, |
451 '&%s->%s' % (dst, prop.unix_name))) | 451 '&%s->%s' % (dst, prop.unix_name))) |
452 .Append(' return %(failure_value)s;') | 452 .Append(' return %(failure_value)s;') |
453 ) | 453 ) |
454 elif self._IsObjectOrObjectRef(prop): | 454 elif self._IsObjectOrObjectRef(prop): |
455 if prop.optional: | 455 if prop.optional: |
456 (c.Append('base::DictionaryValue* dictionary = NULL;') | 456 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
457 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | 457 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
458 .Append(' return %(failure_value)s;') | 458 .Append(' return %(failure_value)s;') |
459 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') | 459 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') |
460 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') | 460 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') |
461 .Append(' return %(failure_value)s;') | 461 .Append(' return %(failure_value)s;') |
462 .Append('%(dst)s->%(name)s = temp.Pass();') | 462 .Append('%(dst)s->%(name)s = temp.Pass();') |
463 ) | 463 ) |
464 else: | 464 else: |
465 (c.Append('base::DictionaryValue* dictionary = NULL;') | 465 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
466 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | 466 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
467 .Append(' return %(failure_value)s;') | 467 .Append(' return %(failure_value)s;') |
468 .Append( | 468 .Append( |
469 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') | 469 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') |
470 .Append(' return %(failure_value)s;') | 470 .Append(' return %(failure_value)s;') |
471 ) | 471 ) |
472 elif prop.type_ == PropertyType.ANY: | 472 elif prop.type_ == PropertyType.ANY: |
473 if prop.optional: | 473 if prop.optional: |
474 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());') | 474 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());') |
475 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') | 475 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') |
476 elif self._IsArrayOrArrayRef(prop): | 476 elif self._IsArrayOrArrayRef(prop): |
477 # util_cc_helper deals with optional and required arrays | 477 # util_cc_helper deals with optional and required arrays |
478 (c.Append('base::ListValue* list = NULL;') | 478 (c.Append('const base::ListValue* list = NULL;') |
479 .Append('if (!%(value_var)s->GetAsList(&list))') | 479 .Append('if (!%(value_var)s->GetAsList(&list))') |
480 .Append(' return %(failure_value)s;')) | 480 .Append(' return %(failure_value)s;')) |
481 if prop.item_type.type_ == PropertyType.ENUM: | 481 if prop.item_type.type_ == PropertyType.ENUM: |
482 self._GenerateListValueToEnumArrayConversion(c, prop) | 482 self._GenerateListValueToEnumArrayConversion(c, prop) |
483 else: | 483 else: |
484 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 484 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( |
485 self._cpp_type_generator.GetReferencedProperty(prop), 'list', | 485 self._cpp_type_generator.GetReferencedProperty(prop), 'list', |
486 dst + '->' + prop.unix_name, prop.optional)) | 486 dst + '->' + prop.unix_name, prop.optional)) |
487 .Append(' return %(failure_value)s;') | 487 .Append(' return %(failure_value)s;') |
488 ) | 488 ) |
(...skipping 17 matching lines...) Expand all Loading... |
506 ) | 506 ) |
507 c.Eblock('}') | 507 c.Eblock('}') |
508 elif prop.type_ == PropertyType.ENUM: | 508 elif prop.type_ == PropertyType.ENUM: |
509 c.Sblock('{') | 509 c.Sblock('{') |
510 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp') | 510 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp') |
511 c.Append('%(dst)s->%(name)s = enum_temp;') | 511 c.Append('%(dst)s->%(name)s = enum_temp;') |
512 c.Eblock('}') | 512 c.Eblock('}') |
513 elif prop.type_ == PropertyType.BINARY: | 513 elif prop.type_ == PropertyType.BINARY: |
514 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') | 514 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') |
515 .Append(' return %(failure_value)s;') | 515 .Append(' return %(failure_value)s;') |
516 .Append('base::BinaryValue* binary_value =') | 516 .Append('const base::BinaryValue* binary_value =') |
517 .Append(' static_cast<base::BinaryValue*>(%(value_var)s);') | 517 .Append(' static_cast<const base::BinaryValue*>(%(value_var)s);') |
518 ) | 518 ) |
519 if prop.optional: | 519 if prop.optional: |
520 (c.Append('%(dst)s->%(name)s.reset(') | 520 (c.Append('%(dst)s->%(name)s.reset(') |
521 .Append(' new std::string(binary_value->GetBuffer(),') | 521 .Append(' new std::string(binary_value->GetBuffer(),') |
522 .Append(' binary_value->GetSize()));') | 522 .Append(' binary_value->GetSize()));') |
523 ) | 523 ) |
524 else: | 524 else: |
525 (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),') | 525 (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),') |
526 .Append(' binary_value->GetSize());') | 526 .Append(' binary_value->GetSize());') |
527 ) | 527 ) |
(...skipping 19 matching lines...) Expand all Loading... |
547 Leaves dst, name, and failure_value unsubstituted. | 547 Leaves dst, name, and failure_value unsubstituted. |
548 | 548 |
549 c: the Code object that is being appended to. | 549 c: the Code object that is being appended to. |
550 prop: the property that the code is populating. | 550 prop: the property that the code is populating. |
551 """ | 551 """ |
552 accessor = '.' | 552 accessor = '.' |
553 if prop.optional: | 553 if prop.optional: |
554 c.Append('%(dst)s->%(name)s.reset(new std::vector<' + ( | 554 c.Append('%(dst)s->%(name)s.reset(new std::vector<' + ( |
555 self._cpp_type_generator.GetType(prop.item_type) + '>);')) | 555 self._cpp_type_generator.GetType(prop.item_type) + '>);')) |
556 accessor = '->' | 556 accessor = '->' |
557 c.Sblock('for (ListValue::iterator it = list->begin(); ' | 557 c.Sblock('for (ListValue::const_iterator it = list->begin(); ' |
558 'it != list->end(); ++it) {') | 558 'it != list->end(); ++it) {') |
559 self._GenerateStringToEnumConversion(c, prop.item_type, | 559 self._GenerateStringToEnumConversion(c, prop.item_type, |
560 '(*it)', 'enum_temp') | 560 '(*it)', 'enum_temp') |
561 c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);') | 561 c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);') |
562 c.Eblock('}') | 562 c.Eblock('}') |
563 | 563 |
564 def _GenerateStringToEnumConversion(self, c, prop, value_var, enum_temp): | 564 def _GenerateStringToEnumConversion(self, c, prop, value_var, enum_temp): |
565 """Appends code that converts a string to an enum. | 565 """Appends code that converts a string to an enum. |
566 Leaves failure_value unsubstituded. | 566 Leaves failure_value unsubstituded. |
567 | 567 |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 """ | 751 """ |
752 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 752 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
753 PropertyType.ARRAY) | 753 PropertyType.ARRAY) |
754 | 754 |
755 def _IsFundamentalOrFundamentalRef(self, prop): | 755 def _IsFundamentalOrFundamentalRef(self, prop): |
756 """Determines if this property is a Fundamental type or is a ref to a | 756 """Determines if this property is a Fundamental type or is a ref to a |
757 Fundamental type. | 757 Fundamental type. |
758 """ | 758 """ |
759 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 759 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
760 is_fundamental) | 760 is_fundamental) |
OLD | NEW |