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, Type |
7 import cpp_util | 7 import cpp_util |
8 import model | 8 import model |
9 import schema_util | 9 import schema_util |
10 import sys | 10 import sys |
(...skipping 13 matching lines...) Expand all Loading... |
24 """A .cc generator for a namespace. | 24 """A .cc generator for a namespace. |
25 """ | 25 """ |
26 def __init__(self, namespace, cpp_type_generator, cpp_namespace): | 26 def __init__(self, namespace, cpp_type_generator, cpp_namespace): |
27 self._namespace = namespace | 27 self._namespace = namespace |
28 self._type_helper = cpp_type_generator | 28 self._type_helper = cpp_type_generator |
29 self._cpp_namespace = cpp_namespace | 29 self._cpp_namespace = cpp_namespace |
30 self._target_namespace = ( | 30 self._target_namespace = ( |
31 self._type_helper.GetCppNamespaceName(self._namespace)) | 31 self._type_helper.GetCppNamespaceName(self._namespace)) |
32 self._util_cc_helper = ( | 32 self._util_cc_helper = ( |
33 util_cc_helper.UtilCCHelper(self._type_helper)) | 33 util_cc_helper.UtilCCHelper(self._type_helper)) |
| 34 self._generate_error_messages = namespace.compiler_options.get( |
| 35 'generate_error_messages', True) |
34 | 36 |
35 def Generate(self): | 37 def Generate(self): |
36 """Generates a Code object with the .cc for a single namespace. | 38 """Generates a Code object with the .cc for a single namespace. |
37 """ | 39 """ |
38 c = Code() | 40 c = Code() |
39 (c.Append(cpp_util.CHROMIUM_LICENSE) | 41 (c.Append(cpp_util.CHROMIUM_LICENSE) |
40 .Append() | 42 .Append() |
41 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 43 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
42 .Append() | 44 .Append() |
43 .Append(self._util_cc_helper.GetIncludePath()) | 45 .Append(self._util_cc_helper.GetIncludePath()) |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 | 177 |
176 def _GenerateTypePopulate(self, cpp_namespace, type_): | 178 def _GenerateTypePopulate(self, cpp_namespace, type_): |
177 """Generates the function for populating a type given a pointer to it. | 179 """Generates the function for populating a type given a pointer to it. |
178 | 180 |
179 E.g for type "Foo", generates Foo::Populate() | 181 E.g for type "Foo", generates Foo::Populate() |
180 """ | 182 """ |
181 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) | 183 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
182 c = Code() | 184 c = Code() |
183 (c.Append('// static') | 185 (c.Append('// static') |
184 .Append('bool %(namespace)s::Populate(') | 186 .Append('bool %(namespace)s::Populate(') |
185 .Sblock(' const base::Value& value, %(name)s* out) {') | 187 .Sblock(' %s) {' % self._GenerateParams( |
186 ) | 188 ('const base::Value& value', '%(name)s* out')))) |
| 189 |
187 if type_.property_type == PropertyType.CHOICES: | 190 if type_.property_type == PropertyType.CHOICES: |
188 for choice in type_.choices: | 191 for choice in type_.choices: |
189 (c.Sblock('if (%s) {' % self._GenerateValueIsTypeExpression('value', | 192 (c.Sblock('if (%s) {' % self._GenerateValueIsTypeExpression('value', |
190 choice)) | 193 choice)) |
191 .Concat(self._GeneratePopulateVariableFromValue( | 194 .Concat(self._GeneratePopulateVariableFromValue( |
192 choice, | 195 choice, |
193 '(&value)', | 196 '(&value)', |
194 'out->as_%s' % choice.unix_name, | 197 'out->as_%s' % choice.unix_name, |
195 'false', | 198 'false', |
196 is_ptr=True)) | 199 is_ptr=True)) |
197 .Append('return true;') | 200 .Append('return true;') |
198 .Eblock('}') | 201 .Eblock('}') |
199 ) | 202 ) |
200 c.Append('return false;') | 203 (c.Concat(self._GenerateError( |
| 204 '"unexpected type, got " + ' + |
| 205 self._util_cc_helper.GetValueTypeString('value'))) |
| 206 .Append('return false;')) |
201 elif type_.property_type == PropertyType.OBJECT: | 207 elif type_.property_type == PropertyType.OBJECT: |
202 (c.Append('if (!value.IsType(base::Value::TYPE_DICTIONARY))') | 208 (c.Sblock('if (!value.IsType(base::Value::TYPE_DICTIONARY)) {') |
203 .Append(' return false;') | 209 .Concat(self._GenerateError( |
204 ) | 210 '"expected dictionary, got " + ' + |
| 211 self._util_cc_helper.GetValueTypeString('value'))) |
| 212 .Append('return false;') |
| 213 .Eblock('}')) |
| 214 |
205 if type_.properties or type_.additional_properties is not None: | 215 if type_.properties or type_.additional_properties is not None: |
206 c.Append('const base::DictionaryValue* dict = ' | 216 c.Append('const base::DictionaryValue* dict = ' |
207 'static_cast<const base::DictionaryValue*>(&value);') | 217 'static_cast<const base::DictionaryValue*>(&value);') |
208 for prop in type_.properties.values(): | 218 for prop in type_.properties.values(): |
209 c.Concat(self._InitializePropertyToDefault(prop, 'out')) | 219 c.Concat(self._InitializePropertyToDefault(prop, 'out')) |
210 for prop in type_.properties.values(): | 220 for prop in type_.properties.values(): |
211 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) | 221 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) |
212 if type_.additional_properties is not None: | 222 if type_.additional_properties is not None: |
213 if type_.additional_properties.property_type == PropertyType.ANY: | 223 if type_.additional_properties.property_type == PropertyType.ANY: |
214 c.Append('out->additional_properties.MergeDictionary(dict);') | 224 c.Append('out->additional_properties.MergeDictionary(dict);') |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') | 263 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') |
254 .Concat(self._GeneratePopulatePropertyFromValue( | 264 .Concat(self._GeneratePopulatePropertyFromValue( |
255 prop, value_var, dst, 'false'))) | 265 prop, value_var, dst, 'false'))) |
256 underlying_type = self._type_helper.FollowRef(prop.type_) | 266 underlying_type = self._type_helper.FollowRef(prop.type_) |
257 if underlying_type.property_type == PropertyType.ENUM: | 267 if underlying_type.property_type == PropertyType.ENUM: |
258 (c.Append('} else {') | 268 (c.Append('} else {') |
259 .Append('%%(dst)s->%%(name)s = %s;' % | 269 .Append('%%(dst)s->%%(name)s = %s;' % |
260 self._type_helper.GetEnumNoneValue(prop.type_))) | 270 self._type_helper.GetEnumNoneValue(prop.type_))) |
261 c.Eblock('}') | 271 c.Eblock('}') |
262 else: | 272 else: |
263 (c.Append( | 273 (c.Sblock( |
264 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))') | 274 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') |
265 .Append(' return false;') | 275 .Concat(self._GenerateError('"\'%%(key)s\' is required"')) |
| 276 .Append('return false;') |
| 277 .Eblock('}') |
266 .Concat(self._GeneratePopulatePropertyFromValue( | 278 .Concat(self._GeneratePopulatePropertyFromValue( |
267 prop, value_var, dst, 'false')) | 279 prop, value_var, dst, 'false')) |
268 ) | 280 ) |
269 c.Append() | 281 c.Append() |
270 c.Substitute({ | 282 c.Substitute({ |
271 'value_var': value_var, | 283 'value_var': value_var, |
272 'key': prop.name, | 284 'key': prop.name, |
273 'src': src, | 285 'src': src, |
274 'dst': dst, | 286 'dst': dst, |
275 'name': prop.unix_name | 287 'name': prop.unix_name |
276 }) | 288 }) |
277 return c | 289 return c |
278 | 290 |
279 def _GenerateTypeFromValue(self, cpp_namespace, type_): | 291 def _GenerateTypeFromValue(self, cpp_namespace, type_): |
280 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) | 292 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
281 c = Code() | 293 c = Code() |
282 (c.Append('// static') | 294 (c.Append('// static') |
283 .Append('scoped_ptr<%s> %s::FromValue(const base::Value& value) {' % ( | 295 .Append('scoped_ptr<%s> %s::FromValue(%s) {' % (classname, |
284 classname, cpp_namespace)) | 296 cpp_namespace, self._GenerateParams(('const base::Value& value',)))) |
285 .Append(' scoped_ptr<%s> out(new %s());' % (classname, classname)) | 297 .Append(' scoped_ptr<%s> out(new %s());' % (classname, classname)) |
286 .Append(' if (!Populate(value, out.get()))') | 298 .Append(' if (!Populate(%s))' % self._GenerateArgs( |
| 299 ('value', 'out.get()'))) |
287 .Append(' return scoped_ptr<%s>();' % classname) | 300 .Append(' return scoped_ptr<%s>();' % classname) |
288 .Append(' return out.Pass();') | 301 .Append(' return out.Pass();') |
289 .Append('}') | 302 .Append('}') |
290 ) | 303 ) |
291 return c | 304 return c |
292 | 305 |
293 def _GenerateTypeToValue(self, cpp_namespace, type_): | 306 def _GenerateTypeToValue(self, cpp_namespace, type_): |
294 """Generates a function that serializes the type into a base::Value. | 307 """Generates a function that serializes the type into a base::Value. |
295 E.g. for type "Foo" generates Foo::ToValue() | 308 E.g. for type "Foo" generates Foo::ToValue() |
296 """ | 309 """ |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 def _GenerateParamsCheck(self, function, var): | 492 def _GenerateParamsCheck(self, function, var): |
480 """Generates a check for the correct number of arguments when creating | 493 """Generates a check for the correct number of arguments when creating |
481 Params. | 494 Params. |
482 """ | 495 """ |
483 c = Code() | 496 c = Code() |
484 num_required = 0 | 497 num_required = 0 |
485 for param in function.params: | 498 for param in function.params: |
486 if not param.optional: | 499 if not param.optional: |
487 num_required += 1 | 500 num_required += 1 |
488 if num_required == len(function.params): | 501 if num_required == len(function.params): |
489 c.Append('if (%(var)s.GetSize() != %(total)d)') | 502 c.Sblock('if (%(var)s.GetSize() != %(total)d) {') |
490 elif not num_required: | 503 elif not num_required: |
491 c.Append('if (%(var)s.GetSize() > %(total)d)') | 504 c.Sblock('if (%(var)s.GetSize() > %(total)d) {') |
492 else: | 505 else: |
493 c.Append('if (%(var)s.GetSize() < %(required)d' | 506 c.Sblock('if (%(var)s.GetSize() < %(required)d' |
494 ' || %(var)s.GetSize() > %(total)d)') | 507 ' || %(var)s.GetSize() > %(total)d) {') |
495 c.Append(' return scoped_ptr<Params>();') | 508 (c.Concat(self._GenerateError( |
496 c.Substitute({ | 509 '"expected %%(total)d arguments, got " ' |
| 510 '+ base::IntToString(%%(var)s.GetSize())')) |
| 511 .Append('return scoped_ptr<Params>();') |
| 512 .Eblock('}') |
| 513 .Substitute({ |
497 'var': var, | 514 'var': var, |
498 'required': num_required, | 515 'required': num_required, |
499 'total': len(function.params), | 516 'total': len(function.params), |
500 }) | 517 })) |
501 return c | 518 return c |
502 | 519 |
503 def _GenerateFunctionParamsCreate(self, function): | 520 def _GenerateFunctionParamsCreate(self, function): |
504 """Generate function to create an instance of Params. The generated | 521 """Generate function to create an instance of Params. The generated |
505 function takes a base::ListValue of arguments. | 522 function takes a base::ListValue of arguments. |
506 | 523 |
507 E.g for function "Bar", generate Bar::Params::Create() | 524 E.g for function "Bar", generate Bar::Params::Create() |
508 """ | 525 """ |
509 c = Code() | 526 c = Code() |
510 (c.Append('// static') | 527 (c.Append('// static') |
511 .Sblock('scoped_ptr<Params> ' | 528 .Sblock('scoped_ptr<Params> Params::Create(%s) {' % self._GenerateParams( |
512 'Params::Create(const base::ListValue& args) {') | 529 ['const base::ListValue& args'])) |
513 .Concat(self._GenerateParamsCheck(function, 'args')) | 530 .Concat(self._GenerateParamsCheck(function, 'args')) |
514 .Append('scoped_ptr<Params> params(new Params());') | 531 .Append('scoped_ptr<Params> params(new Params());')) |
515 ) | |
516 | 532 |
517 for param in function.params: | 533 for param in function.params: |
518 c.Concat(self._InitializePropertyToDefault(param, 'params')) | 534 c.Concat(self._InitializePropertyToDefault(param, 'params')) |
519 | 535 |
520 for i, param in enumerate(function.params): | 536 for i, param in enumerate(function.params): |
521 # Any failure will cause this function to return. If any argument is | 537 # Any failure will cause this function to return. If any argument is |
522 # incorrect or missing, those following it are not processed. Note that | 538 # incorrect or missing, those following it are not processed. Note that |
523 # for optional arguments, we allow missing arguments and proceed because | 539 # for optional arguments, we allow missing arguments and proceed because |
524 # there may be other arguments following it. | 540 # there may be other arguments following it. |
525 failure_value = 'scoped_ptr<Params>()' | 541 failure_value = 'scoped_ptr<Params>()' |
526 c.Append() | 542 c.Append() |
527 value_var = param.unix_name + '_value' | 543 value_var = param.unix_name + '_value' |
528 (c.Append('const base::Value* %(value_var)s = NULL;') | 544 (c.Append('const base::Value* %(value_var)s = NULL;') |
529 .Append('if (args.Get(%(i)s, &%(value_var)s) &&') | 545 .Append('if (args.Get(%(i)s, &%(value_var)s) &&') |
530 .Sblock(' !%(value_var)s->IsType(base::Value::TYPE_NULL)) {') | 546 .Sblock(' !%(value_var)s->IsType(base::Value::TYPE_NULL)) {') |
531 .Concat(self._GeneratePopulatePropertyFromValue( | 547 .Concat(self._GeneratePopulatePropertyFromValue( |
532 param, value_var, 'params', failure_value)) | 548 param, value_var, 'params', failure_value)) |
533 .Eblock('}') | 549 .Eblock('}') |
534 ) | 550 ) |
535 if not param.optional: | 551 if not param.optional: |
536 (c.Sblock('else {') | 552 (c.Sblock('else {') |
| 553 .Concat(self._GenerateError('"\'%%(key)s\' is required"')) |
537 .Append('return %s;' % failure_value) | 554 .Append('return %s;' % failure_value) |
538 .Eblock('}') | 555 .Eblock('}')) |
539 ) | 556 c.Substitute({'value_var': value_var, 'i': i, 'key': param.name}) |
540 c.Substitute({'value_var': value_var, 'i': i}) | |
541 (c.Append() | 557 (c.Append() |
542 .Append('return params.Pass();') | 558 .Append('return params.Pass();') |
543 .Eblock('}') | 559 .Eblock('}') |
544 .Append() | 560 .Append() |
545 ) | 561 ) |
546 | 562 |
547 return c | 563 return c |
548 | 564 |
549 def _GeneratePopulatePropertyFromValue(self, | 565 def _GeneratePopulatePropertyFromValue(self, |
550 prop, | 566 prop, |
(...skipping 23 matching lines...) Expand all Loading... |
574 |failure_value|. | 590 |failure_value|. |
575 """ | 591 """ |
576 c = Code() | 592 c = Code() |
577 c.Sblock('{') | 593 c.Sblock('{') |
578 | 594 |
579 underlying_type = self._type_helper.FollowRef(type_) | 595 underlying_type = self._type_helper.FollowRef(type_) |
580 | 596 |
581 if underlying_type.property_type.is_fundamental: | 597 if underlying_type.property_type.is_fundamental: |
582 if is_ptr: | 598 if is_ptr: |
583 (c.Append('%(cpp_type)s temp;') | 599 (c.Append('%(cpp_type)s temp;') |
584 .Append('if (!%s)' % cpp_util.GetAsFundamentalValue( | 600 .Sblock('if (!%s) {' % cpp_util.GetAsFundamentalValue( |
585 self._type_helper.FollowRef(type_), src_var, '&temp')) | 601 self._type_helper.FollowRef(type_), src_var, '&temp')) |
586 .Append(' return %(failure_value)s;') | 602 .Concat(self._GenerateError( |
| 603 '"\'%%(key)s\': expected %%(cpp_type)s, got " + ' + |
| 604 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 605 .Append('return %(failure_value)s;') |
| 606 .Eblock('}') |
587 .Append('%(dst_var)s.reset(new %(cpp_type)s(temp));') | 607 .Append('%(dst_var)s.reset(new %(cpp_type)s(temp));') |
588 ) | 608 ) |
589 else: | 609 else: |
590 (c.Append('if (!%s)' % cpp_util.GetAsFundamentalValue( | 610 (c.Sblock('if (!%s) {' % cpp_util.GetAsFundamentalValue( |
591 self._type_helper.FollowRef(type_), | 611 self._type_helper.FollowRef(type_), |
592 src_var, | 612 src_var, |
593 '&%s' % dst_var)) | 613 '&%s' % dst_var)) |
594 .Append(' return %(failure_value)s;') | 614 .Concat(self._GenerateError( |
| 615 '"\'%%(key)s\': expected %%(cpp_type)s, got " + ' + |
| 616 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 617 .Append('return %(failure_value)s;') |
| 618 .Eblock('}') |
595 ) | 619 ) |
596 elif underlying_type.property_type == PropertyType.OBJECT: | 620 elif underlying_type.property_type == PropertyType.OBJECT: |
597 if is_ptr: | 621 if is_ptr: |
598 (c.Append('const base::DictionaryValue* dictionary = NULL;') | 622 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
599 .Append('if (!%(src_var)s->GetAsDictionary(&dictionary))') | 623 .Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {') |
| 624 .Concat(self._GenerateError( |
| 625 '"\'%%(key)s\': expected dictionary, got " + ' + |
| 626 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 627 .Append('return %(failure_value)s;') |
| 628 .Eblock('}') |
| 629 .Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') |
| 630 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( |
| 631 ('*dictionary', 'temp.get()'))) |
600 .Append(' return %(failure_value)s;') | 632 .Append(' return %(failure_value)s;') |
601 .Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') | 633 .Append('}') |
602 .Append('if (!%(cpp_type)s::Populate(*dictionary, temp.get()))') | |
603 .Append(' return %(failure_value)s;') | |
604 .Append('%(dst_var)s = temp.Pass();') | 634 .Append('%(dst_var)s = temp.Pass();') |
605 ) | 635 ) |
606 else: | 636 else: |
607 (c.Append('const base::DictionaryValue* dictionary = NULL;') | 637 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
608 .Append('if (!%(src_var)s->GetAsDictionary(&dictionary))') | 638 .Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {') |
| 639 .Concat(self._GenerateError( |
| 640 '"\'%%(key)s\': expected dictionary, got " + ' + |
| 641 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 642 .Append('return %(failure_value)s;') |
| 643 .Eblock('}') |
| 644 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( |
| 645 ('*dictionary', '&%(dst_var)s'))) |
609 .Append(' return %(failure_value)s;') | 646 .Append(' return %(failure_value)s;') |
610 .Append('if (!%(cpp_type)s::Populate(*dictionary, &%(dst_var)s))') | 647 .Append('}') |
611 .Append(' return %(failure_value)s;') | |
612 ) | 648 ) |
613 elif underlying_type.property_type == PropertyType.FUNCTION: | 649 elif underlying_type.property_type == PropertyType.FUNCTION: |
614 if is_ptr: | 650 if is_ptr: |
615 c.Append('%(dst_var)s.reset(new base::DictionaryValue());') | 651 c.Append('%(dst_var)s.reset(new base::DictionaryValue());') |
616 elif underlying_type.property_type == PropertyType.ANY: | 652 elif underlying_type.property_type == PropertyType.ANY: |
617 c.Append('%(dst_var)s.reset(%(src_var)s->DeepCopy());') | 653 c.Append('%(dst_var)s.reset(%(src_var)s->DeepCopy());') |
618 elif underlying_type.property_type == PropertyType.ARRAY: | 654 elif underlying_type.property_type == PropertyType.ARRAY: |
619 # util_cc_helper deals with optional and required arrays | 655 # util_cc_helper deals with optional and required arrays |
620 (c.Append('const base::ListValue* list = NULL;') | 656 (c.Append('const base::ListValue* list = NULL;') |
621 .Append('if (!%(src_var)s->GetAsList(&list))') | 657 .Sblock('if (!%(src_var)s->GetAsList(&list)) {') |
622 .Append(' return %(failure_value)s;')) | 658 .Concat(self._GenerateError( |
| 659 '"\'%%(key)s\': expected list, got " + ' + |
| 660 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 661 .Append('return %(failure_value)s;') |
| 662 .Eblock('}')) |
623 item_type = self._type_helper.FollowRef(underlying_type.item_type) | 663 item_type = self._type_helper.FollowRef(underlying_type.item_type) |
624 if item_type.property_type == PropertyType.ENUM: | 664 if item_type.property_type == PropertyType.ENUM: |
625 c.Concat(self._GenerateListValueToEnumArrayConversion( | 665 c.Concat(self._GenerateListValueToEnumArrayConversion( |
626 item_type, | 666 item_type, |
627 'list', | 667 'list', |
628 dst_var, | 668 dst_var, |
629 failure_value, | 669 failure_value, |
630 is_ptr=is_ptr)) | 670 is_ptr=is_ptr)) |
631 else: | 671 else: |
632 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 672 (c.Sblock('if (!%s) {' % self._util_cc_helper.PopulateArrayFromList( |
633 underlying_type, | 673 underlying_type, |
634 'list', | 674 'list', |
635 dst_var, | 675 dst_var, |
636 is_ptr)) | 676 is_ptr)) |
637 .Append(' return %(failure_value)s;') | 677 .Concat(self._GenerateError( |
| 678 '"unable to populate array \'%%(parent_key)s\'"')) |
| 679 .Append('return %(failure_value)s;') |
| 680 .Eblock('}') |
638 ) | 681 ) |
639 elif underlying_type.property_type == PropertyType.CHOICES: | 682 elif underlying_type.property_type == PropertyType.CHOICES: |
640 if is_ptr: | 683 if is_ptr: |
641 (c.Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') | 684 (c.Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') |
642 .Append('if (!%(cpp_type)s::Populate(*%(src_var)s, temp.get()))') | 685 .Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( |
| 686 ('*%(src_var)s', 'temp.get()'))) |
643 .Append(' return %(failure_value)s;') | 687 .Append(' return %(failure_value)s;') |
644 .Append('%(dst_var)s = temp.Pass();') | 688 .Append('%(dst_var)s = temp.Pass();') |
645 ) | 689 ) |
646 else: | 690 else: |
647 (c.Append('if (!%(cpp_type)s::Populate(*%(src_var)s, &%(dst_var)s))') | 691 (c.Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( |
648 .Append(' return %(failure_value)s;') | 692 ('*%(src_var)s', '&%(dst_var)s'))) |
649 ) | 693 .Append(' return %(failure_value)s;')) |
650 elif underlying_type.property_type == PropertyType.ENUM: | 694 elif underlying_type.property_type == PropertyType.ENUM: |
651 c.Concat(self._GenerateStringToEnumConversion(type_, | 695 c.Concat(self._GenerateStringToEnumConversion(type_, |
652 src_var, | 696 src_var, |
653 dst_var, | 697 dst_var, |
654 failure_value)) | 698 failure_value)) |
655 elif underlying_type.property_type == PropertyType.BINARY: | 699 elif underlying_type.property_type == PropertyType.BINARY: |
656 (c.Append('if (!%(src_var)s->IsType(base::Value::TYPE_BINARY))') | 700 (c.Sblock('if (!%(src_var)s->IsType(base::Value::TYPE_BINARY)) {') |
657 .Append(' return %(failure_value)s;') | 701 .Concat(self._GenerateError( |
| 702 '"\'%%(key)s\': expected BinaryValue, got " + ' + |
| 703 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 704 .Append('return %(failure_value)s;') |
| 705 .Eblock('}') |
658 .Append('const base::BinaryValue* binary_value =') | 706 .Append('const base::BinaryValue* binary_value =') |
659 .Append(' static_cast<const base::BinaryValue*>(%(src_var)s);') | 707 .Append(' static_cast<const base::BinaryValue*>(%(src_var)s);') |
660 ) | 708 ) |
661 if is_ptr: | 709 if is_ptr: |
662 (c.Append('%(dst_var)s.reset(') | 710 (c.Append('%(dst_var)s.reset(') |
663 .Append(' new std::string(binary_value->GetBuffer(),') | 711 .Append(' new std::string(binary_value->GetBuffer(),') |
664 .Append(' binary_value->GetSize()));') | 712 .Append(' binary_value->GetSize()));') |
665 ) | 713 ) |
666 else: | 714 else: |
667 (c.Append('%(dst_var)s.assign(binary_value->GetBuffer(),') | 715 (c.Append('%(dst_var)s.assign(binary_value->GetBuffer(),') |
668 .Append(' binary_value->GetSize());') | 716 .Append(' binary_value->GetSize());') |
669 ) | 717 ) |
670 else: | 718 else: |
671 raise NotImplementedError(type_) | 719 raise NotImplementedError(type_) |
672 return c.Eblock('}').Substitute({ | 720 return c.Eblock('}').Substitute({ |
673 'cpp_type': self._type_helper.GetCppType(type_), | 721 'cpp_type': self._type_helper.GetCppType(type_), |
674 'src_var': src_var, | 722 'src_var': src_var, |
675 'dst_var': dst_var, | 723 'dst_var': dst_var, |
676 'failure_value': failure_value, | 724 'failure_value': failure_value, |
| 725 'key': type_.name, |
| 726 'parent_key': type_.parent.name |
677 }) | 727 }) |
678 | 728 |
679 def _GenerateListValueToEnumArrayConversion(self, | 729 def _GenerateListValueToEnumArrayConversion(self, |
680 item_type, | 730 item_type, |
681 src_var, | 731 src_var, |
682 dst_var, | 732 dst_var, |
683 failure_value, | 733 failure_value, |
684 is_ptr=False): | 734 is_ptr=False): |
685 """Returns Code that converts a ListValue of string constants from | 735 """Returns Code that converts a ListValue of string constants from |
686 |src_var| into an array of enums of |type_| in |dst_var|. On failure, | 736 |src_var| into an array of enums of |type_| in |dst_var|. On failure, |
(...skipping 23 matching lines...) Expand all Loading... |
710 src_var, | 760 src_var, |
711 dst_var, | 761 dst_var, |
712 failure_value): | 762 failure_value): |
713 """Returns Code that converts a string type in |src_var| to an enum with | 763 """Returns Code that converts a string type in |src_var| to an enum with |
714 type |type_| in |dst_var|. In the generated code, if |src_var| is not | 764 type |type_| in |dst_var|. In the generated code, if |src_var| is not |
715 a valid enum name then the function will return |failure_value|. | 765 a valid enum name then the function will return |failure_value|. |
716 """ | 766 """ |
717 c = Code() | 767 c = Code() |
718 enum_as_string = '%s_as_string' % type_.unix_name | 768 enum_as_string = '%s_as_string' % type_.unix_name |
719 (c.Append('std::string %s;' % enum_as_string) | 769 (c.Append('std::string %s;' % enum_as_string) |
720 .Append('if (!%s->GetAsString(&%s))' % (src_var, enum_as_string)) | 770 .Sblock('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string)) |
721 .Append(' return %s;' % failure_value) | 771 .Concat(self._GenerateError( |
| 772 '"\'%%(key)s\': expected string, got " + ' + |
| 773 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
| 774 .Append('return %s;' % failure_value) |
| 775 .Eblock('}') |
722 .Append('%s = Parse%s(%s);' % (dst_var, | 776 .Append('%s = Parse%s(%s);' % (dst_var, |
723 self._type_helper.GetCppType(type_), | 777 self._type_helper.GetCppType(type_), |
724 enum_as_string)) | 778 enum_as_string)) |
725 .Append('if (%s == %s)' % (dst_var, | 779 .Sblock('if (%s == %s) {' % (dst_var, |
726 self._type_helper.GetEnumNoneValue(type_))) | 780 self._type_helper.GetEnumNoneValue(type_))) |
727 .Append(' return %s;' % failure_value) | 781 .Concat(self._GenerateError( |
| 782 '"got bad enum value from \'%%(key)s\'"')) |
| 783 .Append('return %s;' % failure_value) |
| 784 .Eblock('}') |
| 785 .Substitute({'src_var': src_var, 'key': type_.name}) |
728 ) | 786 ) |
729 return c | 787 return c |
730 | 788 |
731 def _GeneratePropertyFunctions(self, namespace, params): | 789 def _GeneratePropertyFunctions(self, namespace, params): |
732 """Generates the member functions for a list of parameters. | 790 """Generates the member functions for a list of parameters. |
733 """ | 791 """ |
734 return self._GenerateTypes(namespace, (param.type_ for param in params)) | 792 return self._GenerateTypes(namespace, (param.type_ for param in params)) |
735 | 793 |
736 def _GenerateTypes(self, namespace, types): | 794 def _GenerateTypes(self, namespace, types): |
737 """Generates the member functions for a list of types. | 795 """Generates the member functions for a list of types. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
843 """ | 901 """ |
844 c = Code() | 902 c = Code() |
845 underlying_type = self._type_helper.FollowRef(prop.type_) | 903 underlying_type = self._type_helper.FollowRef(prop.type_) |
846 if (underlying_type.property_type == PropertyType.ENUM and | 904 if (underlying_type.property_type == PropertyType.ENUM and |
847 prop.optional): | 905 prop.optional): |
848 c.Append('%s->%s = %s;' % ( | 906 c.Append('%s->%s = %s;' % ( |
849 dst, | 907 dst, |
850 prop.unix_name, | 908 prop.unix_name, |
851 self._type_helper.GetEnumNoneValue(prop.type_))) | 909 self._type_helper.GetEnumNoneValue(prop.type_))) |
852 return c | 910 return c |
| 911 |
| 912 def _GenerateError(self, body): |
| 913 """Generates an error message pertaining to population failure. |
| 914 |
| 915 E.g 'expected bool, got int' |
| 916 """ |
| 917 c = Code() |
| 918 if not self._generate_error_messages: |
| 919 return c |
| 920 (c.Append('if (error)') |
| 921 .Append(' *error = ' + body + ';')) |
| 922 return c |
| 923 |
| 924 def _GenerateParams(self, params): |
| 925 """Builds the parameter list for a function, given an array of parameters. |
| 926 """ |
| 927 if self._generate_error_messages: |
| 928 params = list(params) + ['std::string* error'] |
| 929 return ', '.join(str(p) for p in params) |
| 930 |
| 931 def _GenerateArgs(self, args): |
| 932 """Builds the argument list for a function, given an array of arguments. |
| 933 """ |
| 934 if self._generate_error_messages: |
| 935 args = list(args) + ['error'] |
| 936 return ', '.join(str(a) for a in args) |
OLD | NEW |