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 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 // Thread-safety notes: | 10 // Thread-safety notes: |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 // function that makes calling this easier. | 155 // function that makes calling this easier. |
156 template<typename Derived> | 156 template<typename Derived> |
157 static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) { | 157 static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) { |
158 typedef | 158 typedef |
159 is_convertible<Derived, internal::SupportsWeakPtrBase&> convertible; | 159 is_convertible<Derived, internal::SupportsWeakPtrBase&> convertible; |
160 COMPILE_ASSERT(convertible::value, | 160 COMPILE_ASSERT(convertible::value, |
161 AsWeakPtr_argument_inherits_from_SupportsWeakPtr); | 161 AsWeakPtr_argument_inherits_from_SupportsWeakPtr); |
162 return AsWeakPtrImpl<Derived>(t, *t); | 162 return AsWeakPtrImpl<Derived>(t, *t); |
163 } | 163 } |
164 | 164 |
165 protected: | |
166 SupportsWeakPtrBase() {} | |
167 ~SupportsWeakPtrBase() {} | |
Ryan Sleevi
2012/07/10 22:58:38
jar:
Unfortunately, for Windows this requires wra
| |
168 | |
165 private: | 169 private: |
166 // This template function uses type inference to find a Base of Derived | 170 // This template function uses type inference to find a Base of Derived |
167 // which is an instance of SupportsWeakPtr<Base>. We can then safely | 171 // which is an instance of SupportsWeakPtr<Base>. We can then safely |
168 // static_cast the Base* to a Derived*. | 172 // static_cast the Base* to a Derived*. |
169 template <typename Derived, typename Base> | 173 template <typename Derived, typename Base> |
170 static WeakPtr<Derived> AsWeakPtrImpl( | 174 static WeakPtr<Derived> AsWeakPtrImpl( |
171 Derived* t, const SupportsWeakPtr<Base>&) { | 175 Derived* t, const SupportsWeakPtr<Base>&) { |
172 WeakPtr<Base> ptr = t->Base::AsWeakPtr(); | 176 WeakPtr<Base> ptr = t->Base::AsWeakPtr(); |
173 return WeakPtr<Derived>(ptr.ref_, static_cast<Derived*>(ptr.ptr_)); | 177 return WeakPtr<Derived>(ptr.ref_, static_cast<Derived*>(ptr.ptr_)); |
174 } | 178 } |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 | 326 |
323 private: | 327 private: |
324 internal::WeakReferenceOwner weak_reference_owner_; | 328 internal::WeakReferenceOwner weak_reference_owner_; |
325 T* ptr_; | 329 T* ptr_; |
326 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); | 330 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); |
327 }; | 331 }; |
328 | 332 |
329 } // namespace base | 333 } // namespace base |
330 | 334 |
331 #endif // BASE_MEMORY_WEAK_PTR_H_ | 335 #endif // BASE_MEMORY_WEAK_PTR_H_ |
OLD | NEW |