Chromium Code Reviews| 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, Type | 6 from model import PropertyType |
| 7 import cpp_util | 7 import cpp_util |
| 8 import model | |
| 9 import schema_util | 8 import schema_util |
| 10 import sys | |
| 11 import util_cc_helper | 9 import util_cc_helper |
| 12 | 10 |
|
not at google - send to devlin
2013/09/09 23:50:25
now that I know about the Python PEP8 thing, I kno
dhnishi (use Chromium)
2013/09/10 17:21:37
Done.
| |
| 13 class CCGenerator(object): | 11 class CCGenerator(object): |
| 14 def __init__(self, type_generator, cpp_namespace): | 12 def __init__(self, type_generator, cpp_namespace): |
| 15 self._type_generator = type_generator | 13 self._type_generator = type_generator |
| 16 self._cpp_namespace = cpp_namespace | 14 self._cpp_namespace = cpp_namespace |
| 17 | 15 |
| 18 def Generate(self, namespace): | 16 def Generate(self, namespace): |
| 19 return _Generator(namespace, | 17 return _Generator(namespace, |
| 20 self._type_generator, | 18 self._type_generator, |
| 21 self._cpp_namespace).Generate() | 19 self._cpp_namespace).Generate() |
| 22 | 20 |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 # ANY is a base::Value which is abstract and cannot be a direct member, so | 340 # ANY is a base::Value which is abstract and cannot be a direct member, so |
| 343 # it will always be a pointer. | 341 # it will always be a pointer. |
| 344 is_ptr = prop.optional or prop.type_.property_type == PropertyType.ANY | 342 is_ptr = prop.optional or prop.type_.property_type == PropertyType.ANY |
| 345 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( | 343 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( |
| 346 prop.name, | 344 prop.name, |
| 347 self._CreateValueFromType(prop.type_, | 345 self._CreateValueFromType(prop.type_, |
| 348 'this->%s' % prop.unix_name, | 346 'this->%s' % prop.unix_name, |
| 349 is_ptr=is_ptr))) | 347 is_ptr=is_ptr))) |
| 350 | 348 |
| 351 if prop.optional: | 349 if prop.optional: |
| 352 c.Eblock('}'); | 350 c.Eblock('}') |
| 353 | 351 |
| 354 if type_.additional_properties is not None: | 352 if type_.additional_properties is not None: |
| 355 if type_.additional_properties.property_type == PropertyType.ANY: | 353 if type_.additional_properties.property_type == PropertyType.ANY: |
| 356 c.Append('value->MergeDictionary(&additional_properties);') | 354 c.Append('value->MergeDictionary(&additional_properties);') |
| 357 else: | 355 else: |
| 358 # Non-copyable types will be wrapped in a linked_ptr for inclusion in | 356 # Non-copyable types will be wrapped in a linked_ptr for inclusion in |
| 359 # maps, so we need to unwrap them. | 357 # maps, so we need to unwrap them. |
| 360 needs_unwrap = ( | 358 needs_unwrap = ( |
| 361 not self._type_helper.IsCopyable(type_.additional_properties)) | 359 not self._type_helper.IsCopyable(type_.additional_properties)) |
| 362 cpp_type = self._type_helper.GetCppType(type_.additional_properties, | 360 cpp_type = self._type_helper.GetCppType(type_.additional_properties, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 375 return (c.Append() | 373 return (c.Append() |
| 376 .Append('return value.Pass();') | 374 .Append('return value.Pass();') |
| 377 .Eblock('}')) | 375 .Eblock('}')) |
| 378 | 376 |
| 379 def _GenerateChoiceTypeToValue(self, cpp_namespace, type_): | 377 def _GenerateChoiceTypeToValue(self, cpp_namespace, type_): |
| 380 """Generates a function that serializes a choice-representing type | 378 """Generates a function that serializes a choice-representing type |
| 381 into a base::Value. | 379 into a base::Value. |
| 382 """ | 380 """ |
| 383 c = Code() | 381 c = Code() |
| 384 c.Sblock('scoped_ptr<base::Value> %s::ToValue() const {' % cpp_namespace) | 382 c.Sblock('scoped_ptr<base::Value> %s::ToValue() const {' % cpp_namespace) |
| 385 c.Append('scoped_ptr<base::Value> result;'); | 383 c.Append('scoped_ptr<base::Value> result;') |
| 386 for choice in type_.choices: | 384 for choice in type_.choices: |
| 387 choice_var = 'as_%s' % choice.unix_name | 385 choice_var = 'as_%s' % choice.unix_name |
| 388 (c.Sblock('if (%s) {' % choice_var) | 386 (c.Sblock('if (%s) {' % choice_var) |
| 389 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % | 387 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % |
| 390 type_.unix_name) | 388 type_.unix_name) |
| 391 .Append('result.reset(%s);' % | 389 .Append('result.reset(%s);' % |
| 392 self._CreateValueFromType(choice, '*%s' % choice_var)) | 390 self._CreateValueFromType(choice, '*%s' % choice_var)) |
| 393 .Eblock('}') | 391 .Eblock('}') |
| 394 ) | 392 ) |
| 395 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % | 393 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % |
| 396 type_.unix_name) | 394 type_.unix_name) |
| 397 .Append('return result.Pass();') | 395 .Append('return result.Pass();') |
| 398 .Eblock('}') | 396 .Eblock('}') |
| 399 ) | 397 ) |
| 400 return c | 398 return c |
| 401 | 399 |
| 402 def _GenerateFunction(self, function): | 400 def _GenerateFunction(self, function): |
| 403 """Generates the definitions for function structs. | 401 """Generates the definitions for function structs. |
| 404 """ | 402 """ |
| 405 c = Code() | 403 c = Code() |
| 406 | 404 |
| 407 # TODO(kalman): use function.unix_name not Classname. | 405 # TODO(kalman): use function.unix_name not Classname. |
| 408 function_namespace = cpp_util.Classname(function.name) | 406 function_namespace = cpp_util.Classname(function.name) |
| 409 """Windows has a #define for SendMessage, so to avoid any issues, we need | 407 # Windows has a #define for SendMessage, so to avoid any issues, we need |
| 410 to not use the name. | 408 # to not use the name. |
| 411 """ | |
| 412 if function_namespace == 'SendMessage': | 409 if function_namespace == 'SendMessage': |
| 413 function_namespace = 'PassMessage' | 410 function_namespace = 'PassMessage' |
| 414 (c.Append('namespace %s {' % function_namespace) | 411 (c.Append('namespace %s {' % function_namespace) |
| 415 .Append() | 412 .Append() |
| 416 ) | 413 ) |
| 417 | 414 |
| 418 # Params::Populate function | 415 # Params::Populate function |
| 419 if function.params: | 416 if function.params: |
| 420 c.Concat(self._GeneratePropertyFunctions('Params', function.params)) | 417 c.Concat(self._GeneratePropertyFunctions('Params', function.params)) |
| 421 (c.Append('Params::Params() {}') | 418 (c.Append('Params::Params() {}') |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 731 'key': type_.name, | 728 'key': type_.name, |
| 732 'parent_key': type_.parent.name | 729 'parent_key': type_.parent.name |
| 733 }) | 730 }) |
| 734 | 731 |
| 735 def _GenerateListValueToEnumArrayConversion(self, | 732 def _GenerateListValueToEnumArrayConversion(self, |
| 736 item_type, | 733 item_type, |
| 737 src_var, | 734 src_var, |
| 738 dst_var, | 735 dst_var, |
| 739 failure_value, | 736 failure_value, |
| 740 is_ptr=False): | 737 is_ptr=False): |
| 741 """Returns Code that converts a ListValue of string constants from | 738 """Returns Code that converts a ListValue of string constants from |
| 742 |src_var| into an array of enums of |type_| in |dst_var|. On failure, | 739 |src_var| into an array of enums of |type_| in |dst_var|. On failure, |
| 743 returns |failure_value|. | 740 returns |failure_value|. |
| 744 """ | 741 """ |
| 745 c = Code() | 742 c = Code() |
| 746 accessor = '.' | 743 accessor = '.' |
| 747 if is_ptr: | 744 if is_ptr: |
| 748 accessor = '->' | 745 accessor = '->' |
| 749 cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True) | 746 cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True) |
| 750 c.Append('%s.reset(new std::vector<%s>);' % | 747 c.Append('%s.reset(new std::vector<%s>);' % |
| 751 (dst_var, cpp_util.PadForGenerics(cpp_type))) | 748 (dst_var, cpp_util.PadForGenerics(cpp_type))) |
| 752 (c.Sblock('for (base::ListValue::const_iterator it = %s->begin(); ' | 749 (c.Sblock('for (base::ListValue::const_iterator it = %s->begin(); ' |
| 753 'it != %s->end(); ++it) {' % (src_var, src_var)) | 750 'it != %s->end(); ++it) {' % (src_var, src_var)) |
| 754 .Append('%s tmp;' % self._type_helper.GetCppType(item_type)) | 751 .Append('%s tmp;' % self._type_helper.GetCppType(item_type)) |
| 755 .Concat(self._GenerateStringToEnumConversion(item_type, | 752 .Concat(self._GenerateStringToEnumConversion(item_type, |
| 756 '(*it)', | 753 '(*it)', |
| 757 'tmp', | 754 'tmp', |
| 758 failure_value)) | 755 failure_value)) |
| 759 .Append('%s%spush_back(tmp);' % (dst_var, accessor)) | 756 .Append('%s%spush_back(tmp);' % (dst_var, accessor)) |
| 760 .Eblock('}') | 757 .Eblock('}') |
| 761 ) | 758 ) |
| 762 return c | 759 return c |
| 763 | 760 |
| 764 def _GenerateStringToEnumConversion(self, | 761 def _GenerateStringToEnumConversion(self, |
| 765 type_, | 762 type_, |
| 766 src_var, | 763 src_var, |
| 767 dst_var, | 764 dst_var, |
| 768 failure_value): | 765 failure_value): |
| 769 """Returns Code that converts a string type in |src_var| to an enum with | 766 """Returns Code that converts a string type in |src_var| to an enum with |
| 770 type |type_| in |dst_var|. In the generated code, if |src_var| is not | 767 type |type_| in |dst_var|. In the generated code, if |src_var| is not |
| 771 a valid enum name then the function will return |failure_value|. | 768 a valid enum name then the function will return |failure_value|. |
| 772 """ | 769 """ |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 935 if self._generate_error_messages: | 932 if self._generate_error_messages: |
| 936 params = list(params) + ['base::string16* error'] | 933 params = list(params) + ['base::string16* error'] |
| 937 return ', '.join(str(p) for p in params) | 934 return ', '.join(str(p) for p in params) |
| 938 | 935 |
| 939 def _GenerateArgs(self, args): | 936 def _GenerateArgs(self, args): |
| 940 """Builds the argument list for a function, given an array of arguments. | 937 """Builds the argument list for a function, given an array of arguments. |
| 941 """ | 938 """ |
| 942 if self._generate_error_messages: | 939 if self._generate_error_messages: |
| 943 args = list(args) + ['error'] | 940 args = list(args) + ['error'] |
| 944 return ', '.join(str(a) for a in args) | 941 return ', '.join(str(a) for a in args) |
| OLD | NEW |