| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Weak pointers help in cases where you have many objects referring back to a | 5 // Weak pointers help in cases where you have many objects referring back to a |
| 6 // shared object and you wish for the lifetime of the shared object to not be | 6 // shared object and you wish for the lifetime of the shared object to not be |
| 7 // bound to the lifetime of the referrers. In other words, this is useful when | 7 // bound to the lifetime of the referrers. In other words, this is useful when |
| 8 // reference counting is not a good fit. | 8 // reference counting is not a good fit. |
| 9 // | 9 // |
| 10 // Thread-safety notes: | 10 // Thread-safety notes: |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 | 244 |
| 245 WeakPtr<T> AsWeakPtr() { | 245 WeakPtr<T> AsWeakPtr() { |
| 246 return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this)); | 246 return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this)); |
| 247 } | 247 } |
| 248 | 248 |
| 249 // Indicates that this object will be used on another thread from now on. | 249 // Indicates that this object will be used on another thread from now on. |
| 250 void DetachFromThread() { | 250 void DetachFromThread() { |
| 251 weak_reference_owner_.DetachFromThread(); | 251 weak_reference_owner_.DetachFromThread(); |
| 252 } | 252 } |
| 253 | 253 |
| 254 protected: |
| 255 ~SupportsWeakPtr() {} |
| 256 |
| 254 private: | 257 private: |
| 255 internal::WeakReferenceOwner weak_reference_owner_; | 258 internal::WeakReferenceOwner weak_reference_owner_; |
| 256 DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr); | 259 DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr); |
| 257 }; | 260 }; |
| 258 | 261 |
| 259 // Helper function that uses type deduction to safely return a WeakPtr<Derived> | 262 // Helper function that uses type deduction to safely return a WeakPtr<Derived> |
| 260 // when Derived doesn't directly extend SupportsWeakPtr<Derived>, instead it | 263 // when Derived doesn't directly extend SupportsWeakPtr<Derived>, instead it |
| 261 // extends a Base that extends SupportsWeakPtr<Base>. | 264 // extends a Base that extends SupportsWeakPtr<Base>. |
| 262 // | 265 // |
| 263 // EXAMPLE: | 266 // EXAMPLE: |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 | 322 |
| 320 private: | 323 private: |
| 321 internal::WeakReferenceOwner weak_reference_owner_; | 324 internal::WeakReferenceOwner weak_reference_owner_; |
| 322 T* ptr_; | 325 T* ptr_; |
| 323 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); | 326 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); |
| 324 }; | 327 }; |
| 325 | 328 |
| 326 } // namespace base | 329 } // namespace base |
| 327 | 330 |
| 328 #endif // BASE_MEMORY_WEAK_PTR_H_ | 331 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |