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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 namespace base { | 48 namespace base { |
49 | 49 |
50 template <typename Functor, typename... Args> | 50 template <typename Functor, typename... Args> |
51 base::Callback< | 51 base::Callback< |
52 typename internal::BindState< | 52 typename internal::BindState< |
53 typename internal::FunctorTraits<Functor>::RunnableType, | 53 typename internal::FunctorTraits<Functor>::RunnableType, |
54 typename internal::FunctorTraits<Functor>::RunType, | 54 typename internal::FunctorTraits<Functor>::RunType, |
55 typename internal::CallbackParamTraits<Args>::StorageType...> | 55 typename internal::CallbackParamTraits<Args>::StorageType...> |
56 ::UnboundRunType> | 56 ::UnboundRunType> |
57 Bind(Functor functor, const Args&... args) { | 57 Bind(Functor functor, const Args&... args) { |
58 // Typedefs for how to store and run the functor. | 58 // Type aliases for how to store and run the functor. |
59 typedef typename internal::FunctorTraits<Functor>::RunnableType RunnableType; | 59 using RunnableType = typename internal::FunctorTraits<Functor>::RunnableType; |
60 typedef typename internal::FunctorTraits<Functor>::RunType RunType; | 60 using RunType = typename internal::FunctorTraits<Functor>::RunType; |
61 | 61 |
62 // Use RunnableType::RunType instead of RunType above because our | 62 // Use RunnableType::RunType instead of RunType above because our |
63 // checks should below for bound references need to know what the actual | 63 // checks below for bound references need to know what the actual |
64 // functor is going to interpret the argument as. | 64 // functor is going to interpret the argument as. |
65 typedef typename RunnableType::RunType BoundRunType; | 65 using BoundRunType = typename RunnableType::RunType; |
66 | 66 |
67 using BoundArgs = | 67 using BoundArgs = |
68 internal::TakeTypeListItem<sizeof...(Args), | 68 internal::TakeTypeListItem<sizeof...(Args), |
69 internal::ExtractArgs<BoundRunType>>; | 69 internal::ExtractArgs<BoundRunType>>; |
70 | 70 |
71 // 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 |
72 // parameters are disallowed by the Google style guide. Also, binding a | 72 // parameters are disallowed by the Google style guide. Also, binding a |
73 // non-const reference parameter can make for subtle bugs because the | 73 // non-const reference parameter can make for subtle bugs because the |
74 // 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 |
75 // argument and not the original. | 75 // argument and not the original. |
76 static_assert(!internal::HasNonConstReferenceItem<BoundArgs>::value, | 76 static_assert(!internal::HasNonConstReferenceItem<BoundArgs>::value, |
77 "do not bind functions with nonconst ref"); | 77 "do not bind functions with nonconst ref"); |
78 | 78 |
79 const bool is_method = internal::HasIsMethodTag<RunnableType>::value; | 79 const bool is_method = internal::HasIsMethodTag<RunnableType>::value; |
80 | 80 |
81 // 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 |
82 // a scoped_refptr because BindState<> itself takes care of AddRef() for | 82 // a scoped_refptr because BindState<> itself takes care of AddRef() for |
83 // 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 |
84 // object. | 84 // object. |
85 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, | 85 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, |
86 "first bound argument to method cannot be array"); | 86 "first bound argument to method cannot be array"); |
87 static_assert( | 87 static_assert( |
88 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, | 88 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, |
89 "a parameter is a refcounted type and needs scoped_refptr"); | 89 "a parameter is a refcounted type and needs scoped_refptr"); |
90 | 90 |
91 typedef internal::BindState< | 91 using BindState = internal::BindState< |
92 RunnableType, RunType, | 92 RunnableType, RunType, |
93 typename internal::CallbackParamTraits<Args>::StorageType...> | 93 typename internal::CallbackParamTraits<Args>::StorageType...>; |
94 BindState; | |
95 | 94 |
96 return Callback<typename BindState::UnboundRunType>( | 95 return Callback<typename BindState::UnboundRunType>( |
97 new BindState(internal::MakeRunnable(functor), args...)); | 96 new BindState(internal::MakeRunnable(functor), args...)); |
98 } | 97 } |
99 | 98 |
100 } // namespace base | 99 } // namespace base |
101 | 100 |
102 #endif // BASE_BIND_H_ | 101 #endif // BASE_BIND_H_ |
OLD | NEW |