| 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 // 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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 struct CallbackParamTraitsForMoveOnlyType; | 122 struct CallbackParamTraitsForMoveOnlyType; |
| 123 | 123 |
| 124 template <typename> | 124 template <typename> |
| 125 struct CallbackParamTraitsForNonMoveOnlyType; | 125 struct CallbackParamTraitsForNonMoveOnlyType; |
| 126 | 126 |
| 127 // TODO(tzik): Use a default parameter once MSVS supports variadic templates | 127 // TODO(tzik): Use a default parameter once MSVS supports variadic templates |
| 128 // with default values. | 128 // with default values. |
| 129 // http://connect.microsoft.com/VisualStudio/feedbackdetail/view/957801/compilat
ion-error-with-variadic-templates | 129 // http://connect.microsoft.com/VisualStudio/feedbackdetail/view/957801/compilat
ion-error-with-variadic-templates |
| 130 // | 130 // |
| 131 // This is a typetraits object that's used to take an argument type, and | 131 // This is a typetraits object that's used to take an argument type, and |
| 132 // extract a suitable type for forwarding arguments. | 132 // extract a suitable type for storing and forwarding arguments. |
| 133 // |
| 134 // In particular, it strips off references, and converts arrays to |
| 135 // pointers for storage; and it avoids accidentally trying to create a |
| 136 // "reference of a reference" if the argument is a reference type. |
| 137 // |
| 138 // This array type becomes an issue for storage because we are passing bound |
| 139 // parameters by const reference. In this case, we end up passing an actual |
| 140 // array type in the initializer list which C++ does not allow. This will |
| 141 // break passing of C-string literals. |
| 133 template <typename T> | 142 template <typename T> |
| 134 struct CallbackParamTraits | 143 struct CallbackParamTraits |
| 135 : std::conditional<IsMoveOnlyType<T>::value, | 144 : std::conditional<IsMoveOnlyType<T>::value, |
| 136 CallbackParamTraitsForMoveOnlyType<T>, | 145 CallbackParamTraitsForMoveOnlyType<T>, |
| 137 CallbackParamTraitsForNonMoveOnlyType<T>>::type { | 146 CallbackParamTraitsForNonMoveOnlyType<T>>::type { |
| 138 }; | 147 }; |
| 139 | 148 |
| 140 template <typename T> | 149 template <typename T> |
| 141 struct CallbackParamTraitsForNonMoveOnlyType { | 150 struct CallbackParamTraitsForNonMoveOnlyType { |
| 142 using ForwardType = const T&; | 151 using ForwardType = const T&; |
| 152 using StorageType = T; |
| 153 }; |
| 154 |
| 155 // The Storage should almost be impossible to trigger unless someone manually |
| 156 // specifies type of the bind parameters. However, in case they do, |
| 157 // this will guard against us accidentally storing a reference parameter. |
| 158 // |
| 159 // The ForwardType should only be used for unbound arguments. |
| 160 template <typename T> |
| 161 struct CallbackParamTraitsForNonMoveOnlyType<T&> { |
| 162 using ForwardType = T&; |
| 163 using StorageType = T; |
| 143 }; | 164 }; |
| 144 | 165 |
| 145 // Note that for array types, we implicitly add a const in the conversion. This | 166 // Note that for array types, we implicitly add a const in the conversion. This |
| 146 // means that it is not possible to bind array arguments to functions that take | 167 // means that it is not possible to bind array arguments to functions that take |
| 147 // a non-const pointer. Trying to specialize the template based on a "const | 168 // a non-const pointer. Trying to specialize the template based on a "const |
| 148 // T[n]" does not seem to match correctly, so we are stuck with this | 169 // T[n]" does not seem to match correctly, so we are stuck with this |
| 149 // restriction. | 170 // restriction. |
| 150 template <typename T, size_t n> | 171 template <typename T, size_t n> |
| 151 struct CallbackParamTraitsForNonMoveOnlyType<T[n]> { | 172 struct CallbackParamTraitsForNonMoveOnlyType<T[n]> { |
| 152 using ForwardType = const T*; | 173 using ForwardType = const T*; |
| 174 using StorageType = const T*; |
| 153 }; | 175 }; |
| 154 | 176 |
| 155 // See comment for CallbackParamTraits<T[n]>. | 177 // See comment for CallbackParamTraits<T[n]>. |
| 156 template <typename T> | 178 template <typename T> |
| 157 struct CallbackParamTraitsForNonMoveOnlyType<T[]> { | 179 struct CallbackParamTraitsForNonMoveOnlyType<T[]> { |
| 158 using ForwardType = const T*; | 180 using ForwardType = const T*; |
| 181 using StorageType = const T*; |
| 159 }; | 182 }; |
| 160 | 183 |
| 161 // Parameter traits for movable-but-not-copyable scopers. | 184 // Parameter traits for movable-but-not-copyable scopers. |
| 162 // | 185 // |
| 163 // Callback<>/Bind() understands movable-but-not-copyable semantics where | 186 // Callback<>/Bind() understands movable-but-not-copyable semantics where |
| 164 // the type cannot be copied but can still have its state destructively | 187 // the type cannot be copied but can still have its state destructively |
| 165 // transferred (aka. moved) to another instance of the same type by calling a | 188 // transferred (aka. moved) to another instance of the same type by calling a |
| 166 // helper function. When used with Bind(), this signifies transferal of the | 189 // helper function. When used with Bind(), this signifies transferal of the |
| 167 // object's state to the target function. | 190 // object's state to the target function. |
| 168 // | 191 // |
| 169 // For these types, the ForwardType must not be a const reference, or a | 192 // For these types, the ForwardType must not be a const reference, or a |
| 170 // reference. A const reference is inappropriate, and would break const | 193 // reference. A const reference is inappropriate, and would break const |
| 171 // correctness, because we are implementing a destructive move. A non-const | 194 // correctness, because we are implementing a destructive move. A non-const |
| 172 // reference cannot be used with temporaries which means the result of a | 195 // reference cannot be used with temporaries which means the result of a |
| 173 // function or a cast would not be usable with Callback<> or Bind(). | 196 // function or a cast would not be usable with Callback<> or Bind(). |
| 174 template <typename T> | 197 template <typename T> |
| 175 struct CallbackParamTraitsForMoveOnlyType { | 198 struct CallbackParamTraitsForMoveOnlyType { |
| 176 using ForwardType = T; | 199 using ForwardType = T; |
| 200 using StorageType = T; |
| 177 }; | 201 }; |
| 178 | 202 |
| 179 // CallbackForward() is a very limited simulation of C++11's std::forward() | 203 // CallbackForward() is a very limited simulation of C++11's std::forward() |
| 180 // used by the Callback/Bind system for a set of movable-but-not-copyable | 204 // used by the Callback/Bind system for a set of movable-but-not-copyable |
| 181 // types. It is needed because forwarding a movable-but-not-copyable | 205 // types. It is needed because forwarding a movable-but-not-copyable |
| 182 // argument to another function requires us to invoke the proper move | 206 // argument to another function requires us to invoke the proper move |
| 183 // operator to create a rvalue version of the type. The supported types are | 207 // operator to create a rvalue version of the type. The supported types are |
| 184 // whitelisted below as overloads of the CallbackForward() function. The | 208 // whitelisted below as overloads of the CallbackForward() function. The |
| 185 // default template compiles out to be a no-op. | 209 // default template compiles out to be a no-op. |
| 186 // | 210 // |
| (...skipping 14 matching lines...) Expand all Loading... |
| 201 template <typename T> | 225 template <typename T> |
| 202 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward( | 226 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward( |
| 203 T& t) { | 227 T& t) { |
| 204 return std::move(t); | 228 return std::move(t); |
| 205 } | 229 } |
| 206 | 230 |
| 207 } // namespace internal | 231 } // namespace internal |
| 208 } // namespace base | 232 } // namespace base |
| 209 | 233 |
| 210 #endif // BASE_CALLBACK_INTERNAL_H_ | 234 #endif // BASE_CALLBACK_INTERNAL_H_ |
| OLD | NEW |