| 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 // ----------------------------------------------------------------------------- |
| 11 // Usage documentation | 11 // Usage documentation |
| 12 // ----------------------------------------------------------------------------- | 12 // ----------------------------------------------------------------------------- |
| 13 // | 13 // |
| 14 // See base/callback.h for documentation. | 14 // See base/callback.h for documentation. |
| 15 // | 15 // |
| 16 // | 16 // |
| 17 // ----------------------------------------------------------------------------- | 17 // ----------------------------------------------------------------------------- |
| 18 // Implementation notes | 18 // Implementation notes |
| 19 // ----------------------------------------------------------------------------- | 19 // ----------------------------------------------------------------------------- |
| 20 // | 20 // |
| 21 // If you're reading the implementation, before proceeding further, you should | 21 // If you're reading the implementation, before proceeding further, you should |
| 22 // read the top comment of base/bind_internal.h for a definition of common | 22 // read the top comment of base/bind_internal.h for a definition of common |
| 23 // terms and concepts. | 23 // terms and concepts. |
| 24 // | |
| 25 // RETURN TYPES | |
| 26 // | |
| 27 // Though Bind()'s result is meant to be stored in a Callback<> type, it | |
| 28 // cannot actually return the exact type without requiring a large amount | |
| 29 // of extra template specializations. The problem is that in order to | |
| 30 // discern the correct specialization of Callback<>, Bind would need to | |
| 31 // unwrap the function signature to determine the signature's arity, and | |
| 32 // whether or not it is a method. | |
| 33 // | |
| 34 // Each unique combination of (arity, function_type, num_prebound) where | |
| 35 // function_type is one of {function, method, const_method} would require | |
| 36 // one specialization. We eventually have to do a similar number of | |
| 37 // specializations anyways in the implementation (see the Invoker<>, | |
| 38 // classes). However, it is avoidable in Bind if we return the result | |
| 39 // via an indirection like we do below. | |
| 40 // | |
| 41 // TODO(ajwong): We might be able to avoid this now, but need to test. | |
| 42 // | |
| 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 | |
| 45 // open bind_internal.h. On the other hand, it makes Bind() harder to read. | |
| 46 | 24 |
| 47 namespace base { | 25 namespace base { |
| 48 | 26 |
| 49 template <typename Functor, typename... Args> | 27 template <typename Functor, typename... Args> |
| 50 inline base::Callback<MakeUnboundRunType<Functor, Args...>> | 28 inline base::Callback<MakeUnboundRunType<Functor, Args...>> Bind( |
| 51 Bind(Functor functor, Args&&... args) { | 29 Functor&& functor, |
| 52 // Type aliases for how to store and run the functor. | 30 Args&&... args) { |
| 53 using RunnableType = typename internal::FunctorTraits<Functor>::RunnableType; | 31 using BindState = internal::MakeBindStateType<Functor, Args...>; |
| 54 | |
| 55 const bool is_method = internal::HasIsMethodTag<RunnableType>::value; | |
| 56 | |
| 57 // For methods, we need to be careful for parameter 1. We do not require | |
| 58 // a scoped_refptr because BindState<> itself takes care of AddRef() for | |
| 59 // methods. We also disallow binding of an array as the method's target | |
| 60 // object. | |
| 61 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, | |
| 62 "first bound argument to method cannot be array"); | |
| 63 static_assert( | |
| 64 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, | |
| 65 "a parameter is a refcounted type and needs scoped_refptr"); | |
| 66 | |
| 67 using BindState = internal::BindState<RunnableType, Args...>; | |
| 68 using UnboundRunType = MakeUnboundRunType<Functor, Args...>; | 32 using UnboundRunType = MakeUnboundRunType<Functor, Args...>; |
| 69 using CallbackType = Callback<UnboundRunType>; | |
| 70 using Invoker = internal::Invoker<BindState, UnboundRunType>; | 33 using Invoker = internal::Invoker<BindState, UnboundRunType>; |
| 71 | 34 |
| 72 return CallbackType(new BindState(internal::MakeRunnable(functor), | 35 using CallbackType = Callback<UnboundRunType>; |
| 36 return CallbackType(new BindState(std::forward<Functor>(functor), |
| 73 std::forward<Args>(args)...), | 37 std::forward<Args>(args)...), |
| 74 &Invoker::Run); | 38 &Invoker::Run); |
| 75 } | 39 } |
| 76 | 40 |
| 77 } // namespace base | 41 } // namespace base |
| 78 | 42 |
| 79 #endif // BASE_BIND_H_ | 43 #endif // BASE_BIND_H_ |
| OLD | NEW |