blob: ce717972e26cde23941d545ef5e3f8832309466e [file] [log] [blame]
[email protected]b38d3572011-02-15 01:27:381// 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]b38d3572011-02-15 01:27:387
8#include "base/bind_internal.h"
[email protected]b38d3572011-02-15 01:27:389
[email protected]24292642012-07-12 20:06:4010// -----------------------------------------------------------------------------
11// Usage documentation
12// -----------------------------------------------------------------------------
[email protected]b38d3572011-02-15 01:27:3813//
tzik703f1562016-09-02 07:36:5514// See //docs/callback.md for documentation.
[email protected]24292642012-07-12 20:06:4015//
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]b38d3572011-02-15 01:27:3824
25namespace base {
26
tzik27d1e312016-09-13 05:28:5927// Bind as OnceCallback.
tzik401dd3672014-11-26 07:54:5828template <typename Functor, typename... Args>
tzik27d1e312016-09-13 05:28:5929inline OnceCallback<MakeUnboundRunType<Functor, Args...>>
30BindOnce(Functor&& functor, Args&&... args) {
tzik99de02b2016-07-01 05:54:1231 using BindState = internal::MakeBindStateType<Functor, Args...>;
tzikcaf1d84b2016-06-28 12:22:2132 using UnboundRunType = MakeUnboundRunType<Functor, Args...>;
tzikcaf1d84b2016-06-28 12:22:2133 using Invoker = internal::Invoker<BindState, UnboundRunType>;
tzik27d1e312016-09-13 05:28:5934 using CallbackType = OnceCallback<UnboundRunType>;
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::RunOnce;
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)...));
47}
48
49// Bind as RepeatingCallback.
50template <typename Functor, typename... Args>
51inline RepeatingCallback<MakeUnboundRunType<Functor, Args...>>
52BindRepeating(Functor&& functor, Args&&... args) {
53 using BindState = internal::MakeBindStateType<Functor, Args...>;
54 using UnboundRunType = MakeUnboundRunType<Functor, Args...>;
55 using Invoker = internal::Invoker<BindState, UnboundRunType>;
56 using CallbackType = RepeatingCallback<UnboundRunType>;
tzik1886c272016-09-08 05:45:3857
58 // Store the invoke func into PolymorphicInvoke before casting it to
59 // InvokeFuncStorage, so that we can ensure its type matches to
60 // PolymorphicInvoke, to which CallbackType will cast back.
61 using PolymorphicInvoke = typename CallbackType::PolymorphicInvoke;
62 PolymorphicInvoke invoke_func = &Invoker::Run;
63
64 using InvokeFuncStorage = internal::BindStateBase::InvokeFuncStorage;
65 return CallbackType(new BindState(
66 reinterpret_cast<InvokeFuncStorage>(invoke_func),
67 std::forward<Functor>(functor),
68 std::forward<Args>(args)...));
[email protected]fccef1552011-11-28 22:13:5469}
70
tzik27d1e312016-09-13 05:28:5971// Unannotated Bind.
72// TODO(tzik): Deprecate this and migrate to OnceCallback and
73// RepeatingCallback, once they get ready.
74template <typename Functor, typename... Args>
75inline Callback<MakeUnboundRunType<Functor, Args...>>
76Bind(Functor&& functor, Args&&... args) {
tzik07dbcfd2016-10-12 08:49:0377 return BindRepeating(std::forward<Functor>(functor),
78 std::forward<Args>(args)...);
tzik27d1e312016-09-13 05:28:5979}
80
[email protected]b38d3572011-02-15 01:27:3881} // namespace base
82
83#endif // BASE_BIND_H_