| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 // Invalidating the factory's WeakPtrs un-binds it from the thread, allowing it | 60 // Invalidating the factory's WeakPtrs un-binds it from the thread, allowing it |
| 61 // to be passed for a different thread to use or delete it. | 61 // to be passed for a different thread to use or delete it. |
| 62 | 62 |
| 63 #ifndef BASE_MEMORY_WEAK_PTR_H_ | 63 #ifndef BASE_MEMORY_WEAK_PTR_H_ |
| 64 #define BASE_MEMORY_WEAK_PTR_H_ | 64 #define BASE_MEMORY_WEAK_PTR_H_ |
| 65 | 65 |
| 66 #include "base/basictypes.h" | 66 #include "base/basictypes.h" |
| 67 #include "base/base_export.h" | 67 #include "base/base_export.h" |
| 68 #include "base/logging.h" | 68 #include "base/logging.h" |
| 69 #include "base/memory/ref_counted.h" | 69 #include "base/memory/ref_counted.h" |
| 70 #include "base/sequence_checker.h" |
| 70 #include "base/template_util.h" | 71 #include "base/template_util.h" |
| 71 #include "base/threading/thread_checker.h" | |
| 72 | 72 |
| 73 namespace base { | 73 namespace base { |
| 74 | 74 |
| 75 template <typename T> class SupportsWeakPtr; | 75 template <typename T> class SupportsWeakPtr; |
| 76 template <typename T> class WeakPtr; | 76 template <typename T> class WeakPtr; |
| 77 | 77 |
| 78 namespace internal { | 78 namespace internal { |
| 79 // These classes are part of the WeakPtr implementation. | 79 // These classes are part of the WeakPtr implementation. |
| 80 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. | 80 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. |
| 81 | 81 |
| 82 class BASE_EXPORT WeakReference { | 82 class BASE_EXPORT WeakReference { |
| 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. | 93 // Remove this when crbug.com/234964 is addressed. |
| 94 void DetachFromThreadHack() { thread_checker_.DetachFromThread(); } | 94 void DetachFromThreadHack() { sequence_checker_.DetachFromSequence(); } |
| 95 | 95 |
| 96 private: | 96 private: |
| 97 friend class base::RefCountedThreadSafe<Flag>; | 97 friend class base::RefCountedThreadSafe<Flag>; |
| 98 | 98 |
| 99 ~Flag(); | 99 ~Flag(); |
| 100 | 100 |
| 101 ThreadChecker thread_checker_; | 101 SequenceChecker sequence_checker_; |
| 102 bool is_valid_; | 102 bool is_valid_; |
| 103 }; | 103 }; |
| 104 | 104 |
| 105 WeakReference(); | 105 WeakReference(); |
| 106 explicit WeakReference(const Flag* flag); | 106 explicit WeakReference(const Flag* flag); |
| 107 ~WeakReference(); | 107 ~WeakReference(); |
| 108 | 108 |
| 109 bool is_valid() const; | 109 bool is_valid() const; |
| 110 | 110 |
| 111 private: | 111 private: |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. | 346 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
| 347 | 347 |
| 348 template <typename Derived> | 348 template <typename Derived> |
| 349 WeakPtr<Derived> AsWeakPtr(Derived* t) { | 349 WeakPtr<Derived> AsWeakPtr(Derived* t) { |
| 350 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); | 350 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
| 351 } | 351 } |
| 352 | 352 |
| 353 } // namespace base | 353 } // namespace base |
| 354 | 354 |
| 355 #endif // BASE_MEMORY_WEAK_PTR_H_ | 355 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |