| OLD | NEW |
| 1 // Copyright (c) 2009 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 |
| 11 // list of all referrers, and then when the shared object is destroyed, it | 11 // list of all referrers, and then when the shared object is destroyed, it |
| (...skipping 33 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_WEAK_PTR_H_ | 51 #ifndef BASE_WEAK_PTR_H_ |
| 52 #define BASE_WEAK_PTR_H_ | 52 #define BASE_WEAK_PTR_H_ |
| 53 #pragma once | 53 #pragma once |
| 54 | 54 |
| 55 #include "base/base_api.h" |
| 55 #include "base/logging.h" | 56 #include "base/logging.h" |
| 56 #include "base/ref_counted.h" | 57 #include "base/ref_counted.h" |
| 57 #include "base/threading/non_thread_safe.h" | 58 #include "base/threading/non_thread_safe.h" |
| 58 | 59 |
| 59 namespace base { | 60 namespace base { |
| 60 | 61 |
| 61 namespace internal { | 62 namespace internal { |
| 62 // These classes are part of the WeakPtr implementation. | 63 // These classes are part of the WeakPtr implementation. |
| 63 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. | 64 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. |
| 64 | 65 |
| 65 class WeakReference { | 66 class BASE_API WeakReference { |
| 66 public: | 67 public: |
| 67 class Flag : public RefCounted<Flag>, public base::NonThreadSafe { | 68 class Flag : public RefCounted<Flag>, public base::NonThreadSafe { |
| 68 public: | 69 public: |
| 69 Flag(Flag** handle); | 70 Flag(Flag** handle); |
| 70 ~Flag(); | 71 ~Flag(); |
| 71 | 72 |
| 72 void AddRef() const; | 73 void AddRef() const; |
| 73 void Release() const; | 74 void Release() const; |
| 74 void Invalidate(); | 75 void Invalidate(); |
| 75 bool IsValid() const; | 76 bool IsValid() const; |
| 76 | 77 |
| 77 void DetachFromThread() { base::NonThreadSafe::DetachFromThread(); } | 78 void DetachFromThread() { base::NonThreadSafe::DetachFromThread(); } |
| 78 | 79 |
| 79 private: | 80 private: |
| 80 Flag** handle_; | 81 Flag** handle_; |
| 81 }; | 82 }; |
| 82 | 83 |
| 83 WeakReference(); | 84 WeakReference(); |
| 84 WeakReference(Flag* flag); | 85 WeakReference(Flag* flag); |
| 85 ~WeakReference(); | 86 ~WeakReference(); |
| 86 | 87 |
| 87 bool is_valid() const; | 88 bool is_valid() const; |
| 88 | 89 |
| 89 private: | 90 private: |
| 90 scoped_refptr<Flag> flag_; | 91 scoped_refptr<Flag> flag_; |
| 91 }; | 92 }; |
| 92 | 93 |
| 93 class WeakReferenceOwner { | 94 class BASE_API WeakReferenceOwner { |
| 94 public: | 95 public: |
| 95 WeakReferenceOwner(); | 96 WeakReferenceOwner(); |
| 96 ~WeakReferenceOwner(); | 97 ~WeakReferenceOwner(); |
| 97 | 98 |
| 98 WeakReference GetRef() const; | 99 WeakReference GetRef() const; |
| 99 | 100 |
| 100 bool HasRefs() const { | 101 bool HasRefs() const { |
| 101 return flag_ != NULL; | 102 return flag_ != NULL; |
| 102 } | 103 } |
| 103 | 104 |
| 104 void Invalidate(); | 105 void Invalidate(); |
| 105 | 106 |
| 106 // Indicates that this object will be used on another thread from now on. | 107 // Indicates that this object will be used on another thread from now on. |
| 107 void DetachFromThread() { | 108 void DetachFromThread() { |
| 108 if (flag_) flag_->DetachFromThread(); | 109 if (flag_) flag_->DetachFromThread(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 private: | 112 private: |
| 112 mutable WeakReference::Flag* flag_; | 113 mutable WeakReference::Flag* flag_; |
| 113 }; | 114 }; |
| 114 | 115 |
| 115 // This class simplifies the implementation of WeakPtr's type conversion | 116 // This class simplifies the implementation of WeakPtr's type conversion |
| 116 // constructor by avoiding the need for a public accessor for ref_. A | 117 // constructor by avoiding the need for a public accessor for ref_. A |
| 117 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this | 118 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this |
| 118 // base class gives us a way to access ref_ in a protected fashion. | 119 // base class gives us a way to access ref_ in a protected fashion. |
| 119 class WeakPtrBase { | 120 class BASE_API WeakPtrBase { |
| 120 public: | 121 public: |
| 121 WeakPtrBase(); | 122 WeakPtrBase(); |
| 122 ~WeakPtrBase(); | 123 ~WeakPtrBase(); |
| 123 | 124 |
| 124 protected: | 125 protected: |
| 125 WeakPtrBase(const WeakReference& ref); | 126 WeakPtrBase(const WeakReference& ref); |
| 126 | 127 |
| 127 WeakReference ref_; | 128 WeakReference ref_; |
| 128 }; | 129 }; |
| 129 | 130 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 | 237 |
| 237 private: | 238 private: |
| 238 internal::WeakReferenceOwner weak_reference_owner_; | 239 internal::WeakReferenceOwner weak_reference_owner_; |
| 239 T* ptr_; | 240 T* ptr_; |
| 240 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); | 241 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); |
| 241 }; | 242 }; |
| 242 | 243 |
| 243 } // namespace base | 244 } // namespace base |
| 244 | 245 |
| 245 #endif // BASE_WEAK_PTR_H_ | 246 #endif // BASE_WEAK_PTR_H_ |
| OLD | NEW |