OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 | 154 |
155 T* operator*() const { | 155 T* operator*() const { |
156 DCHECK(get() != NULL); | 156 DCHECK(get() != NULL); |
157 return *get(); | 157 return *get(); |
158 } | 158 } |
159 T* operator->() const { | 159 T* operator->() const { |
160 DCHECK(get() != NULL); | 160 DCHECK(get() != NULL); |
161 return get(); | 161 return get(); |
162 } | 162 } |
163 | 163 |
| 164 void reset() { |
| 165 ref_ = internal::WeakReference(); |
| 166 ptr_ = NULL; |
| 167 } |
| 168 |
164 private: | 169 private: |
165 friend class SupportsWeakPtr<T>; | 170 friend class SupportsWeakPtr<T>; |
166 friend class WeakPtrFactory<T>; | 171 friend class WeakPtrFactory<T>; |
167 | 172 |
168 WeakPtr(const internal::WeakReference& ref, T* ptr) | 173 WeakPtr(const internal::WeakReference& ref, T* ptr) |
169 : WeakPtrBase(ref), ptr_(ptr) { | 174 : WeakPtrBase(ref), ptr_(ptr) { |
170 } | 175 } |
171 | 176 |
172 // This pointer is only valid when ref_.is_valid() is true. Otherwise, its | 177 // This pointer is only valid when ref_.is_valid() is true. Otherwise, its |
173 // value is undefined (as opposed to NULL). | 178 // value is undefined (as opposed to NULL). |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 217 |
213 private: | 218 private: |
214 internal::WeakReferenceOwner weak_reference_owner_; | 219 internal::WeakReferenceOwner weak_reference_owner_; |
215 T* ptr_; | 220 T* ptr_; |
216 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); | 221 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); |
217 }; | 222 }; |
218 | 223 |
219 } // namespace base | 224 } // namespace base |
220 | 225 |
221 #endif // BASE_WEAK_PTR_H_ | 226 #endif // BASE_WEAK_PTR_H_ |
OLD | NEW |