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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 bool is_null() const { return bind_state_.get() == NULL; } | 66 bool is_null() const { return bind_state_.get() == NULL; } |
67 | 67 |
68 // Returns the Callback into an uninitialized state. | 68 // Returns the Callback into an uninitialized state. |
69 void Reset(); | 69 void Reset(); |
70 | 70 |
71 protected: | 71 protected: |
72 // In C++, it is safe to cast function pointers to function pointers of | 72 // In C++, it is safe to cast function pointers to function pointers of |
73 // another type. It is not okay to use void*. We create a InvokeFuncStorage | 73 // another type. It is not okay to use void*. We create a InvokeFuncStorage |
74 // that that can store our function pointer, and then cast it back to | 74 // that that can store our function pointer, and then cast it back to |
75 // the original type on usage. | 75 // the original type on usage. |
76 typedef void(*InvokeFuncStorage)(void); | 76 using InvokeFuncStorage = void(*)(); |
77 | 77 |
78 // Returns true if this callback equals |other|. |other| may be null. | 78 // Returns true if this callback equals |other|. |other| may be null. |
79 bool Equals(const CallbackBase& other) const; | 79 bool Equals(const CallbackBase& other) const; |
80 | 80 |
81 // Allow initializing of |bind_state_| via the constructor to avoid default | 81 // Allow initializing of |bind_state_| via the constructor to avoid default |
82 // initialization of the scoped_refptr. We do not also initialize | 82 // initialization of the scoped_refptr. We do not also initialize |
83 // |polymorphic_invoke_| here because doing a normal assignment in the | 83 // |polymorphic_invoke_| here because doing a normal assignment in the |
84 // derived Callback templates makes for much nicer compiler errors. | 84 // derived Callback templates makes for much nicer compiler errors. |
85 explicit CallbackBase(BindStateBase* bind_state); | 85 explicit CallbackBase(BindStateBase* bind_state); |
86 | 86 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // break passing of C-string literals. | 141 // break passing of C-string literals. |
142 template <typename T> | 142 template <typename T> |
143 struct CallbackParamTraits | 143 struct CallbackParamTraits |
144 : std::conditional<IsMoveOnlyType<T>::value, | 144 : std::conditional<IsMoveOnlyType<T>::value, |
145 CallbackParamTraitsForMoveOnlyType<T>, | 145 CallbackParamTraitsForMoveOnlyType<T>, |
146 CallbackParamTraitsForNonMoveOnlyType<T>>::type { | 146 CallbackParamTraitsForNonMoveOnlyType<T>>::type { |
147 }; | 147 }; |
148 | 148 |
149 template <typename T> | 149 template <typename T> |
150 struct CallbackParamTraitsForNonMoveOnlyType { | 150 struct CallbackParamTraitsForNonMoveOnlyType { |
151 typedef const T& ForwardType; | 151 using ForwardType = const T&; |
152 typedef T StorageType; | 152 using StorageType = T; |
153 }; | 153 }; |
154 | 154 |
155 // The Storage should almost be impossible to trigger unless someone manually | 155 // The Storage should almost be impossible to trigger unless someone manually |
156 // specifies type of the bind parameters. However, in case they do, | 156 // specifies type of the bind parameters. However, in case they do, |
157 // this will guard against us accidentally storing a reference parameter. | 157 // this will guard against us accidentally storing a reference parameter. |
158 // | 158 // |
159 // The ForwardType should only be used for unbound arguments. | 159 // The ForwardType should only be used for unbound arguments. |
160 template <typename T> | 160 template <typename T> |
161 struct CallbackParamTraitsForNonMoveOnlyType<T&> { | 161 struct CallbackParamTraitsForNonMoveOnlyType<T&> { |
162 typedef T& ForwardType; | 162 using ForwardType = T&; |
163 typedef T StorageType; | 163 using StorageType = T; |
164 }; | 164 }; |
165 | 165 |
166 // 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 |
167 // 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 |
168 // 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 |
169 // 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 |
170 // restriction. | 170 // restriction. |
171 template <typename T, size_t n> | 171 template <typename T, size_t n> |
172 struct CallbackParamTraitsForNonMoveOnlyType<T[n]> { | 172 struct CallbackParamTraitsForNonMoveOnlyType<T[n]> { |
173 typedef const T* ForwardType; | 173 using ForwardType = const T*; |
174 typedef const T* StorageType; | 174 using StorageType = const T*; |
175 }; | 175 }; |
176 | 176 |
177 // See comment for CallbackParamTraits<T[n]>. | 177 // See comment for CallbackParamTraits<T[n]>. |
178 template <typename T> | 178 template <typename T> |
179 struct CallbackParamTraitsForNonMoveOnlyType<T[]> { | 179 struct CallbackParamTraitsForNonMoveOnlyType<T[]> { |
180 typedef const T* ForwardType; | 180 using ForwardType = const T*; |
181 typedef const T* StorageType; | 181 using StorageType = const T*; |
182 }; | 182 }; |
183 | 183 |
184 // Parameter traits for movable-but-not-copyable scopers. | 184 // Parameter traits for movable-but-not-copyable scopers. |
185 // | 185 // |
186 // Callback<>/Bind() understands movable-but-not-copyable semantics where | 186 // Callback<>/Bind() understands movable-but-not-copyable semantics where |
187 // 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 |
188 // 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 |
189 // helper function. When used with Bind(), this signifies transferal of the | 189 // helper function. When used with Bind(), this signifies transferal of the |
190 // object's state to the target function. | 190 // object's state to the target function. |
191 // | 191 // |
192 // 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 |
193 // reference. A const reference is inappropriate, and would break const | 193 // reference. A const reference is inappropriate, and would break const |
194 // correctness, because we are implementing a destructive move. A non-const | 194 // correctness, because we are implementing a destructive move. A non-const |
195 // 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 |
196 // 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(). |
197 template <typename T> | 197 template <typename T> |
198 struct CallbackParamTraitsForMoveOnlyType { | 198 struct CallbackParamTraitsForMoveOnlyType { |
199 typedef T ForwardType; | 199 using ForwardType = T; |
200 typedef T StorageType; | 200 using StorageType = T; |
201 }; | 201 }; |
202 | 202 |
203 // 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() |
204 // 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 |
205 // types. It is needed because forwarding a movable-but-not-copyable | 205 // types. It is needed because forwarding a movable-but-not-copyable |
206 // argument to another function requires us to invoke the proper move | 206 // argument to another function requires us to invoke the proper move |
207 // 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 |
208 // whitelisted below as overloads of the CallbackForward() function. The | 208 // whitelisted below as overloads of the CallbackForward() function. The |
209 // default template compiles out to be a no-op. | 209 // default template compiles out to be a no-op. |
210 // | 210 // |
(...skipping 14 matching lines...) Expand all Loading... |
225 template <typename T> | 225 template <typename T> |
226 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward( | 226 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward( |
227 T& t) { | 227 T& t) { |
228 return std::move(t); | 228 return std::move(t); |
229 } | 229 } |
230 | 230 |
231 } // namespace internal | 231 } // namespace internal |
232 } // namespace base | 232 } // namespace base |
233 | 233 |
234 #endif // BASE_CALLBACK_INTERNAL_H_ | 234 #endif // BASE_CALLBACK_INTERNAL_H_ |
OLD | NEW |