Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: base/memory/weak_ptr.h

Issue 10387063: Let WeakPtrFactory operations fail once its dtor is called (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 DCHECK(ptr_);
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 DCHECK(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 DCHECK(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 DCHECK(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_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698