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 cpp_util | 7 import cpp_util |
8 import schema_util | 8 import schema_util |
9 import util_cc_helper | 9 import util_cc_helper |
10 | 10 |
(...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) | 777 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
778 .Append('return %s;' % failure_value) | 778 .Append('return %s;' % failure_value) |
779 .Eblock('}') | 779 .Eblock('}') |
780 .Append('%s = Parse%s(%s);' % (dst_var, | 780 .Append('%s = Parse%s(%s);' % (dst_var, |
781 self._type_helper.GetCppType(type_), | 781 self._type_helper.GetCppType(type_), |
782 enum_as_string)) | 782 enum_as_string)) |
783 .Sblock('if (%s == %s) {' % (dst_var, | 783 .Sblock('if (%s == %s) {' % (dst_var, |
784 self._type_helper.GetEnumNoneValue(type_))) | 784 self._type_helper.GetEnumNoneValue(type_))) |
785 .Concat(self._GenerateError( | 785 .Concat(self._GenerateError( |
786 '\"\'%%(key)s\': expected \\"' + | 786 '\"\'%%(key)s\': expected \\"' + |
787 '\\" or \\"'.join( | 787 '\\" or \\"'.join(self._type_helper.FollowRef(type_).enum_values) + |
788 enum_value.name | |
789 for enum_value in self._type_helper.FollowRef(type_).enum_values) + | |
790 '\\", got \\"" + %s + "\\""' % enum_as_string)) | 788 '\\", got \\"" + %s + "\\""' % enum_as_string)) |
791 .Append('return %s;' % failure_value) | 789 .Append('return %s;' % failure_value) |
792 .Eblock('}') | 790 .Eblock('}') |
793 .Substitute({'src_var': src_var, 'key': type_.name}) | 791 .Substitute({'src_var': src_var, 'key': type_.name}) |
794 ) | 792 ) |
795 return c | 793 return c |
796 | 794 |
797 def _GeneratePropertyFunctions(self, namespace, params): | 795 def _GeneratePropertyFunctions(self, namespace, params): |
798 """Generates the member functions for a list of parameters. | 796 """Generates the member functions for a list of parameters. |
799 """ | 797 """ |
(...skipping 15 matching lines...) Expand all Loading... |
815 | 813 |
816 if cpp_namespace is not None: | 814 if cpp_namespace is not None: |
817 c.Append('// static') | 815 c.Append('// static') |
818 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace | 816 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace |
819 | 817 |
820 c.Sblock('std::string %sToString(%s enum_param) {' % | 818 c.Sblock('std::string %sToString(%s enum_param) {' % |
821 (maybe_namespace, classname)) | 819 (maybe_namespace, classname)) |
822 c.Sblock('switch (enum_param) {') | 820 c.Sblock('switch (enum_param) {') |
823 for enum_value in self._type_helper.FollowRef(type_).enum_values: | 821 for enum_value in self._type_helper.FollowRef(type_).enum_values: |
824 (c.Append('case %s: ' % self._type_helper.GetEnumValue(type_, enum_value)) | 822 (c.Append('case %s: ' % self._type_helper.GetEnumValue(type_, enum_value)) |
825 .Append(' return "%s";' % enum_value.name)) | 823 .Append(' return "%s";' % enum_value)) |
826 (c.Append('case %s:' % self._type_helper.GetEnumNoneValue(type_)) | 824 (c.Append('case %s:' % self._type_helper.GetEnumNoneValue(type_)) |
827 .Append(' return "";') | 825 .Append(' return "";') |
828 .Eblock('}') | 826 .Eblock('}') |
829 .Append('NOTREACHED();') | 827 .Append('NOTREACHED();') |
830 .Append('return "";') | 828 .Append('return "";') |
831 .Eblock('}') | 829 .Eblock('}') |
832 ) | 830 ) |
833 return c | 831 return c |
834 | 832 |
835 def _GenerateEnumFromString(self, cpp_namespace, type_): | 833 def _GenerateEnumFromString(self, cpp_namespace, type_): |
836 """Generates FromClassNameString() which gets an enum from its string | 834 """Generates FromClassNameString() which gets an enum from its string |
837 representation. | 835 representation. |
838 """ | 836 """ |
839 c = Code() | 837 c = Code() |
840 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) | 838 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
841 | 839 |
842 if cpp_namespace is not None: | 840 if cpp_namespace is not None: |
843 c.Append('// static') | 841 c.Append('// static') |
844 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace | 842 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace |
845 | 843 |
846 c.Sblock('%s%s %sParse%s(const std::string& enum_string) {' % | 844 c.Sblock('%s%s %sParse%s(const std::string& enum_string) {' % |
847 (maybe_namespace, classname, maybe_namespace, classname)) | 845 (maybe_namespace, classname, maybe_namespace, classname)) |
848 for i, enum_value in enumerate( | 846 for i, enum_value in enumerate( |
849 self._type_helper.FollowRef(type_).enum_values): | 847 self._type_helper.FollowRef(type_).enum_values): |
850 # This is broken up into all ifs with no else ifs because we get | 848 # This is broken up into all ifs with no else ifs because we get |
851 # "fatal error C1061: compiler limit : blocks nested too deeply" | 849 # "fatal error C1061: compiler limit : blocks nested too deeply" |
852 # on Windows. | 850 # on Windows. |
853 (c.Append('if (enum_string == "%s")' % enum_value.name) | 851 (c.Append('if (enum_string == "%s")' % enum_value) |
854 .Append(' return %s;' % | 852 .Append(' return %s;' % |
855 self._type_helper.GetEnumValue(type_, enum_value))) | 853 self._type_helper.GetEnumValue(type_, enum_value))) |
856 (c.Append('return %s;' % self._type_helper.GetEnumNoneValue(type_)) | 854 (c.Append('return %s;' % self._type_helper.GetEnumNoneValue(type_)) |
857 .Eblock('}') | 855 .Eblock('}') |
858 ) | 856 ) |
859 return c | 857 return c |
860 | 858 |
861 def _GenerateCreateCallbackArguments(self, function_scope, callback): | 859 def _GenerateCreateCallbackArguments(self, function_scope, callback): |
862 """Generate all functions to create Value parameters for a callback. | 860 """Generate all functions to create Value parameters for a callback. |
863 | 861 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
935 if self._generate_error_messages: | 933 if self._generate_error_messages: |
936 params = list(params) + ['base::string16* error'] | 934 params = list(params) + ['base::string16* error'] |
937 return ', '.join(str(p) for p in params) | 935 return ', '.join(str(p) for p in params) |
938 | 936 |
939 def _GenerateArgs(self, args): | 937 def _GenerateArgs(self, args): |
940 """Builds the argument list for a function, given an array of arguments. | 938 """Builds the argument list for a function, given an array of arguments. |
941 """ | 939 """ |
942 if self._generate_error_messages: | 940 if self._generate_error_messages: |
943 args = list(args) + ['error'] | 941 args = list(args) + ['error'] |
944 return ', '.join(str(a) for a in args) | 942 return ', '.join(str(a) for a in args) |
OLD | NEW |