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 | 10 |
11 // NOTE: Header files that do not require the full definition of Callback or | 11 // NOTE: Header files that do not require the full definition of Callback or |
12 // Closure should #include "base/callback_forward.h" instead of this file. | 12 // Closure should #include "base/callback_forward.h" instead of this file. |
13 | 13 |
14 // ----------------------------------------------------------------------------- | 14 // ----------------------------------------------------------------------------- |
15 // Usage documentation | 15 // Usage documentation |
16 // ----------------------------------------------------------------------------- | 16 // ----------------------------------------------------------------------------- |
17 // | 17 // |
18 // See //docs/callback.md for documentation. | 18 // See //docs/callback.md for documentation. |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 | 21 |
22 template <typename R, typename... Args, internal::CopyMode copy_mode> | 22 template <typename R, typename... Args, internal::CopyMode copy_mode> |
23 class Callback<R(Args...), copy_mode> | 23 class Callback<R(Args...), copy_mode> |
24 : public internal::CallbackBase<copy_mode> { | 24 : public internal::CallbackBase<copy_mode> { |
25 private: | 25 public: |
26 using PolymorphicInvoke = R (*)(internal::BindStateBase*, Args&&...); | 26 using PolymorphicInvoke = R (*)(internal::BindStateBase*, Args&&...); |
27 | 27 |
28 public: | |
29 // MSVC 2013 doesn't support Type Alias of function types. | 28 // MSVC 2013 doesn't support Type Alias of function types. |
30 // Revisit this after we update it to newer version. | 29 // Revisit this after we update it to newer version. |
31 typedef R RunType(Args...); | 30 typedef R RunType(Args...); |
32 | 31 |
33 Callback() : internal::CallbackBase<copy_mode>(nullptr) {} | 32 Callback() : internal::CallbackBase<copy_mode>(nullptr) {} |
34 | 33 |
35 Callback(internal::BindStateBase* bind_state, PolymorphicInvoke invoke_func) | 34 explicit Callback(internal::BindStateBase* bind_state) |
36 : internal::CallbackBase<copy_mode>(bind_state) { | 35 : internal::CallbackBase<copy_mode>(bind_state) { |
37 using InvokeFuncStorage = | |
38 typename internal::CallbackBase<copy_mode>::InvokeFuncStorage; | |
39 this->polymorphic_invoke_ = | |
40 reinterpret_cast<InvokeFuncStorage>(invoke_func); | |
41 } | 36 } |
42 | 37 |
43 bool Equals(const Callback& other) const { | 38 bool Equals(const Callback& other) const { |
44 return this->EqualsInternal(other); | 39 return this->EqualsInternal(other); |
45 } | 40 } |
46 | 41 |
47 // Run() makes an extra copy compared to directly calling the bound function | 42 // Run() makes an extra copy compared to directly calling the bound function |
48 // if an argument is passed-by-value and is copyable-but-not-movable: | 43 // if an argument is passed-by-value and is copyable-but-not-movable: |
49 // i.e. below copies CopyableNonMovableType twice. | 44 // i.e. below copies CopyableNonMovableType twice. |
50 // void F(CopyableNonMovableType) {} | 45 // void F(CopyableNonMovableType) {} |
51 // Bind(&F).Run(CopyableNonMovableType()); | 46 // Bind(&F).Run(CopyableNonMovableType()); |
52 // | 47 // |
53 // We can not fully apply Perfect Forwarding idiom to the callchain from | 48 // We can not fully apply Perfect Forwarding idiom to the callchain from |
54 // Callback::Run() to the target function. Perfect Forwarding requires | 49 // Callback::Run() to the target function. Perfect Forwarding requires |
55 // knowing how the caller will pass the arguments. However, the signature of | 50 // knowing how the caller will pass the arguments. However, the signature of |
56 // InvokerType::Run() needs to be fixed in the callback constructor, so Run() | 51 // InvokerType::Run() needs to be fixed in the callback constructor, so Run() |
57 // cannot template its arguments based on how it's called. | 52 // cannot template its arguments based on how it's called. |
58 R Run(Args... args) const { | 53 R Run(Args... args) const { |
59 PolymorphicInvoke f = | 54 PolymorphicInvoke f = |
60 reinterpret_cast<PolymorphicInvoke>(this->polymorphic_invoke_); | 55 reinterpret_cast<PolymorphicInvoke>(this->polymorphic_invoke()); |
61 return f(this->bind_state_.get(), std::forward<Args>(args)...); | 56 return f(this->bind_state_.get(), std::forward<Args>(args)...); |
62 } | 57 } |
63 }; | 58 }; |
64 | 59 |
65 } // namespace base | 60 } // namespace base |
66 | 61 |
67 #endif // BASE_CALLBACK_H_ | 62 #endif // BASE_CALLBACK_H_ |
OLD | NEW |