Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 namespace internal { | 62 namespace internal { |
| 63 // These classes are part of the WeakPtr implementation. | 63 // These classes are part of the WeakPtr implementation. |
| 64 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. | 64 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. |
| 65 | 65 |
| 66 class BASE_EXPORT WeakReference { | 66 class BASE_EXPORT WeakReference { |
| 67 public: | 67 public: |
| 68 // While Flag is bound to a specific thread, it may be deleted from another | 68 // While Flag is bound to a specific thread, it may be deleted from another |
| 69 // via base::WeakPtr::~WeakPtr(). | 69 // via base::WeakPtr::~WeakPtr(). |
| 70 class Flag : public RefCountedThreadSafe<Flag> { | 70 class Flag : public RefCountedThreadSafe<Flag> { |
| 71 public: | 71 public: |
| 72 explicit Flag(Flag** handle); | 72 explicit Flag(); |
| 73 | 73 |
| 74 void Invalidate(); | 74 void Invalidate(); |
| 75 bool IsValid() const; | 75 bool IsValid() const; |
| 76 | 76 |
| 77 void DetachFromThread() { thread_checker_.DetachFromThread(); } | 77 void DetachFromThread() { thread_checker_.DetachFromThread(); } |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 friend class base::RefCountedThreadSafe<Flag>; | 80 friend class base::RefCountedThreadSafe<Flag>; |
| 81 | 81 |
| 82 ~Flag(); | 82 ~Flag(); |
| 83 | 83 |
| 84 ThreadChecker thread_checker_; | 84 ThreadChecker thread_checker_; |
| 85 Flag** handle_; | 85 bool valid_; |
|
darin (slow to review)
2011/08/18 18:29:06
nit: valid_ -> is_valid_
| |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 WeakReference(); | 88 WeakReference(); |
| 89 WeakReference(Flag* flag); | 89 WeakReference(scoped_refptr<Flag> flag); |
|
darin (slow to review)
2011/08/18 18:29:06
nit: WeakReference(const scoped_refptr<Flag>& flag
willchan no longer on Chromium
2011/08/18 19:14:12
While I'm glad you seem to have this preference he
| |
| 90 ~WeakReference(); | 90 ~WeakReference(); |
| 91 | 91 |
| 92 bool is_valid() const; | 92 bool is_valid() const; |
| 93 | 93 |
| 94 private: | 94 private: |
| 95 scoped_refptr<Flag> flag_; | 95 scoped_refptr<Flag> flag_; |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 class BASE_EXPORT WeakReferenceOwner { | 98 class BASE_EXPORT WeakReferenceOwner { |
| 99 public: | 99 public: |
| 100 WeakReferenceOwner(); | 100 WeakReferenceOwner(); |
| 101 ~WeakReferenceOwner(); | 101 ~WeakReferenceOwner(); |
| 102 | 102 |
| 103 WeakReference GetRef() const; | 103 WeakReference GetRef() const; |
| 104 | 104 |
| 105 bool HasRefs() const { | 105 bool HasRefs() const { |
| 106 return flag_ != NULL; | 106 return flag_.get() && !flag_->HasOneRef(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void Invalidate(); | 109 void Invalidate(); |
| 110 | 110 |
| 111 // Indicates that this object will be used on another thread from now on. | 111 // Indicates that this object will be used on another thread from now on. |
| 112 void DetachFromThread() { | 112 void DetachFromThread() { |
| 113 if (flag_) flag_->DetachFromThread(); | 113 if (flag_) flag_->DetachFromThread(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 private: | 116 private: |
| 117 mutable WeakReference::Flag* flag_; | 117 mutable scoped_refptr<WeakReference::Flag> flag_; |
| 118 }; | 118 }; |
| 119 | 119 |
| 120 // This class simplifies the implementation of WeakPtr's type conversion | 120 // This class simplifies the implementation of WeakPtr's type conversion |
| 121 // constructor by avoiding the need for a public accessor for ref_. A | 121 // constructor by avoiding the need for a public accessor for ref_. A |
| 122 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this | 122 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this |
| 123 // base class gives us a way to access ref_ in a protected fashion. | 123 // base class gives us a way to access ref_ in a protected fashion. |
| 124 class BASE_EXPORT WeakPtrBase { | 124 class BASE_EXPORT WeakPtrBase { |
| 125 public: | 125 public: |
| 126 WeakPtrBase(); | 126 WeakPtrBase(); |
| 127 ~WeakPtrBase(); | 127 ~WeakPtrBase(); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 | 246 |
| 247 private: | 247 private: |
| 248 internal::WeakReferenceOwner weak_reference_owner_; | 248 internal::WeakReferenceOwner weak_reference_owner_; |
| 249 T* ptr_; | 249 T* ptr_; |
| 250 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); | 250 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); |
| 251 }; | 251 }; |
| 252 | 252 |
| 253 } // namespace base | 253 } // namespace base |
| 254 | 254 |
| 255 #endif // BASE_MEMORY_WEAK_PTR_H_ | 255 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |