| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ | 5 #ifndef MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ |
| 6 #define MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ | 6 #define MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ |
| 7 | 7 |
| 8 #include <assert.h> | 8 #include <assert.h> |
| 9 | 9 |
| 10 #include "mojo/public/bindings/lib/connector.h" | 10 #include "mojo/public/bindings/lib/connector.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // } | 41 // } |
| 42 // virtual void Ping() { | 42 // virtual void Ping() { |
| 43 // client_->Pong(); | 43 // client_->Pong(); |
| 44 // } | 44 // } |
| 45 // private: | 45 // private: |
| 46 // mojo::RemotePtr<FooClient> client_; | 46 // mojo::RemotePtr<FooClient> client_; |
| 47 // }; | 47 // }; |
| 48 // | 48 // |
| 49 template <typename S> | 49 template <typename S> |
| 50 class RemotePtr { | 50 class RemotePtr { |
| 51 struct State; |
| 51 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(RemotePtr, RValue); | 52 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(RemotePtr, RValue); |
| 52 | 53 |
| 53 public: | 54 public: |
| 54 RemotePtr() : state_(NULL) {} | 55 RemotePtr() : state_(NULL) {} |
| 55 explicit RemotePtr(ScopedMessagePipeHandle message_pipe, | 56 explicit RemotePtr(ScopedMessagePipeHandle message_pipe, |
| 56 typename S::_Peer* peer = NULL) | 57 typename S::_Peer* peer = NULL, |
| 57 : state_(new State(message_pipe.Pass(), peer)) { | 58 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) |
| 59 : state_(new State(message_pipe.Pass(), peer, waiter)) { |
| 58 } | 60 } |
| 59 | 61 |
| 60 // Move-only constructor and operator=. | 62 // Move-only constructor and operator=. |
| 61 RemotePtr(RValue other) : state_(other.object->release()) {} | 63 RemotePtr(RValue other) : state_(other.object->release()) {} |
| 62 RemotePtr& operator=(RValue other) { | 64 RemotePtr& operator=(RValue other) { |
| 63 state_ = other.object->release(); | 65 state_ = other.object->release(); |
| 64 return *this; | 66 return *this; |
| 65 } | 67 } |
| 66 | 68 |
| 67 ~RemotePtr() { | 69 ~RemotePtr() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 80 S* operator->() { | 82 S* operator->() { |
| 81 return get(); | 83 return get(); |
| 82 } | 84 } |
| 83 | 85 |
| 84 void reset() { | 86 void reset() { |
| 85 delete state_; | 87 delete state_; |
| 86 state_ = NULL; | 88 state_ = NULL; |
| 87 } | 89 } |
| 88 | 90 |
| 89 void reset(ScopedMessagePipeHandle message_pipe, | 91 void reset(ScopedMessagePipeHandle message_pipe, |
| 90 typename S::_Peer* peer = NULL) { | 92 typename S::_Peer* peer = NULL, |
| 93 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { |
| 91 delete state_; | 94 delete state_; |
| 92 state_ = new State(message_pipe.Pass(), peer); | 95 state_ = new State(message_pipe.Pass(), peer, waiter); |
| 93 } | 96 } |
| 94 | 97 |
| 95 bool encountered_error() const { | 98 bool encountered_error() const { |
| 96 assert(state_); | 99 assert(state_); |
| 97 return state_->connector.encountered_error(); | 100 return state_->connector.encountered_error(); |
| 98 } | 101 } |
| 99 | 102 |
| 100 private: | 103 private: |
| 101 struct State { | 104 struct State { |
| 102 State(ScopedMessagePipeHandle message_pipe, typename S::_Peer* peer) | 105 State(ScopedMessagePipeHandle message_pipe, typename S::_Peer* peer, |
| 103 : connector(message_pipe.Pass()), | 106 MojoAsyncWaiter* waiter) |
| 107 : connector(message_pipe.Pass(), waiter), |
| 104 proxy(&connector), | 108 proxy(&connector), |
| 105 stub(peer) { | 109 stub(peer) { |
| 106 if (peer) | 110 if (peer) |
| 107 connector.SetIncomingReceiver(&stub); | 111 connector.SetIncomingReceiver(&stub); |
| 108 } | 112 } |
| 109 internal::Connector connector; | 113 internal::Connector connector; |
| 110 typename S::_Proxy proxy; | 114 typename S::_Proxy proxy; |
| 111 typename S::_Peer::_Stub stub; | 115 typename S::_Peer::_Stub stub; |
| 112 }; | 116 }; |
| 113 | 117 |
| 114 State* release() { | 118 State* release() { |
| 115 State* state = state_; | 119 State* state = state_; |
| 116 state_ = NULL; | 120 state_ = NULL; |
| 117 return state; | 121 return state; |
| 118 } | 122 } |
| 119 | 123 |
| 120 State* state_; | 124 State* state_; |
| 121 }; | 125 }; |
| 122 | 126 |
| 123 } // namespace mojo | 127 } // namespace mojo |
| 124 | 128 |
| 125 #endif // MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ | 129 #endif // MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ |
| OLD | NEW |