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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 // unbound from the SequencedTaskRunner/Thread. The WeakPtrFactory may then be | 63 // unbound from the SequencedTaskRunner/Thread. The WeakPtrFactory may then be |
64 // destroyed, or new WeakPtr objects may be used, from a different sequence. | 64 // destroyed, or new WeakPtr objects may be used, from a different sequence. |
65 // | 65 // |
66 // Thus, at least one WeakPtr object must exist and have been dereferenced on | 66 // Thus, at least one WeakPtr object must exist and have been dereferenced on |
67 // the correct thread to enforce that other WeakPtr objects will enforce they | 67 // the correct thread to enforce that other WeakPtr objects will enforce they |
68 // are used on the desired thread. | 68 // are used on the desired thread. |
69 | 69 |
70 #ifndef BASE_MEMORY_WEAK_PTR_H_ | 70 #ifndef BASE_MEMORY_WEAK_PTR_H_ |
71 #define BASE_MEMORY_WEAK_PTR_H_ | 71 #define BASE_MEMORY_WEAK_PTR_H_ |
72 | 72 |
| 73 #include <type_traits> |
| 74 |
73 #include "base/base_export.h" | 75 #include "base/base_export.h" |
74 #include "base/logging.h" | 76 #include "base/logging.h" |
75 #include "base/macros.h" | 77 #include "base/macros.h" |
76 #include "base/memory/ref_counted.h" | 78 #include "base/memory/ref_counted.h" |
77 #include "base/sequence_checker.h" | 79 #include "base/sequence_checker.h" |
78 #include "base/template_util.h" | |
79 | 80 |
80 namespace base { | 81 namespace base { |
81 | 82 |
82 template <typename T> class SupportsWeakPtr; | 83 template <typename T> class SupportsWeakPtr; |
83 template <typename T> class WeakPtr; | 84 template <typename T> class WeakPtr; |
84 | 85 |
85 namespace internal { | 86 namespace internal { |
86 // These classes are part of the WeakPtr implementation. | 87 // These classes are part of the WeakPtr implementation. |
87 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. | 88 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. |
88 | 89 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 // otherwise get instantiated separately for each distinct instantiation of | 154 // otherwise get instantiated separately for each distinct instantiation of |
154 // SupportsWeakPtr<>. | 155 // SupportsWeakPtr<>. |
155 class SupportsWeakPtrBase { | 156 class SupportsWeakPtrBase { |
156 public: | 157 public: |
157 // A safe static downcast of a WeakPtr<Base> to WeakPtr<Derived>. This | 158 // A safe static downcast of a WeakPtr<Base> to WeakPtr<Derived>. This |
158 // conversion will only compile if there is exists a Base which inherits | 159 // conversion will only compile if there is exists a Base which inherits |
159 // from SupportsWeakPtr<Base>. See base::AsWeakPtr() below for a helper | 160 // from SupportsWeakPtr<Base>. See base::AsWeakPtr() below for a helper |
160 // function that makes calling this easier. | 161 // function that makes calling this easier. |
161 template<typename Derived> | 162 template<typename Derived> |
162 static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) { | 163 static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) { |
163 typedef | 164 using convertible = |
164 is_convertible<Derived, internal::SupportsWeakPtrBase&> convertible; | 165 std::is_convertible<Derived*, internal::SupportsWeakPtrBase*>; |
165 static_assert(convertible::value, | 166 static_assert(convertible::value, |
166 "AsWeakPtr argument must inherit from SupportsWeakPtr"); | 167 "AsWeakPtr argument must inherit from SupportsWeakPtr"); |
167 return AsWeakPtrImpl<Derived>(t, *t); | 168 return AsWeakPtrImpl<Derived>(t, *t); |
168 } | 169 } |
169 | 170 |
170 private: | 171 private: |
171 // This template function uses type inference to find a Base of Derived | 172 // This template function uses type inference to find a Base of Derived |
172 // which is an instance of SupportsWeakPtr<Base>. We can then safely | 173 // which is an instance of SupportsWeakPtr<Base>. We can then safely |
173 // static_cast the Base* to a Derived*. | 174 // static_cast the Base* to a Derived*. |
174 template <typename Derived, typename Base> | 175 template <typename Derived, typename Base> |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. | 349 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
349 | 350 |
350 template <typename Derived> | 351 template <typename Derived> |
351 WeakPtr<Derived> AsWeakPtr(Derived* t) { | 352 WeakPtr<Derived> AsWeakPtr(Derived* t) { |
352 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); | 353 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
353 } | 354 } |
354 | 355 |
355 } // namespace base | 356 } // namespace base |
356 | 357 |
357 #endif // BASE_MEMORY_WEAK_PTR_H_ | 358 #endif // BASE_MEMORY_WEAK_PTR_H_ |
OLD | NEW |