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 <stdint.h> | 8 #include <stdint.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 // This class is thread hostile, as is the local proxy it manages, while bound | 29 // This class is thread hostile, as is the local proxy it manages, while bound |
30 // to a message pipe. All calls to this class or the proxy should be from the | 30 // to a message pipe. All calls to this class or the proxy should be from the |
31 // same thread that bound it. If you need to move the proxy to a different | 31 // same thread that bound it. If you need to move the proxy to a different |
32 // thread, extract the InterfacePtrInfo (containing just the message pipe and | 32 // thread, extract the InterfacePtrInfo (containing just the message pipe and |
33 // any version information) using PassInterface(), pass it to a different | 33 // any version information) using PassInterface(), pass it to a different |
34 // thread, and create and bind a new InterfacePtr from that thread. If an | 34 // thread, and create and bind a new InterfacePtr from that thread. If an |
35 // InterfacePtr is not bound to a message pipe, it may be bound or destroyed on | 35 // InterfacePtr is not bound to a message pipe, it may be bound or destroyed on |
36 // any thread. | 36 // any thread. |
37 template <typename Interface> | 37 template <typename Interface> |
38 class InterfacePtr { | 38 class InterfacePtr { |
| 39 DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND(InterfacePtr); |
| 40 |
39 public: | 41 public: |
40 // Constructs an unbound InterfacePtr. | 42 // Constructs an unbound InterfacePtr. |
41 InterfacePtr() {} | 43 InterfacePtr() {} |
42 InterfacePtr(decltype(nullptr)) {} | 44 InterfacePtr(decltype(nullptr)) {} |
43 | 45 |
44 // Takes over the binding of another InterfacePtr. | 46 // Takes over the binding of another InterfacePtr. |
45 InterfacePtr(InterfacePtr&& other) { | 47 InterfacePtr(InterfacePtr&& other) { |
46 internal_state_.Swap(&other.internal_state_); | 48 internal_state_.Swap(&other.internal_state_); |
47 } | 49 } |
48 | 50 |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 | 194 |
193 // DO NOT USE. Exposed only for internal use and for testing. | 195 // DO NOT USE. Exposed only for internal use and for testing. |
194 internal::InterfacePtrState<Interface, Interface::PassesAssociatedKinds_>* | 196 internal::InterfacePtrState<Interface, Interface::PassesAssociatedKinds_>* |
195 internal_state() { | 197 internal_state() { |
196 return &internal_state_; | 198 return &internal_state_; |
197 } | 199 } |
198 | 200 |
199 // Allow InterfacePtr<> to be used in boolean expressions, but not | 201 // Allow InterfacePtr<> to be used in boolean expressions, but not |
200 // implicitly convertible to a real bool (which is dangerous). | 202 // implicitly convertible to a real bool (which is dangerous). |
201 private: | 203 private: |
202 // TODO(dcheng): Use an explicit conversion operator. | |
203 typedef internal::InterfacePtrState<Interface, | 204 typedef internal::InterfacePtrState<Interface, |
204 Interface::PassesAssociatedKinds_> | 205 Interface::PassesAssociatedKinds_> |
205 InterfacePtr::*Testable; | 206 InterfacePtr::*Testable; |
206 | 207 |
207 public: | 208 public: |
208 operator Testable() const { | 209 operator Testable() const { |
209 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ | 210 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ |
210 : nullptr; | 211 : nullptr; |
211 } | 212 } |
212 | 213 |
213 private: | 214 private: |
214 // Forbid the == and != operators explicitly, otherwise InterfacePtr will be | 215 // Forbid the == and != operators explicitly, otherwise InterfacePtr will be |
215 // converted to Testable to do == or != comparison. | 216 // converted to Testable to do == or != comparison. |
216 template <typename T> | 217 template <typename T> |
217 bool operator==(const InterfacePtr<T>& other) const = delete; | 218 bool operator==(const InterfacePtr<T>& other) const = delete; |
218 template <typename T> | 219 template <typename T> |
219 bool operator!=(const InterfacePtr<T>& other) const = delete; | 220 bool operator!=(const InterfacePtr<T>& other) const = delete; |
220 | 221 |
221 typedef internal::InterfacePtrState<Interface, | 222 typedef internal::InterfacePtrState<Interface, |
222 Interface::PassesAssociatedKinds_> State; | 223 Interface::PassesAssociatedKinds_> State; |
223 mutable State internal_state_; | 224 mutable State internal_state_; |
224 | |
225 DISALLOW_COPY_AND_ASSIGN(InterfacePtr); | |
226 }; | 225 }; |
227 | 226 |
228 // If |info| is valid (containing a valid message pipe handle), returns an | 227 // If |info| is valid (containing a valid message pipe handle), returns an |
229 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. | 228 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. |
230 template <typename Interface> | 229 template <typename Interface> |
231 InterfacePtr<Interface> MakeProxy( | 230 InterfacePtr<Interface> MakeProxy( |
232 InterfacePtrInfo<Interface> info, | 231 InterfacePtrInfo<Interface> info, |
233 scoped_refptr<base::SingleThreadTaskRunner> runner = | 232 scoped_refptr<base::SingleThreadTaskRunner> runner = |
234 base::ThreadTaskRunnerHandle::Get()) { | 233 base::ThreadTaskRunnerHandle::Get()) { |
235 InterfacePtr<Interface> ptr; | 234 InterfacePtr<Interface> ptr; |
236 if (info.is_valid()) | 235 if (info.is_valid()) |
237 ptr.Bind(std::move(info), std::move(runner)); | 236 ptr.Bind(std::move(info), std::move(runner)); |
238 return std::move(ptr); | 237 return std::move(ptr); |
239 } | 238 } |
240 | 239 |
241 } // namespace mojo | 240 } // namespace mojo |
242 | 241 |
243 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 242 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
OLD | NEW |