diff options
author | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-04 23:34:47 +0000 |
---|---|---|
committer | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-04 23:34:47 +0000 |
commit | 47596812be83756a3009b068c38113f898a7077a (patch) | |
tree | 4d6f6c27e83ac436f1fb6f6b3a31dec22e13d787 /mojo | |
parent | 86c0dceffa32d67d8a622db20a835d5f5ed8e536 (diff) | |
download | chromium_src-47596812be83756a3009b068c38113f898a7077a.zip chromium_src-47596812be83756a3009b068c38113f898a7077a.tar.gz chromium_src-47596812be83756a3009b068c38113f898a7077a.tar.bz2 |
Mojo: Add move semantics to RemotePtr<T>
Also, put Connector into the mojo::internal:: namespace.
BUG=325806
R=viettrungluu@chromium.org
Review URL: https://codereview.chromium.org/103193004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238814 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo')
-rw-r--r-- | mojo/public/bindings/lib/connector.cc | 2 | ||||
-rw-r--r-- | mojo/public/bindings/lib/connector.h | 2 | ||||
-rw-r--r-- | mojo/public/bindings/lib/remote_ptr.h | 58 | ||||
-rw-r--r-- | mojo/public/tests/bindings_connector_unittest.cc | 22 | ||||
-rw-r--r-- | mojo/public/tests/bindings_remote_ptr_unittest.cc | 32 |
5 files changed, 98 insertions, 18 deletions
diff --git a/mojo/public/bindings/lib/connector.cc b/mojo/public/bindings/lib/connector.cc index 6b701a7..872fbbf 100644 --- a/mojo/public/bindings/lib/connector.cc +++ b/mojo/public/bindings/lib/connector.cc @@ -10,6 +10,7 @@ #include <algorithm> namespace mojo { +namespace internal { // ---------------------------------------------------------------------------- @@ -173,4 +174,5 @@ void Connector::Callback::OnHandleReady(MojoResult result) { owner->OnHandleReady(this, result); } +} // namespace internal } // namespace mojo diff --git a/mojo/public/bindings/lib/connector.h b/mojo/public/bindings/lib/connector.h index 1fbf1d8..b235437 100644 --- a/mojo/public/bindings/lib/connector.h +++ b/mojo/public/bindings/lib/connector.h @@ -11,6 +11,7 @@ #include "mojo/public/system/core_cpp.h" namespace mojo { +namespace internal { // The Connector class is responsible for performing read/write operations on a // MessagePipe. It writes messages it receives through the MessageReceiver @@ -73,6 +74,7 @@ class Connector : public MessageReceiver { MOJO_DISALLOW_COPY_AND_ASSIGN(Connector); }; +} // namespace internal } // namespace mojo #endif // MOJO_PUBLIC_BINDINGS_LIB_CONNECTOR_H_ diff --git a/mojo/public/bindings/lib/remote_ptr.h b/mojo/public/bindings/lib/remote_ptr.h index 26056e9..58f77ad 100644 --- a/mojo/public/bindings/lib/remote_ptr.h +++ b/mojo/public/bindings/lib/remote_ptr.h @@ -5,7 +5,10 @@ #ifndef MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ #define MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ +#include <assert.h> + #include "mojo/public/bindings/lib/connector.h" +#include "mojo/public/system/macros.h" namespace mojo { @@ -47,29 +50,70 @@ namespace mojo { // template <typename S> class RemotePtr { + MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(RemotePtr, RValue); + public: + RemotePtr() : state_(NULL) {} explicit RemotePtr(ScopedMessagePipeHandle message_pipe) - : connector_(message_pipe.Pass()), - proxy_(&connector_) { + : state_(new State(message_pipe.Pass())) { + } + + // Move-only constructor and operator=. + RemotePtr(RValue other) : state_(other.object->release()) {} + RemotePtr& operator=(RValue other) { + state_ = other.object->release(); + return *this; + } + + ~RemotePtr() { + delete state_; + } + + bool is_valid() const { + return !!state_; } S* get() { - return &proxy_; + assert(state_); + return &state_->proxy; } S* operator->() { return get(); } + void reset() { + delete state_; + state_ = NULL; + } + + void reset(ScopedMessagePipeHandle message_pipe) { + delete state_; + state_ = new State(message_pipe.Pass()); + } + void SetPeer(typename S::_Peer::_Stub* peer) { - connector_.SetIncomingReceiver(peer); + assert(state_); + state_->connector.SetIncomingReceiver(peer); } private: - Connector connector_; - typename S::_Proxy proxy_; + struct State { + State(ScopedMessagePipeHandle message_pipe) + : connector(message_pipe.Pass()), + proxy(&connector) { + } + internal::Connector connector; + typename S::_Proxy proxy; + }; + + State* release() { + State* state = state_; + state_ = NULL; + return state; + } - MOJO_DISALLOW_COPY_AND_ASSIGN(RemotePtr); + State* state_; }; } // namespace mojo diff --git a/mojo/public/tests/bindings_connector_unittest.cc b/mojo/public/tests/bindings_connector_unittest.cc index 47052ab..737f26a 100644 --- a/mojo/public/tests/bindings_connector_unittest.cc +++ b/mojo/public/tests/bindings_connector_unittest.cc @@ -71,8 +71,8 @@ class BindingsConnectorTest : public testing::Test { }; TEST_F(BindingsConnectorTest, Basic) { - Connector connector0(handle0_.Pass()); - Connector connector1(handle1_.Pass()); + internal::Connector connector0(handle0_.Pass()); + internal::Connector connector1(handle1_.Pass()); const char kText[] = "hello world"; @@ -97,8 +97,8 @@ TEST_F(BindingsConnectorTest, Basic) { } TEST_F(BindingsConnectorTest, Basic_EarlyIncomingReceiver) { - Connector connector0(handle0_.Pass()); - Connector connector1(handle1_.Pass()); + internal::Connector connector0(handle0_.Pass()); + internal::Connector connector1(handle1_.Pass()); MessageAccumulator accumulator; connector1.SetIncomingReceiver(&accumulator); @@ -123,8 +123,8 @@ TEST_F(BindingsConnectorTest, Basic_EarlyIncomingReceiver) { } TEST_F(BindingsConnectorTest, Basic_TwoMessages) { - Connector connector0(handle0_.Pass()); - Connector connector1(handle1_.Pass()); + internal::Connector connector0(handle0_.Pass()); + internal::Connector connector1(handle1_.Pass()); const char* kText[] = { "hello", "world" }; @@ -155,7 +155,7 @@ TEST_F(BindingsConnectorTest, Basic_TwoMessages) { TEST_F(BindingsConnectorTest, WriteToClosedPipe) { // Leak this, so the closed handle isn't closed again. MojoHandle mojo_handle = handle0_.get().value(); - Connector* connector0 = new Connector(handle0_.Pass()); + internal::Connector* connector0 = new internal::Connector(handle0_.Pass()); const char kText[] = "hello world"; @@ -173,8 +173,8 @@ TEST_F(BindingsConnectorTest, WriteToClosedPipe) { // Enable this test once MojoWriteMessage supports passing handles. TEST_F(BindingsConnectorTest, MessageWithHandles) { - Connector connector0(handle0_.Pass()); - Connector connector1(handle1_.Pass()); + internal::Connector connector0(handle0_.Pass()); + internal::Connector connector1(handle1_.Pass()); const char kText[] = "hello world"; @@ -212,8 +212,8 @@ TEST_F(BindingsConnectorTest, MessageWithHandles) { smph.reset(MessagePipeHandle(message_received.handles[0].value())); message_received.handles[0] = Handle(); // |smph| now owns this handle. - Connector connector_received(smph.Pass()); - Connector connector_original(handles[1].Pass()); + internal::Connector connector_received(smph.Pass()); + internal::Connector connector_original(handles[1].Pass()); AllocMessage(kText, &message); diff --git a/mojo/public/tests/bindings_remote_ptr_unittest.cc b/mojo/public/tests/bindings_remote_ptr_unittest.cc index ebad72a..c236695 100644 --- a/mojo/public/tests/bindings_remote_ptr_unittest.cc +++ b/mojo/public/tests/bindings_remote_ptr_unittest.cc @@ -111,5 +111,37 @@ TEST_F(BindingsRemotePtrTest, EndToEnd) { EXPECT_EQ(10.0, calculator_ui.GetOutput()); } +TEST_F(BindingsRemotePtrTest, Movable) { + RemotePtr<math::Calculator> a; + RemotePtr<math::Calculator> b(pipe0_.Pass()); + + EXPECT_FALSE(a.is_valid()); + EXPECT_TRUE(b.is_valid()); + + a = b.Pass(); + + EXPECT_TRUE(a.is_valid()); + EXPECT_FALSE(b.is_valid()); +} + +TEST_F(BindingsRemotePtrTest, Resettable) { + RemotePtr<math::Calculator> a; + + EXPECT_FALSE(a.is_valid()); + + MessagePipeHandle handle = pipe0_.get(); + + a.reset(pipe0_.Pass()); + + EXPECT_TRUE(a.is_valid()); + + a.reset(); + + EXPECT_FALSE(a.is_valid()); + + // Test that handle was closed. + EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle)); +} + } // namespace test } // namespace mojo |