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>::UnboundRunType> | |
55 Bind(Functor functor) { | |
56 // Typedefs for how to store and run the functor. | |
57 typedef typename internal::FunctorTraits<Functor>::RunnableType RunnableType; | |
58 typedef typename internal::FunctorTraits<Functor>::RunType RunType; | |
59 | |
60 typedef internal::BindState<RunnableType, RunType> BindState; | |
61 | |
62 return Callback<typename BindState::UnboundRunType>( | |
63 new BindState(internal::MakeRunnable(functor))); | |
64 } | |
65 | |
66 template <typename Functor, typename... Args> | 50 template <typename Functor, typename... Args> |
67 base::Callback< | 51 base::Callback< |
68 typename internal::BindState< | 52 typename internal::BindState< |
69 typename internal::FunctorTraits<Functor>::RunnableType, | 53 typename internal::FunctorTraits<Functor>::RunnableType, |
70 typename internal::FunctorTraits<Functor>::RunType, | 54 typename internal::FunctorTraits<Functor>::RunType, |
71 typename internal::CallbackParamTraits<Args>::StorageType...> | 55 typename internal::CallbackParamTraits<Args>::StorageType...> |
72 ::UnboundRunType> | 56 ::UnboundRunType> |
73 Bind(Functor functor, const Args&... args) { | 57 Bind(Functor functor, const Args&... args) { |
74 // Typedefs for how to store and run the functor. | 58 // Typedefs for how to store and run the functor. |
75 typedef typename internal::FunctorTraits<Functor>::RunnableType RunnableType; | 59 typedef typename internal::FunctorTraits<Functor>::RunnableType RunnableType; |
76 typedef typename internal::FunctorTraits<Functor>::RunType RunType; | 60 typedef typename internal::FunctorTraits<Functor>::RunType RunType; |
77 | 61 |
78 // Use RunnableType::RunType instead of RunType above because our | 62 // Use RunnableType::RunType instead of RunType above because our |
79 // checks should below for bound references need to know what the actual | 63 // checks should below for bound references need to know what the actual |
80 // functor is going to interpret the argument as. | 64 // functor is going to interpret the argument as. |
81 typedef typename RunnableType::RunType BoundRunType; | 65 typedef typename RunnableType::RunType BoundRunType; |
82 | 66 |
67 using BoundArgs = | |
68 internal::TakeTypeListItem<sizeof...(Args), | |
69 internal::ExtractArgs<BoundRunType>>; | |
dcheng
2015/12/15 19:42:54
OK, I understand why this is necessary now.
Shoul
tzik
2015/12/16 08:02:47
Added. Yes, we should.
| |
70 | |
83 // Do not allow binding a non-const reference parameter. Non-const reference | 71 // Do not allow binding a non-const reference parameter. Non-const reference |
84 // parameters are disallowed by the Google style guide. Also, binding a | 72 // parameters are disallowed by the Google style guide. Also, binding a |
85 // non-const reference parameter can make for subtle bugs because the | 73 // non-const reference parameter can make for subtle bugs because the |
86 // invoked function will receive a reference to the stored copy of the | 74 // invoked function will receive a reference to the stored copy of the |
87 // argument and not the original. | 75 // argument and not the original. |
88 static_assert(!internal::HasNonConstReferenceParam<BoundRunType>::value, | 76 static_assert(!internal::HasNonConstReferenceItem<BoundArgs>::value, |
89 "do not bind functions with nonconst ref"); | 77 "do not bind functions with nonconst ref"); |
90 | 78 |
91 const bool is_method = internal::HasIsMethodTag<RunnableType>::value; | 79 const bool is_method = internal::HasIsMethodTag<RunnableType>::value; |
92 | 80 |
93 // For methods, we need to be careful for parameter 1. We do not require | 81 // 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 | 82 // 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 | 83 // methods. We also disallow binding of an array as the method's target |
96 // object. | 84 // object. |
97 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, | 85 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, |
98 "first bound argument to method cannot be array"); | 86 "first bound argument to method cannot be array"); |
99 static_assert( | 87 static_assert( |
100 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, | 88 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, |
101 "a parameter is a refcounted type and needs scoped_refptr"); | 89 "a parameter is a refcounted type and needs scoped_refptr"); |
102 | 90 |
103 typedef internal::BindState< | 91 typedef internal::BindState< |
104 RunnableType, RunType, | 92 RunnableType, RunType, |
105 typename internal::CallbackParamTraits<Args>::StorageType...> | 93 typename internal::CallbackParamTraits<Args>::StorageType...> |
106 BindState; | 94 BindState; |
107 | 95 |
108 return Callback<typename BindState::UnboundRunType>( | 96 return Callback<typename BindState::UnboundRunType>( |
109 new BindState(internal::MakeRunnable(functor), args...)); | 97 new BindState(internal::MakeRunnable(functor), args...)); |
110 } | 98 } |
111 | 99 |
112 } // namespace base | 100 } // namespace base |
113 | 101 |
114 #endif // BASE_BIND_H_ | 102 #endif // BASE_BIND_H_ |
OLD | NEW |