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 28 matching lines...) Expand all Loading... |
39 c = Code() | 39 c = Code() |
40 (c.Append(cpp_util.CHROMIUM_LICENSE) | 40 (c.Append(cpp_util.CHROMIUM_LICENSE) |
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/memory/ptr_util.h"') | 46 .Append('#include "base/memory/ptr_util.h"') |
47 .Append('#include "base/strings/string_number_conversions.h"') | 47 .Append('#include "base/strings/string_number_conversions.h"') |
48 .Append('#include "base/strings/utf_string_conversions.h"') | 48 .Append('#include "base/strings/utf_string_conversions.h"') |
| 49 .Append('#include "base/values.h"') |
49 .Append('#include "%s/%s.h"' % | 50 .Append('#include "%s/%s.h"' % |
50 (self._namespace.source_file_dir, self._namespace.short_filename)) | 51 (self._namespace.source_file_dir, self._namespace.short_filename)) |
51 .Append('#include <set>') | 52 .Append('#include <set>') |
52 .Append('#include <utility>') | 53 .Append('#include <utility>') |
53 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) | 54 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) |
54 .Append() | 55 .Append() |
55 .Append('using base::UTF8ToUTF16;') | 56 .Append('using base::UTF8ToUTF16;') |
56 .Append() | 57 .Append() |
57 .Concat(cpp_util.OpenNamespace(cpp_namespace)) | 58 .Concat(cpp_util.OpenNamespace(cpp_namespace)) |
58 ) | 59 ) |
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 maybe_namespace = '' | 632 maybe_namespace = '' |
632 if type_.property_type == PropertyType.REF: | 633 if type_.property_type == PropertyType.REF: |
633 maybe_namespace = '%s::' % underlying_type.namespace.unix_name | 634 maybe_namespace = '%s::' % underlying_type.namespace.unix_name |
634 return 'base::MakeUnique<base::Value>(%sToString(%s))' % ( | 635 return 'base::MakeUnique<base::Value>(%sToString(%s))' % ( |
635 maybe_namespace, var) | 636 maybe_namespace, var) |
636 elif underlying_type.property_type == PropertyType.BINARY: | 637 elif underlying_type.property_type == PropertyType.BINARY: |
637 if is_ptr: | 638 if is_ptr: |
638 vardot = var + '->' | 639 vardot = var + '->' |
639 else: | 640 else: |
640 vardot = var + '.' | 641 vardot = var + '.' |
641 return ('base::BinaryValue::CreateWithCopiedBuffer(' | 642 return ('base::Value::CreateWithCopiedBuffer(' |
642 '%sdata(), %ssize())' % (vardot, vardot)) | 643 '%sdata(), %ssize())' % (vardot, vardot)) |
643 elif underlying_type.property_type == PropertyType.ARRAY: | 644 elif underlying_type.property_type == PropertyType.ARRAY: |
644 return '%s' % self._util_cc_helper.CreateValueFromArray( | 645 return '%s' % self._util_cc_helper.CreateValueFromArray( |
645 var, | 646 var, |
646 is_ptr) | 647 is_ptr) |
647 elif underlying_type.property_type.is_fundamental: | 648 elif underlying_type.property_type.is_fundamental: |
648 if is_ptr: | 649 if is_ptr: |
649 var = '*%s' % var | 650 var = '*%s' % var |
650 if underlying_type.property_type == PropertyType.STRING: | 651 if underlying_type.property_type == PropertyType.STRING: |
651 return 'base::MakeUnique<base::Value>(%s)' % var | 652 return 'base::MakeUnique<base::Value>(%s)' % var |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
880 else: | 881 else: |
881 (c.Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( | 882 (c.Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( |
882 ('*%(src_var)s', '&%(dst_var)s'))) | 883 ('*%(src_var)s', '&%(dst_var)s'))) |
883 .Append(' return %(failure_value)s;')) | 884 .Append(' return %(failure_value)s;')) |
884 elif underlying_type.property_type == PropertyType.ENUM: | 885 elif underlying_type.property_type == PropertyType.ENUM: |
885 c.Concat(self._GenerateStringToEnumConversion(underlying_type, | 886 c.Concat(self._GenerateStringToEnumConversion(underlying_type, |
886 src_var, | 887 src_var, |
887 dst_var, | 888 dst_var, |
888 failure_value)) | 889 failure_value)) |
889 elif underlying_type.property_type == PropertyType.BINARY: | 890 elif underlying_type.property_type == PropertyType.BINARY: |
890 (c.Append('const base::BinaryValue* binary_value = NULL;') | 891 (c.Append('const base::Value* binary_value = NULL;') |
891 .Sblock('if (!%(src_var)s->IsType(base::Value::Type::BINARY)) {') | 892 .Sblock('if (!%(src_var)s->IsType(base::Value::Type::BINARY)) {') |
892 .Concat(self._GenerateError( | 893 .Concat(self._GenerateError( |
893 '"\'%%(key)s\': expected binary, got " + ' + | 894 '"\'%%(key)s\': expected binary, got " + ' + |
894 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) | 895 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
895 ) | 896 ) |
896 if not self._generate_error_messages: | 897 if not self._generate_error_messages: |
897 c.Append('return %(failure_value)s;') | 898 c.Append('return %(failure_value)s;') |
898 (c.Eblock('}') | 899 (c.Eblock('}') |
899 .Sblock('else {') | 900 .Sblock('else {') |
900 .Append(' binary_value =') | 901 .Append(' binary_value =') |
901 .Append(' static_cast<const base::BinaryValue*>(%(src_var)s);') | 902 .Append(' static_cast<const base::Value*>(%(src_var)s);') |
902 ) | 903 ) |
903 if is_ptr: | 904 if is_ptr: |
904 (c.Append('%(dst_var)s.reset(new std::vector<char>(') | 905 (c.Append('%(dst_var)s.reset(new std::vector<char>(') |
905 .Append(' binary_value->GetBuffer(),') | 906 .Append(' binary_value->GetBuffer(),') |
906 .Append(' binary_value->GetBuffer() + binary_value->GetSize()));') | 907 .Append(' binary_value->GetBuffer() + binary_value->GetSize()));') |
907 ) | 908 ) |
908 else: | 909 else: |
909 (c.Append('%(dst_var)s.assign(') | 910 (c.Append('%(dst_var)s.assign(') |
910 .Append(' binary_value->GetBuffer(),') | 911 .Append(' binary_value->GetBuffer(),') |
911 .Append(' binary_value->GetBuffer() + binary_value->GetSize());') | 912 .Append(' binary_value->GetBuffer() + binary_value->GetSize());') |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1150 if self._generate_error_messages: | 1151 if self._generate_error_messages: |
1151 params = list(params) + ['base::string16* error'] | 1152 params = list(params) + ['base::string16* error'] |
1152 return ', '.join(str(p) for p in params) | 1153 return ', '.join(str(p) for p in params) |
1153 | 1154 |
1154 def _GenerateArgs(self, args): | 1155 def _GenerateArgs(self, args): |
1155 """Builds the argument list for a function, given an array of arguments. | 1156 """Builds the argument list for a function, given an array of arguments. |
1156 """ | 1157 """ |
1157 if self._generate_error_messages: | 1158 if self._generate_error_messages: |
1158 args = list(args) + ['error'] | 1159 args = list(args) + ['error'] |
1159 return ', '.join(str(a) for a in args) | 1160 return ', '.join(str(a) for a in args) |
OLD | NEW |