Skip to content

Commit

Permalink
Mojo: Make C++ CreateMessagePipe() wrapper return result.
Browse files Browse the repository at this point in the history
(Also make declaration order match that of DataPipe's -- handle, then
scoped handle, then functions, and then creation wrapper.)

R=sky@chromium.org

Review URL: https://codereview.chromium.org/129793014

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@250812 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
viettrungluu@chromium.org committed Feb 12, 2014
1 parent 5feb6cd commit 0c25942
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions mojo/public/system/core_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,37 +190,19 @@ MOJO_COMPILE_ASSERT(sizeof(ScopedMessagePipeHandle) ==
sizeof(MessagePipeHandle),
bad_size_for_cpp_ScopedMessagePipeHandle);

// TODO(vtl): In C++11, we could instead return a pair of
// |ScopedMessagePipeHandle|s.
inline void CreateMessagePipe(ScopedMessagePipeHandle* message_pipe0,
ScopedMessagePipeHandle* message_pipe1) {
inline MojoResult CreateMessagePipe(ScopedMessagePipeHandle* message_pipe0,
ScopedMessagePipeHandle* message_pipe1) {
assert(message_pipe0);
assert(message_pipe1);
MessagePipeHandle h0;
MessagePipeHandle h1;
MojoResult result MOJO_ALLOW_UNUSED =
MojoCreateMessagePipe(h0.mutable_value(), h1.mutable_value());
assert(result == MOJO_RESULT_OK);
message_pipe0->reset(h0);
message_pipe1->reset(h1);
}

// A wrapper class that automatically creates a message pipe and owns both
// handles.
class MessagePipe {
public:
MessagePipe();
~MessagePipe();

ScopedMessagePipeHandle handle0;
ScopedMessagePipeHandle handle1;
};

inline MessagePipe::MessagePipe() {
CreateMessagePipe(&handle0, &handle1);
}

inline MessagePipe::~MessagePipe() {
MessagePipeHandle handle0;
MessagePipeHandle handle1;
MojoResult rv = MojoCreateMessagePipe(handle0.mutable_value(),
handle1.mutable_value());
// Reset even on failure (reduces the chances that a "stale"/incorrect handle
// will be used).
message_pipe0->reset(handle0);
message_pipe1->reset(handle1);
return rv;
}

// These "raw" versions fully expose the underlying API, but don't help with
Expand All @@ -246,6 +228,25 @@ inline MojoResult ReadMessageRaw(MessagePipeHandle message_pipe,
num_handles, flags);
}

// A wrapper class that automatically creates a message pipe and owns both
// handles.
class MessagePipe {
public:
MessagePipe();
~MessagePipe();

ScopedMessagePipeHandle handle0;
ScopedMessagePipeHandle handle1;
};

inline MessagePipe::MessagePipe() {
MojoResult result MOJO_ALLOW_UNUSED = CreateMessagePipe(&handle0, &handle1);
assert(result == MOJO_RESULT_OK);
}

inline MessagePipe::~MessagePipe() {
}

// DataPipeProducerHandle and DataPipeConsumerHandle ---------------------------

class DataPipeProducerHandle : public Handle {
Expand Down Expand Up @@ -353,11 +354,15 @@ class DataPipe {
};

inline DataPipe::DataPipe() {
CreateDataPipe(NULL, &producer_handle, &consumer_handle);
MojoResult result MOJO_ALLOW_UNUSED =
CreateDataPipe(NULL, &producer_handle, &consumer_handle);
assert(result == MOJO_RESULT_OK);
}

inline DataPipe::DataPipe(const MojoCreateDataPipeOptions& options) {
CreateDataPipe(&options, &producer_handle, &consumer_handle);
MojoResult result MOJO_ALLOW_UNUSED =
CreateDataPipe(&options, &producer_handle, &consumer_handle);
assert(result == MOJO_RESULT_OK);
}

inline DataPipe::~DataPipe() {
Expand Down

0 comments on commit 0c25942

Please sign in to comment.