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 #include "base/callback_internal.h" | 9 #include "base/callback_internal.h" |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 // via an indirection like we do below. | 40 // via an indirection like we do below. |
41 // | 41 // |
42 // TODO(ajwong): We might be able to avoid this now, but need to test. | 42 // TODO(ajwong): We might be able to avoid this now, but need to test. |
43 // | 43 // |
44 // It is possible to move most of the static_assert into BindState<>, but it | 44 // It is possible to move most of the static_assert into BindState<>, but it |
45 // feels a little nicer to have the asserts here so people do not need to crack | 45 // feels a little nicer to have the asserts here so people do not need to crack |
46 // open bind_internal.h. On the other hand, it makes Bind() harder to read. | 46 // open bind_internal.h. On the other hand, it makes Bind() harder to read. |
47 | 47 |
48 namespace base { | 48 namespace base { |
49 | 49 |
50 template <typename Functor> | |
51 base::Callback< | |
52 typename internal::BindState< | |
53 typename internal::FunctorTraits<Functor>::RunnableType, | |
54 typename internal::FunctorTraits<Functor>::RunType, | |
55 internal::TypeList<>>::UnboundRunType> | |
56 Bind(Functor functor) { | |
57 // Typedefs for how to store and run the functor. | |
58 typedef typename internal::FunctorTraits<Functor>::RunnableType RunnableType; | |
59 typedef typename internal::FunctorTraits<Functor>::RunType RunType; | |
60 | |
61 typedef internal::BindState<RunnableType, RunType, | |
62 internal::TypeList<>> BindState; | |
63 | |
64 return Callback<typename BindState::UnboundRunType>( | |
65 new BindState(internal::MakeRunnable(functor))); | |
66 } | |
67 | |
68 template <typename Functor, typename... Args> | 50 template <typename Functor, typename... Args> |
69 base::Callback< | 51 base::Callback< |
70 typename internal::BindState< | 52 typename internal::BindState< |
71 typename internal::FunctorTraits<Functor>::RunnableType, | 53 typename internal::FunctorTraits<Functor>::RunnableType, |
72 typename internal::FunctorTraits<Functor>::RunType, | 54 typename internal::FunctorTraits<Functor>::RunType, |
73 internal::TypeList< | 55 internal::TypeList< |
74 typename internal::CallbackParamTraits<Args>::StorageType...>> | 56 typename internal::CallbackParamTraits<Args>::StorageType...>> |
75 ::UnboundRunType> | 57 ::UnboundRunType> |
76 Bind(Functor functor, const Args&... args) { | 58 Bind(Functor functor, const Args&... args) { |
77 // Typedefs for how to store and run the functor. | 59 // Typedefs for how to store and run the functor. |
78 typedef typename internal::FunctorTraits<Functor>::RunnableType RunnableType; | 60 typedef typename internal::FunctorTraits<Functor>::RunnableType RunnableType; |
79 typedef typename internal::FunctorTraits<Functor>::RunType RunType; | 61 typedef typename internal::FunctorTraits<Functor>::RunType RunType; |
80 | 62 |
81 // Use RunnableType::RunType instead of RunType above because our | 63 // Use RunnableType::RunType instead of RunType above because our |
82 // checks should below for bound references need to know what the actual | 64 // checks should below for bound references need to know what the actual |
83 // functor is going to interpret the argument as. | 65 // functor is going to interpret the argument as. |
84 typedef typename RunnableType::RunType BoundRunType; | 66 typedef typename RunnableType::RunType BoundRunType; |
85 | 67 |
| 68 using BoundArgs = |
| 69 internal::TakeTypeListItem<sizeof...(Args), |
| 70 internal::ExtractArgs<BoundRunType>>; |
| 71 |
86 // Do not allow binding a non-const reference parameter. Non-const reference | 72 // Do not allow binding a non-const reference parameter. Non-const reference |
87 // parameters are disallowed by the Google style guide. Also, binding a | 73 // parameters are disallowed by the Google style guide. Also, binding a |
88 // non-const reference parameter can make for subtle bugs because the | 74 // non-const reference parameter can make for subtle bugs because the |
89 // invoked function will receive a reference to the stored copy of the | 75 // invoked function will receive a reference to the stored copy of the |
90 // argument and not the original. | 76 // argument and not the original. |
91 static_assert(!internal::HasNonConstReferenceParam<BoundRunType>::value, | 77 static_assert(!internal::HasNonConstReferenceItem<BoundArgs>::value, |
92 "do not bind functions with nonconst ref"); | 78 "do not bind functions with nonconst ref"); |
93 | 79 |
94 const bool is_method = internal::HasIsMethodTag<RunnableType>::value; | 80 const bool is_method = internal::HasIsMethodTag<RunnableType>::value; |
95 | 81 |
96 // For methods, we need to be careful for parameter 1. We do not require | 82 // For methods, we need to be careful for parameter 1. We do not require |
97 // a scoped_refptr because BindState<> itself takes care of AddRef() for | 83 // a scoped_refptr because BindState<> itself takes care of AddRef() for |
98 // methods. We also disallow binding of an array as the method's target | 84 // methods. We also disallow binding of an array as the method's target |
99 // object. | 85 // object. |
100 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, | 86 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, |
101 "first bound argument to method cannot be array"); | 87 "first bound argument to method cannot be array"); |
102 static_assert( | 88 static_assert( |
103 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, | 89 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, |
104 "a parameter is a refcounted type and needs scoped_refptr"); | 90 "a parameter is a refcounted type and needs scoped_refptr"); |
105 | 91 |
106 typedef internal::BindState< | 92 typedef internal::BindState< |
107 RunnableType, RunType, | 93 RunnableType, RunType, |
108 internal::TypeList< | 94 internal::TypeList< |
109 typename internal::CallbackParamTraits<Args>::StorageType...>> | 95 typename internal::CallbackParamTraits<Args>::StorageType...>> |
110 BindState; | 96 BindState; |
111 | 97 |
112 return Callback<typename BindState::UnboundRunType>( | 98 return Callback<typename BindState::UnboundRunType>( |
113 new BindState(internal::MakeRunnable(functor), args...)); | 99 new BindState(internal::MakeRunnable(functor), args...)); |
114 } | 100 } |
115 | 101 |
116 } // namespace base | 102 } // namespace base |
117 | 103 |
118 #endif // BASE_BIND_H_ | 104 #endif // BASE_BIND_H_ |
OLD | NEW |