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" |
11 #include "mojo/public/system/macros.h" | 11 #include "mojo/public/system/macros.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 | 14 |
15 // A RemotePtr is a smart-pointer for managing the connection of a message pipe | 15 // A RemotePtr is a smart-pointer for managing the connection of a message pipe |
16 // to an interface proxy. | 16 // to an interface proxy. |
17 // | 17 // |
18 // EXAMPLE | 18 // EXAMPLE |
19 // | 19 // |
20 // On the client side of a service, RemotePtr might be used like so: | 20 // On the client side of a service, RemotePtr might be used like so: |
21 // | 21 // |
22 // class FooClientImpl : public FooClientStub { | 22 // class FooClientImpl : public FooClientStub { |
23 // public: | 23 // public: |
24 // explicit FooClientImpl(const mojo::MessagePipeHandle& message_pipe) | 24 // explicit FooClientImpl(const mojo::MessagePipeHandle& message_pipe) |
25 // : foo_(message_pipe) { | 25 // : foo_(message_pipe, this) { |
26 // foo_.SetPeer(this); | |
27 // foo_.Ping(); | 26 // foo_.Ping(); |
28 // } | 27 // } |
29 // virtual void Pong() { | 28 // virtual void Pong() { |
30 // ... | 29 // ... |
31 // } | 30 // } |
32 // private: | 31 // private: |
33 // mojo::RemotePtr<Foo> foo_; | 32 // mojo::RemotePtr<Foo> foo_; |
34 // }; | 33 // }; |
35 // | 34 // |
36 // On the implementation side of a service, RemotePtr might be used like so: | 35 // On the implementation side of a service, RemotePtr might be used like so: |
37 // | 36 // |
38 // class FooImpl : public FooStub { | 37 // class FooImpl : public FooStub { |
39 // public: | 38 // public: |
40 // explicit FooImpl(const mojo::MessagePipeHandle& message_pipe) | 39 // explicit FooImpl(const mojo::MessagePipeHandle& message_pipe) |
41 // : client_(message_pipe) { | 40 // : client_(message_pipe, this) { |
42 // client_.SetPeer(this); | |
43 // } | 41 // } |
44 // virtual void Ping() { | 42 // virtual void Ping() { |
45 // client_->Pong(); | 43 // client_->Pong(); |
46 // } | 44 // } |
47 // private: | 45 // private: |
48 // mojo::RemotePtr<FooClient> client_; | 46 // mojo::RemotePtr<FooClient> client_; |
49 // }; | 47 // }; |
50 // | 48 // |
51 template <typename S> | 49 template <typename S> |
52 class RemotePtr { | 50 class RemotePtr { |
53 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(RemotePtr, RValue); | 51 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(RemotePtr, RValue); |
54 | 52 |
55 public: | 53 public: |
56 RemotePtr() : state_(NULL) {} | 54 RemotePtr() : state_(NULL) {} |
57 explicit RemotePtr(ScopedMessagePipeHandle message_pipe) | 55 explicit RemotePtr(ScopedMessagePipeHandle message_pipe, |
58 : state_(new State(message_pipe.Pass())) { | 56 typename S::_Peer* peer = NULL) |
| 57 : state_(new State(message_pipe.Pass(), peer)) { |
59 } | 58 } |
60 | 59 |
61 // Move-only constructor and operator=. | 60 // Move-only constructor and operator=. |
62 RemotePtr(RValue other) : state_(other.object->release()) {} | 61 RemotePtr(RValue other) : state_(other.object->release()) {} |
63 RemotePtr& operator=(RValue other) { | 62 RemotePtr& operator=(RValue other) { |
64 state_ = other.object->release(); | 63 state_ = other.object->release(); |
65 return *this; | 64 return *this; |
66 } | 65 } |
67 | 66 |
68 ~RemotePtr() { | 67 ~RemotePtr() { |
(...skipping 11 matching lines...) Expand all Loading... |
80 | 79 |
81 S* operator->() { | 80 S* operator->() { |
82 return get(); | 81 return get(); |
83 } | 82 } |
84 | 83 |
85 void reset() { | 84 void reset() { |
86 delete state_; | 85 delete state_; |
87 state_ = NULL; | 86 state_ = NULL; |
88 } | 87 } |
89 | 88 |
90 void reset(ScopedMessagePipeHandle message_pipe) { | 89 void reset(ScopedMessagePipeHandle message_pipe, |
| 90 typename S::_Peer* peer = NULL) { |
91 delete state_; | 91 delete state_; |
92 state_ = new State(message_pipe.Pass()); | 92 state_ = new State(message_pipe.Pass(), peer); |
93 } | 93 } |
94 | 94 |
95 bool encountered_error() const { | 95 bool encountered_error() const { |
96 assert(state_); | 96 assert(state_); |
97 return state_->connector.encountered_error(); | 97 return state_->connector.encountered_error(); |
98 } | 98 } |
99 | 99 |
100 void SetPeer(typename S::_Peer::_Stub* peer) { | |
101 assert(state_); | |
102 state_->connector.SetIncomingReceiver(peer); | |
103 } | |
104 | |
105 private: | 100 private: |
106 struct State { | 101 struct State { |
107 State(ScopedMessagePipeHandle message_pipe) | 102 State(ScopedMessagePipeHandle message_pipe, typename S::_Peer* peer) |
108 : connector(message_pipe.Pass()), | 103 : connector(message_pipe.Pass()), |
109 proxy(&connector) { | 104 proxy(&connector), |
| 105 stub(peer) { |
| 106 if (peer) |
| 107 connector.SetIncomingReceiver(&stub); |
110 } | 108 } |
111 internal::Connector connector; | 109 internal::Connector connector; |
112 typename S::_Proxy proxy; | 110 typename S::_Proxy proxy; |
| 111 typename S::_Peer::_Stub stub; |
113 }; | 112 }; |
114 | 113 |
115 State* release() { | 114 State* release() { |
116 State* state = state_; | 115 State* state = state_; |
117 state_ = NULL; | 116 state_ = NULL; |
118 return state; | 117 return state; |
119 } | 118 } |
120 | 119 |
121 State* state_; | 120 State* state_; |
122 }; | 121 }; |
123 | 122 |
124 } // namespace mojo | 123 } // namespace mojo |
125 | 124 |
126 #endif // MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ | 125 #endif // MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ |
OLD | NEW |