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 #ifndef BASE_CALLBACK_H_ | 5 #ifndef BASE_CALLBACK_H_ |
6 #define BASE_CALLBACK_H_ | 6 #define BASE_CALLBACK_H_ |
7 | 7 |
8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
9 #include "base/callback_internal.h" | 9 #include "base/callback_internal.h" |
10 #include "base/template_util.h" | 10 #include "base/template_util.h" |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 PolymorphicInvoke invoke_func = | 379 PolymorphicInvoke invoke_func = |
380 &internal::BindState<Runnable, BindRunType, BoundArgsType...> | 380 &internal::BindState<Runnable, BindRunType, BoundArgsType...> |
381 ::InvokerType::Run; | 381 ::InvokerType::Run; |
382 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); | 382 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
383 } | 383 } |
384 | 384 |
385 bool Equals(const Callback& other) const { | 385 bool Equals(const Callback& other) const { |
386 return CallbackBase::Equals(other); | 386 return CallbackBase::Equals(other); |
387 } | 387 } |
388 | 388 |
389 R Run(typename internal::CallbackParamTraits<Args>::ForwardType... args) | 389 // Run() makes an extra copy compared to directly calling the bound function |
390 const { | 390 // if an argument is passed-by-value and is copyable-but-not-movable: |
| 391 // i.e. below copies CopyableNonMovableType twice. |
| 392 // void F(CopyableNonMovableType) {} |
| 393 // Bind(&F).Run(CopyableNonMovableType()); |
| 394 // |
| 395 // We can not fully apply Perfect Forwarding idiom to the callchain from |
| 396 // Callback::Run() to the target function. Perfect Forwarding requires |
| 397 // knowing how the caller will pass the arguments. However, the signature of |
| 398 // InvokerType::Run() needs to be fixed in the callback constructor, so Run() |
| 399 // cannot template its arguments based on how it's called. |
| 400 R Run(Args... args) const { |
391 PolymorphicInvoke f = | 401 PolymorphicInvoke f = |
392 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); | 402 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); |
393 | 403 return f(bind_state_.get(), std::forward<Args>(args)...); |
394 return f(bind_state_.get(), internal::CallbackForward(args)...); | |
395 } | 404 } |
396 | 405 |
397 private: | 406 private: |
398 using PolymorphicInvoke = | 407 using PolymorphicInvoke = R (*)(internal::BindStateBase*, Args&&...); |
399 R(*)(internal::BindStateBase*, | |
400 typename internal::CallbackParamTraits<Args>::ForwardType...); | |
401 }; | 408 }; |
402 | 409 |
403 } // namespace base | 410 } // namespace base |
404 | 411 |
405 #endif // BASE_CALLBACK_H_ | 412 #endif // BASE_CALLBACK_H_ |
OLD | NEW |