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

Side by Side Diff: mojo/public/tools/bindings/generators/mojom_cpp_generator.py

Issue 2965663002: Mojo C++ bindings: support setting default value for fields whose types are typemapped enums. (Closed)
Patch Set: . Created 3 years, 5 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
« no previous file with comments | « mojo/public/interfaces/bindings/tests/struct_with_traits.mojom ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 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 """Generates C++ source files from a mojom.Module.""" 5 """Generates C++ source files from a mojom.Module."""
6 6
7 import mojom.generate.generator as generator 7 import mojom.generate.generator as generator
8 import mojom.generate.module as mojom 8 import mojom.generate.module as mojom
9 import mojom.generate.pack as pack 9 import mojom.generate.pack as pack
10 from mojom.generate.template_expander import UseJinja 10 from mojom.generate.template_expander import UseJinja
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 else: 393 else:
394 suffix = "-%s" % self.variant if self.variant else "" 394 suffix = "-%s" % self.variant if self.variant else ""
395 self.Write(self._GenerateModuleHeader(), 395 self.Write(self._GenerateModuleHeader(),
396 "%s%s.h" % (self.module.path, suffix)) 396 "%s%s.h" % (self.module.path, suffix))
397 self.Write(self._GenerateModuleSource(), 397 self.Write(self._GenerateModuleSource(),
398 "%s%s.cc" % (self.module.path, suffix)) 398 "%s%s.cc" % (self.module.path, suffix))
399 399
400 def _ConstantValue(self, constant): 400 def _ConstantValue(self, constant):
401 return self._ExpressionToText(constant.value, kind=constant.kind) 401 return self._ExpressionToText(constant.value, kind=constant.kind)
402 402
403 # TODO(yzshen): Revisit the default value feature. It was designed prior to
404 # custom type mapping.
405 def _DefaultValue(self, field): 403 def _DefaultValue(self, field):
406 if field.default: 404 if not field.default:
407 if mojom.IsStructKind(field.kind): 405 return ""
408 assert field.default == "default" 406
409 if not self._IsTypemappedKind(field.kind): 407 if mojom.IsStructKind(field.kind):
410 return "%s::New()" % self._GetNameForKind(field.kind) 408 assert field.default == "default"
411 return self._ExpressionToText(field.default, kind=field.kind) 409 if self._IsTypemappedKind(field.kind):
412 return "" 410 return ""
411 return "%s::New()" % self._GetNameForKind(field.kind)
412
413 expression = self._ExpressionToText(field.default, kind=field.kind)
414 if mojom.IsEnumKind(field.kind) and self._IsTypemappedKind(field.kind):
415 expression = "mojo::internal::ConvertEnumValue<%s, %s>(%s)" % (
416 self._GetNameForKind(field.kind), self._GetCppWrapperType(field.kind),
417 expression)
418 return expression
413 419
414 def _GetNameForKind(self, kind, internal=False, flatten_nested_kind=False, 420 def _GetNameForKind(self, kind, internal=False, flatten_nested_kind=False,
415 add_same_module_namespaces=False): 421 add_same_module_namespaces=False):
416 return _NameFormatter(kind, self.variant).FormatForCpp( 422 return _NameFormatter(kind, self.variant).FormatForCpp(
417 internal=internal, flatten_nested_kind=flatten_nested_kind, 423 internal=internal, flatten_nested_kind=flatten_nested_kind,
418 omit_namespace_for_module = (None if add_same_module_namespaces 424 omit_namespace_for_module = (None if add_same_module_namespaces
419 else self.module)) 425 else self.module))
420 426
421 def _GetQualifiedNameForKind(self, kind, internal=False, 427 def _GetQualifiedNameForKind(self, kind, internal=False,
422 flatten_nested_kind=False, include_variant=True): 428 flatten_nested_kind=False, include_variant=True):
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 if mojom.IsDataPipeProducerKind(kind): 801 if mojom.IsDataPipeProducerKind(kind):
796 return "mojo::ScopedDataPipeProducerHandle" 802 return "mojo::ScopedDataPipeProducerHandle"
797 if mojom.IsMessagePipeKind(kind): 803 if mojom.IsMessagePipeKind(kind):
798 return "mojo::ScopedMessagePipeHandle" 804 return "mojo::ScopedMessagePipeHandle"
799 if mojom.IsSharedBufferKind(kind): 805 if mojom.IsSharedBufferKind(kind):
800 return "mojo::ScopedSharedBufferHandle" 806 return "mojo::ScopedSharedBufferHandle"
801 return _kind_to_cpp_type[kind] 807 return _kind_to_cpp_type[kind]
802 808
803 def _GetUnmappedTypeForSerializer(self, kind): 809 def _GetUnmappedTypeForSerializer(self, kind):
804 return self._GetCppDataViewType(kind, qualified=True) 810 return self._GetCppDataViewType(kind, qualified=True)
OLDNEW
« no previous file with comments | « mojo/public/interfaces/bindings/tests/struct_with_traits.mojom ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698