| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 // This file provides a C++ wrapping around the Mojo C API for message pipes, |
| 6 // replacing the prefix of "Mojo" with a "mojo" namespace, and using more |
| 7 // strongly-typed representations of |MojoHandle|s. |
| 8 // |
| 9 // Please see "mojo/public/c/system/message_pipe.h" for complete documentation |
| 10 // of the API. |
| 11 |
| 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ | 12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ |
| 6 #define MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ | 13 #define MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ |
| 7 | 14 |
| 8 #include <assert.h> | 15 #include <assert.h> |
| 9 | 16 |
| 10 #include "mojo/public/c/system/message_pipe.h" | 17 #include "mojo/public/c/system/message_pipe.h" |
| 11 #include "mojo/public/cpp/system/handle.h" | 18 #include "mojo/public/cpp/system/handle.h" |
| 12 #include "mojo/public/cpp/system/macros.h" | 19 #include "mojo/public/cpp/system/macros.h" |
| 13 | 20 |
| 14 namespace mojo { | 21 namespace mojo { |
| 15 | 22 |
| 16 // MessagePipeHandle ----------------------------------------------------------- | 23 // A strongly-typed representation of a |MojoHandle| to one end of a message |
| 17 | 24 // pipe. |
| 18 class MessagePipeHandle : public Handle { | 25 class MessagePipeHandle : public Handle { |
| 19 public: | 26 public: |
| 20 MessagePipeHandle() {} | 27 MessagePipeHandle() {} |
| 21 explicit MessagePipeHandle(MojoHandle value) : Handle(value) {} | 28 explicit MessagePipeHandle(MojoHandle value) : Handle(value) {} |
| 22 | 29 |
| 23 // Copying and assignment allowed. | 30 // Copying and assignment allowed. |
| 24 }; | 31 }; |
| 25 | 32 |
| 26 static_assert(sizeof(MessagePipeHandle) == sizeof(Handle), | 33 static_assert(sizeof(MessagePipeHandle) == sizeof(Handle), |
| 27 "Bad size for C++ MessagePipeHandle"); | 34 "Bad size for C++ MessagePipeHandle"); |
| 28 | 35 |
| 29 typedef ScopedHandleBase<MessagePipeHandle> ScopedMessagePipeHandle; | 36 typedef ScopedHandleBase<MessagePipeHandle> ScopedMessagePipeHandle; |
| 30 static_assert(sizeof(ScopedMessagePipeHandle) == sizeof(MessagePipeHandle), | 37 static_assert(sizeof(ScopedMessagePipeHandle) == sizeof(MessagePipeHandle), |
| 31 "Bad size for C++ ScopedMessagePipeHandle"); | 38 "Bad size for C++ ScopedMessagePipeHandle"); |
| 32 | 39 |
| 40 // Creates a message pipe. See |MojoCreateMessagePipe()| for complete |
| 41 // documentation. |
| 33 inline MojoResult CreateMessagePipe(const MojoCreateMessagePipeOptions* options, | 42 inline MojoResult CreateMessagePipe(const MojoCreateMessagePipeOptions* options, |
| 34 ScopedMessagePipeHandle* message_pipe0, | 43 ScopedMessagePipeHandle* message_pipe0, |
| 35 ScopedMessagePipeHandle* message_pipe1) { | 44 ScopedMessagePipeHandle* message_pipe1) { |
| 36 assert(message_pipe0); | 45 assert(message_pipe0); |
| 37 assert(message_pipe1); | 46 assert(message_pipe1); |
| 38 MessagePipeHandle handle0; | 47 MessagePipeHandle handle0; |
| 39 MessagePipeHandle handle1; | 48 MessagePipeHandle handle1; |
| 40 MojoResult rv = MojoCreateMessagePipe( | 49 MojoResult rv = MojoCreateMessagePipe( |
| 41 options, handle0.mutable_value(), handle1.mutable_value()); | 50 options, handle0.mutable_value(), handle1.mutable_value()); |
| 42 // Reset even on failure (reduces the chances that a "stale"/incorrect handle | 51 // Reset even on failure (reduces the chances that a "stale"/incorrect handle |
| 43 // will be used). | 52 // will be used). |
| 44 message_pipe0->reset(handle0); | 53 message_pipe0->reset(handle0); |
| 45 message_pipe1->reset(handle1); | 54 message_pipe1->reset(handle1); |
| 46 return rv; | 55 return rv; |
| 47 } | 56 } |
| 48 | 57 |
| 49 // These "raw" versions fully expose the underlying API, but don't help with | 58 // The following "...Raw" versions fully expose the underlying API, and don't |
| 50 // ownership of handles (especially when writing messages). | 59 // help with ownership of handles (especially when writing messages). It is |
| 51 // TODO(vtl): Write "baked" versions. | 60 // expected that in most cases these methods will be called through generated |
| 61 // bindings anyway. |
| 62 // TODO(vtl): Write friendlier versions of these functions (using scoped |
| 63 // handles and/or vectors) if there is a demonstrated need for them. |
| 64 |
| 65 // Writes to a message pipe. If handles are attached, on success the handles |
| 66 // will no longer be valid (the receiver will receive equivalent, but logically |
| 67 // different, handles). See |MojoWriteMessage()| for complete documentation. |
| 52 inline MojoResult WriteMessageRaw(MessagePipeHandle message_pipe, | 68 inline MojoResult WriteMessageRaw(MessagePipeHandle message_pipe, |
| 53 const void* bytes, | 69 const void* bytes, |
| 54 uint32_t num_bytes, | 70 uint32_t num_bytes, |
| 55 const MojoHandle* handles, | 71 const MojoHandle* handles, |
| 56 uint32_t num_handles, | 72 uint32_t num_handles, |
| 57 MojoWriteMessageFlags flags) { | 73 MojoWriteMessageFlags flags) { |
| 58 return MojoWriteMessage( | 74 return MojoWriteMessage( |
| 59 message_pipe.value(), bytes, num_bytes, handles, num_handles, flags); | 75 message_pipe.value(), bytes, num_bytes, handles, num_handles, flags); |
| 60 } | 76 } |
| 61 | 77 |
| 78 // Reads from a message pipe. See |MojoReadMessage()| for complete |
| 79 // documentation. |
| 62 inline MojoResult ReadMessageRaw(MessagePipeHandle message_pipe, | 80 inline MojoResult ReadMessageRaw(MessagePipeHandle message_pipe, |
| 63 void* bytes, | 81 void* bytes, |
| 64 uint32_t* num_bytes, | 82 uint32_t* num_bytes, |
| 65 MojoHandle* handles, | 83 MojoHandle* handles, |
| 66 uint32_t* num_handles, | 84 uint32_t* num_handles, |
| 67 MojoReadMessageFlags flags) { | 85 MojoReadMessageFlags flags) { |
| 68 return MojoReadMessage( | 86 return MojoReadMessage( |
| 69 message_pipe.value(), bytes, num_bytes, handles, num_handles, flags); | 87 message_pipe.value(), bytes, num_bytes, handles, num_handles, flags); |
| 70 } | 88 } |
| 71 | 89 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 92 MOJO_ALLOW_UNUSED_LOCAL(result); | 110 MOJO_ALLOW_UNUSED_LOCAL(result); |
| 93 assert(result == MOJO_RESULT_OK); | 111 assert(result == MOJO_RESULT_OK); |
| 94 } | 112 } |
| 95 | 113 |
| 96 inline MessagePipe::~MessagePipe() { | 114 inline MessagePipe::~MessagePipe() { |
| 97 } | 115 } |
| 98 | 116 |
| 99 } // namespace mojo | 117 } // namespace mojo |
| 100 | 118 |
| 101 #endif // MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ | 119 #endif // MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ |
| OLD | NEW |