1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_
10 #include "mojo/public/c/system/message_pipe.h"
11 #include "mojo/public/cpp/system/handle.h"
12 #include "mojo/public/cpp/system/macros.h"
16 // MessagePipeHandle -----------------------------------------------------------
18 class MessagePipeHandle
: public Handle
{
20 MessagePipeHandle() {}
21 explicit MessagePipeHandle(MojoHandle value
) : Handle(value
) {}
23 // Copying and assignment allowed.
26 static_assert(sizeof(MessagePipeHandle
) == sizeof(Handle
),
27 "Bad size for C++ MessagePipeHandle");
29 typedef ScopedHandleBase
<MessagePipeHandle
> ScopedMessagePipeHandle
;
30 static_assert(sizeof(ScopedMessagePipeHandle
) == sizeof(MessagePipeHandle
),
31 "Bad size for C++ ScopedMessagePipeHandle");
33 inline MojoResult
CreateMessagePipe(const MojoCreateMessagePipeOptions
* options
,
34 ScopedMessagePipeHandle
* message_pipe0
,
35 ScopedMessagePipeHandle
* message_pipe1
) {
36 assert(message_pipe0
);
37 assert(message_pipe1
);
38 MessagePipeHandle handle0
;
39 MessagePipeHandle handle1
;
40 MojoResult rv
= MojoCreateMessagePipe(
41 options
, handle0
.mutable_value(), handle1
.mutable_value());
42 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
44 message_pipe0
->reset(handle0
);
45 message_pipe1
->reset(handle1
);
49 // These "raw" versions fully expose the underlying API, but don't help with
50 // ownership of handles (especially when writing messages).
51 // TODO(vtl): Write "baked" versions.
52 inline MojoResult
WriteMessageRaw(MessagePipeHandle message_pipe
,
55 const MojoHandle
* handles
,
57 MojoWriteMessageFlags flags
) {
58 return MojoWriteMessage(
59 message_pipe
.value(), bytes
, num_bytes
, handles
, num_handles
, flags
);
62 inline MojoResult
ReadMessageRaw(MessagePipeHandle message_pipe
,
66 uint32_t* num_handles
,
67 MojoReadMessageFlags flags
) {
68 return MojoReadMessage(
69 message_pipe
.value(), bytes
, num_bytes
, handles
, num_handles
, flags
);
72 // A wrapper class that automatically creates a message pipe and owns both
77 explicit MessagePipe(const MojoCreateMessagePipeOptions
& options
);
80 ScopedMessagePipeHandle handle0
;
81 ScopedMessagePipeHandle handle1
;
84 inline MessagePipe::MessagePipe() {
85 MojoResult result MOJO_ALLOW_UNUSED
=
86 CreateMessagePipe(nullptr, &handle0
, &handle1
);
87 assert(result
== MOJO_RESULT_OK
);
90 inline MessagePipe::MessagePipe(const MojoCreateMessagePipeOptions
& options
) {
91 MojoResult result MOJO_ALLOW_UNUSED
=
92 CreateMessagePipe(&options
, &handle0
, &handle1
);
93 assert(result
== MOJO_RESULT_OK
);
96 inline MessagePipe::~MessagePipe() {
101 #endif // MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_