OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_CPP_BINDINGS_INTERFACE_PTR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
7 | 7 |
8 #include <assert.h> | 8 #include <assert.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 | 11 |
12 #include "mojo/public/cpp/bindings/error_handler.h" | 12 #include "mojo/public/cpp/bindings/error_handler.h" |
13 #include "mojo/public/cpp/bindings/lib/interface_ptr_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/interface_ptr_internal.h" |
14 #include "mojo/public/cpp/system/macros.h" | 14 #include "mojo/public/cpp/system/macros.h" |
15 | 15 |
16 namespace mojo { | 16 namespace mojo { |
17 class ErrorHandler; | 17 class ErrorHandler; |
18 | 18 |
19 // InterfacePtr represents a proxy to a remote instance of an interface. | 19 // InterfacePtr represents a proxy to a remote instance of an interface. |
20 template <typename Interface> | 20 template <typename Interface> |
21 class InterfacePtr { | 21 class InterfacePtr { |
22 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InterfacePtr, RValue) | 22 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InterfacePtr, RValue) |
23 public: | 23 public: |
24 InterfacePtr() {} | 24 InterfacePtr() {} |
25 | 25 |
26 InterfacePtr(RValue other) { | 26 InterfacePtr(RValue other) { |
27 other.object->internal_state_.Swap(&internal_state_); | 27 internal_state_.Swap(&other.object->internal_state_); |
28 } | 28 } |
29 InterfacePtr& operator=(RValue other) { | 29 InterfacePtr& operator=(RValue other) { |
30 other.object->internal_state_.Swap(&internal_state_); | 30 reset(); |
| 31 internal_state_.Swap(&other.object->internal_state_); |
31 return *this; | 32 return *this; |
32 } | 33 } |
33 | 34 |
34 ~InterfacePtr() {} | 35 ~InterfacePtr() {} |
35 | 36 |
36 Interface* get() const { | 37 Interface* get() const { |
37 return internal_state_.instance(); | 38 return internal_state_.instance(); |
38 } | 39 } |
39 Interface* operator->() const { return get(); } | 40 Interface* operator->() const { return get(); } |
40 Interface* operator*() const { return get(); } | 41 Interface& operator*() const { return *get(); } |
41 | 42 |
42 void reset() { | 43 void reset() { |
43 State doomed; | 44 State doomed; |
44 internal_state_.Swap(&doomed); | 45 internal_state_.Swap(&doomed); |
45 } | 46 } |
46 | 47 |
47 // This method configures the InterfacePtr<..> to be a proxy to a remote | 48 // This method configures the InterfacePtr<..> to be a proxy to a remote |
48 // object on the other end of the given pipe. | 49 // object on the other end of the given pipe. |
49 // | 50 // |
50 // The proxy is bound to the current thread, which means its methods may | 51 // The proxy is bound to the current thread, which means its methods may |
(...skipping 26 matching lines...) Expand all Loading... |
77 // connection error on the underlying pipe. The callback runs asynchronously | 78 // connection error on the underlying pipe. The callback runs asynchronously |
78 // from the current message loop. | 79 // from the current message loop. |
79 void set_error_handler(ErrorHandler* error_handler) { | 80 void set_error_handler(ErrorHandler* error_handler) { |
80 assert(internal_state_.router()); | 81 assert(internal_state_.router()); |
81 internal_state_.router()->set_error_handler(error_handler); | 82 internal_state_.router()->set_error_handler(error_handler); |
82 } | 83 } |
83 | 84 |
84 // Returns the underlying message pipe handle (if any) and resets the | 85 // Returns the underlying message pipe handle (if any) and resets the |
85 // InterfacePtr<..> to its uninitialized state. This method is helpful if you | 86 // InterfacePtr<..> to its uninitialized state. This method is helpful if you |
86 // need to move a proxy to another thread. See related notes for Bind. | 87 // need to move a proxy to another thread. See related notes for Bind. |
87 ScopedMessagePipeHandle ResetAndReturnMessagePipe() { | 88 ScopedMessagePipeHandle PassMessagePipe() { |
88 State state; | 89 State state; |
89 internal_state_.Swap(&state); | 90 internal_state_.Swap(&state); |
90 return state.router() ? | 91 return state.router() ? |
91 state.router()->ReleaseMessagePipe() : ScopedMessagePipeHandle(); | 92 state.router()->PassMessagePipe() : ScopedMessagePipeHandle(); |
92 } | 93 } |
93 | 94 |
94 // DO NOT USE. Exposed only for internal use and for testing. | 95 // DO NOT USE. Exposed only for internal use and for testing. |
95 internal::InterfacePtrState<Interface>* internal_state() { | 96 internal::InterfacePtrState<Interface>* internal_state() { |
96 return &internal_state_; | 97 return &internal_state_; |
97 } | 98 } |
98 | 99 |
99 private: | 100 private: |
100 typedef internal::InterfacePtrState<Interface> State; | 101 typedef internal::InterfacePtrState<Interface> State; |
101 State internal_state_; | 102 State internal_state_; |
(...skipping 10 matching lines...) Expand all Loading... |
112 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { | 113 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { |
113 InterfacePtr<Interface> ptr; | 114 InterfacePtr<Interface> ptr; |
114 if (handle.is_valid()) | 115 if (handle.is_valid()) |
115 ptr.Bind(handle.Pass(), waiter); | 116 ptr.Bind(handle.Pass(), waiter); |
116 return ptr.Pass(); | 117 return ptr.Pass(); |
117 } | 118 } |
118 | 119 |
119 } // namespace mojo | 120 } // namespace mojo |
120 | 121 |
121 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 122 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
OLD | NEW |