[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_BIND_H_ |
| 6 | #define BASE_BIND_H_ |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 7 | |
| 8 | #include "base/bind_internal.h" |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 9 | |
[email protected] | 2429264 | 2012-07-12 20:06:40 | [diff] [blame] | 10 | // ----------------------------------------------------------------------------- |
| 11 | // Usage documentation |
| 12 | // ----------------------------------------------------------------------------- |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 13 | // |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 14 | // See //docs/callback.md for documentation. |
[email protected] | 2429264 | 2012-07-12 20:06:40 | [diff] [blame] | 15 | // |
| 16 | // |
| 17 | // ----------------------------------------------------------------------------- |
| 18 | // Implementation notes |
| 19 | // ----------------------------------------------------------------------------- |
| 20 | // |
| 21 | // If you're reading the implementation, before proceeding further, you should |
| 22 | // read the top comment of base/bind_internal.h for a definition of common |
| 23 | // terms and concepts. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 24 | |
| 25 | namespace base { |
| 26 | |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 27 | template <typename Functor, typename... Args> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 28 | inline base::Callback<MakeUnboundRunType<Functor, Args...>> Bind( |
| 29 | Functor&& functor, |
| 30 | Args&&... args) { |
| 31 | using BindState = internal::MakeBindStateType<Functor, Args...>; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 32 | using UnboundRunType = MakeUnboundRunType<Functor, Args...>; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 33 | using Invoker = internal::Invoker<BindState, UnboundRunType>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 34 | using CallbackType = Callback<UnboundRunType>; |
tzik | 1886c27 | 2016-09-08 05:45:38 | [diff] [blame^] | 35 | |
| 36 | // Store the invoke func into PolymorphicInvoke before casting it to |
| 37 | // InvokeFuncStorage, so that we can ensure its type matches to |
| 38 | // PolymorphicInvoke, to which CallbackType will cast back. |
| 39 | using PolymorphicInvoke = typename CallbackType::PolymorphicInvoke; |
| 40 | PolymorphicInvoke invoke_func = &Invoker::Run; |
| 41 | |
| 42 | using InvokeFuncStorage = internal::BindStateBase::InvokeFuncStorage; |
| 43 | return CallbackType(new BindState( |
| 44 | reinterpret_cast<InvokeFuncStorage>(invoke_func), |
| 45 | std::forward<Functor>(functor), |
| 46 | std::forward<Args>(args)...)); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 47 | } |
| 48 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 49 | } // namespace base |
| 50 | |
| 51 | #endif // BASE_BIND_H_ |