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 // A common alternative to weak pointers is to have the shared object hold a | 10 // A common alternative to weak pointers is to have the shared object hold a |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 // may only be dereferenced on the thread that the original WeakPtr was bound | 67 // may only be dereferenced on the thread that the original WeakPtr was bound |
| 68 // to. | 68 // to. |
| 69 | 69 |
| 70 #ifndef BASE_MEMORY_WEAK_PTR_H_ | 70 #ifndef BASE_MEMORY_WEAK_PTR_H_ |
| 71 #define BASE_MEMORY_WEAK_PTR_H_ | 71 #define BASE_MEMORY_WEAK_PTR_H_ |
| 72 | 72 |
| 73 #include "base/basictypes.h" | 73 #include "base/basictypes.h" |
| 74 #include "base/base_export.h" | 74 #include "base/base_export.h" |
| 75 #include "base/logging.h" | 75 #include "base/logging.h" |
| 76 #include "base/memory/ref_counted.h" | 76 #include "base/memory/ref_counted.h" |
| 77 #include "base/sequence_checker.h" | |
| 77 #include "base/template_util.h" | 78 #include "base/template_util.h" |
| 78 #include "base/threading/thread_checker.h" | |
| 79 | 79 |
| 80 namespace base { | 80 namespace base { |
| 81 | 81 |
| 82 template <typename T> class SupportsWeakPtr; | 82 template <typename T> class SupportsWeakPtr; |
| 83 template <typename T> class WeakPtr; | 83 template <typename T> class WeakPtr; |
| 84 | 84 |
| 85 namespace internal { | 85 namespace internal { |
| 86 // These classes are part of the WeakPtr implementation. | 86 // These classes are part of the WeakPtr implementation. |
| 87 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. | 87 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. |
| 88 | 88 |
| 89 class BASE_EXPORT WeakReference { | 89 class BASE_EXPORT WeakReference { |
| 90 public: | 90 public: |
| 91 // While Flag is bound to a specific thread, it may be deleted from another | 91 // While Flag is bound to a specific thread, it may be deleted from another |
| 92 // via base::WeakPtr::~WeakPtr(). | 92 // via base::WeakPtr::~WeakPtr(). |
| 93 class Flag : public RefCountedThreadSafe<Flag> { | 93 class Flag : public RefCountedThreadSafe<Flag> { |
| 94 public: | 94 public: |
| 95 Flag(); | 95 Flag(); |
| 96 | 96 |
| 97 void Invalidate(); | 97 void Invalidate(); |
| 98 bool IsValid() const; | 98 bool IsValid() const; |
| 99 | 99 |
| 100 void DetachFromThread() { thread_checker_.DetachFromThread(); } | 100 void DetachFromThread(); |
| 101 | 101 |
| 102 private: | 102 private: |
| 103 friend class base::RefCountedThreadSafe<Flag>; | 103 friend class base::RefCountedThreadSafe<Flag>; |
| 104 | 104 |
| 105 ~Flag(); | 105 ~Flag(); |
| 106 | 106 |
| 107 ThreadChecker thread_checker_; | 107 SequenceChecker sequence_checker_; |
| 108 bool is_valid_; | 108 bool is_valid_; |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 WeakReference(); | 111 WeakReference(); |
| 112 explicit WeakReference(const Flag* flag); | 112 explicit WeakReference(const Flag* flag); |
| 113 ~WeakReference(); | 113 ~WeakReference(); |
| 114 | 114 |
| 115 bool is_valid() const; | 115 bool is_valid() const; |
| 116 | 116 |
| 117 private: | 117 private: |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 WeakPtr(const internal::WeakReference& ref, T* ptr) | 238 WeakPtr(const internal::WeakReference& ref, T* ptr) |
| 239 : WeakPtrBase(ref), | 239 : WeakPtrBase(ref), |
| 240 ptr_(ptr) { | 240 ptr_(ptr) { |
| 241 } | 241 } |
| 242 | 242 |
| 243 // This pointer is only valid when ref_.is_valid() is true. Otherwise, its | 243 // This pointer is only valid when ref_.is_valid() is true. Otherwise, its |
| 244 // value is undefined (as opposed to NULL). | 244 // value is undefined (as opposed to NULL). |
| 245 T* ptr_; | 245 T* ptr_; |
| 246 }; | 246 }; |
| 247 | 247 |
| 248 // TODO(akalin): Make WeakPtrFactory and SupportsWeakPtr support | |
|
jar (doing other things)
2013/02/16 02:37:27
nit: Can you add a bug number.
| |
| 249 // binding to a SequencedTaskRunner instead of just a single thread. | |
| 250 | |
| 248 // A class may extend from SupportsWeakPtr to expose weak pointers to itself. | 251 // A class may extend from SupportsWeakPtr to expose weak pointers to itself. |
| 249 // This is useful in cases where you want others to be able to get a weak | 252 // This is useful in cases where you want others to be able to get a weak |
| 250 // pointer to your class. It also has the property that you don't need to | 253 // pointer to your class. It also has the property that you don't need to |
| 251 // initialize it from your constructor. | 254 // initialize it from your constructor. |
| 252 template <class T> | 255 template <class T> |
| 253 class SupportsWeakPtr : public internal::SupportsWeakPtrBase { | 256 class SupportsWeakPtr : public internal::SupportsWeakPtrBase { |
| 254 public: | 257 public: |
| 255 SupportsWeakPtr() {} | 258 SupportsWeakPtr() {} |
| 256 | 259 |
| 257 WeakPtr<T> AsWeakPtr() { | 260 WeakPtr<T> AsWeakPtr() { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 | 337 |
| 335 private: | 338 private: |
| 336 internal::WeakReferenceOwner weak_reference_owner_; | 339 internal::WeakReferenceOwner weak_reference_owner_; |
| 337 T* ptr_; | 340 T* ptr_; |
| 338 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); | 341 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); |
| 339 }; | 342 }; |
| 340 | 343 |
| 341 } // namespace base | 344 } // namespace base |
| 342 | 345 |
| 343 #endif // BASE_MEMORY_WEAK_PTR_H_ | 346 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |