Chromium Code Reviews| 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 are pointers to an object that do not affect its lifetime, | 5 // Weak pointers are pointers to an object that do not affect its lifetime, |
| 6 // and which may be invalidated (i.e. reset to NULL) by the object, or its | 6 // and which may be invalidated (i.e. reset to NULL) by the object, or its |
| 7 // owner, at any time, most commonly when the object is about to be deleted. | 7 // owner, at any time, most commonly when the object is about to be deleted. |
| 8 | 8 |
| 9 // Weak pointers are useful when an object needs to be accessed safely by one | 9 // Weak pointers are useful when an object needs to be accessed safely by one |
| 10 // or more objects other than its owner, and those callers can cope with the | 10 // or more objects other than its owner, and those callers can cope with the |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 template <class T> | 265 template <class T> |
| 266 class WeakPtrFactory { | 266 class WeakPtrFactory { |
| 267 public: | 267 public: |
| 268 explicit WeakPtrFactory(T* ptr) : ptr_(ptr) { | 268 explicit WeakPtrFactory(T* ptr) : ptr_(ptr) { |
| 269 } | 269 } |
| 270 | 270 |
| 271 ~WeakPtrFactory() { | 271 ~WeakPtrFactory() { |
| 272 ptr_ = NULL; | 272 ptr_ = NULL; |
| 273 } | 273 } |
| 274 | 274 |
| 275 // Returns a new WeakPtr. This is not thread-safe - in general callers should | |
|
dcheng
2016/02/19 20:45:00
"in general" is only true for code that wants a we
Wez
2016/03/01 23:20:28
I've reworded the comment to be more explicit abou
| |
| 276 // take and store a WeakPtr to pass to code that needs to post tasks rather | |
| 277 // than call GetWeakPtr() from different threads. | |
| 275 WeakPtr<T> GetWeakPtr() { | 278 WeakPtr<T> GetWeakPtr() { |
| 276 DCHECK(ptr_); | 279 DCHECK(ptr_); |
| 277 return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_); | 280 return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_); |
| 278 } | 281 } |
| 279 | 282 |
| 280 // Call this method to invalidate all existing weak pointers. | 283 // Call this method to invalidate all existing weak pointers. |
| 281 void InvalidateWeakPtrs() { | 284 void InvalidateWeakPtrs() { |
| 282 DCHECK(ptr_); | 285 DCHECK(ptr_); |
| 283 weak_reference_owner_.Invalidate(); | 286 weak_reference_owner_.Invalidate(); |
| 284 } | 287 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 336 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. | 339 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
| 337 | 340 |
| 338 template <typename Derived> | 341 template <typename Derived> |
| 339 WeakPtr<Derived> AsWeakPtr(Derived* t) { | 342 WeakPtr<Derived> AsWeakPtr(Derived* t) { |
| 340 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); | 343 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
| 341 } | 344 } |
| 342 | 345 |
| 343 } // namespace base | 346 } // namespace base |
| 344 | 347 |
| 345 #endif // BASE_MEMORY_WEAK_PTR_H_ | 348 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |