| 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 | |
| 41 public: | 39 public: |
| 42 // Constructs an unbound InterfacePtr. | 40 // Constructs an unbound InterfacePtr. |
| 43 InterfacePtr() {} | 41 InterfacePtr() {} |
| 44 InterfacePtr(decltype(nullptr)) {} | 42 InterfacePtr(decltype(nullptr)) {} |
| 45 | 43 |
| 46 // Takes over the binding of another InterfacePtr. | 44 // Takes over the binding of another InterfacePtr. |
| 47 InterfacePtr(InterfacePtr&& other) { | 45 InterfacePtr(InterfacePtr&& other) { |
| 48 internal_state_.Swap(&other.internal_state_); | 46 internal_state_.Swap(&other.internal_state_); |
| 49 } | 47 } |
| 50 | 48 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 192 |
| 195 // DO NOT USE. Exposed only for internal use and for testing. | 193 // DO NOT USE. Exposed only for internal use and for testing. |
| 196 internal::InterfacePtrState<Interface, Interface::PassesAssociatedKinds_>* | 194 internal::InterfacePtrState<Interface, Interface::PassesAssociatedKinds_>* |
| 197 internal_state() { | 195 internal_state() { |
| 198 return &internal_state_; | 196 return &internal_state_; |
| 199 } | 197 } |
| 200 | 198 |
| 201 // Allow InterfacePtr<> to be used in boolean expressions, but not | 199 // Allow InterfacePtr<> to be used in boolean expressions, but not |
| 202 // implicitly convertible to a real bool (which is dangerous). | 200 // implicitly convertible to a real bool (which is dangerous). |
| 203 private: | 201 private: |
| 202 // TODO(dcheng): Use an explicit conversion operator. |
| 204 typedef internal::InterfacePtrState<Interface, | 203 typedef internal::InterfacePtrState<Interface, |
| 205 Interface::PassesAssociatedKinds_> | 204 Interface::PassesAssociatedKinds_> |
| 206 InterfacePtr::*Testable; | 205 InterfacePtr::*Testable; |
| 207 | 206 |
| 208 public: | 207 public: |
| 209 operator Testable() const { | 208 operator Testable() const { |
| 210 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ | 209 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ |
| 211 : nullptr; | 210 : nullptr; |
| 212 } | 211 } |
| 213 | 212 |
| 214 private: | 213 private: |
| 215 // Forbid the == and != operators explicitly, otherwise InterfacePtr will be | 214 // Forbid the == and != operators explicitly, otherwise InterfacePtr will be |
| 216 // converted to Testable to do == or != comparison. | 215 // converted to Testable to do == or != comparison. |
| 217 template <typename T> | 216 template <typename T> |
| 218 bool operator==(const InterfacePtr<T>& other) const = delete; | 217 bool operator==(const InterfacePtr<T>& other) const = delete; |
| 219 template <typename T> | 218 template <typename T> |
| 220 bool operator!=(const InterfacePtr<T>& other) const = delete; | 219 bool operator!=(const InterfacePtr<T>& other) const = delete; |
| 221 | 220 |
| 222 typedef internal::InterfacePtrState<Interface, | 221 typedef internal::InterfacePtrState<Interface, |
| 223 Interface::PassesAssociatedKinds_> State; | 222 Interface::PassesAssociatedKinds_> State; |
| 224 mutable State internal_state_; | 223 mutable State internal_state_; |
| 224 |
| 225 DISALLOW_COPY_AND_ASSIGN(InterfacePtr); |
| 225 }; | 226 }; |
| 226 | 227 |
| 227 // If |info| is valid (containing a valid message pipe handle), returns an | 228 // If |info| is valid (containing a valid message pipe handle), returns an |
| 228 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. | 229 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. |
| 229 template <typename Interface> | 230 template <typename Interface> |
| 230 InterfacePtr<Interface> MakeProxy( | 231 InterfacePtr<Interface> MakeProxy( |
| 231 InterfacePtrInfo<Interface> info, | 232 InterfacePtrInfo<Interface> info, |
| 232 scoped_refptr<base::SingleThreadTaskRunner> runner = | 233 scoped_refptr<base::SingleThreadTaskRunner> runner = |
| 233 base::ThreadTaskRunnerHandle::Get()) { | 234 base::ThreadTaskRunnerHandle::Get()) { |
| 234 InterfacePtr<Interface> ptr; | 235 InterfacePtr<Interface> ptr; |
| 235 if (info.is_valid()) | 236 if (info.is_valid()) |
| 236 ptr.Bind(std::move(info), std::move(runner)); | 237 ptr.Bind(std::move(info), std::move(runner)); |
| 237 return std::move(ptr); | 238 return std::move(ptr); |
| 238 } | 239 } |
| 239 | 240 |
| 240 } // namespace mojo | 241 } // namespace mojo |
| 241 | 242 |
| 242 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 243 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
| OLD | NEW |