| 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 inline base::Callback<MakeUnboundRunType<Functor, Args...>> |
| 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 | |
| 63 template <typename Functor, typename... Args> | |
| 64 using MakeUnboundRunType = | |
| 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) { | 51 Bind(Functor functor, Args&&... args) { |
| 70 // Type aliases for how to store and run the functor. | 52 // Type aliases for how to store and run the functor. |
| 71 using RunnableType = typename internal::FunctorTraits<Functor>::RunnableType; | 53 using RunnableType = typename internal::FunctorTraits<Functor>::RunnableType; |
| 72 using RunType = typename internal::FunctorTraits<Functor>::RunType; | 54 |
| 73 const bool is_method = internal::HasIsMethodTag<RunnableType>::value; | 55 const bool is_method = internal::HasIsMethodTag<RunnableType>::value; |
| 74 | 56 |
| 75 // For methods, we need to be careful for parameter 1. We do not require | 57 // For methods, we need to be careful for parameter 1. We do not require |
| 76 // a scoped_refptr because BindState<> itself takes care of AddRef() for | 58 // a scoped_refptr because BindState<> itself takes care of AddRef() for |
| 77 // methods. We also disallow binding of an array as the method's target | 59 // methods. We also disallow binding of an array as the method's target |
| 78 // object. | 60 // object. |
| 79 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, | 61 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, |
| 80 "first bound argument to method cannot be array"); | 62 "first bound argument to method cannot be array"); |
| 81 static_assert( | 63 static_assert( |
| 82 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, | 64 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, |
| 83 "a parameter is a refcounted type and needs scoped_refptr"); | 65 "a parameter is a refcounted type and needs scoped_refptr"); |
| 84 | 66 |
| 85 using BindState = internal::BindState<RunnableType, RunType, Args...>; | 67 using BindState = internal::BindState<RunnableType, Args...>; |
| 68 using UnboundRunType = MakeUnboundRunType<Functor, Args...>; |
| 69 using CallbackType = Callback<UnboundRunType>; |
| 70 using Invoker = internal::Invoker<BindState, UnboundRunType>; |
| 86 | 71 |
| 87 return Callback<typename BindState::UnboundRunType>( | 72 return CallbackType(new BindState(internal::MakeRunnable(functor), |
| 88 new BindState(internal::MakeRunnable(functor), | 73 std::forward<Args>(args)...), |
| 89 std::forward<Args>(args)...)); | 74 &Invoker::Run); |
| 90 } | 75 } |
| 91 | 76 |
| 92 } // namespace base | 77 } // namespace base |
| 93 | 78 |
| 94 #endif // BASE_BIND_H_ | 79 #endif // BASE_BIND_H_ |
| OLD | NEW |