OLD | NEW |
(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_BINDINGS_LIB_REMOTE_PTR_H_ |
| 6 #define MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ |
| 7 |
| 8 #include "mojo/public/bindings/lib/connector.h" |
| 9 |
| 10 namespace mojo { |
| 11 |
| 12 // A RemotePtr is a smart-pointer for managing the connection of a message pipe |
| 13 // to an interface proxy. |
| 14 // |
| 15 // EXAMPLE |
| 16 // |
| 17 // On the client side of a service, RemotePtr might be used like so: |
| 18 // |
| 19 // class FooClientImpl : public FooClientStub { |
| 20 // public: |
| 21 // explicit FooClientImpl(mojo::Handle message_pipe) |
| 22 // : foo_(message_pipe) { |
| 23 // foo_.SetPeer(this); |
| 24 // foo_.Ping(); |
| 25 // } |
| 26 // virtual void Pong() { |
| 27 // ... |
| 28 // } |
| 29 // private: |
| 30 // mojo::RemotePtr<Foo> foo_; |
| 31 // }; |
| 32 // |
| 33 // On the implementation side of a service, RemotePtr might be used like so: |
| 34 // |
| 35 // class FooImpl : public FooStub { |
| 36 // public: |
| 37 // explicit FooImpl(mojo::Handle message_pipe) |
| 38 // : client_(message_pipe) { |
| 39 // client_.SetPeer(this); |
| 40 // } |
| 41 // virtual void Ping() { |
| 42 // client_->Pong(); |
| 43 // } |
| 44 // private: |
| 45 // mojo::RemotePtr<FooClient> client_; |
| 46 // }; |
| 47 // |
| 48 template <typename S> |
| 49 class RemotePtr { |
| 50 public: |
| 51 explicit RemotePtr(Handle message_pipe) |
| 52 : connector_(message_pipe), |
| 53 proxy_(&connector_) { |
| 54 } |
| 55 |
| 56 S* get() { |
| 57 return &proxy_; |
| 58 } |
| 59 |
| 60 S* operator->() { |
| 61 return get(); |
| 62 } |
| 63 |
| 64 void SetPeer(typename S::_Peer::_Stub* peer) { |
| 65 connector_.SetIncomingReceiver(peer); |
| 66 } |
| 67 |
| 68 private: |
| 69 Connector connector_; |
| 70 typename S::_Proxy proxy_; |
| 71 |
| 72 MOJO_DISALLOW_COPY_AND_ASSIGN(RemotePtr); |
| 73 }; |
| 74 |
| 75 } // namespace mojo |
| 76 |
| 77 #endif // MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ |
OLD | NEW |