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

Side by Side Diff: base/callback_internal.h

Issue 1496403002: base: Stop using Pass() on move-only types in Bind and Callback. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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
« base/bind_helpers.h ('K') | « base/bind_helpers.h ('k') | no next file » | 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 // This file contains utility functions and classes that help the 5 // This file contains utility functions and classes that help the
6 // implementation, and management of the Callback objects. 6 // implementation, and management of the Callback objects.
7 7
8 #ifndef BASE_CALLBACK_INTERNAL_H_ 8 #ifndef BASE_CALLBACK_INTERNAL_H_
9 #define BASE_CALLBACK_INTERNAL_H_ 9 #define BASE_CALLBACK_INTERNAL_H_
10 10
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 // Force the destructor to be instantiated inside this translation unit so 86 // Force the destructor to be instantiated inside this translation unit so
87 // that our subclasses will not get inlined versions. Avoids more template 87 // that our subclasses will not get inlined versions. Avoids more template
88 // bloat. 88 // bloat.
89 ~CallbackBase(); 89 ~CallbackBase();
90 90
91 scoped_refptr<BindStateBase> bind_state_; 91 scoped_refptr<BindStateBase> bind_state_;
92 InvokeFuncStorage polymorphic_invoke_; 92 InvokeFuncStorage polymorphic_invoke_;
93 }; 93 };
94 94
95 // A helper template to determine if given type is non-const move-only-type, 95 // A helper template to determine if given type is non-const move-only-type,
96 // i.e. if a value of the given type should be passed via .Pass() in a 96 // i.e. if a value of the given type should be passed via std::move() in a
97 // destructive way. 97 // destructive way.
98 template <typename T> struct IsMoveOnlyType { 98 template <typename T> struct IsMoveOnlyType {
99 template <typename U> 99 template <typename U>
100 static YesType Test(const typename U::MoveOnlyTypeForCPP03*); 100 static YesType Test(const typename U::MoveOnlyTypeForCPP03*);
101 101
102 template <typename U> 102 template <typename U>
103 static NoType Test(...); 103 static NoType Test(...);
104 104
105 static const bool value = sizeof((Test<T>(0))) == sizeof(YesType) && 105 static const bool value = sizeof((Test<T>(0))) == sizeof(YesType) &&
106 !is_const<T>::value; 106 !is_const<T>::value;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 190
191 // CallbackForward() is a very limited simulation of C++11's std::forward() 191 // CallbackForward() is a very limited simulation of C++11's std::forward()
192 // used by the Callback/Bind system for a set of movable-but-not-copyable 192 // used by the Callback/Bind system for a set of movable-but-not-copyable
193 // types. It is needed because forwarding a movable-but-not-copyable 193 // types. It is needed because forwarding a movable-but-not-copyable
194 // argument to another function requires us to invoke the proper move 194 // argument to another function requires us to invoke the proper move
195 // operator to create a rvalue version of the type. The supported types are 195 // operator to create a rvalue version of the type. The supported types are
196 // whitelisted below as overloads of the CallbackForward() function. The 196 // whitelisted below as overloads of the CallbackForward() function. The
197 // default template compiles out to be a no-op. 197 // default template compiles out to be a no-op.
198 // 198 //
199 // In C++11, std::forward would replace all uses of this function. However, it 199 // In C++11, std::forward would replace all uses of this function. However, it
200 // is impossible to implement a general std::forward with C++11 due to a lack 200 // is impossible to implement a general std::forward without C++11 due to a lack
201 // of rvalue references. 201 // of rvalue references.
202 // 202 //
203 // In addition to Callback/Bind, this is used by PostTaskAndReplyWithResult to 203 // In addition to Callback/Bind, this is used by PostTaskAndReplyWithResult to
204 // simulate std::forward() and forward the result of one Callback as a 204 // simulate std::forward() and forward the result of one Callback as a
205 // parameter to another callback. This is to support Callbacks that return 205 // parameter to another callback. This is to support Callbacks that return
206 // the movable-but-not-copyable types whitelisted above. 206 // the movable-but-not-copyable types whitelisted above.
207 template <typename T> 207 template <typename T>
208 typename std::enable_if<!IsMoveOnlyType<T>::value, T>::type& CallbackForward( 208 typename std::enable_if<!IsMoveOnlyType<T>::value, T>::type& CallbackForward(
209 T& t) { 209 T& t) {
210 return t; 210 return t;
211 } 211 }
212 212
213 template <typename T> 213 template <typename T>
214 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward( 214 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(
215 T& t) { 215 T& t) {
216 return t.Pass(); 216 return std::move(t);
217 } 217 }
218 218
219 } // namespace internal 219 } // namespace internal
220 } // namespace base 220 } // namespace base
221 221
222 #endif // BASE_CALLBACK_INTERNAL_H_ 222 #endif // BASE_CALLBACK_INTERNAL_H_
OLDNEW
« base/bind_helpers.h ('K') | « base/bind_helpers.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698