| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 // dereferencing the Controller back pointer after the Controller has been | 45 // dereferencing the Controller back pointer after the Controller has been |
| 46 // destroyed. | 46 // destroyed. |
| 47 // | 47 // |
| 48 // WARNING: weak pointers are not threadsafe!!! You must only use a WeakPtr | 48 // WARNING: weak pointers are not threadsafe!!! You must only use a WeakPtr |
| 49 // instance on thread where it was created. | 49 // instance on thread where it was created. |
| 50 | 50 |
| 51 #ifndef BASE_MEMORY_WEAK_PTR_H_ | 51 #ifndef BASE_MEMORY_WEAK_PTR_H_ |
| 52 #define BASE_MEMORY_WEAK_PTR_H_ | 52 #define BASE_MEMORY_WEAK_PTR_H_ |
| 53 #pragma once | 53 #pragma once |
| 54 | 54 |
| 55 #include "base/base_api.h" | 55 #include "base/base_export.h" |
| 56 #include "base/logging.h" | 56 #include "base/logging.h" |
| 57 #include "base/memory/ref_counted.h" | 57 #include "base/memory/ref_counted.h" |
| 58 #include "base/threading/thread_checker.h" | 58 #include "base/threading/thread_checker.h" |
| 59 | 59 |
| 60 namespace base { | 60 namespace base { |
| 61 | 61 |
| 62 namespace internal { | 62 namespace internal { |
| 63 // These classes are part of the WeakPtr implementation. | 63 // These classes are part of the WeakPtr implementation. |
| 64 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. | 64 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. |
| 65 | 65 |
| 66 class BASE_API WeakReference { | 66 class BASE_EXPORT WeakReference { |
| 67 public: | 67 public: |
| 68 // While Flag is bound to a specific thread, it may be deleted from another | 68 // While Flag is bound to a specific thread, it may be deleted from another |
| 69 // via base::WeakPtr::~WeakPtr(). | 69 // via base::WeakPtr::~WeakPtr(). |
| 70 class Flag : public RefCountedThreadSafe<Flag> { | 70 class Flag : public RefCountedThreadSafe<Flag> { |
| 71 public: | 71 public: |
| 72 explicit Flag(Flag** handle); | 72 explicit Flag(Flag** handle); |
| 73 | 73 |
| 74 void Invalidate(); | 74 void Invalidate(); |
| 75 bool IsValid() const; | 75 bool IsValid() const; |
| 76 | 76 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 88 WeakReference(); | 88 WeakReference(); |
| 89 WeakReference(Flag* flag); | 89 WeakReference(Flag* flag); |
| 90 ~WeakReference(); | 90 ~WeakReference(); |
| 91 | 91 |
| 92 bool is_valid() const; | 92 bool is_valid() const; |
| 93 | 93 |
| 94 private: | 94 private: |
| 95 scoped_refptr<Flag> flag_; | 95 scoped_refptr<Flag> flag_; |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 class BASE_API WeakReferenceOwner { | 98 class BASE_EXPORT WeakReferenceOwner { |
| 99 public: | 99 public: |
| 100 WeakReferenceOwner(); | 100 WeakReferenceOwner(); |
| 101 ~WeakReferenceOwner(); | 101 ~WeakReferenceOwner(); |
| 102 | 102 |
| 103 WeakReference GetRef() const; | 103 WeakReference GetRef() const; |
| 104 | 104 |
| 105 bool HasRefs() const { | 105 bool HasRefs() const { |
| 106 return flag_ != NULL; | 106 return flag_ != NULL; |
| 107 } | 107 } |
| 108 | 108 |
| 109 void Invalidate(); | 109 void Invalidate(); |
| 110 | 110 |
| 111 // Indicates that this object will be used on another thread from now on. | 111 // Indicates that this object will be used on another thread from now on. |
| 112 void DetachFromThread() { | 112 void DetachFromThread() { |
| 113 if (flag_) flag_->DetachFromThread(); | 113 if (flag_) flag_->DetachFromThread(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 private: | 116 private: |
| 117 mutable WeakReference::Flag* flag_; | 117 mutable WeakReference::Flag* flag_; |
| 118 }; | 118 }; |
| 119 | 119 |
| 120 // This class simplifies the implementation of WeakPtr's type conversion | 120 // This class simplifies the implementation of WeakPtr's type conversion |
| 121 // constructor by avoiding the need for a public accessor for ref_. A | 121 // constructor by avoiding the need for a public accessor for ref_. A |
| 122 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this | 122 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this |
| 123 // base class gives us a way to access ref_ in a protected fashion. | 123 // base class gives us a way to access ref_ in a protected fashion. |
| 124 class BASE_API WeakPtrBase { | 124 class BASE_EXPORT WeakPtrBase { |
| 125 public: | 125 public: |
| 126 WeakPtrBase(); | 126 WeakPtrBase(); |
| 127 ~WeakPtrBase(); | 127 ~WeakPtrBase(); |
| 128 | 128 |
| 129 protected: | 129 protected: |
| 130 WeakPtrBase(const WeakReference& ref); | 130 WeakPtrBase(const WeakReference& ref); |
| 131 | 131 |
| 132 WeakReference ref_; | 132 WeakReference ref_; |
| 133 }; | 133 }; |
| 134 | 134 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 | 246 |
| 247 private: | 247 private: |
| 248 internal::WeakReferenceOwner weak_reference_owner_; | 248 internal::WeakReferenceOwner weak_reference_owner_; |
| 249 T* ptr_; | 249 T* ptr_; |
| 250 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); | 250 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); |
| 251 }; | 251 }; |
| 252 | 252 |
| 253 } // namespace base | 253 } // namespace base |
| 254 | 254 |
| 255 #endif // BASE_MEMORY_WEAK_PTR_H_ | 255 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |