Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: tools/json_schema_compiler/cc_generator.py

Issue 820673004: json_schema_compiler: Use std::vector<char> for binary values. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@simplify_json_schema
Patch Set: Fix merge error. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 25 matching lines...) Expand all
36 self._namespace.environment.namespace_pattern, 36 self._namespace.environment.namespace_pattern,
37 self._namespace.unix_name) 37 self._namespace.unix_name)
38 38
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/stl_util.h"')
46 .Append('#include "base/strings/string_number_conversions.h"') 47 .Append('#include "base/strings/string_number_conversions.h"')
47 .Append('#include "base/strings/utf_string_conversions.h"') 48 .Append('#include "base/strings/utf_string_conversions.h"')
48 .Append('#include "%s/%s.h"' % 49 .Append('#include "%s/%s.h"' %
49 (self._namespace.source_file_dir, self._namespace.short_filename)) 50 (self._namespace.source_file_dir, self._namespace.short_filename))
50 .Append('#include <set>') 51 .Append('#include <set>')
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))
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 elif prop.optional: 154 elif prop.optional:
154 continue 155 continue
155 elif t.property_type == PropertyType.INTEGER: 156 elif t.property_type == PropertyType.INTEGER:
156 items.append('%s(0)' % prop.unix_name) 157 items.append('%s(0)' % prop.unix_name)
157 elif t.property_type == PropertyType.DOUBLE: 158 elif t.property_type == PropertyType.DOUBLE:
158 items.append('%s(0.0)' % prop.unix_name) 159 items.append('%s(0.0)' % prop.unix_name)
159 elif t.property_type == PropertyType.BOOLEAN: 160 elif t.property_type == PropertyType.BOOLEAN:
160 items.append('%s(false)' % prop.unix_name) 161 items.append('%s(false)' % prop.unix_name)
161 elif (t.property_type == PropertyType.ANY or 162 elif (t.property_type == PropertyType.ANY or
162 t.property_type == PropertyType.ARRAY or 163 t.property_type == PropertyType.ARRAY or
163 t.property_type == PropertyType.BINARY or # mapped to std::string 164 t.property_type == PropertyType.BINARY or
164 t.property_type == PropertyType.CHOICES or 165 t.property_type == PropertyType.CHOICES or
165 t.property_type == PropertyType.OBJECT or 166 t.property_type == PropertyType.OBJECT or
166 t.property_type == PropertyType.FUNCTION or 167 t.property_type == PropertyType.FUNCTION or
167 t.property_type == PropertyType.REF or 168 t.property_type == PropertyType.REF or
168 t.property_type == PropertyType.STRING): 169 t.property_type == PropertyType.STRING):
169 # TODO(miket): It would be nice to initialize CHOICES, but we 170 # TODO(miket): It would be nice to initialize CHOICES, but we
170 # don't presently have the semantics to indicate which one of a set 171 # don't presently have the semantics to indicate which one of a set
171 # should be the default. 172 # should be the default.
172 continue 173 continue
173 else: 174 else:
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 vardot = '(%s).' % var 535 vardot = '(%s).' % var
535 return '%sDeepCopy()' % vardot 536 return '%sDeepCopy()' % vardot
536 elif underlying_type.property_type == PropertyType.ENUM: 537 elif underlying_type.property_type == PropertyType.ENUM:
537 maybe_namespace = '' 538 maybe_namespace = ''
538 if type_.property_type == PropertyType.REF: 539 if type_.property_type == PropertyType.REF:
539 maybe_namespace = '%s::' % underlying_type.namespace.unix_name 540 maybe_namespace = '%s::' % underlying_type.namespace.unix_name
540 return 'new base::StringValue(%sToString(%s))' % (maybe_namespace, var) 541 return 'new base::StringValue(%sToString(%s))' % (maybe_namespace, var)
541 elif underlying_type.property_type == PropertyType.BINARY: 542 elif underlying_type.property_type == PropertyType.BINARY:
542 if is_ptr: 543 if is_ptr:
543 vardot = var + '->' 544 vardot = var + '->'
545 ref = var + '.get()'
544 else: 546 else:
545 vardot = var + '.' 547 vardot = var + '.'
546 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' % 548 ref = '&' + var
547 (vardot, vardot)) 549 return ('base::BinaryValue::CreateWithCopiedBuffer(vector_as_array(%s),'
550 ' %ssize())' % (ref, vardot))
548 elif underlying_type.property_type == PropertyType.ARRAY: 551 elif underlying_type.property_type == PropertyType.ARRAY:
549 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( 552 return '%s.release()' % self._util_cc_helper.CreateValueFromArray(
550 var, 553 var,
551 is_ptr) 554 is_ptr)
552 elif underlying_type.property_type.is_fundamental: 555 elif underlying_type.property_type.is_fundamental:
553 if is_ptr: 556 if is_ptr:
554 var = '*%s' % var 557 var = '*%s' % var
555 if underlying_type.property_type == PropertyType.STRING: 558 if underlying_type.property_type == PropertyType.STRING:
556 return 'new base::StringValue(%s)' % var 559 return 'new base::StringValue(%s)' % var
557 else: 560 else:
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) 803 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True)))
801 ) 804 )
802 if not self._generate_error_messages: 805 if not self._generate_error_messages:
803 c.Append('return %(failure_value)s;') 806 c.Append('return %(failure_value)s;')
804 (c.Eblock('}') 807 (c.Eblock('}')
805 .Sblock('else {') 808 .Sblock('else {')
806 .Append(' binary_value =') 809 .Append(' binary_value =')
807 .Append(' static_cast<const base::BinaryValue*>(%(src_var)s);') 810 .Append(' static_cast<const base::BinaryValue*>(%(src_var)s);')
808 ) 811 )
809 if is_ptr: 812 if is_ptr:
810 (c.Append('%(dst_var)s.reset(') 813 (c.Append('%(dst_var)s.reset(new std::vector<char>(')
811 .Append(' new std::string(binary_value->GetBuffer(),') 814 .Append(' binary_value->GetBuffer(),')
812 .Append(' binary_value->GetSize()));') 815 .Append(' binary_value->GetBuffer() + binary_value->GetSize()));')
813 ) 816 )
814 else: 817 else:
815 (c.Append('%(dst_var)s.assign(binary_value->GetBuffer(),') 818 (c.Append('%(dst_var)s.assign(')
816 .Append(' binary_value->GetSize());') 819 .Append(' binary_value->GetBuffer(),')
820 .Append(' binary_value->GetBuffer() + binary_value->GetSize());')
817 ) 821 )
818 c.Eblock('}') 822 c.Eblock('}')
819 else: 823 else:
820 raise NotImplementedError(type_) 824 raise NotImplementedError(type_)
821 if c.IsEmpty(): 825 if c.IsEmpty():
822 return c 826 return c
823 return Code().Sblock('{').Concat(c.Substitute({ 827 return Code().Sblock('{').Concat(c.Substitute({
824 'cpp_type': self._type_helper.GetCppType(type_), 828 'cpp_type': self._type_helper.GetCppType(type_),
825 'src_var': src_var, 829 'src_var': src_var,
826 'dst_var': dst_var, 830 'dst_var': dst_var,
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
1048 if self._generate_error_messages: 1052 if self._generate_error_messages:
1049 params = list(params) + ['base::string16* error'] 1053 params = list(params) + ['base::string16* error']
1050 return ', '.join(str(p) for p in params) 1054 return ', '.join(str(p) for p in params)
1051 1055
1052 def _GenerateArgs(self, args): 1056 def _GenerateArgs(self, args):
1053 """Builds the argument list for a function, given an array of arguments. 1057 """Builds the argument list for a function, given an array of arguments.
1054 """ 1058 """
1055 if self._generate_error_messages: 1059 if self._generate_error_messages:
1056 args = list(args) + ['error'] 1060 args = list(args) + ['error']
1057 return ', '.join(str(a) for a in args) 1061 return ', '.join(str(a) for a in args)
OLDNEW
« no previous file with comments | « extensions/browser/api/vpn_provider/vpn_service.cc ('k') | tools/json_schema_compiler/cpp_type_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698