| 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 """ | 102 """ |
| 103 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) | 103 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) |
| 104 c = Code() | 104 c = Code() |
| 105 | 105 |
| 106 if type_.functions: | 106 if type_.functions: |
| 107 for function in type_.functions.values(): | 107 for function in type_.functions.values(): |
| 108 (c.Concat( | 108 (c.Concat( |
| 109 self._GenerateFunction( | 109 self._GenerateFunction( |
| 110 cpp_namespace + '::' + cpp_util.Classname(function.name), | 110 cpp_namespace + '::' + cpp_util.Classname(function.name), |
| 111 function)) | 111 function)) |
| 112 .Append() | 112 .Append()) |
| 113 ) | |
| 114 elif type_.type_ == PropertyType.OBJECT: | 113 elif type_.type_ == PropertyType.OBJECT: |
| 115 (c.Concat(self._GeneratePropertyFunctions( | 114 (c.Concat(self._GeneratePropertyFunctions( |
| 116 cpp_namespace, type_.properties.values())) | 115 cpp_namespace, type_.properties.values())) |
| 117 .Sblock('%(namespace)s::%(classname)s()') | 116 .Sblock('%(namespace)s::%(classname)s()') |
| 118 .Concat(self._GenerateInitializersAndBody(type_)) | 117 .Concat(self._GenerateInitializersAndBody(type_)) |
| 119 .Eblock('%(namespace)s::~%(classname)s() {}') | 118 .Eblock('%(namespace)s::~%(classname)s() {}') |
| 120 .Append() | 119 .Append()) |
| 121 ) | |
| 122 if type_.from_json: | 120 if type_.from_json: |
| 123 (c.Concat(self._GenerateTypePopulate(cpp_namespace, type_)) | 121 (c.Concat(self._GenerateTypePopulate(cpp_namespace, type_)) |
| 124 .Append() | 122 .Append()) |
| 125 ) | |
| 126 if type_.from_client: | 123 if type_.from_client: |
| 127 (c.Concat(self._GenerateTypeToValue(cpp_namespace, type_)) | 124 (c.Concat(self._GenerateTypeToValue(cpp_namespace, type_)) |
| 128 .Append() | 125 .Append()) |
| 129 ) | |
| 130 elif self._cpp_type_generator.IsEnumOrEnumRef(type_): | 126 elif self._cpp_type_generator.IsEnumOrEnumRef(type_): |
| 131 c.Concat(self._GenerateCreateEnumTypeValue(cpp_namespace, type_)) | 127 (c.Concat(self._GenerateCreateEnumTypeValue(cpp_namespace, type_)) |
| 132 c.Append() | 128 .Append() |
| 129 .Concat(self._GenerateEnumFromString(cpp_namespace, type_)) |
| 130 .Append() |
| 131 .Concat(self._GenerateEnumToString(cpp_namespace, type_)) |
| 132 .Append()) |
| 133 c.Substitute({'classname': classname, 'namespace': cpp_namespace}) | 133 c.Substitute({'classname': classname, 'namespace': cpp_namespace}) |
| 134 | 134 |
| 135 return c | 135 return c |
| 136 | 136 |
| 137 def _GenerateInitializersAndBody(self, type_): | 137 def _GenerateInitializersAndBody(self, type_): |
| 138 items = [] | 138 items = [] |
| 139 for prop in type_.properties.values(): | 139 for prop in type_.properties.values(): |
| 140 if prop.optional: | 140 if prop.optional: |
| 141 continue | 141 continue |
| 142 | 142 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 """Generate the code to populate a single property in a type. | 213 """Generate the code to populate a single property in a type. |
| 214 | 214 |
| 215 src: base::DictionaryValue* | 215 src: base::DictionaryValue* |
| 216 dst: Type* | 216 dst: Type* |
| 217 """ | 217 """ |
| 218 c = Code() | 218 c = Code() |
| 219 value_var = prop.unix_name + '_value' | 219 value_var = prop.unix_name + '_value' |
| 220 c.Append('const base::Value* %(value_var)s = NULL;') | 220 c.Append('const base::Value* %(value_var)s = NULL;') |
| 221 if prop.optional: | 221 if prop.optional: |
| 222 (c.Sblock( | 222 (c.Sblock( |
| 223 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {' | 223 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') |
| 224 ) | |
| 225 .Concat(self._GeneratePopulatePropertyFromValue( | 224 .Concat(self._GeneratePopulatePropertyFromValue( |
| 226 prop, value_var, dst, 'false')) | 225 prop, value_var, dst, 'false'))) |
| 227 .Eblock('}') | 226 if self._cpp_type_generator.IsEnumOrEnumRef(prop): |
| 228 ) | 227 (c.Append('} else {') |
| 228 .Append('%%(dst)s->%%(name)s = %s;' % |
| 229 self._cpp_type_generator.GetEnumNoneValue(prop))) |
| 230 c.Eblock('}') |
| 229 else: | 231 else: |
| 230 (c.Append( | 232 (c.Append( |
| 231 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))') | 233 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))') |
| 232 .Append(' return false;') | 234 .Append(' return false;') |
| 233 .Concat(self._GeneratePopulatePropertyFromValue( | 235 .Concat(self._GeneratePopulatePropertyFromValue( |
| 234 prop, value_var, dst, 'false')) | 236 prop, value_var, dst, 'false')) |
| 235 ) | 237 ) |
| 236 c.Append() | 238 c.Append() |
| 237 c.Substitute({'value_var': value_var, 'key': prop.name, 'src': src}) | 239 c.Substitute({ |
| 240 'value_var': value_var, |
| 241 'key': prop.name, |
| 242 'src': src, |
| 243 'dst': dst, |
| 244 'name': prop.unix_name |
| 245 }) |
| 238 return c | 246 return c |
| 239 | 247 |
| 240 def _GenerateTypeToValue(self, cpp_namespace, type_): | 248 def _GenerateTypeToValue(self, cpp_namespace, type_): |
| 241 """Generates a function that serializes the type into a | 249 """Generates a function that serializes the type into a |
| 242 |base::DictionaryValue|. | 250 |base::DictionaryValue|. |
| 243 | 251 |
| 244 E.g. for type "Foo" generates Foo::ToValue() | 252 E.g. for type "Foo" generates Foo::ToValue() |
| 245 """ | 253 """ |
| 246 c = Code() | 254 c = Code() |
| 247 (c.Sblock('scoped_ptr<base::DictionaryValue> %s::ToValue() const {' % | 255 (c.Sblock('scoped_ptr<base::DictionaryValue> %s::ToValue() const {' % |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) | 344 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) |
| 337 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 345 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
| 338 return '%s.DeepCopy()' % var | 346 return '%s.DeepCopy()' % var |
| 339 elif prop.type_ == PropertyType.FUNCTION: | 347 elif prop.type_ == PropertyType.FUNCTION: |
| 340 if prop.optional: | 348 if prop.optional: |
| 341 vardot = var + '->' | 349 vardot = var + '->' |
| 342 else: | 350 else: |
| 343 vardot = var + '.' | 351 vardot = var + '.' |
| 344 return '%sDeepCopy()' % vardot | 352 return '%sDeepCopy()' % vardot |
| 345 elif self._cpp_type_generator.IsEnumOrEnumRef(prop): | 353 elif self._cpp_type_generator.IsEnumOrEnumRef(prop): |
| 346 return 'CreateEnumValue(%s).release()' % var | 354 return 'base::Value::CreateStringValue(ToString(%s))' % var |
| 347 elif prop.type_ == PropertyType.BINARY: | 355 elif prop.type_ == PropertyType.BINARY: |
| 348 if prop.optional: | 356 if prop.optional: |
| 349 vardot = var + '->' | 357 vardot = var + '->' |
| 350 else: | 358 else: |
| 351 vardot = var + '.' | 359 vardot = var + '.' |
| 352 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' % | 360 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' % |
| 353 (vardot, vardot)) | 361 (vardot, vardot)) |
| 354 elif self._IsArrayOrArrayRef(prop): | 362 elif self._IsArrayOrArrayRef(prop): |
| 355 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( | 363 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( |
| 356 self._cpp_type_generator.GetReferencedProperty(prop), var, | 364 self._cpp_type_generator.GetReferencedProperty(prop), var, |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 c.Append('%(dst)s->%(name)s.reset(new std::vector<' + ( | 649 c.Append('%(dst)s->%(name)s.reset(new std::vector<' + ( |
| 642 self._cpp_type_generator.GetType(prop.item_type) + '>);')) | 650 self._cpp_type_generator.GetType(prop.item_type) + '>);')) |
| 643 accessor = '->' | 651 accessor = '->' |
| 644 c.Sblock('for (ListValue::const_iterator it = list->begin(); ' | 652 c.Sblock('for (ListValue::const_iterator it = list->begin(); ' |
| 645 'it != list->end(); ++it) {') | 653 'it != list->end(); ++it) {') |
| 646 self._GenerateStringToEnumConversion( | 654 self._GenerateStringToEnumConversion( |
| 647 c, prop.item_type, '(*it)', 'enum_temp') | 655 c, prop.item_type, '(*it)', 'enum_temp') |
| 648 c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);') | 656 c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);') |
| 649 c.Eblock('}') | 657 c.Eblock('}') |
| 650 | 658 |
| 651 def _GenerateStringToEnumConversion(self, | 659 def _GenerateStringToEnumConversion(self, c, prop, value_var, enum_temp): |
| 652 c, | |
| 653 prop, | |
| 654 value_var, | |
| 655 enum_temp): | |
| 656 """Appends code that converts a string to an enum. | 660 """Appends code that converts a string to an enum. |
| 657 Leaves failure_value unsubstituted. | 661 Leaves failure_value unsubstituted. |
| 658 | 662 |
| 659 c: the code that is appended to. | 663 c: the code that is appended to. |
| 660 prop: the property that the code is populating. | 664 prop: the property that the code is populating. |
| 661 value_var: the string value that is being converted. | 665 value_var: the string value that is being converted. |
| 662 enum_temp: the name used to store the temporary enum value. | 666 enum_temp: the name used to store the temporary enum value. |
| 663 """ | 667 """ |
| 664 (c.Append('%s %s;' % (self._cpp_type_generator.GetCompiledType(prop), | 668 (c.Append('std::string enum_as_string;') |
| 665 enum_temp)) | |
| 666 .Append('std::string enum_as_string;') | |
| 667 .Append('if (!%s->GetAsString(&enum_as_string))' % value_var) | 669 .Append('if (!%s->GetAsString(&enum_as_string))' % value_var) |
| 668 .Append(' return %(failure_value)s;') | 670 .Append(' return %(failure_value)s;') |
| 669 ) | 671 .Append('%(type)s %(enum)s = From%(type)sString(enum_as_string);' % { |
| 670 for i, enum_value in enumerate( | 672 'type': self._cpp_type_generator.GetCompiledType(prop), |
| 671 self._cpp_type_generator.GetReferencedProperty(prop).enum_values): | 673 'enum': enum_temp |
| 672 (c.Append( | 674 }) |
| 673 ('if' if i == 0 else 'else if') + | 675 .Append('if (%s == %s)' % |
| 674 '(enum_as_string == "%s")' % enum_value) | 676 (enum_temp, self._cpp_type_generator.GetEnumNoneValue(prop))) |
| 675 .Append(' ' + enum_temp + ' = %s;' % ( | 677 .Append(' return %(failure_value)s;')) |
| 676 self._cpp_type_generator.GetEnumValue(prop, enum_value))) | |
| 677 ) | |
| 678 (c.Append('else') | |
| 679 .Append(' return %(failure_value)s;') | |
| 680 ) | |
| 681 | 678 |
| 682 def _GeneratePropertyFunctions(self, param_namespace, params): | 679 def _GeneratePropertyFunctions(self, param_namespace, params): |
| 683 """Generate the functions for structures generated by a property such as | 680 """Generate the functions for structures generated by a property such as |
| 684 CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects. | 681 CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects. |
| 685 """ | 682 """ |
| 686 c = Code() | 683 c = Code() |
| 687 for param in params: | 684 for param in params: |
| 688 if param.type_ == PropertyType.OBJECT: | 685 if param.type_ == PropertyType.OBJECT: |
| 689 c.Concat(self._GenerateType( | 686 c.Concat(self._GenerateType( |
| 690 param_namespace + '::' + cpp_util.Classname(param.name), | 687 param_namespace + '::' + cpp_util.Classname(param.name), |
| 691 param)) | 688 param)) |
| 692 c.Append() | 689 c.Append() |
| 693 elif param.type_ == PropertyType.ARRAY: | 690 elif param.type_ == PropertyType.ARRAY: |
| 694 c.Concat(self._GeneratePropertyFunctions( | 691 c.Concat(self._GeneratePropertyFunctions( |
| 695 param_namespace, [param.item_type])) | 692 param_namespace, [param.item_type])) |
| 696 elif param.type_ == PropertyType.CHOICES: | 693 elif param.type_ == PropertyType.CHOICES: |
| 697 c.Concat(self._GeneratePropertyFunctions( | 694 c.Concat(self._GeneratePropertyFunctions( |
| 698 param_namespace, param.choices.values())) | 695 param_namespace, param.choices.values())) |
| 699 if param.from_client: | 696 if param.from_client: |
| 700 c.Concat(self._GenerateGetChoiceValue(param_namespace, param)) | 697 c.Concat(self._GenerateGetChoiceValue(param_namespace, param)) |
| 701 elif param.type_ == PropertyType.ENUM: | 698 elif param.type_ == PropertyType.ENUM: |
| 702 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) | 699 (c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) |
| 703 c.Append() | 700 .Append() |
| 701 .Concat(self._GenerateEnumFromString(param_namespace, |
| 702 param, |
| 703 use_namespace=True)) |
| 704 .Append() |
| 705 .Concat(self._GenerateEnumToString(param_namespace, |
| 706 param, |
| 707 use_namespace=True)) |
| 708 .Append()) |
| 704 return c | 709 return c |
| 705 | 710 |
| 706 def _GenerateGetChoiceValue(self, cpp_namespace, prop): | 711 def _GenerateGetChoiceValue(self, cpp_namespace, prop): |
| 707 """Generates Get<Type>ChoiceValue() that returns a scoped_ptr<base::Value> | 712 """Generates Get<Type>ChoiceValue() that returns a scoped_ptr<base::Value> |
| 708 representing the choice value. | 713 representing the choice value. |
| 709 """ | 714 """ |
| 710 c = Code() | 715 c = Code() |
| 711 (c.Sblock('scoped_ptr<base::Value> ' | 716 (c.Sblock('scoped_ptr<base::Value> ' |
| 712 '%(cpp_namespace)s::Get%(choice)sChoiceValue() const {') | 717 '%(cpp_namespace)s::Get%(choice)sChoiceValue() const {') |
| 713 .Sblock('switch (%s_type) {' % prop.unix_name) | 718 .Sblock('switch (%s_type) {' % prop.unix_name) |
| 714 ) | 719 .Concat(self._GenerateReturnCase( |
| 715 if prop.optional: | |
| 716 c.Concat(self._GenerateReturnCase( | |
| 717 self._cpp_type_generator.GetEnumNoneValue(prop), | 720 self._cpp_type_generator.GetEnumNoneValue(prop), |
| 718 'scoped_ptr<base::Value>()')) | 721 'scoped_ptr<base::Value>()'))) |
| 719 for choice in self._cpp_type_generator.ExpandParams([prop]): | 722 for choice in self._cpp_type_generator.ExpandParams([prop]): |
| 720 c.Concat(self._GenerateReturnCase( | 723 c.Concat(self._GenerateReturnCase( |
| 721 self._cpp_type_generator.GetEnumValue(prop, choice.type_.name), | 724 self._cpp_type_generator.GetEnumValue(prop, choice.type_.name), |
| 722 'make_scoped_ptr<base::Value>(%s)' % | 725 'make_scoped_ptr<base::Value>(%s)' % |
| 723 self._CreateValueFromProperty(choice, choice.unix_name))) | 726 self._CreateValueFromProperty(choice, choice.unix_name))) |
| 724 (c.Eblock('}') | 727 (c.Eblock('}') |
| 725 .Append('NOTREACHED();') | |
| 726 .Append('return scoped_ptr<base::Value>();') | 728 .Append('return scoped_ptr<base::Value>();') |
| 727 .Eblock('}') | 729 .Eblock('}') |
| 728 .Append() | 730 .Append() |
| 729 .Substitute({ | 731 .Substitute({ |
| 730 'cpp_namespace': cpp_namespace, | 732 'cpp_namespace': cpp_namespace, |
| 731 'choice': cpp_util.Classname(prop.name) | 733 'choice': cpp_util.Classname(prop.name) |
| 732 }) | 734 }) |
| 733 ) | 735 ) |
| 734 return c | 736 return c |
| 735 | 737 |
| 736 def _GenerateCreateEnumTypeValue(self, cpp_namespace, prop): | 738 def _GenerateCreateEnumTypeValue(self, cpp_namespace, prop): |
| 737 """Generates CreateEnumValue() that returns the base::StringValue | 739 """Generates CreateEnumValue() that returns the base::StringValue |
| 738 representation of an enum type. | 740 representation of an enum type. |
| 739 """ | 741 """ |
| 740 c = Code() | 742 c = Code() |
| 741 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(prop.name)) | 743 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(prop.name)) |
| 742 c.Sblock('scoped_ptr<base::Value> CreateEnumValue(%s %s) {' % ( | 744 (c.Sblock('scoped_ptr<base::Value> CreateEnumValue(%s %s) {' % |
| 743 classname, classname.lower())) | 745 (classname, classname.lower())) |
| 744 c.Sblock('switch (%s) {' % classname.lower()) | 746 .Append('std::string enum_temp = ToString(%s);' % classname.lower()) |
| 747 .Append('if (enum_temp.empty())') |
| 748 .Append(' return scoped_ptr<base::Value>();') |
| 749 .Append('return scoped_ptr<base::Value>(' |
| 750 'base::Value::CreateStringValue(enum_temp));') |
| 751 .Eblock('}')) |
| 752 return c |
| 745 | 753 |
| 754 def _GenerateEnumToString(self, cpp_namespace, prop, use_namespace=False): |
| 755 """Generates ToString() which gets the string representation of an enum. |
| 756 """ |
| 757 c = Code() |
| 758 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(prop.name)) |
| 759 if use_namespace: |
| 760 namespace = '%s::' % cpp_namespace |
| 761 else: |
| 762 namespace = '' |
| 763 |
| 764 (c.Append('// static') |
| 765 .Sblock('std::string %(namespace)sToString(%(class)s enum_param) {')) |
| 746 enum_prop = self._cpp_type_generator.GetReferencedProperty(prop) | 766 enum_prop = self._cpp_type_generator.GetReferencedProperty(prop) |
| 767 c.Sblock('switch (enum_param) {') |
| 747 for enum_value in enum_prop.enum_values: | 768 for enum_value in enum_prop.enum_values: |
| 748 c.Concat(self._GenerateReturnCase( | 769 c.Concat(self._GenerateReturnCase( |
| 749 '%s_%s' % (classname.upper(), enum_value.upper()), | 770 self._cpp_type_generator.GetEnumValue(prop, enum_value), |
| 750 'scoped_ptr<base::Value>(base::Value::CreateStringValue("%s"))' % | 771 '"%s"' % enum_value)) |
| 751 enum_value)) | 772 (c.Append('case %s:' % self._cpp_type_generator.GetEnumNoneValue(prop)) |
| 752 (c.Eblock('}') | 773 .Append(' return "";') |
| 753 .Append('NOTREACHED();') | |
| 754 .Append('return scoped_ptr<base::Value>();') | |
| 755 .Eblock('}') | 774 .Eblock('}') |
| 756 ) | 775 .Append('return "";') |
| 776 .Eblock('}') |
| 777 .Substitute({ |
| 778 'namespace': namespace, |
| 779 'class': classname |
| 780 })) |
| 781 return c |
| 782 |
| 783 def _GenerateEnumFromString(self, cpp_namespace, prop, use_namespace=False): |
| 784 """Generates FromClassNameString() which gets an enum from its string |
| 785 representation. |
| 786 """ |
| 787 c = Code() |
| 788 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(prop.name)) |
| 789 if use_namespace: |
| 790 namespace = '%s::' % cpp_namespace |
| 791 else: |
| 792 namespace = '' |
| 793 |
| 794 (c.Append('// static') |
| 795 .Sblock('%(namespace)s%(class)s' |
| 796 ' %(namespace)sFrom%(class)sString(' |
| 797 'const std::string& enum_string) {')) |
| 798 enum_prop = self._cpp_type_generator.GetReferencedProperty(prop) |
| 799 for i, enum_value in enumerate( |
| 800 self._cpp_type_generator.GetReferencedProperty(prop).enum_values): |
| 801 # This is broken up into all ifs with no else ifs because we get |
| 802 # "fatal error C1061: compiler limit : blocks nested too deeply" |
| 803 # on Windows. |
| 804 (c.Append('if (enum_string == "%s")' % enum_value) |
| 805 .Append(' return %s;' % |
| 806 self._cpp_type_generator.GetEnumValue(prop, enum_value))) |
| 807 (c.Append('return %s;' % |
| 808 self._cpp_type_generator.GetEnumNoneValue(prop)) |
| 809 .Eblock('}') |
| 810 .Substitute({ |
| 811 'namespace': namespace, |
| 812 'class': classname |
| 813 })) |
| 757 return c | 814 return c |
| 758 | 815 |
| 759 # TODO(chebert): This is basically the same as GenerateCreateEnumTypeValue(). | 816 # 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 | 817 # The plan is to phase out the old-style enums, and make all enums into REF |
| 761 # types. | 818 # types. |
| 762 def _GenerateCreateEnumValue(self, cpp_namespace, prop): | 819 def _GenerateCreateEnumValue(self, cpp_namespace, prop): |
| 763 """Generates CreateEnumValue() that returns the base::StringValue | 820 """Generates CreateEnumValue() that returns the base::StringValue |
| 764 representation of an enum. | 821 representation of an enum. |
| 765 """ | 822 """ |
| 766 c = Code() | 823 c = Code() |
| 767 c.Append('// static') | 824 (c.Append('// static') |
| 768 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' | 825 .Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' |
| 769 '%(arg)s) {') | 826 '%(arg)s) {') |
| 770 c.Sblock('switch (%s) {' % prop.unix_name) | 827 .Append('std::string enum_temp = ToString(%s);' % prop.unix_name) |
| 771 if prop.optional: | 828 .Append('if (enum_temp.empty())') |
| 772 c.Concat(self._GenerateReturnCase( | 829 .Append(' return scoped_ptr<base::Value>();') |
| 773 self._cpp_type_generator.GetEnumNoneValue(prop), | 830 .Append('return scoped_ptr<base::Value>(' |
| 774 'scoped_ptr<base::Value>()')) | 831 'base::Value::CreateStringValue(enum_temp));') |
| 775 for enum_value in prop.enum_values: | |
| 776 c.Concat(self._GenerateReturnCase( | |
| 777 self._cpp_type_generator.GetEnumValue(prop, enum_value), | |
| 778 'scoped_ptr<base::Value>(base::Value::CreateStringValue("%s"))' % | |
| 779 enum_value)) | |
| 780 (c.Eblock('}') | |
| 781 .Append('NOTREACHED();') | |
| 782 .Append('return scoped_ptr<base::Value>();') | |
| 783 .Eblock('}') | 832 .Eblock('}') |
| 784 .Substitute({ | 833 .Substitute({ |
| 785 'cpp_namespace': cpp_namespace, | 834 'cpp_namespace': cpp_namespace, |
| 786 'arg': cpp_util.GetParameterDeclaration( | 835 'arg': cpp_util.GetParameterDeclaration( |
| 787 prop, self._cpp_type_generator.GetType(prop)) | 836 prop, self._cpp_type_generator.GetType(prop)) |
| 788 }) | 837 })) |
| 789 ) | |
| 790 return c | 838 return c |
| 791 | 839 |
| 792 def _GenerateReturnCase(self, case_value, return_value): | 840 def _GenerateReturnCase(self, case_value, return_value): |
| 793 """Generates a single return case for a switch block. | 841 """Generates a single return case for a switch block. |
| 794 """ | 842 """ |
| 795 c = Code() | 843 c = Code() |
| 796 (c.Append('case %s:' % case_value) | 844 (c.Append('case %s:' % case_value) |
| 797 .Append(' return %s;' % return_value) | 845 .Append(' return %s;' % return_value) |
| 798 ) | 846 ) |
| 799 return c | 847 return c |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 898 """ | 946 """ |
| 899 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 947 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
| 900 PropertyType.ARRAY) | 948 PropertyType.ARRAY) |
| 901 | 949 |
| 902 def _IsFundamentalOrFundamentalRef(self, prop): | 950 def _IsFundamentalOrFundamentalRef(self, prop): |
| 903 """Determines if this property is a Fundamental type or is a ref to a | 951 """Determines if this property is a Fundamental type or is a ref to a |
| 904 Fundamental type. | 952 Fundamental type. |
| 905 """ | 953 """ |
| 906 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 954 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
| 907 is_fundamental) | 955 is_fundamental) |
| OLD | NEW |