Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: base/bind.h

Issue 2042223002: Introduce OnceClosure and BindOnce (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: swap Once & Repeating positions Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/bind_internal.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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_BIND_H_ 5 #ifndef BASE_BIND_H_
6 #define BASE_BIND_H_ 6 #define BASE_BIND_H_
7 7
8 #include "base/bind_internal.h" 8 #include "base/bind_internal.h"
9 9
10 // ----------------------------------------------------------------------------- 10 // -----------------------------------------------------------------------------
11 // Usage documentation 11 // Usage documentation
12 // ----------------------------------------------------------------------------- 12 // -----------------------------------------------------------------------------
13 // 13 //
14 // See //docs/callback.md for documentation. 14 // See //docs/callback.md for documentation.
15 // 15 //
16 // 16 //
17 // ----------------------------------------------------------------------------- 17 // -----------------------------------------------------------------------------
18 // Implementation notes 18 // Implementation notes
19 // ----------------------------------------------------------------------------- 19 // -----------------------------------------------------------------------------
20 // 20 //
21 // If you're reading the implementation, before proceeding further, you should 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 22 // read the top comment of base/bind_internal.h for a definition of common
23 // terms and concepts. 23 // terms and concepts.
24 24
25 namespace base { 25 namespace base {
26 namespace internal {
26 27
28 // Bind as OnceCallback.
27 template <typename Functor, typename... Args> 29 template <typename Functor, typename... Args>
28 inline base::Callback<MakeUnboundRunType<Functor, Args...>> Bind( 30 inline OnceCallback<MakeUnboundRunType<Functor, Args...>>
29 Functor&& functor, 31 BindOnce(Functor&& functor, Args&&... args) {
30 Args&&... args) {
31 using BindState = internal::MakeBindStateType<Functor, Args...>; 32 using BindState = internal::MakeBindStateType<Functor, Args...>;
32 using UnboundRunType = MakeUnboundRunType<Functor, Args...>; 33 using UnboundRunType = MakeUnboundRunType<Functor, Args...>;
33 using Invoker = internal::Invoker<BindState, UnboundRunType>; 34 using Invoker = internal::Invoker<BindState, UnboundRunType>;
34 using CallbackType = Callback<UnboundRunType>; 35 using CallbackType = OnceCallback<UnboundRunType>;
35 36
36 // Store the invoke func into PolymorphicInvoke before casting it to 37 // Store the invoke func into PolymorphicInvoke before casting it to
37 // InvokeFuncStorage, so that we can ensure its type matches to 38 // InvokeFuncStorage, so that we can ensure its type matches to
39 // PolymorphicInvoke, to which CallbackType will cast back.
40 using PolymorphicInvoke = typename CallbackType::PolymorphicInvoke;
41 PolymorphicInvoke invoke_func = &Invoker::RunOnce;
42
43 using InvokeFuncStorage = internal::BindStateBase::InvokeFuncStorage;
44 return CallbackType(new BindState(
45 reinterpret_cast<InvokeFuncStorage>(invoke_func),
46 std::forward<Functor>(functor),
47 std::forward<Args>(args)...));
48 }
49
50 // Bind as RepeatingCallback.
51 template <typename Functor, typename... Args>
52 inline RepeatingCallback<MakeUnboundRunType<Functor, Args...>>
53 BindRepeating(Functor&& functor, Args&&... args) {
54 using BindState = internal::MakeBindStateType<Functor, Args...>;
55 using UnboundRunType = MakeUnboundRunType<Functor, Args...>;
56 using Invoker = internal::Invoker<BindState, UnboundRunType>;
57 using CallbackType = RepeatingCallback<UnboundRunType>;
58
59 // Store the invoke func into PolymorphicInvoke before casting it to
60 // InvokeFuncStorage, so that we can ensure its type matches to
38 // PolymorphicInvoke, to which CallbackType will cast back. 61 // PolymorphicInvoke, to which CallbackType will cast back.
39 using PolymorphicInvoke = typename CallbackType::PolymorphicInvoke; 62 using PolymorphicInvoke = typename CallbackType::PolymorphicInvoke;
40 PolymorphicInvoke invoke_func = &Invoker::Run; 63 PolymorphicInvoke invoke_func = &Invoker::Run;
41 64
42 using InvokeFuncStorage = internal::BindStateBase::InvokeFuncStorage; 65 using InvokeFuncStorage = internal::BindStateBase::InvokeFuncStorage;
43 return CallbackType(new BindState( 66 return CallbackType(new BindState(
44 reinterpret_cast<InvokeFuncStorage>(invoke_func), 67 reinterpret_cast<InvokeFuncStorage>(invoke_func),
45 std::forward<Functor>(functor), 68 std::forward<Functor>(functor),
46 std::forward<Args>(args)...)); 69 std::forward<Args>(args)...));
47 } 70 }
48 71
72 } // namespace internal
73
74 // Unannotated Bind.
75 // TODO(tzik): Deprecate this and migrate to OnceCallback and
76 // RepeatingCallback, once they get ready.
77 template <typename Functor, typename... Args>
78 inline Callback<MakeUnboundRunType<Functor, Args...>>
79 Bind(Functor&& functor, Args&&... args) {
80 return internal::BindRepeating(std::forward<Functor>(functor),
81 std::forward<Args>(args)...);
82 }
83
49 } // namespace base 84 } // namespace base
50 85
51 #endif // BASE_BIND_H_ 86 #endif // BASE_BIND_H_
OLDNEW
« no previous file with comments | « no previous file | base/bind_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698