| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 public: | 83 public: |
| 84 // Although Flag is bound to a specific thread, it may be deleted from another | 84 // Although Flag is bound to a specific thread, it may be deleted from another |
| 85 // via base::WeakPtr::~WeakPtr(). | 85 // via base::WeakPtr::~WeakPtr(). |
| 86 class Flag : public RefCountedThreadSafe<Flag> { | 86 class Flag : public RefCountedThreadSafe<Flag> { |
| 87 public: | 87 public: |
| 88 Flag(); | 88 Flag(); |
| 89 | 89 |
| 90 void Invalidate(); | 90 void Invalidate(); |
| 91 bool IsValid() const; | 91 bool IsValid() const; |
| 92 | 92 |
| 93 // Remove this when crbug.com/234964 is addressed. | |
| 94 void DetachFromThreadHack() { thread_checker_.DetachFromThread(); } | |
| 95 | |
| 96 private: | 93 private: |
| 97 friend class base::RefCountedThreadSafe<Flag>; | 94 friend class base::RefCountedThreadSafe<Flag>; |
| 98 | 95 |
| 99 ~Flag(); | 96 ~Flag(); |
| 100 | 97 |
| 101 ThreadChecker thread_checker_; | 98 ThreadChecker thread_checker_; |
| 102 bool is_valid_; | 99 bool is_valid_; |
| 103 }; | 100 }; |
| 104 | 101 |
| 105 WeakReference(); | 102 WeakReference(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 118 ~WeakReferenceOwner(); | 115 ~WeakReferenceOwner(); |
| 119 | 116 |
| 120 WeakReference GetRef() const; | 117 WeakReference GetRef() const; |
| 121 | 118 |
| 122 bool HasRefs() const { | 119 bool HasRefs() const { |
| 123 return flag_.get() && !flag_->HasOneRef(); | 120 return flag_.get() && !flag_->HasOneRef(); |
| 124 } | 121 } |
| 125 | 122 |
| 126 void Invalidate(); | 123 void Invalidate(); |
| 127 | 124 |
| 128 // Remove this when crbug.com/234964 is addressed. | |
| 129 void DetachFromThreadHack() { | |
| 130 if (flag_.get()) | |
| 131 flag_->DetachFromThreadHack(); | |
| 132 } | |
| 133 | |
| 134 private: | 125 private: |
| 135 mutable scoped_refptr<WeakReference::Flag> flag_; | 126 mutable scoped_refptr<WeakReference::Flag> flag_; |
| 136 }; | 127 }; |
| 137 | 128 |
| 138 // This class simplifies the implementation of WeakPtr's type conversion | 129 // This class simplifies the implementation of WeakPtr's type conversion |
| 139 // constructor by avoiding the need for a public accessor for ref_. A | 130 // constructor by avoiding the need for a public accessor for ref_. A |
| 140 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this | 131 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this |
| 141 // base class gives us a way to access ref_ in a protected fashion. | 132 // base class gives us a way to access ref_ in a protected fashion. |
| 142 class BASE_EXPORT WeakPtrBase { | 133 class BASE_EXPORT WeakPtrBase { |
| 143 public: | 134 public: |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 // destroyed, its use can lead to subtle use-after-destroy issues. | 295 // destroyed, its use can lead to subtle use-after-destroy issues. |
| 305 template <class T> | 296 template <class T> |
| 306 class SupportsWeakPtr : public internal::SupportsWeakPtrBase { | 297 class SupportsWeakPtr : public internal::SupportsWeakPtrBase { |
| 307 public: | 298 public: |
| 308 SupportsWeakPtr() {} | 299 SupportsWeakPtr() {} |
| 309 | 300 |
| 310 WeakPtr<T> AsWeakPtr() { | 301 WeakPtr<T> AsWeakPtr() { |
| 311 return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this)); | 302 return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this)); |
| 312 } | 303 } |
| 313 | 304 |
| 314 // Removes the binding, if any, from this object to a particular thread. | |
| 315 // This is used in WebGraphicsContext3DInProcessCommandBufferImpl to work- | |
| 316 // around access to cmmand buffer objects by more than one thread. | |
| 317 // Remove this when crbug.com/234964 is addressed. | |
| 318 void DetachFromThreadHack() { | |
| 319 weak_reference_owner_.DetachFromThreadHack(); | |
| 320 } | |
| 321 | |
| 322 protected: | 305 protected: |
| 323 ~SupportsWeakPtr() {} | 306 ~SupportsWeakPtr() {} |
| 324 | 307 |
| 325 private: | 308 private: |
| 326 internal::WeakReferenceOwner weak_reference_owner_; | 309 internal::WeakReferenceOwner weak_reference_owner_; |
| 327 DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr); | 310 DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr); |
| 328 }; | 311 }; |
| 329 | 312 |
| 330 // Helper function that uses type deduction to safely return a WeakPtr<Derived> | 313 // Helper function that uses type deduction to safely return a WeakPtr<Derived> |
| 331 // when Derived doesn't directly extend SupportsWeakPtr<Derived>, instead it | 314 // when Derived doesn't directly extend SupportsWeakPtr<Derived>, instead it |
| (...skipping 14 matching lines...) Expand all Loading... |
| 346 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. | 329 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
| 347 | 330 |
| 348 template <typename Derived> | 331 template <typename Derived> |
| 349 WeakPtr<Derived> AsWeakPtr(Derived* t) { | 332 WeakPtr<Derived> AsWeakPtr(Derived* t) { |
| 350 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); | 333 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
| 351 } | 334 } |
| 352 | 335 |
| 353 } // namespace base | 336 } // namespace base |
| 354 | 337 |
| 355 #endif // BASE_MEMORY_WEAK_PTR_H_ | 338 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |