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 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 bool Equals(const InterfacePtr& other) const { | 194 bool Equals(const InterfacePtr& other) const { |
195 if (this == &other) | 195 if (this == &other) |
196 return true; | 196 return true; |
197 | 197 |
198 // Now that the two refer to different objects, they are equivalent if | 198 // Now that the two refer to different objects, they are equivalent if |
199 // and only if they are both null. | 199 // and only if they are both null. |
200 return !(*this) && !other; | 200 return !(*this) && !other; |
201 } | 201 } |
202 | 202 |
203 // DO NOT USE. Exposed only for internal use and for testing. | 203 // DO NOT USE. Exposed only for internal use and for testing. |
204 internal::InterfacePtrState<Interface, Interface::PassesAssociatedKinds_>* | 204 internal::InterfacePtrState<Interface, true>* internal_state() { |
205 internal_state() { | |
206 return &internal_state_; | 205 return &internal_state_; |
207 } | 206 } |
208 | 207 |
209 // Allow InterfacePtr<> to be used in boolean expressions, but not | 208 // Allow InterfacePtr<> to be used in boolean expressions, but not |
210 // implicitly convertible to a real bool (which is dangerous). | 209 // implicitly convertible to a real bool (which is dangerous). |
211 private: | 210 private: |
212 // TODO(dcheng): Use an explicit conversion operator. | 211 // TODO(dcheng): Use an explicit conversion operator. |
213 typedef internal::InterfacePtrState<Interface, | 212 typedef internal::InterfacePtrState<Interface, true> InterfacePtr::*Testable; |
214 Interface::PassesAssociatedKinds_> | |
215 InterfacePtr::*Testable; | |
216 | 213 |
217 public: | 214 public: |
218 operator Testable() const { | 215 operator Testable() const { |
219 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ | 216 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ |
220 : nullptr; | 217 : nullptr; |
221 } | 218 } |
222 | 219 |
223 private: | 220 private: |
224 // Forbid the == and != operators explicitly, otherwise InterfacePtr will be | 221 // Forbid the == and != operators explicitly, otherwise InterfacePtr will be |
225 // converted to Testable to do == or != comparison. | 222 // converted to Testable to do == or != comparison. |
226 template <typename T> | 223 template <typename T> |
227 bool operator==(const InterfacePtr<T>& other) const = delete; | 224 bool operator==(const InterfacePtr<T>& other) const = delete; |
228 template <typename T> | 225 template <typename T> |
229 bool operator!=(const InterfacePtr<T>& other) const = delete; | 226 bool operator!=(const InterfacePtr<T>& other) const = delete; |
230 | 227 |
231 typedef internal::InterfacePtrState<Interface, | 228 typedef internal::InterfacePtrState<Interface, true> State; |
232 Interface::PassesAssociatedKinds_> State; | |
233 mutable State internal_state_; | 229 mutable State internal_state_; |
234 | 230 |
235 DISALLOW_COPY_AND_ASSIGN(InterfacePtr); | 231 DISALLOW_COPY_AND_ASSIGN(InterfacePtr); |
236 }; | 232 }; |
237 | 233 |
238 // If |info| is valid (containing a valid message pipe handle), returns an | 234 // If |info| is valid (containing a valid message pipe handle), returns an |
239 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. | 235 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. |
240 template <typename Interface> | 236 template <typename Interface> |
241 InterfacePtr<Interface> MakeProxy( | 237 InterfacePtr<Interface> MakeProxy( |
242 InterfacePtrInfo<Interface> info, | 238 InterfacePtrInfo<Interface> info, |
243 scoped_refptr<base::SingleThreadTaskRunner> runner = | 239 scoped_refptr<base::SingleThreadTaskRunner> runner = |
244 base::ThreadTaskRunnerHandle::Get()) { | 240 base::ThreadTaskRunnerHandle::Get()) { |
245 InterfacePtr<Interface> ptr; | 241 InterfacePtr<Interface> ptr; |
246 if (info.is_valid()) | 242 if (info.is_valid()) |
247 ptr.Bind(std::move(info), std::move(runner)); | 243 ptr.Bind(std::move(info), std::move(runner)); |
248 return std::move(ptr); | 244 return std::move(ptr); |
249 } | 245 } |
250 | 246 |
251 } // namespace mojo | 247 } // namespace mojo |
252 | 248 |
253 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 249 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
OLD | NEW |