Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: base/memory/weak_ptr.h

Issue 1467003002: Switch to static_assert in base/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: / Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/memory/singleton_unittest.cc ('k') | base/message_loop/message_loop.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 class SupportsWeakPtrBase { 154 class SupportsWeakPtrBase {
155 public: 155 public:
156 // A safe static downcast of a WeakPtr<Base> to WeakPtr<Derived>. This 156 // A safe static downcast of a WeakPtr<Base> to WeakPtr<Derived>. This
157 // conversion will only compile if there is exists a Base which inherits 157 // conversion will only compile if there is exists a Base which inherits
158 // from SupportsWeakPtr<Base>. See base::AsWeakPtr() below for a helper 158 // from SupportsWeakPtr<Base>. See base::AsWeakPtr() below for a helper
159 // function that makes calling this easier. 159 // function that makes calling this easier.
160 template<typename Derived> 160 template<typename Derived>
161 static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) { 161 static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) {
162 typedef 162 typedef
163 is_convertible<Derived, internal::SupportsWeakPtrBase&> convertible; 163 is_convertible<Derived, internal::SupportsWeakPtrBase&> convertible;
164 COMPILE_ASSERT(convertible::value, 164 static_assert(convertible::value,
165 AsWeakPtr_argument_inherits_from_SupportsWeakPtr); 165 "AsWeakPtr argument must inherit from SupportsWeakPtr");
166 return AsWeakPtrImpl<Derived>(t, *t); 166 return AsWeakPtrImpl<Derived>(t, *t);
167 } 167 }
168 168
169 private: 169 private:
170 // 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
171 // which is an instance of SupportsWeakPtr<Base>. We can then safely 171 // which is an instance of SupportsWeakPtr<Base>. We can then safely
172 // static_cast the Base* to a Derived*. 172 // static_cast the Base* to a Derived*.
173 template <typename Derived, typename Base> 173 template <typename Derived, typename Base>
174 static WeakPtr<Derived> AsWeakPtrImpl( 174 static WeakPtr<Derived> AsWeakPtrImpl(
175 Derived* t, const SupportsWeakPtr<Base>&) { 175 Derived* t, const SupportsWeakPtr<Base>&) {
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. 336 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails.
337 337
338 template <typename Derived> 338 template <typename Derived>
339 WeakPtr<Derived> AsWeakPtr(Derived* t) { 339 WeakPtr<Derived> AsWeakPtr(Derived* t) {
340 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); 340 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t);
341 } 341 }
342 342
343 } // namespace base 343 } // namespace base
344 344
345 #endif // BASE_MEMORY_WEAK_PTR_H_ 345 #endif // BASE_MEMORY_WEAK_PTR_H_
OLDNEW
« no previous file with comments | « base/memory/singleton_unittest.cc ('k') | base/message_loop/message_loop.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698