| 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 nullptr) by the object, or its | 6 // and which may be invalidated (i.e. reset to nullptr) 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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 T* operator->() const { | 219 T* operator->() const { |
| 220 DCHECK(get() != nullptr); | 220 DCHECK(get() != nullptr); |
| 221 return get(); | 221 return get(); |
| 222 } | 222 } |
| 223 | 223 |
| 224 void reset() { | 224 void reset() { |
| 225 ref_ = internal::WeakReference(); | 225 ref_ = internal::WeakReference(); |
| 226 ptr_ = nullptr; | 226 ptr_ = nullptr; |
| 227 } | 227 } |
| 228 | 228 |
| 229 // Implement "Safe Bool Idiom" | 229 explicit operator bool() const { return get() != nullptr; } |
| 230 // https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool | |
| 231 // | |
| 232 // Allow WeakPtr<element_type> to be used in boolean expressions such as | |
| 233 // if (weak_ptr_instance) | |
| 234 // But do not become convertible to a real bool (which is dangerous). | |
| 235 // Implementation requires: | |
| 236 // typedef Testable | |
| 237 // operator Testable() const | |
| 238 // operator== | |
| 239 // operator!= | |
| 240 // | |
| 241 // == and != operators must be declared explicitly or dissallowed, as | |
| 242 // otherwise "ptr1 == ptr2" will compile but do the wrong thing (i.e., convert | |
| 243 // to Testable and then do the comparison). | |
| 244 // | |
| 245 // C++11 provides for "explicit operator bool()", however it is currently | |
| 246 // banned due to MSVS2013. https://chromium-cpp.appspot.com/#core-blacklist | |
| 247 private: | |
| 248 typedef T* WeakPtr::*Testable; | |
| 249 | |
| 250 public: | |
| 251 operator Testable() const { return get() ? &WeakPtr::ptr_ : nullptr; } | |
| 252 | 230 |
| 253 private: | 231 private: |
| 254 // Explicitly declare comparison operators as required by the "Safe Bool | |
| 255 // Idiom", but keep them private. | |
| 256 template <class U> bool operator==(WeakPtr<U> const&) const; | |
| 257 template <class U> bool operator!=(WeakPtr<U> const&) const; | |
| 258 | |
| 259 friend class internal::SupportsWeakPtrBase; | 232 friend class internal::SupportsWeakPtrBase; |
| 260 template <typename U> friend class WeakPtr; | 233 template <typename U> friend class WeakPtr; |
| 261 friend class SupportsWeakPtr<T>; | 234 friend class SupportsWeakPtr<T>; |
| 262 friend class WeakPtrFactory<T>; | 235 friend class WeakPtrFactory<T>; |
| 263 | 236 |
| 264 WeakPtr(const internal::WeakReference& ref, T* ptr) | 237 WeakPtr(const internal::WeakReference& ref, T* ptr) |
| 265 : WeakPtrBase(ref), | 238 : WeakPtrBase(ref), |
| 266 ptr_(ptr) { | 239 ptr_(ptr) { |
| 267 } | 240 } |
| 268 | 241 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. | 321 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
| 349 | 322 |
| 350 template <typename Derived> | 323 template <typename Derived> |
| 351 WeakPtr<Derived> AsWeakPtr(Derived* t) { | 324 WeakPtr<Derived> AsWeakPtr(Derived* t) { |
| 352 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); | 325 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
| 353 } | 326 } |
| 354 | 327 |
| 355 } // namespace base | 328 } // namespace base |
| 356 | 329 |
| 357 #endif // BASE_MEMORY_WEAK_PTR_H_ | 330 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |