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

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

Issue 10690080: Rename/re-comment tests and added new death tests for WeakPtr (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 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 | base/memory/weak_ptr_unittest.cc » ('j') | base/memory/weak_ptr_unittest.cc » ('J')
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:
11 // When you get a WeakPtr (from a WeakPtrFactory or SupportsWeakPtr), 11 // When you get a WeakPtr (from a WeakPtrFactory or SupportsWeakPtr), the
12 // the WeakPtr becomes bound to the current thread. You may only 12 // WeakPtr becomes bound to the current thread. You may only dereference the
13 // dereference the WeakPtr on that thread. However, it is safe to 13 // WeakPtr on that thread. However, it is safe to destroy the WeakPtr object
14 // destroy the WeakPtr object on another thread. 14 // on another thread. Since a WeakPtr object may be destroyed on a background
15 // Since a WeakPtr object may be destroyed on a background thread, 15 // thread, querying WeakPtrFactory's HasWeakPtrs() method can be racy.
16 // querying WeakPtrFactory's HasWeakPtrs() method can be racy. 16 // On the other hand, the object that supports WeakPtr (extends
17 // 17 // SupportsWeakPtr) will be bound to the first thread that creates a WeakPtr
18 // pointing to it, and can not be deleted from other threads unless all
19 // WeakPtrs are deleted. To work around of this, call
Ryan Sleevi 2012/07/10 00:06:44 nit: To work around this
kaiwang 2012/07/10 22:21:19 Done.
20 // SupportsWeakPtr::DetachFromThread().
Ryan Sleevi 2012/07/10 00:06:44 I'm not sure we should encourage calling DetachFro
kaiwang 2012/07/10 22:21:19 Changed the wording.
18 // 21 //
19 // A common alternative to weak pointers is to have the shared object hold a 22 // A common alternative to weak pointers is to have the shared object hold a
20 // list of all referrers, and then when the shared object is destroyed, it 23 // list of all referrers, and then when the shared object is destroyed, it
21 // calls a method on the referrers to tell them to drop their references. This 24 // calls a method on the referrers to tell them to drop their references. This
22 // approach also requires the referrers to tell the shared object when they get 25 // approach also requires the referrers to tell the shared object when they get
23 // destroyed so that the shared object can remove the referrer from its list of 26 // destroyed so that the shared object can remove the referrer from its list of
24 // referrers. Such a solution works, but it is a bit complex. 27 // referrers. Such a solution works, but it is a bit complex.
25 // 28 //
26 // EXAMPLE: 29 // EXAMPLE:
27 // 30 //
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 ref_ = internal::WeakReference(); 221 ref_ = internal::WeakReference();
219 ptr_ = NULL; 222 ptr_ = NULL;
220 } 223 }
221 224
222 private: 225 private:
223 friend class internal::SupportsWeakPtrBase; 226 friend class internal::SupportsWeakPtrBase;
224 friend class SupportsWeakPtr<T>; 227 friend class SupportsWeakPtr<T>;
225 friend class WeakPtrFactory<T>; 228 friend class WeakPtrFactory<T>;
226 229
227 WeakPtr(const internal::WeakReference& ref, T* ptr) 230 WeakPtr(const internal::WeakReference& ref, T* ptr)
228 : WeakPtrBase(ref), ptr_(ptr) { 231 : WeakPtrBase(ref),
232 ptr_(ptr) {
229 } 233 }
230 234
231 // This pointer is only valid when ref_.is_valid() is true. Otherwise, its 235 // This pointer is only valid when ref_.is_valid() is true. Otherwise, its
232 // value is undefined (as opposed to NULL). 236 // value is undefined (as opposed to NULL).
233 T* ptr_; 237 T* ptr_;
234 }; 238 };
235 239
236 // A class may extend from SupportsWeakPtr to expose weak pointers to itself. 240 // A class may extend from SupportsWeakPtr to expose weak pointers to itself.
237 // This is useful in cases where you want others to be able to get a weak 241 // This is useful in cases where you want others to be able to get a weak
238 // pointer to your class. It also has the property that you don't need to 242 // pointer to your class. It also has the property that you don't need to
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 326
323 private: 327 private:
324 internal::WeakReferenceOwner weak_reference_owner_; 328 internal::WeakReferenceOwner weak_reference_owner_;
325 T* ptr_; 329 T* ptr_;
326 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); 330 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory);
327 }; 331 };
328 332
329 } // namespace base 333 } // namespace base
330 334
331 #endif // BASE_MEMORY_WEAK_PTR_H_ 335 #endif // BASE_MEMORY_WEAK_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/weak_ptr_unittest.cc » ('j') | base/memory/weak_ptr_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698