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

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 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 // A class may alternatively be composed of a WeakPtrFactory and thereby 224 // A class may alternatively be composed of a WeakPtrFactory and thereby
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 ~WeakPtrFactory(){
darin (slow to review) 2012/05/17 22:00:52 nit: add new line above the destructor
oshima 2012/05/17 22:16:43 Done.
235 ptr_ = NULL;
darin (slow to review) 2012/05/17 22:00:52 i wonder... should we null this out only in debug
oshima 2012/05/17 22:16:43 I wanted this to fail on trybots, which is release
236 }
234 237
235 WeakPtr<T> GetWeakPtr() { 238 WeakPtr<T> GetWeakPtr() {
239 DCHECK(ptr_);
236 return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_); 240 return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_);
237 } 241 }
238 242
239 // Call this method to invalidate all existing weak pointers. 243 // Call this method to invalidate all existing weak pointers.
240 void InvalidateWeakPtrs() { 244 void InvalidateWeakPtrs() {
245 DCHECK(ptr_);
241 weak_reference_owner_.Invalidate(); 246 weak_reference_owner_.Invalidate();
242 } 247 }
243 248
244 // Call this method to determine if any weak pointers exist. 249 // Call this method to determine if any weak pointers exist.
245 bool HasWeakPtrs() const { 250 bool HasWeakPtrs() const {
251 DCHECK(ptr_);
246 return weak_reference_owner_.HasRefs(); 252 return weak_reference_owner_.HasRefs();
247 } 253 }
248 254
249 // Indicates that this object will be used on another thread from now on. 255 // Indicates that this object will be used on another thread from now on.
250 void DetachFromThread() { 256 void DetachFromThread() {
257 DCHECK(ptr_);
251 weak_reference_owner_.DetachFromThread(); 258 weak_reference_owner_.DetachFromThread();
252 } 259 }
253 260
254 private: 261 private:
255 internal::WeakReferenceOwner weak_reference_owner_; 262 internal::WeakReferenceOwner weak_reference_owner_;
256 T* ptr_; 263 T* ptr_;
257 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); 264 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory);
258 }; 265 };
259 266
260 } // namespace base 267 } // namespace base
261 268
262 #endif // BASE_MEMORY_WEAK_PTR_H_ 269 #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