| 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 30 matching lines...) Expand all Loading... |
| 41 .Append() | 41 .Append() |
| 42 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 42 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
| 43 .Append() | 43 .Append() |
| 44 .Append(self._util_cc_helper.GetIncludePath()) | 44 .Append(self._util_cc_helper.GetIncludePath()) |
| 45 .Append('#include "base/logging.h"') | 45 .Append('#include "base/logging.h"') |
| 46 .Append('#include "base/strings/string_number_conversions.h"') | 46 .Append('#include "base/strings/string_number_conversions.h"') |
| 47 .Append('#include "base/strings/utf_string_conversions.h"') | 47 .Append('#include "base/strings/utf_string_conversions.h"') |
| 48 .Append('#include "%s/%s.h"' % | 48 .Append('#include "%s/%s.h"' % |
| 49 (self._namespace.source_file_dir, self._namespace.short_filename)) | 49 (self._namespace.source_file_dir, self._namespace.short_filename)) |
| 50 .Append('#include <set>') | 50 .Append('#include <set>') |
| 51 .Append('#include <utility>') |
| 51 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) | 52 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) |
| 52 .Append() | 53 .Append() |
| 53 .Append('using base::UTF8ToUTF16;') | 54 .Append('using base::UTF8ToUTF16;') |
| 54 .Append() | 55 .Append() |
| 55 .Concat(cpp_util.OpenNamespace(cpp_namespace)) | 56 .Concat(cpp_util.OpenNamespace(cpp_namespace)) |
| 56 ) | 57 ) |
| 57 if self._namespace.properties: | 58 if self._namespace.properties: |
| 58 (c.Append('//') | 59 (c.Append('//') |
| 59 .Append('// Properties') | 60 .Append('// Properties') |
| 60 .Append('//') | 61 .Append('//') |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 (c.Append('// static') | 325 (c.Append('// static') |
| 325 .Append('scoped_ptr<%s> %s::FromValue(%s) {' % (classname, | 326 .Append('scoped_ptr<%s> %s::FromValue(%s) {' % (classname, |
| 326 cpp_namespace, self._GenerateParams(('const base::Value& value',)))) | 327 cpp_namespace, self._GenerateParams(('const base::Value& value',)))) |
| 327 ) | 328 ) |
| 328 if self._generate_error_messages: | 329 if self._generate_error_messages: |
| 329 c.Append('DCHECK(error);') | 330 c.Append('DCHECK(error);') |
| 330 (c.Append(' scoped_ptr<%s> out(new %s());' % (classname, classname)) | 331 (c.Append(' scoped_ptr<%s> out(new %s());' % (classname, classname)) |
| 331 .Append(' if (!Populate(%s))' % self._GenerateArgs( | 332 .Append(' if (!Populate(%s))' % self._GenerateArgs( |
| 332 ('value', 'out.get()'))) | 333 ('value', 'out.get()'))) |
| 333 .Append(' return scoped_ptr<%s>();' % classname) | 334 .Append(' return scoped_ptr<%s>();' % classname) |
| 334 .Append(' return out.Pass();') | 335 .Append(' return out;') |
| 335 .Append('}') | 336 .Append('}') |
| 336 ) | 337 ) |
| 337 return c | 338 return c |
| 338 | 339 |
| 339 def _GenerateTypeToValue(self, cpp_namespace, type_): | 340 def _GenerateTypeToValue(self, cpp_namespace, type_): |
| 340 """Generates a function that serializes the type into a base::Value. | 341 """Generates a function that serializes the type into a base::Value. |
| 341 E.g. for type "Foo" generates Foo::ToValue() | 342 E.g. for type "Foo" generates Foo::ToValue() |
| 342 """ | 343 """ |
| 343 if type_.property_type == PropertyType.OBJECT: | 344 if type_.property_type == PropertyType.OBJECT: |
| 344 return self._GenerateObjectTypeToValue(cpp_namespace, type_) | 345 return self._GenerateObjectTypeToValue(cpp_namespace, type_) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 (c.Sblock('for (const auto& it : additional_properties) {') | 401 (c.Sblock('for (const auto& it : additional_properties) {') |
| 401 .Cblock(self._CreateValueFromType( | 402 .Cblock(self._CreateValueFromType( |
| 402 'value->SetWithoutPathExpansion(it.first, %s);', | 403 'value->SetWithoutPathExpansion(it.first, %s);', |
| 403 type_.additional_properties.name, | 404 type_.additional_properties.name, |
| 404 type_.additional_properties, | 405 type_.additional_properties, |
| 405 '%sit.second' % ('*' if needs_unwrap else ''))) | 406 '%sit.second' % ('*' if needs_unwrap else ''))) |
| 406 .Eblock('}') | 407 .Eblock('}') |
| 407 ) | 408 ) |
| 408 | 409 |
| 409 return (c.Append() | 410 return (c.Append() |
| 410 .Append('return value.Pass();') | 411 .Append('return value;') |
| 411 .Eblock('}')) | 412 .Eblock('}')) |
| 412 | 413 |
| 413 def _GenerateChoiceTypeToValue(self, cpp_namespace, type_): | 414 def _GenerateChoiceTypeToValue(self, cpp_namespace, type_): |
| 414 """Generates a function that serializes a choice-representing type | 415 """Generates a function that serializes a choice-representing type |
| 415 into a base::Value. | 416 into a base::Value. |
| 416 """ | 417 """ |
| 417 c = Code() | 418 c = Code() |
| 418 c.Sblock('scoped_ptr<base::Value> %s::ToValue() const {' % cpp_namespace) | 419 c.Sblock('scoped_ptr<base::Value> %s::ToValue() const {' % cpp_namespace) |
| 419 c.Append('scoped_ptr<base::Value> result;') | 420 c.Append('scoped_ptr<base::Value> result;') |
| 420 for choice in type_.choices: | 421 for choice in type_.choices: |
| 421 choice_var = 'as_%s' % choice.unix_name | 422 choice_var = 'as_%s' % choice.unix_name |
| 422 # Enums cannot be wrapped with scoped_ptr, but the XXX_NONE enum value | 423 # Enums cannot be wrapped with scoped_ptr, but the XXX_NONE enum value |
| 423 # is equal to 0. | 424 # is equal to 0. |
| 424 (c.Sblock('if (%s) {' % choice_var) | 425 (c.Sblock('if (%s) {' % choice_var) |
| 425 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % | 426 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % |
| 426 type_.unix_name) | 427 type_.unix_name) |
| 427 .Cblock(self._CreateValueFromType('result.reset(%s);', | 428 .Cblock(self._CreateValueFromType('result.reset(%s);', |
| 428 choice.name, | 429 choice.name, |
| 429 choice, | 430 choice, |
| 430 choice_var, | 431 choice_var, |
| 431 True)) | 432 True)) |
| 432 .Eblock('}') | 433 .Eblock('}') |
| 433 ) | 434 ) |
| 434 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % | 435 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % |
| 435 type_.unix_name) | 436 type_.unix_name) |
| 436 .Append('return result.Pass();') | 437 .Append('return result;') |
| 437 .Eblock('}') | 438 .Eblock('}') |
| 438 ) | 439 ) |
| 439 return c | 440 return c |
| 440 | 441 |
| 441 def _GenerateFunction(self, function): | 442 def _GenerateFunction(self, function): |
| 442 """Generates the definitions for function structs. | 443 """Generates the definitions for function structs. |
| 443 """ | 444 """ |
| 444 c = Code() | 445 c = Code() |
| 445 | 446 |
| 446 # TODO(kalman): use function.unix_name not Classname. | 447 # TODO(kalman): use function.unix_name not Classname. |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 param, value_var, 'params', failure_value)) | 639 param, value_var, 'params', failure_value)) |
| 639 .Eblock('}') | 640 .Eblock('}') |
| 640 ) | 641 ) |
| 641 if not param.optional: | 642 if not param.optional: |
| 642 (c.Sblock('else {') | 643 (c.Sblock('else {') |
| 643 .Concat(self._GenerateError('"\'%%(key)s\' is required"')) | 644 .Concat(self._GenerateError('"\'%%(key)s\' is required"')) |
| 644 .Append('return %s;' % failure_value) | 645 .Append('return %s;' % failure_value) |
| 645 .Eblock('}')) | 646 .Eblock('}')) |
| 646 c.Substitute({'value_var': value_var, 'i': i, 'key': param.name}) | 647 c.Substitute({'value_var': value_var, 'i': i, 'key': param.name}) |
| 647 (c.Append() | 648 (c.Append() |
| 648 .Append('return params.Pass();') | 649 .Append('return params;') |
| 649 .Eblock('}') | 650 .Eblock('}') |
| 650 .Append() | 651 .Append() |
| 651 ) | 652 ) |
| 652 | 653 |
| 653 return c | 654 return c |
| 654 | 655 |
| 655 def _GeneratePopulatePropertyFromValue(self, | 656 def _GeneratePopulatePropertyFromValue(self, |
| 656 prop, | 657 prop, |
| 657 src_var, | 658 src_var, |
| 658 dst_class_var, | 659 dst_class_var, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 c.Append('return %(failure_value)s;') | 728 c.Append('return %(failure_value)s;') |
| 728 (c.Eblock('}') | 729 (c.Eblock('}') |
| 729 .Sblock('else {') | 730 .Sblock('else {') |
| 730 .Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') | 731 .Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') |
| 731 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( | 732 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( |
| 732 ('*dictionary', 'temp.get()'))) | 733 ('*dictionary', 'temp.get()'))) |
| 733 .Append(' return %(failure_value)s;') | 734 .Append(' return %(failure_value)s;') |
| 734 ) | 735 ) |
| 735 (c.Append('}') | 736 (c.Append('}') |
| 736 .Append('else') | 737 .Append('else') |
| 737 .Append(' %(dst_var)s = temp.Pass();') | 738 .Append(' %(dst_var)s = std::move(temp);') |
| 738 .Eblock('}') | 739 .Eblock('}') |
| 739 ) | 740 ) |
| 740 else: | 741 else: |
| 741 (c.Append('const base::DictionaryValue* dictionary = NULL;') | 742 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
| 742 .Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {') | 743 .Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {') |
| 743 .Concat(self._GenerateError( | 744 .Concat(self._GenerateError( |
| 744 '"\'%%(key)s\': expected dictionary, got " + ' + | 745 '"\'%%(key)s\': expected dictionary, got " + ' + |
| 745 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) | 746 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 746 .Append('return %(failure_value)s;') | 747 .Append('return %(failure_value)s;') |
| 747 .Eblock('}') | 748 .Eblock('}') |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 788 else: | 789 else: |
| 789 c.Append('return %(failure_value)s;') | 790 c.Append('return %(failure_value)s;') |
| 790 c.Eblock('}') | 791 c.Eblock('}') |
| 791 c.Eblock('}') | 792 c.Eblock('}') |
| 792 elif underlying_type.property_type == PropertyType.CHOICES: | 793 elif underlying_type.property_type == PropertyType.CHOICES: |
| 793 if is_ptr: | 794 if is_ptr: |
| 794 (c.Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') | 795 (c.Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') |
| 795 .Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( | 796 .Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( |
| 796 ('*%(src_var)s', 'temp.get()'))) | 797 ('*%(src_var)s', 'temp.get()'))) |
| 797 .Append(' return %(failure_value)s;') | 798 .Append(' return %(failure_value)s;') |
| 798 .Append('%(dst_var)s = temp.Pass();') | 799 .Append('%(dst_var)s = std::move(temp);') |
| 799 ) | 800 ) |
| 800 else: | 801 else: |
| 801 (c.Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( | 802 (c.Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( |
| 802 ('*%(src_var)s', '&%(dst_var)s'))) | 803 ('*%(src_var)s', '&%(dst_var)s'))) |
| 803 .Append(' return %(failure_value)s;')) | 804 .Append(' return %(failure_value)s;')) |
| 804 elif underlying_type.property_type == PropertyType.ENUM: | 805 elif underlying_type.property_type == PropertyType.ENUM: |
| 805 c.Concat(self._GenerateStringToEnumConversion(underlying_type, | 806 c.Concat(self._GenerateStringToEnumConversion(underlying_type, |
| 806 src_var, | 807 src_var, |
| 807 dst_var, | 808 dst_var, |
| 808 failure_value)) | 809 failure_value)) |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1006 'new base::ListValue());') | 1007 'new base::ListValue());') |
| 1007 ) | 1008 ) |
| 1008 declaration_list = [] | 1009 declaration_list = [] |
| 1009 for param in params: | 1010 for param in params: |
| 1010 declaration_list.append(cpp_util.GetParameterDeclaration( | 1011 declaration_list.append(cpp_util.GetParameterDeclaration( |
| 1011 param, self._type_helper.GetCppType(param.type_))) | 1012 param, self._type_helper.GetCppType(param.type_))) |
| 1012 c.Cblock(self._CreateValueFromType('create_results->Append(%s);', | 1013 c.Cblock(self._CreateValueFromType('create_results->Append(%s);', |
| 1013 param.name, | 1014 param.name, |
| 1014 param.type_, | 1015 param.type_, |
| 1015 param.unix_name)) | 1016 param.unix_name)) |
| 1016 c.Append('return create_results.Pass();') | 1017 c.Append('return create_results;') |
| 1017 c.Eblock('}') | 1018 c.Eblock('}') |
| 1018 c.Substitute({ | 1019 c.Substitute({ |
| 1019 'function_scope': ('%s::' % function_scope) if function_scope else '', | 1020 'function_scope': ('%s::' % function_scope) if function_scope else '', |
| 1020 'declaration_list': ', '.join(declaration_list), | 1021 'declaration_list': ', '.join(declaration_list), |
| 1021 'param_names': ', '.join(param.unix_name for param in params) | 1022 'param_names': ', '.join(param.unix_name for param in params) |
| 1022 }) | 1023 }) |
| 1023 return c | 1024 return c |
| 1024 | 1025 |
| 1025 def _GenerateEventNameConstant(self, event): | 1026 def _GenerateEventNameConstant(self, event): |
| 1026 """Generates a constant string array for the event name. | 1027 """Generates a constant string array for the event name. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1070 if self._generate_error_messages: | 1071 if self._generate_error_messages: |
| 1071 params = list(params) + ['base::string16* error'] | 1072 params = list(params) + ['base::string16* error'] |
| 1072 return ', '.join(str(p) for p in params) | 1073 return ', '.join(str(p) for p in params) |
| 1073 | 1074 |
| 1074 def _GenerateArgs(self, args): | 1075 def _GenerateArgs(self, args): |
| 1075 """Builds the argument list for a function, given an array of arguments. | 1076 """Builds the argument list for a function, given an array of arguments. |
| 1076 """ | 1077 """ |
| 1077 if self._generate_error_messages: | 1078 if self._generate_error_messages: |
| 1078 args = list(args) + ['error'] | 1079 args = list(args) + ['error'] |
| 1079 return ', '.join(str(a) for a in args) | 1080 return ', '.join(str(a) for a in args) |
| OLD | NEW |