| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 // dereferenced and invalidated on the same SequencedTaskRunner otherwise | 52 // dereferenced and invalidated on the same SequencedTaskRunner otherwise |
| 53 // checking the pointer would be racey. | 53 // checking the pointer would be racey. |
| 54 // | 54 // |
| 55 // To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory | 55 // To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory |
| 56 // is dereferenced, the factory and its WeakPtrs become bound to the calling | 56 // is dereferenced, the factory and its WeakPtrs become bound to the calling |
| 57 // thread or current SequencedWorkerPool token, and cannot be dereferenced or | 57 // thread or current SequencedWorkerPool token, and cannot be dereferenced or |
| 58 // invalidated on any other task runner. Bound WeakPtrs can still be handed | 58 // invalidated on any other task runner. Bound WeakPtrs can still be handed |
| 59 // off to other task runners, e.g. to use to post tasks back to object on the | 59 // off to other task runners, e.g. to use to post tasks back to object on the |
| 60 // bound sequence. | 60 // bound sequence. |
| 61 // | 61 // |
| 62 // Invalidating the factory's WeakPtrs un-binds it from the sequence, allowing | 62 // If all WeakPtr objects are destroyed or invalidated then the factory is |
| 63 // it to be passed for a different sequence to use or delete it. | 63 // unbound from the SequencedTaskRunner/Thread. The WeakPtrFactory may then be |
| 64 // destroyed, or new WeakPtr objects may be used, from a different sequence. |
| 65 // |
| 66 // Thus, at least one WeakPtr object must exist and have been dereferenced on |
| 67 // the correct thread to enforce that other WeakPtr objects will enforce they |
| 68 // are used on the desired thread. |
| 64 | 69 |
| 65 #ifndef BASE_MEMORY_WEAK_PTR_H_ | 70 #ifndef BASE_MEMORY_WEAK_PTR_H_ |
| 66 #define BASE_MEMORY_WEAK_PTR_H_ | 71 #define BASE_MEMORY_WEAK_PTR_H_ |
| 67 | 72 |
| 68 #include "base/basictypes.h" | 73 #include "base/basictypes.h" |
| 69 #include "base/base_export.h" | 74 #include "base/base_export.h" |
| 70 #include "base/logging.h" | 75 #include "base/logging.h" |
| 71 #include "base/memory/ref_counted.h" | 76 #include "base/memory/ref_counted.h" |
| 72 #include "base/sequence_checker.h" | 77 #include "base/sequence_checker.h" |
| 73 #include "base/template_util.h" | 78 #include "base/template_util.h" |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. | 336 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
| 332 | 337 |
| 333 template <typename Derived> | 338 template <typename Derived> |
| 334 WeakPtr<Derived> AsWeakPtr(Derived* t) { | 339 WeakPtr<Derived> AsWeakPtr(Derived* t) { |
| 335 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); | 340 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
| 336 } | 341 } |
| 337 | 342 |
| 338 } // namespace base | 343 } // namespace base |
| 339 | 344 |
| 340 #endif // BASE_MEMORY_WEAK_PTR_H_ | 345 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |