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 any_helper | 7 import any_helper |
8 import cpp_util | 8 import cpp_util |
9 import model | 9 import model |
10 import schema_util | 10 import schema_util |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 .Append() | 247 .Append() |
248 ) | 248 ) |
249 for prop in type_.properties.values(): | 249 for prop in type_.properties.values(): |
250 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 250 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
251 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) | 251 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) |
252 else: | 252 else: |
253 if prop.optional: | 253 if prop.optional: |
254 if prop.type_ == PropertyType.ENUM: | 254 if prop.type_ == PropertyType.ENUM: |
255 c.Sblock('if (%s != %s)' % | 255 c.Sblock('if (%s != %s)' % |
256 (prop.unix_name, | 256 (prop.unix_name, |
257 self._cpp_type_generator.GetEnumNoneValue(prop))) | 257 self._cpp_type_generator.GetEnumNoneValue(prop))) |
258 elif prop.type_ == PropertyType.CHOICES: | |
259 c.Sblock('if (%s_type != %s)' % | |
260 (prop.unix_name, | |
261 self._cpp_type_generator.GetEnumNoneValue(prop))) | |
258 else: | 262 else: |
259 c.Sblock('if (%s.get())' % prop.unix_name) | 263 c.Sblock('if (%s.get())' % prop.unix_name) |
260 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( | 264 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( |
261 prop.name, | 265 prop.name, |
262 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name))) | 266 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name))) |
263 if prop.optional: | 267 if prop.optional: |
264 c.Eblock(); | 268 c.Eblock(); |
265 (c.Append() | 269 (c.Append() |
266 .Append('return value.Pass();') | 270 .Append('return value.Pass();') |
267 .Eblock('}') | 271 .Eblock('}') |
(...skipping 18 matching lines...) Expand all Loading... | |
286 | 290 |
287 # Results::Create function | 291 # Results::Create function |
288 if function.callback: | 292 if function.callback: |
289 c.Concat(self._GenerateCreateCallbackArguments( | 293 c.Concat(self._GenerateCreateCallbackArguments( |
290 "%s::Results" % cpp_namespace, function.callback)) | 294 "%s::Results" % cpp_namespace, function.callback)) |
291 | 295 |
292 c.Substitute({'cpp_namespace': cpp_namespace}) | 296 c.Substitute({'cpp_namespace': cpp_namespace}) |
293 | 297 |
294 return c | 298 return c |
295 | 299 |
296 def _GenerateCreateEnumValue(self, cpp_namespace, prop): | |
297 """Generates CreateEnumValue() that returns the |base::StringValue| | |
298 representation of an enum. | |
299 """ | |
300 c = Code() | |
301 c.Append('// static') | |
302 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' | |
303 '%(arg)s) {') | |
304 c.Sblock('switch (%s) {' % prop.unix_name) | |
305 if prop.optional: | |
306 (c.Append('case %s: {' % self._cpp_type_generator.GetEnumNoneValue(prop)) | |
307 .Append(' return scoped_ptr<base::Value>();') | |
308 .Append('}') | |
309 ) | |
310 for enum_value in prop.enum_values: | |
311 (c.Append('case %s: {' % | |
312 self._cpp_type_generator.GetEnumValue(prop, enum_value)) | |
313 .Append(' return scoped_ptr<base::Value>(' | |
314 'base::Value::CreateStringValue("%s"));' % | |
315 enum_value) | |
316 .Append('}') | |
317 ) | |
318 (c.Append('default: {') | |
319 .Append(' return scoped_ptr<base::Value>();') | |
320 .Append('}') | |
321 ) | |
322 c.Eblock('}') | |
323 c.Eblock('}') | |
324 c.Substitute({ | |
325 'cpp_namespace': cpp_namespace, | |
326 'arg': cpp_util.GetParameterDeclaration( | |
327 prop, self._cpp_type_generator.GetType(prop)) | |
328 }) | |
329 return c | |
330 | |
331 def _CreateValueFromProperty(self, prop, var): | 300 def _CreateValueFromProperty(self, prop, var): |
332 """Creates a base::Value given a property. Generated code passes ownership | 301 """Creates a base::Value given a property. Generated code passes ownership |
333 to caller. | 302 to caller. |
334 | 303 |
335 var: variable or variable* | 304 var: variable or variable* |
336 | 305 |
337 E.g for std::string, generate base::Value::CreateStringValue(var) | 306 E.g for std::string, generate base::Value::CreateStringValue(var) |
338 """ | 307 """ |
339 if prop.type_ == PropertyType.CHOICES: | 308 if prop.type_ == PropertyType.CHOICES: |
340 # CHOICES conversion not implemented. If needed, write something to | 309 return 'GetChoiceValue(%s_type).release()' % var |
341 # generate a function that returns a scoped_ptr<base::Value> and put it in | 310 elif self._IsObjectOrObjectRef(prop): |
342 # _GeneratePropertyFunctions, then use it here. Look at CreateEnumValue() | |
343 # for reference. | |
344 raise NotImplementedError( | |
345 'Conversion of CHOICES to base::Value not implemented') | |
346 if self._IsObjectOrObjectRef(prop): | |
347 if prop.optional: | 311 if prop.optional: |
348 return '%s->ToValue().release()' % var | 312 return '%s->ToValue().release()' % var |
349 else: | 313 else: |
350 return '%s.ToValue().release()' % var | 314 return '%s.ToValue().release()' % var |
351 elif prop.type_ == PropertyType.ANY: | 315 elif prop.type_ == PropertyType.ANY: |
352 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) | 316 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) |
353 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 317 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
354 return '%s.DeepCopy()' % var | 318 return '%s.DeepCopy()' % var |
355 elif prop.type_ == PropertyType.ENUM: | 319 elif prop.type_ == PropertyType.ENUM: |
356 return 'CreateEnumValue(%s).release()' % var | 320 return 'CreateEnumValue(%s).release()' % var |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
622 c.Concat(self._GenerateType( | 586 c.Concat(self._GenerateType( |
623 param_namespace + '::' + cpp_util.Classname(param.name), | 587 param_namespace + '::' + cpp_util.Classname(param.name), |
624 param)) | 588 param)) |
625 c.Append() | 589 c.Append() |
626 elif param.type_ == PropertyType.ARRAY: | 590 elif param.type_ == PropertyType.ARRAY: |
627 c.Concat(self._GeneratePropertyFunctions( | 591 c.Concat(self._GeneratePropertyFunctions( |
628 param_namespace, [param.item_type])) | 592 param_namespace, [param.item_type])) |
629 elif param.type_ == PropertyType.CHOICES: | 593 elif param.type_ == PropertyType.CHOICES: |
630 c.Concat(self._GeneratePropertyFunctions( | 594 c.Concat(self._GeneratePropertyFunctions( |
631 param_namespace, param.choices.values())) | 595 param_namespace, param.choices.values())) |
596 if param.from_client: | |
597 c.Concat(self._GenerateGetChoiceValue(param_namespace, param)) | |
632 elif param.type_ == PropertyType.ENUM: | 598 elif param.type_ == PropertyType.ENUM: |
633 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) | 599 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) |
634 c.Append() | 600 c.Append() |
635 return c | 601 return c |
636 | 602 |
603 def _GenerateGetChoiceValue(self, cpp_namespace, prop): | |
604 """Generates GetChoiceValue() that returns a scoped_ptr<base::Value> | |
605 representing the choice value. | |
606 """ | |
607 c = Code() | |
608 (c.Sblock('scoped_ptr<base::Value> ' | |
not at google - send to devlin
2012/07/19 23:59:53
where's the static? Should be able to be static li
| |
609 '%(cpp_namespace)s::GetChoiceValue(%(arg)s) const {') | |
610 .Sblock('switch(%s) {' % prop.unix_name) | |
611 ) | |
612 if prop.optional: | |
613 c.Concat(self._GenerateReturnCase( | |
614 self._cpp_type_generator.GetEnumNoneValue(prop), | |
615 'scoped_ptr<base::Value>()')) | |
616 for choice in self._cpp_type_generator.ExpandParams([prop]): | |
617 c.Concat(self._GenerateReturnCase( | |
618 self._cpp_type_generator.GetEnumValue(prop, choice.type_.name), | |
619 'make_scoped_ptr<base::Value>(%s)' % | |
620 self._CreateValueFromProperty(choice, choice.unix_name))) | |
621 (c.Eblock('}') | |
622 .Append('NOTREACHED();') | |
623 .Append('return scoped_ptr<base::Value>();') | |
624 .Eblock('}') | |
625 .Append() | |
626 .Substitute({ | |
627 'cpp_namespace': cpp_namespace, | |
628 'arg': cpp_util.GetParameterDeclaration( | |
629 prop, self._cpp_type_generator.GetChoicesEnumType(prop)) | |
630 }) | |
631 ) | |
632 return c | |
633 | |
634 def _GenerateCreateEnumValue(self, cpp_namespace, prop): | |
635 """Generates CreateEnumValue() that returns the base::StringValue | |
636 representation of an enum. | |
637 """ | |
638 c = Code() | |
639 c.Append('// static') | |
640 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' | |
641 '%(arg)s) {') | |
642 c.Sblock('switch (%s) {' % prop.unix_name) | |
643 if prop.optional: | |
644 c.Concat(self._GenerateReturnCase( | |
645 self._cpp_type_generator.GetEnumNoneValue(prop), | |
646 'scoped_ptr<base::Value>()')) | |
647 for enum_value in prop.enum_values: | |
648 c.Concat(self._GenerateReturnCase( | |
649 self._cpp_type_generator.GetEnumValue(prop, enum_value), | |
650 'scoped_ptr<base::Value>(base::Value::CreateStringValue("%s"))' % | |
651 enum_value)) | |
652 (c.Eblock('}') | |
653 .Append('NOTREACHED();') | |
654 .Append('return scoped_ptr<base::Value>();') | |
655 .Eblock('}') | |
656 .Substitute({ | |
657 'cpp_namespace': cpp_namespace, | |
658 'arg': cpp_util.GetParameterDeclaration( | |
659 prop, self._cpp_type_generator.GetType(prop)) | |
660 }) | |
661 ) | |
662 return c | |
663 | |
664 def _GenerateReturnCase(self, case_value, return_value): | |
665 """Generates a single return case for a switch block. | |
not at google - send to devlin
2012/07/19 23:59:53
haha, sure that's cool.
| |
666 """ | |
667 c = Code() | |
668 (c.Append('case %s:' % case_value) | |
669 .Append(' return %s;' % return_value) | |
670 ) | |
671 return c | |
672 | |
637 def _GenerateCreateCallbackArguments(self, function_scope, callback): | 673 def _GenerateCreateCallbackArguments(self, function_scope, callback): |
638 """Generate all functions to create Value parameters for a callback. | 674 """Generate all functions to create Value parameters for a callback. |
639 | 675 |
640 E.g for function "Bar", generate Bar::Results::Create | 676 E.g for function "Bar", generate Bar::Results::Create |
641 E.g for event "Baz", generate Baz::Create | 677 E.g for event "Baz", generate Baz::Create |
642 | 678 |
643 function_scope: the function scope path, e.g. Foo::Bar for the function | 679 function_scope: the function scope path, e.g. Foo::Bar for the function |
644 Foo::Bar::Baz(). | 680 Foo::Bar::Baz(). |
645 callback: the Function object we are creating callback arguments for. | 681 callback: the Function object we are creating callback arguments for. |
646 """ | 682 """ |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
706 """ | 742 """ |
707 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 743 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
708 PropertyType.ARRAY) | 744 PropertyType.ARRAY) |
709 | 745 |
710 def _IsFundamentalOrFundamentalRef(self, prop): | 746 def _IsFundamentalOrFundamentalRef(self, prop): |
711 """Determines if this property is a Fundamental type or is a ref to a | 747 """Determines if this property is a Fundamental type or is a ref to a |
712 Fundamental type. | 748 Fundamental type. |
713 """ | 749 """ |
714 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 750 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
715 is_fundamental) | 751 is_fundamental) |
OLD | NEW |