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.
5 #ifndef MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_
6 #define MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_
10 #include "mojo/public/bindings/lib/connector.h"
11 #include "mojo/public/system/macros.h"
15 // A RemotePtr is a smart-pointer for managing the connection of a message pipe
16 // to an interface proxy.
20 // On the client side of a service, RemotePtr might be used like so:
22 // class FooClientImpl : public FooClientStub {
24 // explicit FooClientImpl(const mojo::MessagePipeHandle& message_pipe)
25 // : foo_(message_pipe, this) {
28 // virtual void Pong() {
32 // mojo::RemotePtr<Foo> foo_;
35 // On the implementation side of a service, RemotePtr might be used like so:
37 // class FooImpl : public FooStub {
39 // explicit FooImpl(const mojo::MessagePipeHandle& message_pipe)
40 // : client_(message_pipe, this) {
42 // virtual void Ping() {
46 // mojo::RemotePtr<FooClient> client_;
52 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(RemotePtr, RValue);
55 RemotePtr() : state_(NULL) {}
56 explicit RemotePtr(ScopedMessagePipeHandle message_pipe,
57 typename S::_Peer* peer = NULL,
58 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter())
59 : state_(new State(message_pipe.Pass(), peer, waiter)) {
62 // Move-only constructor and operator=.
63 RemotePtr(RValue other) : state_(other.object->release()) {}
64 RemotePtr& operator=(RValue other) {
65 state_ = other.object->release();
73 bool is_null() const {
79 return &state_->proxy;
91 void reset(ScopedMessagePipeHandle message_pipe,
92 typename S::_Peer* peer = NULL,
93 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
95 state_ = new State(message_pipe.Pass(), peer, waiter);
98 bool encountered_error() const {
100 return state_->connector.encountered_error();
105 State(ScopedMessagePipeHandle message_pipe, typename S::_Peer* peer,
106 MojoAsyncWaiter* waiter)
107 : connector(message_pipe.Pass(), waiter),
111 connector.SetIncomingReceiver(&stub);
113 internal::Connector connector;
114 typename S::_Proxy proxy;
115 typename S::_Peer::_Stub stub;
119 State* state = state_;
129 #endif // MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_