OLD | NEW |
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 base/callback.h for documentation. | 14 // See base/callback.h 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 RepeatingCallback. |
27 template <typename Functor, typename... Args> | 29 template <typename Functor, typename... Args> |
28 inline base::Callback<MakeUnboundRunType<Functor, Args...>> Bind( | 30 inline RepeatingCallback<MakeUnboundRunType<Functor, Args...>> |
29 Functor&& functor, | 31 BindRepeating(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 | 35 |
35 using CallbackType = Callback<UnboundRunType>; | 36 using CallbackType = RepeatingCallback<UnboundRunType>; |
36 return CallbackType(new BindState(std::forward<Functor>(functor), | 37 return CallbackType(new BindState(std::forward<Functor>(functor), |
37 std::forward<Args>(args)...), | 38 std::forward<Args>(args)...), |
38 &Invoker::Run); | 39 &Invoker::Run); |
39 } | 40 } |
40 | 41 |
| 42 // Bind as OneShotCallback. |
| 43 template <typename Functor, typename... Args> |
| 44 inline OneShotCallback<MakeUnboundRunType<Functor, Args...>> |
| 45 BindOneShot(Functor&& functor, Args&&... args) { |
| 46 using BindState = internal::MakeBindStateType<Functor, Args...>; |
| 47 using UnboundRunType = MakeUnboundRunType<Functor, Args...>; |
| 48 using Invoker = internal::Invoker<BindState, UnboundRunType>; |
| 49 |
| 50 using CallbackType = OneShotCallback<UnboundRunType>; |
| 51 return CallbackType(new BindState(std::forward<Functor>(functor), |
| 52 std::forward<Args>(args)...), |
| 53 &Invoker::RunOneShot); |
| 54 } |
| 55 |
| 56 } // namespace internal |
| 57 |
| 58 // Unannotated Bind. |
| 59 template <typename Functor, typename... Args> |
| 60 inline Callback<MakeUnboundRunType<Functor, Args...>> |
| 61 Bind(Functor&& functor, Args&&... args) { |
| 62 return internal::BindRepeating(std::forward<Functor>(functor), |
| 63 std::forward<Args>(args)...); |
| 64 } |
| 65 |
41 } // namespace base | 66 } // namespace base |
42 | 67 |
43 #endif // BASE_BIND_H_ | 68 #endif // BASE_BIND_H_ |
OLD | NEW |