Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: mojo/public/cpp/bindings/remote_ptr.h

Issue 265793015: Mojo: Replace RemotePtr with InterfacePtr and InterfaceImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_REMOTE_PTR_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_REMOTE_PTR_H_
7
8 #include <assert.h>
9
10 #include "mojo/public/cpp/bindings/interface.h"
11 #include "mojo/public/cpp/bindings/lib/router.h"
12 #include "mojo/public/cpp/system/macros.h"
13
14 namespace mojo {
15
16 // A RemotePtr is a smart-pointer for managing the connection of a message pipe
17 // to an interface proxy.
18 //
19 // EXAMPLE:
20 //
21 // Given foo.mojom containing the following interfaces:
22 //
23 // [Peer=FooClient]
24 // interface Foo {
25 // void Ping();
26 // };
27 //
28 // [Peer=Foo]
29 // interface FooClient {
30 // void Pong();
31 // };
32 //
33 // On the client side of a service, RemotePtr might be used like so:
34 //
35 // class FooClientImpl : public FooClient {
36 // public:
37 // explicit FooClientImpl(ScopedFooHandle handle)
38 // : foo_(handle.Pass(), this) {
39 // foo_.Ping();
40 // }
41 // virtual void Pong() {
42 // ...
43 // }
44 // private:
45 // mojo::RemotePtr<Foo> foo_;
46 // };
47 //
48 // On the implementation side of a service, RemotePtr might be used like so:
49 //
50 // class FooImpl : public Foo {
51 // public:
52 // explicit FooImpl(ScopedFooClientHandle handle)
53 // : client_(handle.Pass(), this) {
54 // }
55 // virtual void Ping() {
56 // client_->Pong();
57 // }
58 // private:
59 // mojo::RemotePtr<FooClient> client_;
60 // };
61 //
62 // NOTE:
63 //
64 // 1- It is valid to pass NULL for the peer if you are not interested in
65 // receiving incoming messages. Those messages will still be consumed.
66 //
67 // 2- You may optionally register an ErrorHandler on the RemotePtr to be
68 // notified if the peer has gone away. Alternatively, you may poll the
69 // |encountered_error()| method to check if the peer has gone away.
70 //
71 template <typename S>
72 class RemotePtr {
73 struct State;
74 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(RemotePtr, RValue)
75
76 public:
77 RemotePtr() : state_(NULL) {}
78 explicit RemotePtr(typename Interface<S>::ScopedHandle interface_handle,
79 typename S::_Peer* peer,
80 ErrorHandler* error_handler = NULL,
81 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter())
82 : state_(new State(ScopedMessagePipeHandle(interface_handle.Pass()), peer,
83 error_handler, waiter)) {
84 }
85
86 // Move-only constructor and operator=.
87 RemotePtr(RValue other) : state_(other.object->release()) {}
88 RemotePtr& operator=(RValue other) {
89 delete state_;
90 state_ = other.object->release();
91 return *this;
92 }
93
94 ~RemotePtr() {
95 delete state_;
96 }
97
98 bool is_null() const {
99 return !state_;
100 }
101
102 S* get() {
103 assert(state_);
104 return &state_->proxy;
105 }
106
107 S* operator->() {
108 return get();
109 }
110
111 void reset() {
112 delete state_;
113 state_ = NULL;
114 }
115
116 void reset(typename Interface<S>::ScopedHandle interface_handle,
117 typename S::_Peer* peer,
118 ErrorHandler* error_handler = NULL,
119 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
120 delete state_;
121 state_ = new State(ScopedMessagePipeHandle(interface_handle.Pass()), peer,
122 error_handler, waiter);
123 }
124
125 bool encountered_error() const {
126 assert(state_);
127 return state_->router.encountered_error();
128 }
129
130 internal::Router* router_for_testing() {
131 return &state_->router;
132 }
133
134 private:
135 struct State {
136 State(ScopedMessagePipeHandle message_pipe, typename S::_Peer* peer,
137 ErrorHandler* error_handler, MojoAsyncWaiter* waiter)
138 : router(message_pipe.Pass(), waiter),
139 proxy(&router),
140 stub(peer) {
141 router.set_error_handler(error_handler);
142 if (peer)
143 router.set_incoming_receiver(&stub);
144 }
145 internal::Router router;
146 typename S::_Proxy proxy;
147 typename S::_Peer::_Stub stub;
148 };
149
150 State* release() {
151 State* state = state_;
152 state_ = NULL;
153 return state;
154 }
155
156 State* state_;
157 };
158
159 } // namespace mojo
160
161 #endif // MOJO_PUBLIC_CPP_BINDINGS_REMOTE_PTR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698