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 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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 // control how it exposes weak pointers to itself. This is helpful if you only | 225 // control how it exposes weak pointers to itself. This is helpful if you only |
| 226 // need weak pointers within the implementation of a class. This class is also | 226 // need weak pointers within the implementation of a class. This class is also |
| 227 // useful when working with primitive types. For example, you could have a | 227 // useful when working with primitive types. For example, you could have a |
| 228 // WeakPtrFactory<bool> that is used to pass around a weak reference to a bool. | 228 // WeakPtrFactory<bool> that is used to pass around a weak reference to a bool. |
| 229 template <class T> | 229 template <class T> |
| 230 class WeakPtrFactory { | 230 class WeakPtrFactory { |
| 231 public: | 231 public: |
| 232 explicit WeakPtrFactory(T* ptr) : ptr_(ptr) { | 232 explicit WeakPtrFactory(T* ptr) : ptr_(ptr) { |
| 233 } | 233 } |
| 234 | 234 |
| 235 ~WeakPtrFactory() { | |
| 236 ptr_ = NULL; | |
| 237 } | |
| 238 | |
| 235 WeakPtr<T> GetWeakPtr() { | 239 WeakPtr<T> GetWeakPtr() { |
| 240 CHECK(ptr_); | |
|
darin (slow to review)
2012/05/18 17:45:13
I think it is a bit bloaty to inline this into eve
| |
| 236 return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_); | 241 return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_); |
| 237 } | 242 } |
| 238 | 243 |
| 239 // Call this method to invalidate all existing weak pointers. | 244 // Call this method to invalidate all existing weak pointers. |
| 240 void InvalidateWeakPtrs() { | 245 void InvalidateWeakPtrs() { |
| 246 CHECK(ptr_); | |
| 241 weak_reference_owner_.Invalidate(); | 247 weak_reference_owner_.Invalidate(); |
| 242 } | 248 } |
| 243 | 249 |
| 244 // Call this method to determine if any weak pointers exist. | 250 // Call this method to determine if any weak pointers exist. |
| 245 bool HasWeakPtrs() const { | 251 bool HasWeakPtrs() const { |
| 252 CHECK(ptr_); | |
| 246 return weak_reference_owner_.HasRefs(); | 253 return weak_reference_owner_.HasRefs(); |
| 247 } | 254 } |
| 248 | 255 |
| 249 // Indicates that this object will be used on another thread from now on. | 256 // Indicates that this object will be used on another thread from now on. |
| 250 void DetachFromThread() { | 257 void DetachFromThread() { |
| 258 CHECK(ptr_); | |
| 251 weak_reference_owner_.DetachFromThread(); | 259 weak_reference_owner_.DetachFromThread(); |
| 252 } | 260 } |
| 253 | 261 |
| 254 private: | 262 private: |
| 255 internal::WeakReferenceOwner weak_reference_owner_; | 263 internal::WeakReferenceOwner weak_reference_owner_; |
| 256 T* ptr_; | 264 T* ptr_; |
| 257 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); | 265 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); |
| 258 }; | 266 }; |
| 259 | 267 |
| 260 } // namespace base | 268 } // namespace base |
| 261 | 269 |
| 262 #endif // BASE_MEMORY_WEAK_PTR_H_ | 270 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |