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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 .Append() | 120 .Append() |
121 ) | 121 ) |
122 if type_.from_json: | 122 if type_.from_json: |
123 (c.Concat(self._GenerateTypePopulate(cpp_namespace, type_)) | 123 (c.Concat(self._GenerateTypePopulate(cpp_namespace, type_)) |
124 .Append() | 124 .Append() |
125 ) | 125 ) |
126 if type_.from_client: | 126 if type_.from_client: |
127 (c.Concat(self._GenerateTypeToValue(cpp_namespace, type_)) | 127 (c.Concat(self._GenerateTypeToValue(cpp_namespace, type_)) |
128 .Append() | 128 .Append() |
129 ) | 129 ) |
| 130 elif self._cpp_type_generator.IsEnumOrEnumRef(type_): |
| 131 c.Concat(self._GenerateCreateEnumTypeValue(cpp_namespace, type_)) |
| 132 c.Append() |
130 c.Substitute({'classname': classname, 'namespace': cpp_namespace}) | 133 c.Substitute({'classname': classname, 'namespace': cpp_namespace}) |
131 | 134 |
132 return c | 135 return c |
133 | 136 |
134 def _GenerateInitializersAndBody(self, type_): | 137 def _GenerateInitializersAndBody(self, type_): |
135 items = [] | 138 items = [] |
136 for prop in type_.properties.values(): | 139 for prop in type_.properties.values(): |
137 if prop.optional: | 140 if prop.optional: |
138 continue | 141 continue |
139 | 142 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 cpp_namespace) | 248 cpp_namespace) |
246 .Append('scoped_ptr<base::DictionaryValue> value(' | 249 .Append('scoped_ptr<base::DictionaryValue> value(' |
247 'new base::DictionaryValue());') | 250 'new base::DictionaryValue());') |
248 .Append() | 251 .Append() |
249 ) | 252 ) |
250 for prop in type_.properties.values(): | 253 for prop in type_.properties.values(): |
251 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 254 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
252 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) | 255 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) |
253 else: | 256 else: |
254 if prop.optional: | 257 if prop.optional: |
255 if prop.type_ == PropertyType.ENUM: | 258 if self._cpp_type_generator.IsEnumOrEnumRef(prop): |
256 c.Sblock('if (%s != %s) {' % | 259 c.Sblock('if (%s != %s) {' % |
257 (prop.unix_name, | 260 (prop.unix_name, |
258 self._cpp_type_generator.GetEnumNoneValue(prop))) | 261 self._cpp_type_generator.GetEnumNoneValue(prop))) |
259 elif prop.type_ == PropertyType.CHOICES: | 262 elif prop.type_ == PropertyType.CHOICES: |
260 c.Sblock('if (%s_type != %s) {' % | 263 c.Sblock('if (%s_type != %s) {' % |
261 (prop.unix_name, | 264 (prop.unix_name, |
262 self._cpp_type_generator.GetEnumNoneValue(prop))) | 265 self._cpp_type_generator.GetEnumNoneValue(prop))) |
263 else: | 266 else: |
264 c.Sblock('if (%s.get()) {' % prop.unix_name) | 267 c.Sblock('if (%s.get()) {' % prop.unix_name) |
265 | 268 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 elif prop.type_ == PropertyType.ANY: | 335 elif prop.type_ == PropertyType.ANY: |
333 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) | 336 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) |
334 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 337 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
335 return '%s.DeepCopy()' % var | 338 return '%s.DeepCopy()' % var |
336 elif prop.type_ == PropertyType.FUNCTION: | 339 elif prop.type_ == PropertyType.FUNCTION: |
337 if prop.optional: | 340 if prop.optional: |
338 vardot = var + '->' | 341 vardot = var + '->' |
339 else: | 342 else: |
340 vardot = var + '.' | 343 vardot = var + '.' |
341 return '%sDeepCopy()' % vardot | 344 return '%sDeepCopy()' % vardot |
342 elif prop.type_ == PropertyType.ENUM: | 345 elif self._cpp_type_generator.IsEnumOrEnumRef(prop): |
343 return 'CreateEnumValue(%s).release()' % var | 346 return 'CreateEnumValue(%s).release()' % var |
344 elif prop.type_ == PropertyType.BINARY: | 347 elif prop.type_ == PropertyType.BINARY: |
345 if prop.optional: | 348 if prop.optional: |
346 vardot = var + '->' | 349 vardot = var + '->' |
347 else: | 350 else: |
348 vardot = var + '.' | 351 vardot = var + '.' |
349 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' % | 352 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' % |
350 (vardot, vardot)) | 353 (vardot, vardot)) |
351 elif self._IsArrayOrArrayRef(prop): | 354 elif self._IsArrayOrArrayRef(prop): |
352 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( | 355 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 dst: the object with |prop| as a member. | 456 dst: the object with |prop| as a member. |
454 failure_value: the value to return if |prop| cannot be extracted from | 457 failure_value: the value to return if |prop| cannot be extracted from |
455 |value_var| | 458 |value_var| |
456 check_type: if true, will check if |value_var| is the correct | 459 check_type: if true, will check if |value_var| is the correct |
457 base::Value::Type | 460 base::Value::Type |
458 """ | 461 """ |
459 c = Code() | 462 c = Code() |
460 c.Sblock('{') | 463 c.Sblock('{') |
461 | 464 |
462 if self._IsFundamentalOrFundamentalRef(prop): | 465 if self._IsFundamentalOrFundamentalRef(prop): |
463 if prop.optional: | 466 self._GenerateFundamentalOrFundamentalRefPopulate(c, prop, value_var, dst) |
464 (c.Append('%(ctype)s temp;') | |
465 .Append('if (!%s)' % | |
466 cpp_util.GetAsFundamentalValue( | |
467 self._cpp_type_generator.GetReferencedProperty(prop), | |
468 value_var, | |
469 '&temp')) | |
470 .Append(' return %(failure_value)s;') | |
471 ) | |
472 if prop.type_ != prop.compiled_type: | |
473 (c.Append('%(compiled_ctype)s temp2;') | |
474 .Append('if (!%s)' % | |
475 cpp_util.GenerateTypeToCompiledTypeConversion( | |
476 self._cpp_type_generator.GetReferencedProperty(prop), | |
477 'temp', | |
478 'temp2')) | |
479 .Append(' return %(failure_value)s;') | |
480 .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));') | |
481 ) | |
482 else: | |
483 c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));') | |
484 | |
485 else: | |
486 if prop.type_ == prop.compiled_type: | |
487 assignment_target = '&%s->%s' % (dst, prop.unix_name) | |
488 else: | |
489 c.Append('%(ctype)s temp;') | |
490 assignment_target = '&temp' | |
491 (c.Append('if (!%s)' % | |
492 cpp_util.GetAsFundamentalValue( | |
493 self._cpp_type_generator.GetReferencedProperty(prop), | |
494 value_var, | |
495 assignment_target)) | |
496 .Append(' return %(failure_value)s;') | |
497 ) | |
498 if prop.type_ != prop.compiled_type: | |
499 (c.Append('if (!%s)' % | |
500 cpp_util.GenerateTypeToCompiledTypeConversion( | |
501 self._cpp_type_generator.GetReferencedProperty(prop), | |
502 'temp', | |
503 '%s->%s' % (dst, prop.unix_name))) | |
504 .Append(' return %(failure_value)s;') | |
505 ) | |
506 | |
507 elif self._IsObjectOrObjectRef(prop): | 467 elif self._IsObjectOrObjectRef(prop): |
508 if prop.optional: | 468 self._GenerateObjectOrObjectRefPopulate(c, prop) |
509 (c.Append('const base::DictionaryValue* dictionary = NULL;') | |
510 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | |
511 .Append(' return %(failure_value)s;') | |
512 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') | |
513 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') | |
514 .Append(' return %(failure_value)s;') | |
515 .Append('%(dst)s->%(name)s = temp.Pass();') | |
516 ) | |
517 else: | |
518 (c.Append('const base::DictionaryValue* dictionary = NULL;') | |
519 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | |
520 .Append(' return %(failure_value)s;') | |
521 .Append( | |
522 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') | |
523 .Append(' return %(failure_value)s;') | |
524 ) | |
525 elif prop.type_ == PropertyType.FUNCTION: | 469 elif prop.type_ == PropertyType.FUNCTION: |
526 if prop.optional: | 470 self._GenerateFunctionPopulate(c, prop) |
527 c.Append('%(dst)s->%(name)s.reset(new base::DictionaryValue());') | |
528 elif prop.type_ == PropertyType.ANY: | 471 elif prop.type_ == PropertyType.ANY: |
529 if prop.optional: | 472 self._GenerateAnyPopulate(c, prop, value_var, dst) |
530 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());') | |
531 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') | |
532 elif self._IsArrayOrArrayRef(prop): | 473 elif self._IsArrayOrArrayRef(prop): |
533 # util_cc_helper deals with optional and required arrays | 474 self._GenerateArrayOrArrayRefPopulate(c, prop, dst) |
534 (c.Append('const base::ListValue* list = NULL;') | |
535 .Append('if (!%(value_var)s->GetAsList(&list))') | |
536 .Append(' return %(failure_value)s;')) | |
537 if prop.item_type.type_ == PropertyType.ENUM: | |
538 self._GenerateListValueToEnumArrayConversion(c, prop) | |
539 else: | |
540 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | |
541 self._cpp_type_generator.GetReferencedProperty(prop), 'list', | |
542 dst + '->' + prop.unix_name, prop.optional)) | |
543 .Append(' return %(failure_value)s;') | |
544 ) | |
545 elif prop.type_ == PropertyType.CHOICES: | 475 elif prop.type_ == PropertyType.CHOICES: |
546 type_var = '%(dst)s->%(name)s_type' | 476 self._GenerateChoicePopulate(c, prop, value_var, dst, failure_value) |
547 c.Sblock('switch (%(value_var)s->GetType()) {') | 477 elif self._cpp_type_generator.IsEnumOrEnumRef(prop): |
548 for choice in self._cpp_type_generator.ExpandParams([prop]): | 478 self._GenerateEnumPopulate(c, prop, value_var) |
549 (c.Sblock('case %s: {' % cpp_util.GetValueType( | |
550 self._cpp_type_generator.GetReferencedProperty(choice).type_)) | |
551 .Concat(self._GeneratePopulatePropertyFromValue( | |
552 choice, value_var, dst, failure_value, check_type=False)) | |
553 .Append('%s = %s;' % | |
554 (type_var, | |
555 self._cpp_type_generator.GetEnumValue( | |
556 prop, choice.type_.name))) | |
557 .Append('break;') | |
558 .Eblock('}') | |
559 ) | |
560 (c.Append('default:') | |
561 .Append(' return %(failure_value)s;') | |
562 ) | |
563 c.Eblock('}') | |
564 elif prop.type_ == PropertyType.ENUM: | |
565 c.Sblock('{') | |
566 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp') | |
567 c.Append('%(dst)s->%(name)s = enum_temp;') | |
568 c.Eblock('}') | |
569 elif prop.type_ == PropertyType.BINARY: | 479 elif prop.type_ == PropertyType.BINARY: |
570 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') | 480 self._GenerateBinaryPopulate(c, prop) |
571 .Append(' return %(failure_value)s;') | |
572 .Append('const base::BinaryValue* binary_value =') | |
573 .Append(' static_cast<const base::BinaryValue*>(%(value_var)s);') | |
574 ) | |
575 if prop.optional: | |
576 (c.Append('%(dst)s->%(name)s.reset(') | |
577 .Append(' new std::string(binary_value->GetBuffer(),') | |
578 .Append(' binary_value->GetSize()));') | |
579 ) | |
580 else: | |
581 (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),') | |
582 .Append(' binary_value->GetSize());') | |
583 ) | |
584 else: | 481 else: |
585 raise NotImplementedError(prop.type_) | 482 raise NotImplementedError(prop.type_) |
586 c.Eblock('}') | 483 c.Eblock('}') |
587 sub = { | 484 sub = { |
588 'value_var': value_var, | 485 'value_var': value_var, |
589 'name': prop.unix_name, | 486 'name': prop.unix_name, |
590 'dst': dst, | 487 'dst': dst, |
591 'failure_value': failure_value, | 488 'failure_value': failure_value, |
592 } | 489 } |
593 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY): | 490 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY): |
594 sub['ctype'] = self._cpp_type_generator.GetType(prop) | 491 sub['ctype'] = self._cpp_type_generator.GetType(prop) |
595 sub['compiled_ctype'] = self._cpp_type_generator.GetCompiledType(prop) | 492 sub['compiled_ctype'] = self._cpp_type_generator.GetCompiledType(prop) |
596 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator | 493 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator |
597 .GetReferencedProperty(prop).type_) | 494 .GetReferencedProperty(prop).type_) |
598 c.Substitute(sub) | 495 c.Substitute(sub) |
599 return c | 496 return c |
600 | 497 |
| 498 def _GenerateFundamentalOrFundamentalRefPopulate(self, |
| 499 c, |
| 500 prop, |
| 501 value_var, |
| 502 dst): |
| 503 if prop.optional: |
| 504 (c.Append('%(ctype)s temp;') |
| 505 .Append('if (!%s)' % |
| 506 cpp_util.GetAsFundamentalValue( |
| 507 self._cpp_type_generator.GetReferencedProperty(prop), |
| 508 value_var, |
| 509 '&temp')) |
| 510 .Append(' return %(failure_value)s;') |
| 511 ) |
| 512 if prop.type_ != prop.compiled_type: |
| 513 (c.Append('%(compiled_ctype)s temp2;') |
| 514 .Append('if (!%s)' % |
| 515 cpp_util.GenerateTypeToCompiledTypeConversion( |
| 516 self._cpp_type_generator.GetReferencedProperty(prop), |
| 517 'temp', |
| 518 'temp2')) |
| 519 .Append(' return %(failure_value)s;') |
| 520 .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));') |
| 521 ) |
| 522 else: |
| 523 c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));') |
| 524 |
| 525 else: |
| 526 if prop.type_ == prop.compiled_type: |
| 527 assignment_target = '&%s->%s' % (dst, prop.unix_name) |
| 528 else: |
| 529 c.Append('%(ctype)s temp;') |
| 530 assignment_target = '&temp' |
| 531 (c.Append('if (!%s)' % |
| 532 cpp_util.GetAsFundamentalValue( |
| 533 self._cpp_type_generator.GetReferencedProperty(prop), |
| 534 value_var, |
| 535 assignment_target)) |
| 536 .Append(' return %(failure_value)s;') |
| 537 ) |
| 538 if prop.type_ != prop.compiled_type: |
| 539 (c.Append('if (!%s)' % |
| 540 cpp_util.GenerateTypeToCompiledTypeConversion( |
| 541 self._cpp_type_generator.GetReferencedProperty(prop), |
| 542 'temp', |
| 543 '%s->%s' % (dst, prop.unix_name))) |
| 544 .Append(' return %(failure_value)s;') |
| 545 ) |
| 546 |
| 547 def _GenerateObjectOrObjectRefPopulate(self, c, prop): |
| 548 if prop.optional: |
| 549 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
| 550 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
| 551 .Append(' return %(failure_value)s;') |
| 552 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') |
| 553 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') |
| 554 .Append(' return %(failure_value)s;') |
| 555 .Append('%(dst)s->%(name)s = temp.Pass();') |
| 556 ) |
| 557 else: |
| 558 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
| 559 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
| 560 .Append(' return %(failure_value)s;') |
| 561 .Append( |
| 562 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') |
| 563 .Append(' return %(failure_value)s;') |
| 564 ) |
| 565 |
| 566 def _GenerateFunctionPopulate(self, c, prop): |
| 567 if prop.optional: |
| 568 c.Append('%(dst)s->%(name)s.reset(new base::DictionaryValue());') |
| 569 |
| 570 def _GenerateAnyPopulate(self, c, prop, value_var, dst): |
| 571 if prop.optional: |
| 572 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());') |
| 573 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') |
| 574 |
| 575 def _GenerateArrayOrArrayRefPopulate(self, c, prop, dst): |
| 576 # util_cc_helper deals with optional and required arrays |
| 577 (c.Append('const base::ListValue* list = NULL;') |
| 578 .Append('if (!%(value_var)s->GetAsList(&list))') |
| 579 .Append(' return %(failure_value)s;')) |
| 580 if prop.item_type.type_ == PropertyType.ENUM: |
| 581 self._GenerateListValueToEnumArrayConversion(c, prop) |
| 582 else: |
| 583 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( |
| 584 self._cpp_type_generator.GetReferencedProperty(prop), 'list', |
| 585 dst + '->' + prop.unix_name, prop.optional)) |
| 586 .Append(' return %(failure_value)s;') |
| 587 ) |
| 588 |
| 589 def _GenerateChoicePopulate(self, c, prop, value_var, dst, failure_value): |
| 590 type_var = '%(dst)s->%(name)s_type' |
| 591 c.Sblock('switch (%(value_var)s->GetType()) {') |
| 592 for choice in self._cpp_type_generator.ExpandParams([prop]): |
| 593 (c.Sblock('case %s: {' % cpp_util.GetValueType( |
| 594 self._cpp_type_generator.GetReferencedProperty(choice).type_)) |
| 595 .Concat(self._GeneratePopulatePropertyFromValue( |
| 596 choice, value_var, dst, failure_value, check_type=False)) |
| 597 .Append('%s = %s;' % |
| 598 (type_var, |
| 599 self._cpp_type_generator.GetEnumValue( |
| 600 prop, choice.type_.name))) |
| 601 .Append('break;') |
| 602 .Eblock('}') |
| 603 ) |
| 604 (c.Append('default:') |
| 605 .Append(' return %(failure_value)s;') |
| 606 ) |
| 607 c.Eblock('}') |
| 608 |
| 609 def _GenerateEnumPopulate(self, c, prop, value_var): |
| 610 c.Sblock('{') |
| 611 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp') |
| 612 c.Append('%(dst)s->%(name)s = enum_temp;') |
| 613 c.Eblock('}') |
| 614 |
| 615 def _GenerateBinaryPopulate(self, c, prop): |
| 616 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') |
| 617 .Append(' return %(failure_value)s;') |
| 618 .Append('const base::BinaryValue* binary_value =') |
| 619 .Append(' static_cast<const base::BinaryValue*>(%(value_var)s);') |
| 620 ) |
| 621 if prop.optional: |
| 622 (c.Append('%(dst)s->%(name)s.reset(') |
| 623 .Append(' new std::string(binary_value->GetBuffer(),') |
| 624 .Append(' binary_value->GetSize()));') |
| 625 ) |
| 626 else: |
| 627 (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),') |
| 628 .Append(' binary_value->GetSize());') |
| 629 ) |
| 630 |
601 def _GenerateListValueToEnumArrayConversion(self, c, prop): | 631 def _GenerateListValueToEnumArrayConversion(self, c, prop): |
602 """Appends code that converts a ListValue of string contstants to | 632 """Appends code that converts a ListValue of string contstants to |
603 an array of enums in dst. | 633 an array of enums in dst. |
604 Leaves dst, name, and failure_value unsubstituted. | 634 Leaves dst, name, and failure_value unsubstituted. |
605 | 635 |
606 c: the Code object that is being appended to. | 636 c: the Code object that is being appended to. |
607 prop: the property that the code is populating. | 637 prop: the property that the code is populating. |
608 """ | 638 """ |
609 accessor = '.' | 639 accessor = '.' |
610 if prop.optional: | 640 if prop.optional: |
611 c.Append('%(dst)s->%(name)s.reset(new std::vector<' + ( | 641 c.Append('%(dst)s->%(name)s.reset(new std::vector<' + ( |
612 self._cpp_type_generator.GetType(prop.item_type) + '>);')) | 642 self._cpp_type_generator.GetType(prop.item_type) + '>);')) |
613 accessor = '->' | 643 accessor = '->' |
614 c.Sblock('for (ListValue::const_iterator it = list->begin(); ' | 644 c.Sblock('for (ListValue::const_iterator it = list->begin(); ' |
615 'it != list->end(); ++it) {') | 645 'it != list->end(); ++it) {') |
616 self._GenerateStringToEnumConversion(c, prop.item_type, | 646 self._GenerateStringToEnumConversion( |
617 '(*it)', 'enum_temp') | 647 c, prop.item_type, '(*it)', 'enum_temp') |
618 c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);') | 648 c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);') |
619 c.Eblock('}') | 649 c.Eblock('}') |
620 | 650 |
621 def _GenerateStringToEnumConversion(self, c, prop, value_var, enum_temp): | 651 def _GenerateStringToEnumConversion(self, |
| 652 c, |
| 653 prop, |
| 654 value_var, |
| 655 enum_temp): |
622 """Appends code that converts a string to an enum. | 656 """Appends code that converts a string to an enum. |
623 Leaves failure_value unsubstituded. | 657 Leaves failure_value unsubstituted. |
624 | 658 |
625 c: the code that is appended to. | 659 c: the code that is appended to. |
626 prop: the property that the code is populating. | 660 prop: the property that the code is populating. |
627 value_var: the string value that is being converted. | 661 value_var: the string value that is being converted. |
628 enum_temp: the name used to store the temporary enum value. | 662 enum_temp: the name used to store the temporary enum value. |
629 """ | 663 """ |
630 (c.Append('%s %s;' % (self._cpp_type_generator.GetType(prop), enum_temp)) | 664 (c.Append('%s %s;' % (self._cpp_type_generator.GetCompiledType(prop), |
| 665 enum_temp)) |
631 .Append('std::string enum_as_string;') | 666 .Append('std::string enum_as_string;') |
632 .Append('if (!%s->GetAsString(&enum_as_string))' % value_var) | 667 .Append('if (!%s->GetAsString(&enum_as_string))' % value_var) |
633 .Append(' return %(failure_value)s;') | 668 .Append(' return %(failure_value)s;') |
634 ) | 669 ) |
635 for i, enum_value in enumerate(prop.enum_values): | 670 for i, enum_value in enumerate( |
| 671 self._cpp_type_generator.GetReferencedProperty(prop).enum_values): |
636 (c.Append( | 672 (c.Append( |
637 ('if' if i == 0 else 'else if') + | 673 ('if' if i == 0 else 'else if') + |
638 '(enum_as_string == "%s")' % enum_value) | 674 '(enum_as_string == "%s")' % enum_value) |
639 .Append(' ' + enum_temp + ' = %s;' % ( | 675 .Append(' ' + enum_temp + ' = %s;' % ( |
640 self._cpp_type_generator.GetEnumValue(prop, enum_value))) | 676 self._cpp_type_generator.GetEnumValue(prop, enum_value))) |
641 ) | 677 ) |
642 (c.Append('else') | 678 (c.Append('else') |
643 .Append(' return %(failure_value)s;') | 679 .Append(' return %(failure_value)s;') |
644 ) | 680 ) |
645 | 681 |
646 def _GeneratePropertyFunctions(self, param_namespace, params): | 682 def _GeneratePropertyFunctions(self, param_namespace, params): |
647 """Generate the functions for structures generated by a property such as | 683 """Generate the functions for structures generated by a property such as |
648 CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects. | 684 CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects. |
649 """ | 685 """ |
650 c = Code() | 686 c = Code() |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 .Append('return scoped_ptr<base::Value>();') | 726 .Append('return scoped_ptr<base::Value>();') |
691 .Eblock('}') | 727 .Eblock('}') |
692 .Append() | 728 .Append() |
693 .Substitute({ | 729 .Substitute({ |
694 'cpp_namespace': cpp_namespace, | 730 'cpp_namespace': cpp_namespace, |
695 'choice': cpp_util.Classname(prop.name) | 731 'choice': cpp_util.Classname(prop.name) |
696 }) | 732 }) |
697 ) | 733 ) |
698 return c | 734 return c |
699 | 735 |
| 736 def _GenerateCreateEnumTypeValue(self, cpp_namespace, prop): |
| 737 """Generates CreateEnumValue() that returns the base::StringValue |
| 738 representation of an enum type. |
| 739 """ |
| 740 c = Code() |
| 741 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(prop.name)) |
| 742 c.Sblock('scoped_ptr<base::Value> CreateEnumValue(%s %s) {' % ( |
| 743 classname, classname.lower())) |
| 744 c.Sblock('switch (%s) {' % classname.lower()) |
| 745 |
| 746 enum_prop = self._cpp_type_generator.GetReferencedProperty(prop) |
| 747 for enum_value in enum_prop.enum_values: |
| 748 c.Concat(self._GenerateReturnCase( |
| 749 '%s_%s' % (classname.upper(), enum_value.upper()), |
| 750 'scoped_ptr<base::Value>(base::Value::CreateStringValue("%s"))' % |
| 751 enum_value)) |
| 752 (c.Eblock('}') |
| 753 .Append('NOTREACHED();') |
| 754 .Append('return scoped_ptr<base::Value>();') |
| 755 .Eblock('}') |
| 756 ) |
| 757 return c |
| 758 |
| 759 # TODO(chebert): This is basically the same as GenerateCreateEnumTypeValue(). |
| 760 # The plan is to phase out the old-style enums, and make all enums into REF |
| 761 # types. |
700 def _GenerateCreateEnumValue(self, cpp_namespace, prop): | 762 def _GenerateCreateEnumValue(self, cpp_namespace, prop): |
701 """Generates CreateEnumValue() that returns the base::StringValue | 763 """Generates CreateEnumValue() that returns the base::StringValue |
702 representation of an enum. | 764 representation of an enum. |
703 """ | 765 """ |
704 c = Code() | 766 c = Code() |
705 c.Append('// static') | 767 c.Append('// static') |
706 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' | 768 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' |
707 '%(arg)s) {') | 769 '%(arg)s) {') |
708 c.Sblock('switch (%s) {' % prop.unix_name) | 770 c.Sblock('switch (%s) {' % prop.unix_name) |
709 if prop.optional: | 771 if prop.optional: |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
806 return c | 868 return c |
807 | 869 |
808 def _InitializePropertyToDefault(self, prop, dst): | 870 def _InitializePropertyToDefault(self, prop, dst): |
809 """Initialize a model.Property to its default value inside an object. | 871 """Initialize a model.Property to its default value inside an object. |
810 | 872 |
811 E.g for optional enum "state", generate dst->state = STATE_NONE; | 873 E.g for optional enum "state", generate dst->state = STATE_NONE; |
812 | 874 |
813 dst: Type* | 875 dst: Type* |
814 """ | 876 """ |
815 c = Code() | 877 c = Code() |
816 if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES): | 878 if (self._cpp_type_generator.IsEnumOrEnumRef(prop) or |
| 879 prop.type_ == PropertyType.CHOICES): |
817 if prop.optional: | 880 if prop.optional: |
818 prop_name = prop.unix_name | 881 prop_name = prop.unix_name |
819 if prop.type_ == PropertyType.CHOICES: | 882 if prop.type_ == PropertyType.CHOICES: |
820 prop_name = prop.unix_name + '_type' | 883 prop_name = prop.unix_name + '_type' |
821 c.Append('%s->%s = %s;' % ( | 884 c.Append('%s->%s = %s;' % ( |
822 dst, | 885 dst, |
823 prop_name, | 886 prop_name, |
824 self._cpp_type_generator.GetEnumNoneValue(prop))) | 887 self._cpp_type_generator.GetEnumNoneValue(prop))) |
825 return c | 888 return c |
826 | 889 |
827 def _IsObjectOrObjectRef(self, prop): | 890 def _IsObjectOrObjectRef(self, prop): |
828 """Determines if this property is an Object or is a ref to an Object. | 891 """Determines if this property is an Object or is a ref to an Object. |
829 """ | 892 """ |
830 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 893 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
831 PropertyType.OBJECT) | 894 PropertyType.OBJECT) |
832 | 895 |
833 def _IsArrayOrArrayRef(self, prop): | 896 def _IsArrayOrArrayRef(self, prop): |
834 """Determines if this property is an Array or is a ref to an Array. | 897 """Determines if this property is an Array or is a ref to an Array. |
835 """ | 898 """ |
836 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 899 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
837 PropertyType.ARRAY) | 900 PropertyType.ARRAY) |
838 | 901 |
839 def _IsFundamentalOrFundamentalRef(self, prop): | 902 def _IsFundamentalOrFundamentalRef(self, prop): |
840 """Determines if this property is a Fundamental type or is a ref to a | 903 """Determines if this property is a Fundamental type or is a ref to a |
841 Fundamental type. | 904 Fundamental type. |
842 """ | 905 """ |
843 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 906 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
844 is_fundamental) | 907 is_fundamental) |
OLD | NEW |