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

Unified Diff: mojo/public/tools/bindings/generators/mojom_cpp_generator.py

Issue 294833002: Mojo: more idiomatic C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more Created 6 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/generators/mojom_cpp_generator.py
diff --git a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
index 52d380aa2e7964b2b853ce7675eec3d96c923e3a..71e6dcaf5503ed762c67e047432136455eb75f3c 100644
--- a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
@@ -37,8 +37,8 @@ def GetNameForKind(kind, internal = False):
parts = []
if kind.imported_from:
parts.extend(NamespaceToArray(kind.imported_from["namespace"]))
- if internal:
- parts.append("internal")
+ if internal:
+ parts.append("internal")
if kind.parent_kind:
parts.append(kind.parent_kind.name)
parts.append(kind.name)
@@ -63,21 +63,35 @@ def GetCppPodType(kind):
return _kind_to_cpp_type[kind]
def GetCppArrayArgWrapperType(kind):
- if isinstance(kind, (mojom.Struct, mojom.Enum)):
+ if isinstance(kind, mojom.Enum):
return GetNameForKind(kind)
+ if isinstance(kind, mojom.Struct):
+ return "%sPtr" % GetNameForKind(kind)
if isinstance(kind, mojom.Array):
- return "mojo::Array<%s >" % GetCppArrayArgWrapperType(kind.kind)
+ return "mojo::Array<%s> " % GetCppArrayArgWrapperType(kind.kind)
if isinstance(kind, mojom.Interface):
raise Exception("Arrays of interfaces not yet supported!")
if kind.spec == 's':
return "mojo::String"
+ if kind.spec == 'h':
+ return "mojo::ScopedHandle"
+ if kind.spec == 'h:d:c':
+ return "mojo::ScopedDataPipeConsumerHandle"
+ if kind.spec == 'h:d:p':
+ return "mojo::ScopedDataPipeProducerHandle"
+ if kind.spec == 'h:m':
+ return "mojo::ScopedMessagePipeHandle"
+ if kind.spec == 'h:s':
+ return "mojo::ScopedSharedBufferHandle"
return _kind_to_cpp_type[kind]
def GetCppResultWrapperType(kind):
- if isinstance(kind, (mojom.Struct, mojom.Enum)):
+ if isinstance(kind, mojom.Enum):
return GetNameForKind(kind)
+ if isinstance(kind, mojom.Struct):
+ return "%sPtr" % GetNameForKind(kind)
if isinstance(kind, mojom.Array):
- return "mojo::Array<%s >" % GetCppArrayArgWrapperType(kind.kind)
+ return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
if isinstance(kind, mojom.Interface):
return "%sPtr" % kind.name
if kind.spec == 's':
@@ -95,29 +109,39 @@ def GetCppResultWrapperType(kind):
return _kind_to_cpp_type[kind]
def GetCppWrapperType(kind):
- if isinstance(kind, (mojom.Struct, mojom.Enum)):
+ if isinstance(kind, mojom.Enum):
return GetNameForKind(kind)
+ if isinstance(kind, mojom.Struct):
+ return "%sPtr" % GetNameForKind(kind)
if isinstance(kind, mojom.Array):
- return "mojo::Array<%s >" % GetCppArrayArgWrapperType(kind.kind)
+ return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
if isinstance(kind, mojom.Interface):
- return "mojo::Passable<mojo::MessagePipeHandle>"
+ return "mojo::ScopedMessagePipeHandle"
if kind.spec == 's':
return "mojo::String"
- if generator.IsHandleKind(kind):
- return "mojo::Passable<%s>" % _kind_to_cpp_type[kind]
+ if kind.spec == 'h':
+ return "mojo::ScopedHandle"
+ if kind.spec == 'h:d:c':
+ return "mojo::ScopedDataPipeConsumerHandle"
+ if kind.spec == 'h:d:p':
+ return "mojo::ScopedDataPipeProducerHandle"
+ if kind.spec == 'h:m':
+ return "mojo::ScopedMessagePipeHandle"
+ if kind.spec == 'h:s':
+ return "mojo::ScopedSharedBufferHandle"
return _kind_to_cpp_type[kind]
def GetCppConstWrapperType(kind):
if isinstance(kind, mojom.Struct):
- return "const %s&" % GetNameForKind(kind)
+ return "%sPtr" % GetNameForKind(kind)
if isinstance(kind, mojom.Array):
- return "const mojo::Array<%s >&" % GetCppArrayArgWrapperType(kind.kind)
+ return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
if isinstance(kind, mojom.Interface):
return "%sPtr" % kind.name
if isinstance(kind, mojom.Enum):
return GetNameForKind(kind)
if kind.spec == 's':
- return "const mojo::String&"
+ return "mojo::String"
if kind.spec == 'h':
return "mojo::ScopedHandle"
if kind.spec == 'h:d:c':
@@ -177,6 +201,15 @@ def HasCallbacks(interface):
return True
return False
+def ShouldInlineStruct(struct):
+ # TODO(darin): Base this on the size of the wrapper class.
+ if len(struct.fields) > 4:
+ return False
+ for field in struct.fields:
+ if isinstance(field.kind, (mojom.Array, mojom.Struct)):
+ return False
+ return True
+
_HEADER_SIZE = 8
class Generator(generator.Generator):
@@ -191,7 +224,9 @@ class Generator(generator.Generator):
"expression_to_text": ExpressionToText,
"get_pad": pack.GetPad,
"has_callbacks": HasCallbacks,
+ "should_inline": ShouldInlineStruct,
"is_enum_kind": generator.IsEnumKind,
+ "is_move_only_kind": generator.IsMoveOnlyKind,
"is_handle_kind": generator.IsHandleKind,
"is_interface_kind": generator.IsInterfaceKind,
"is_object_kind": generator.IsObjectKind,

Powered by Google App Engine
This is Rietveld 408576698