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 25 matching lines...) Expand all Loading... | |
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/memory/ptr_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 .Append('#include <utility>') | 52 .Append('#include <utility>') |
52 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) | 53 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) |
53 .Append() | 54 .Append() |
54 .Append('using base::UTF8ToUTF16;') | 55 .Append('using base::UTF8ToUTF16;') |
55 .Append() | 56 .Append() |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
310 .Concat(self._GenerateError( | 311 .Concat(self._GenerateError( |
311 '"expected dictionary, got " + ' + | 312 '"expected dictionary, got " + ' + |
312 self._util_cc_helper.GetValueTypeString('value'))) | 313 self._util_cc_helper.GetValueTypeString('value'))) |
313 .Append('return false;') | 314 .Append('return false;') |
314 .Eblock('}')) | 315 .Eblock('}')) |
315 | 316 |
316 if type_.properties or type_.additional_properties is not None: | 317 if type_.properties or type_.additional_properties is not None: |
317 c.Append('const base::DictionaryValue* dict = ' | 318 c.Append('const base::DictionaryValue* dict = ' |
318 'static_cast<const base::DictionaryValue*>(&value);') | 319 'static_cast<const base::DictionaryValue*>(&value);') |
319 if self._generate_error_messages: | 320 if self._generate_error_messages: |
320 c.Append('std::set<std::string> keys;') | 321 c.Append('std::set<std::string> keys;') |
321 for prop in type_.properties.itervalues(): | 322 for prop in type_.properties.itervalues(): |
322 c.Concat(self._InitializePropertyToDefault(prop, 'out')) | 323 c.Concat(self._InitializePropertyToDefault(prop, 'out')) |
323 for prop in type_.properties.itervalues(): | 324 for prop in type_.properties.itervalues(): |
324 if self._generate_error_messages: | 325 if self._generate_error_messages: |
325 c.Append('keys.insert("%s");' % (prop.name)) | 326 c.Append('keys.insert("%s");' % (prop.name)) |
326 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) | 327 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) |
327 # Check for extra values. | 328 # Check for extra values. |
328 if self._generate_error_messages: | 329 if self._generate_error_messages: |
329 (c.Sblock('for (base::DictionaryValue::Iterator it(*dict); ' | 330 (c.Sblock('for (base::DictionaryValue::Iterator it(*dict); ' |
330 '!it.IsAtEnd(); it.Advance()) {') | 331 '!it.IsAtEnd(); it.Advance()) {') |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
503 c.Sblock('std::unique_ptr<base::Value> %s::ToValue() const {' % | 504 c.Sblock('std::unique_ptr<base::Value> %s::ToValue() const {' % |
504 cpp_namespace) | 505 cpp_namespace) |
505 c.Append('std::unique_ptr<base::Value> result;') | 506 c.Append('std::unique_ptr<base::Value> result;') |
506 for choice in type_.choices: | 507 for choice in type_.choices: |
507 choice_var = 'as_%s' % choice.unix_name | 508 choice_var = 'as_%s' % choice.unix_name |
508 # Enums cannot be wrapped with scoped_ptr, but the XXX_NONE enum value | 509 # Enums cannot be wrapped with scoped_ptr, but the XXX_NONE enum value |
509 # is equal to 0. | 510 # is equal to 0. |
510 (c.Sblock('if (%s) {' % choice_var) | 511 (c.Sblock('if (%s) {' % choice_var) |
511 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % | 512 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % |
512 type_.unix_name) | 513 type_.unix_name) |
513 .Cblock(self._CreateValueFromType('result.reset(%s);', | 514 .Cblock(self._CreateValueFromType('result = %s;', |
Devlin
2016/04/27 22:33:21
.Append indentation is off now?
dcheng
2016/04/27 22:43:34
Ran it through the google3 formatter. I hope the r
| |
514 choice.name, | 515 choice.name, |
515 choice, | 516 choice, |
516 choice_var, | 517 choice_var, |
517 True)) | 518 True)) |
518 .Eblock('}') | 519 .Eblock('}') |
519 ) | 520 ) |
520 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % | 521 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % |
521 type_.unix_name) | 522 type_.unix_name) |
522 .Append('return result;') | 523 .Append('return result;') |
523 .Eblock('}') | 524 .Eblock('}') |
524 ) | 525 ) |
525 return c | 526 return c |
526 | 527 |
527 def _GenerateFunction(self, function): | 528 def _GenerateFunction(self, function): |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
563 (c.Append('namespace %s {' % event_namespace) | 564 (c.Append('namespace %s {' % event_namespace) |
564 .Append() | 565 .Append() |
565 .Cblock(self._GenerateEventNameConstant(event)) | 566 .Cblock(self._GenerateEventNameConstant(event)) |
566 .Cblock(self._GenerateCreateCallbackArguments(None, event)) | 567 .Cblock(self._GenerateCreateCallbackArguments(None, event)) |
567 .Append('} // namespace %s' % event_namespace) | 568 .Append('} // namespace %s' % event_namespace) |
568 ) | 569 ) |
569 return c | 570 return c |
570 | 571 |
571 def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False): | 572 def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False): |
572 """Creates a base::Value given a type. Generated code passes ownership | 573 """Creates a base::Value given a type. Generated code passes ownership |
573 to caller. | 574 to caller via std::unique_ptr. |
574 | 575 |
575 var: variable or variable* | 576 var: variable or variable* |
576 | 577 |
577 E.g for std::string, generate new base::StringValue(var) | 578 E.g for std::string, generate new base::StringValue(var) |
578 """ | 579 """ |
579 c = Code() | 580 c = Code() |
580 underlying_type = self._type_helper.FollowRef(type_) | 581 underlying_type = self._type_helper.FollowRef(type_) |
581 if underlying_type.property_type == PropertyType.ARRAY: | 582 if underlying_type.property_type == PropertyType.ARRAY: |
582 # Enums are treated specially because C++ templating thinks that they're | 583 # Enums are treated specially because C++ templating thinks that they're |
583 # ints, but really they're strings. So we create a vector of strings and | 584 # ints, but really they're strings. So we create a vector of strings and |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
617 """Generates the statement to create a base::Value given a type. | 618 """Generates the statement to create a base::Value given a type. |
618 | 619 |
619 type_: The type of the values being converted. | 620 type_: The type of the values being converted. |
620 var: The name of the variable. | 621 var: The name of the variable. |
621 is_ptr: Whether |type_| is optional. | 622 is_ptr: Whether |type_| is optional. |
622 """ | 623 """ |
623 underlying_type = self._type_helper.FollowRef(type_) | 624 underlying_type = self._type_helper.FollowRef(type_) |
624 if (underlying_type.property_type == PropertyType.CHOICES or | 625 if (underlying_type.property_type == PropertyType.CHOICES or |
625 underlying_type.property_type == PropertyType.OBJECT): | 626 underlying_type.property_type == PropertyType.OBJECT): |
626 if is_ptr: | 627 if is_ptr: |
627 return '(%s)->ToValue().release()' % var | 628 return '(%s)->ToValue()' % var |
628 else: | 629 else: |
629 return '(%s).ToValue().release()' % var | 630 return '(%s).ToValue()' % var |
630 elif (underlying_type.property_type == PropertyType.ANY or | 631 elif (underlying_type.property_type == PropertyType.ANY or |
631 underlying_type.property_type == PropertyType.FUNCTION): | 632 underlying_type.property_type == PropertyType.FUNCTION): |
632 if is_ptr: | 633 if is_ptr: |
633 vardot = '(%s)->' % var | 634 vardot = '(%s)->' % var |
634 else: | 635 else: |
635 vardot = '(%s).' % var | 636 vardot = '(%s).' % var |
636 return '%sDeepCopy()' % vardot | 637 return '%sCreateDeepCopy()' % vardot |
637 elif underlying_type.property_type == PropertyType.ENUM: | 638 elif underlying_type.property_type == PropertyType.ENUM: |
638 maybe_namespace = '' | 639 maybe_namespace = '' |
639 if type_.property_type == PropertyType.REF: | 640 if type_.property_type == PropertyType.REF: |
640 maybe_namespace = '%s::' % underlying_type.namespace.unix_name | 641 maybe_namespace = '%s::' % underlying_type.namespace.unix_name |
641 return 'new base::StringValue(%sToString(%s))' % (maybe_namespace, var) | 642 return 'base::WrapUnique(new base::StringValue(%sToString(%s)))' % ( |
643 maybe_namespace, var) | |
642 elif underlying_type.property_type == PropertyType.BINARY: | 644 elif underlying_type.property_type == PropertyType.BINARY: |
643 if is_ptr: | 645 if is_ptr: |
644 vardot = var + '->' | 646 vardot = var + '->' |
645 else: | 647 else: |
646 vardot = var + '.' | 648 vardot = var + '.' |
647 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(),' | 649 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(),' |
648 ' %ssize())' % (vardot, vardot)) | 650 ' %ssize())' % (vardot, vardot)) |
649 elif underlying_type.property_type == PropertyType.ARRAY: | 651 elif underlying_type.property_type == PropertyType.ARRAY: |
650 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( | 652 return '%s' % self._util_cc_helper.CreateValueFromArray( |
651 var, | 653 var, |
652 is_ptr) | 654 is_ptr) |
653 elif underlying_type.property_type.is_fundamental: | 655 elif underlying_type.property_type.is_fundamental: |
654 if is_ptr: | 656 if is_ptr: |
655 var = '*%s' % var | 657 var = '*%s' % var |
656 if underlying_type.property_type == PropertyType.STRING: | 658 if underlying_type.property_type == PropertyType.STRING: |
657 return 'new base::StringValue(%s)' % var | 659 return 'base::WrapUnique(new base::StringValue(%s))' % var |
658 else: | 660 else: |
659 return 'new base::FundamentalValue(%s)' % var | 661 return 'base::WrapUnique(new base::FundamentalValue(%s))' % var |
660 else: | 662 else: |
661 raise NotImplementedError('Conversion of %s to base::Value not ' | 663 raise NotImplementedError('Conversion of %s to base::Value not ' |
662 'implemented' % repr(type_.type_)) | 664 'implemented' % repr(type_.type_)) |
663 | 665 |
664 def _GenerateParamsCheck(self, function, var): | 666 def _GenerateParamsCheck(self, function, var): |
665 """Generates a check for the correct number of arguments when creating | 667 """Generates a check for the correct number of arguments when creating |
666 Params. | 668 Params. |
667 """ | 669 """ |
668 c = Code() | 670 c = Code() |
669 num_required = 0 | 671 num_required = 0 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
833 .Eblock('}') | 835 .Eblock('}') |
834 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( | 836 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( |
835 ('*dictionary', '&%(dst_var)s'))) | 837 ('*dictionary', '&%(dst_var)s'))) |
836 .Append(' return %(failure_value)s;') | 838 .Append(' return %(failure_value)s;') |
837 .Append('}') | 839 .Append('}') |
838 ) | 840 ) |
839 elif underlying_type.property_type == PropertyType.FUNCTION: | 841 elif underlying_type.property_type == PropertyType.FUNCTION: |
840 if is_ptr: | 842 if is_ptr: |
841 c.Append('%(dst_var)s.reset(new base::DictionaryValue());') | 843 c.Append('%(dst_var)s.reset(new base::DictionaryValue());') |
842 elif underlying_type.property_type == PropertyType.ANY: | 844 elif underlying_type.property_type == PropertyType.ANY: |
843 c.Append('%(dst_var)s.reset(%(src_var)s->DeepCopy());') | 845 c.Append('%(dst_var)s = %(src_var)s->CreateDeepCopy();') |
844 elif underlying_type.property_type == PropertyType.ARRAY: | 846 elif underlying_type.property_type == PropertyType.ARRAY: |
845 # util_cc_helper deals with optional and required arrays | 847 # util_cc_helper deals with optional and required arrays |
846 (c.Append('const base::ListValue* list = NULL;') | 848 (c.Append('const base::ListValue* list = NULL;') |
847 .Sblock('if (!%(src_var)s->GetAsList(&list)) {') | 849 .Sblock('if (!%(src_var)s->GetAsList(&list)) {') |
848 .Concat(self._GenerateError( | 850 .Concat(self._GenerateError( |
849 '"\'%%(key)s\': expected list, got " + ' + | 851 '"\'%%(key)s\': expected list, got " + ' + |
850 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) | 852 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
851 ) | 853 ) |
852 if is_ptr and self._generate_error_messages: | 854 if is_ptr and self._generate_error_messages: |
853 c.Append('%(dst_var)s.reset();') | 855 c.Append('%(dst_var)s.reset();') |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1156 if self._generate_error_messages: | 1158 if self._generate_error_messages: |
1157 params = list(params) + ['base::string16* error'] | 1159 params = list(params) + ['base::string16* error'] |
1158 return ', '.join(str(p) for p in params) | 1160 return ', '.join(str(p) for p in params) |
1159 | 1161 |
1160 def _GenerateArgs(self, args): | 1162 def _GenerateArgs(self, args): |
1161 """Builds the argument list for a function, given an array of arguments. | 1163 """Builds the argument list for a function, given an array of arguments. |
1162 """ | 1164 """ |
1163 if self._generate_error_messages: | 1165 if self._generate_error_messages: |
1164 args = list(args) + ['error'] | 1166 args = list(args) + ['error'] |
1165 return ', '.join(str(a) for a in args) | 1167 return ', '.join(str(a) for a in args) |
OLD | NEW |