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