| 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 from cpp_namespace_environment import CppNamespaceEnvironment | 10 from cpp_namespace_environment import CppNamespaceEnvironment |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 .Append('using base::UTF8ToUTF16;') | 53 .Append('using base::UTF8ToUTF16;') |
| 54 .Append() | 54 .Append() |
| 55 .Concat(cpp_util.OpenNamespace(cpp_namespace)) | 55 .Concat(cpp_util.OpenNamespace(cpp_namespace)) |
| 56 ) | 56 ) |
| 57 if self._namespace.properties: | 57 if self._namespace.properties: |
| 58 (c.Append('//') | 58 (c.Append('//') |
| 59 .Append('// Properties') | 59 .Append('// Properties') |
| 60 .Append('//') | 60 .Append('//') |
| 61 .Append() | 61 .Append() |
| 62 ) | 62 ) |
| 63 for property in self._namespace.properties.values(): | 63 for prop in self._namespace.properties.values(): |
| 64 property_code = self._type_helper.GeneratePropertyValues( | 64 property_code = self._type_helper.GeneratePropertyValues( |
| 65 property, | 65 prop, |
| 66 'const %(type)s %(name)s = %(value)s;', | 66 'const %(type)s %(name)s = %(value)s;', |
| 67 nodoc=True) | 67 nodoc=True) |
| 68 if property_code: | 68 if property_code: |
| 69 c.Cblock(property_code) | 69 c.Cblock(property_code) |
| 70 if self._namespace.types: | 70 if self._namespace.types: |
| 71 (c.Append('//') | 71 (c.Append('//') |
| 72 .Append('// Types') | 72 .Append('// Types') |
| 73 .Append('//') | 73 .Append('//') |
| 74 .Append() | 74 .Append() |
| 75 .Cblock(self._GenerateTypes(None, self._namespace.types.values())) | 75 .Cblock(self._GenerateTypes(None, self._namespace.types.values())) |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 if function.params: | 447 if function.params: |
| 448 c.Concat(self._GeneratePropertyFunctions('Params', function.params)) | 448 c.Concat(self._GeneratePropertyFunctions('Params', function.params)) |
| 449 (c.Append('Params::Params() {}') | 449 (c.Append('Params::Params() {}') |
| 450 .Append('Params::~Params() {}') | 450 .Append('Params::~Params() {}') |
| 451 .Append() | 451 .Append() |
| 452 .Cblock(self._GenerateFunctionParamsCreate(function)) | 452 .Cblock(self._GenerateFunctionParamsCreate(function)) |
| 453 ) | 453 ) |
| 454 | 454 |
| 455 # Results::Create function | 455 # Results::Create function |
| 456 if function.callback: | 456 if function.callback: |
| 457 c.Concat(self._GenerateCreateCallbackArguments(function_namespace, | 457 c.Concat(self._GenerateCreateCallbackArguments('Results', |
| 458 'Results', | |
| 459 function.callback)) | 458 function.callback)) |
| 460 | 459 |
| 461 c.Append('} // namespace %s' % function_namespace) | 460 c.Append('} // namespace %s' % function_namespace) |
| 462 return c | 461 return c |
| 463 | 462 |
| 464 def _GenerateEvent(self, event): | 463 def _GenerateEvent(self, event): |
| 465 # TODO(kalman): use event.unix_name not Classname. | 464 # TODO(kalman): use event.unix_name not Classname. |
| 466 c = Code() | 465 c = Code() |
| 467 event_namespace = cpp_util.Classname(event.name) | 466 event_namespace = cpp_util.Classname(event.name) |
| 468 (c.Append('namespace %s {' % event_namespace) | 467 (c.Append('namespace %s {' % event_namespace) |
| 469 .Append() | 468 .Append() |
| 470 .Cblock(self._GenerateEventNameConstant(None, event)) | 469 .Cblock(self._GenerateEventNameConstant(event)) |
| 471 .Cblock(self._GenerateCreateCallbackArguments(event_namespace, | 470 .Cblock(self._GenerateCreateCallbackArguments(None, event)) |
| 472 None, | |
| 473 event)) | |
| 474 .Append('} // namespace %s' % event_namespace) | 471 .Append('} // namespace %s' % event_namespace) |
| 475 ) | 472 ) |
| 476 return c | 473 return c |
| 477 | 474 |
| 478 def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False): | 475 def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False): |
| 479 """Creates a base::Value given a type. Generated code passes ownership | 476 """Creates a base::Value given a type. Generated code passes ownership |
| 480 to caller. | 477 to caller. |
| 481 | 478 |
| 482 var: variable or variable* | 479 var: variable or variable* |
| 483 | 480 |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 877 type |type_| in |dst_var|. In the generated code, if |src_var| is not | 874 type |type_| in |dst_var|. In the generated code, if |src_var| is not |
| 878 a valid enum name then the function will return |failure_value|. | 875 a valid enum name then the function will return |failure_value|. |
| 879 """ | 876 """ |
| 880 if type_.property_type != PropertyType.ENUM: | 877 if type_.property_type != PropertyType.ENUM: |
| 881 raise TypeError(type_) | 878 raise TypeError(type_) |
| 882 c = Code() | 879 c = Code() |
| 883 enum_as_string = '%s_as_string' % type_.unix_name | 880 enum_as_string = '%s_as_string' % type_.unix_name |
| 884 cpp_type_namespace = '' | 881 cpp_type_namespace = '' |
| 885 if type_.namespace != self._namespace: | 882 if type_.namespace != self._namespace: |
| 886 cpp_type_namespace = '%s::' % type_.namespace.unix_name | 883 cpp_type_namespace = '%s::' % type_.namespace.unix_name |
| 887 cpp_type_name = self._type_helper.GetCppType(type_) | |
| 888 (c.Append('std::string %s;' % enum_as_string) | 884 (c.Append('std::string %s;' % enum_as_string) |
| 889 .Sblock('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string)) | 885 .Sblock('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string)) |
| 890 .Concat(self._GenerateError( | 886 .Concat(self._GenerateError( |
| 891 '"\'%%(key)s\': expected string, got " + ' + | 887 '"\'%%(key)s\': expected string, got " + ' + |
| 892 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) | 888 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 893 .Append('return %s;' % failure_value) | 889 .Append('return %s;' % failure_value) |
| 894 .Eblock('}') | 890 .Eblock('}') |
| 895 .Append('%s = %sParse%s(%s);' % (dst_var, | 891 .Append('%s = %sParse%s(%s);' % (dst_var, |
| 896 cpp_type_namespace, | 892 cpp_type_namespace, |
| 897 cpp_util.Classname(type_.name), | 893 cpp_util.Classname(type_.name), |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 958 """ | 954 """ |
| 959 c = Code() | 955 c = Code() |
| 960 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) | 956 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
| 961 | 957 |
| 962 if cpp_namespace is not None: | 958 if cpp_namespace is not None: |
| 963 c.Append('// static') | 959 c.Append('// static') |
| 964 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace | 960 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace |
| 965 | 961 |
| 966 c.Sblock('%s%s %sParse%s(const std::string& enum_string) {' % | 962 c.Sblock('%s%s %sParse%s(const std::string& enum_string) {' % |
| 967 (maybe_namespace, classname, maybe_namespace, classname)) | 963 (maybe_namespace, classname, maybe_namespace, classname)) |
| 968 for i, enum_value in enumerate( | 964 for _, enum_value in enumerate( |
| 969 self._type_helper.FollowRef(type_).enum_values): | 965 self._type_helper.FollowRef(type_).enum_values): |
| 970 # This is broken up into all ifs with no else ifs because we get | 966 # This is broken up into all ifs with no else ifs because we get |
| 971 # "fatal error C1061: compiler limit : blocks nested too deeply" | 967 # "fatal error C1061: compiler limit : blocks nested too deeply" |
| 972 # on Windows. | 968 # on Windows. |
| 973 (c.Append('if (enum_string == "%s")' % enum_value.name) | 969 (c.Append('if (enum_string == "%s")' % enum_value.name) |
| 974 .Append(' return %s;' % | 970 .Append(' return %s;' % |
| 975 self._type_helper.GetEnumValue(type_, enum_value))) | 971 self._type_helper.GetEnumValue(type_, enum_value))) |
| 976 (c.Append('return %s;' % self._type_helper.GetEnumNoneValue(type_)) | 972 (c.Append('return %s;' % self._type_helper.GetEnumNoneValue(type_)) |
| 977 .Eblock('}') | 973 .Eblock('}') |
| 978 ) | 974 ) |
| 979 return c | 975 return c |
| 980 | 976 |
| 981 def _GenerateCreateCallbackArguments(self, | 977 def _GenerateCreateCallbackArguments(self, |
| 982 cpp_namespace, | |
| 983 function_scope, | 978 function_scope, |
| 984 callback): | 979 callback): |
| 985 """Generate all functions to create Value parameters for a callback. | 980 """Generate all functions to create Value parameters for a callback. |
| 986 | 981 |
| 987 E.g for function "Bar", generate Bar::Results::Create | 982 E.g for function "Bar", generate Bar::Results::Create |
| 988 E.g for event "Baz", generate Baz::Create | 983 E.g for event "Baz", generate Baz::Create |
| 989 | 984 |
| 990 function_scope: the function scope path, e.g. Foo::Bar for the function | 985 function_scope: the function scope path, e.g. Foo::Bar for the function |
| 991 Foo::Bar::Baz(). May be None if there is no function scope. | 986 Foo::Bar::Baz(). May be None if there is no function scope. |
| 992 callback: the Function object we are creating callback arguments for. | 987 callback: the Function object we are creating callback arguments for. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1010 param.unix_name)) | 1005 param.unix_name)) |
| 1011 c.Append('return create_results.Pass();') | 1006 c.Append('return create_results.Pass();') |
| 1012 c.Eblock('}') | 1007 c.Eblock('}') |
| 1013 c.Substitute({ | 1008 c.Substitute({ |
| 1014 'function_scope': ('%s::' % function_scope) if function_scope else '', | 1009 'function_scope': ('%s::' % function_scope) if function_scope else '', |
| 1015 'declaration_list': ', '.join(declaration_list), | 1010 'declaration_list': ', '.join(declaration_list), |
| 1016 'param_names': ', '.join(param.unix_name for param in params) | 1011 'param_names': ', '.join(param.unix_name for param in params) |
| 1017 }) | 1012 }) |
| 1018 return c | 1013 return c |
| 1019 | 1014 |
| 1020 def _GenerateEventNameConstant(self, function_scope, event): | 1015 def _GenerateEventNameConstant(self, event): |
| 1021 """Generates a constant string array for the event name. | 1016 """Generates a constant string array for the event name. |
| 1022 """ | 1017 """ |
| 1023 c = Code() | 1018 c = Code() |
| 1024 c.Append('const char kEventName[] = "%s.%s";' % ( | 1019 c.Append('const char kEventName[] = "%s.%s";' % ( |
| 1025 self._namespace.name, event.name)) | 1020 self._namespace.name, event.name)) |
| 1026 return c | 1021 return c |
| 1027 | 1022 |
| 1028 def _InitializePropertyToDefault(self, prop, dst): | 1023 def _InitializePropertyToDefault(self, prop, dst): |
| 1029 """Initialize a model.Property to its default value inside an object. | 1024 """Initialize a model.Property to its default value inside an object. |
| 1030 | 1025 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1061 if self._generate_error_messages: | 1056 if self._generate_error_messages: |
| 1062 params = list(params) + ['base::string16* error'] | 1057 params = list(params) + ['base::string16* error'] |
| 1063 return ', '.join(str(p) for p in params) | 1058 return ', '.join(str(p) for p in params) |
| 1064 | 1059 |
| 1065 def _GenerateArgs(self, args): | 1060 def _GenerateArgs(self, args): |
| 1066 """Builds the argument list for a function, given an array of arguments. | 1061 """Builds the argument list for a function, given an array of arguments. |
| 1067 """ | 1062 """ |
| 1068 if self._generate_error_messages: | 1063 if self._generate_error_messages: |
| 1069 args = list(args) + ['error'] | 1064 args = list(args) + ['error'] |
| 1070 return ', '.join(str(a) for a in args) | 1065 return ', '.join(str(a) for a in args) |
| OLD | NEW |