| 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 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 214 |
| 215 T& operator*() const { | 215 T& operator*() const { |
| 216 DCHECK(get() != NULL); | 216 DCHECK(get() != NULL); |
| 217 return *get(); | 217 return *get(); |
| 218 } | 218 } |
| 219 T* operator->() const { | 219 T* operator->() const { |
| 220 DCHECK(get() != NULL); | 220 DCHECK(get() != NULL); |
| 221 return get(); | 221 return get(); |
| 222 } | 222 } |
| 223 | 223 |
| 224 // Allow WeakPtr<element_type> to be used in boolean expressions, but not |
| 225 // implicitly convertible to a real bool (which is dangerous). |
| 226 // |
| 227 // Note that this trick is only safe when the == and != operators |
| 228 // are declared explicitly, as otherwise "weak_ptr1 == weak_ptr2" |
| 229 // will compile but do the wrong thing (i.e., convert to Testable |
| 230 // and then do the comparison). |
| 231 private: |
| 232 typedef T* WeakPtr::*Testable; |
| 233 |
| 234 public: |
| 235 operator Testable() const { return get() ? &WeakPtr::ptr_ : NULL; } |
| 236 |
| 224 void reset() { | 237 void reset() { |
| 225 ref_ = internal::WeakReference(); | 238 ref_ = internal::WeakReference(); |
| 226 ptr_ = NULL; | 239 ptr_ = NULL; |
| 227 } | 240 } |
| 228 | 241 |
| 229 private: | 242 private: |
| 243 // Explicitly declare comparison operators as required by the bool |
| 244 // trick, but keep them private. |
| 245 template <class U> bool operator==(WeakPtr<U> const&) const; |
| 246 template <class U> bool operator!=(WeakPtr<U> const&) const; |
| 247 |
| 230 friend class internal::SupportsWeakPtrBase; | 248 friend class internal::SupportsWeakPtrBase; |
| 231 template <typename U> friend class WeakPtr; | 249 template <typename U> friend class WeakPtr; |
| 232 friend class SupportsWeakPtr<T>; | 250 friend class SupportsWeakPtr<T>; |
| 233 friend class WeakPtrFactory<T>; | 251 friend class WeakPtrFactory<T>; |
| 234 | 252 |
| 235 WeakPtr(const internal::WeakReference& ref, T* ptr) | 253 WeakPtr(const internal::WeakReference& ref, T* ptr) |
| 236 : WeakPtrBase(ref), | 254 : WeakPtrBase(ref), |
| 237 ptr_(ptr) { | 255 ptr_(ptr) { |
| 238 } | 256 } |
| 239 | 257 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. | 347 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
| 330 | 348 |
| 331 template <typename Derived> | 349 template <typename Derived> |
| 332 WeakPtr<Derived> AsWeakPtr(Derived* t) { | 350 WeakPtr<Derived> AsWeakPtr(Derived* t) { |
| 333 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); | 351 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
| 334 } | 352 } |
| 335 | 353 |
| 336 } // namespace base | 354 } // namespace base |
| 337 | 355 |
| 338 #endif // BASE_MEMORY_WEAK_PTR_H_ | 356 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |