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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 WeakReference GetRef() const; | 126 WeakReference GetRef() const; |
127 | 127 |
128 bool HasRefs() const { | 128 bool HasRefs() const { |
129 return flag_.get() && !flag_->HasOneRef(); | 129 return flag_.get() && !flag_->HasOneRef(); |
130 } | 130 } |
131 | 131 |
132 void Invalidate(); | 132 void Invalidate(); |
133 | 133 |
134 // Indicates that this object will be used on another thread from now on. | 134 // Indicates that this object will be used on another thread from now on. |
135 void DetachFromThread() { | 135 void DetachFromThread() { |
136 if (flag_) flag_->DetachFromThread(); | 136 if (flag_.get()) flag_->DetachFromThread(); |
137 } | 137 } |
138 | 138 |
139 private: | 139 private: |
140 mutable scoped_refptr<WeakReference::Flag> flag_; | 140 mutable scoped_refptr<WeakReference::Flag> flag_; |
141 }; | 141 }; |
142 | 142 |
143 // This class simplifies the implementation of WeakPtr's type conversion | 143 // This class simplifies the implementation of WeakPtr's type conversion |
144 // constructor by avoiding the need for a public accessor for ref_. A | 144 // constructor by avoiding the need for a public accessor for ref_. A |
145 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this | 145 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this |
146 // base class gives us a way to access ref_ in a protected fashion. | 146 // base class gives us a way to access ref_ in a protected fashion. |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 | 334 |
335 private: | 335 private: |
336 internal::WeakReferenceOwner weak_reference_owner_; | 336 internal::WeakReferenceOwner weak_reference_owner_; |
337 T* ptr_; | 337 T* ptr_; |
338 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); | 338 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); |
339 }; | 339 }; |
340 | 340 |
341 } // namespace base | 341 } // namespace base |
342 | 342 |
343 #endif // BASE_MEMORY_WEAK_PTR_H_ | 343 #endif // BASE_MEMORY_WEAK_PTR_H_ |
OLD | NEW |