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 // ----------------------------------------------------------------------------- |
(...skipping 28 matching lines...) Expand all Loading... | |
39 // via an indirection like we do below. | 39 // via an indirection like we do below. |
40 // | 40 // |
41 // TODO(ajwong): We might be able to avoid this now, but need to test. | 41 // TODO(ajwong): We might be able to avoid this now, but need to test. |
42 // | 42 // |
43 // It is possible to move most of the static_assert into BindState<>, but it | 43 // It is possible to move most of the static_assert into BindState<>, but it |
44 // feels a little nicer to have the asserts here so people do not need to crack | 44 // feels a little nicer to have the asserts here so people do not need to crack |
45 // open bind_internal.h. On the other hand, it makes Bind() harder to read. | 45 // open bind_internal.h. On the other hand, it makes Bind() harder to read. |
46 | 46 |
47 namespace base { | 47 namespace base { |
48 | 48 |
49 namespace internal { | |
50 | |
51 // Don't use Alias Template directly here to avoid a compile error on MSVC2013. | |
52 template <typename Functor, typename... Args> | 49 template <typename Functor, typename... Args> |
53 struct MakeUnboundRunTypeImpl { | 50 using MakeUnboundRunType = internal::MakeUnboundRunType<Functor, Args...>; |
dcheng
2016/06/27 07:57:22
Nit: It seems a little unusual to have base::MakeU
tzik
2016/06/27 08:43:11
We have several place that uses MakeUnboundRunType
| |
54 using Type = | |
55 typename BindState< | |
56 typename FunctorTraits<Functor>::RunnableType, | |
57 typename FunctorTraits<Functor>::RunType, | |
58 Args...>::UnboundRunType; | |
59 }; | |
60 | |
61 } // namespace internal | |
62 | 51 |
63 template <typename Functor, typename... Args> | 52 template <typename Functor, typename... Args> |
64 using MakeUnboundRunType = | 53 inline base::Callback<MakeUnboundRunType<Functor, Args...>> |
65 typename internal::MakeUnboundRunTypeImpl<Functor, Args...>::Type; | |
66 | |
67 template <typename Functor, typename... Args> | |
68 base::Callback<MakeUnboundRunType<Functor, Args...>> | |
69 Bind(Functor functor, Args&&... args) { | 54 Bind(Functor functor, Args&&... args) { |
70 // Type aliases for how to store and run the functor. | 55 // Type aliases for how to store and run the functor. |
71 using RunnableType = typename internal::FunctorTraits<Functor>::RunnableType; | 56 using RunnableType = typename internal::FunctorTraits<Functor>::RunnableType; |
72 using RunType = typename internal::FunctorTraits<Functor>::RunType; | |
73 | 57 |
74 // Use RunnableType::RunType instead of RunType above because our | 58 // Use RunnableType::RunType instead of RunType above because our |
75 // checks below for bound references need to know what the actual | 59 // checks below for bound references need to know what the actual |
76 // functor is going to interpret the argument as. | 60 // functor is going to interpret the argument as. |
77 using BoundRunType = typename RunnableType::RunType; | 61 using BoundRunType = typename RunnableType::RunType; |
78 | 62 |
79 using BoundArgs = | 63 using BoundArgs = |
80 internal::TakeTypeListItem<sizeof...(Args), | 64 internal::TakeTypeListItem<sizeof...(Args), |
81 internal::ExtractArgs<BoundRunType>>; | 65 internal::ExtractArgs<BoundRunType>>; |
82 | 66 |
(...skipping 10 matching lines...) Expand all Loading... | |
93 // For methods, we need to be careful for parameter 1. We do not require | 77 // For methods, we need to be careful for parameter 1. We do not require |
94 // a scoped_refptr because BindState<> itself takes care of AddRef() for | 78 // a scoped_refptr because BindState<> itself takes care of AddRef() for |
95 // methods. We also disallow binding of an array as the method's target | 79 // methods. We also disallow binding of an array as the method's target |
96 // object. | 80 // object. |
97 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, | 81 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, |
98 "first bound argument to method cannot be array"); | 82 "first bound argument to method cannot be array"); |
99 static_assert( | 83 static_assert( |
100 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, | 84 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, |
101 "a parameter is a refcounted type and needs scoped_refptr"); | 85 "a parameter is a refcounted type and needs scoped_refptr"); |
102 | 86 |
103 using BindState = internal::BindState<RunnableType, RunType, Args...>; | 87 using BindState = internal::BindState<RunnableType, Args...>; |
88 using UnboundRunType = MakeUnboundRunType<Functor, Args...>; | |
89 using CallbackType = Callback<UnboundRunType>; | |
90 using Invoker = internal::Invoker<BindState, UnboundRunType>; | |
104 | 91 |
105 return Callback<typename BindState::UnboundRunType>( | 92 return CallbackType(new BindState(internal::MakeRunnable(functor), |
106 new BindState(internal::MakeRunnable(functor), | 93 std::forward<Args>(args)...), |
107 std::forward<Args>(args)...)); | 94 &Invoker::Run); |
108 } | 95 } |
109 | 96 |
110 } // namespace base | 97 } // namespace base |
111 | 98 |
112 #endif // BASE_BIND_H_ | 99 #endif // BASE_BIND_H_ |
OLD | NEW |