| 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 | 10 |
| 10 // ----------------------------------------------------------------------------- | 11 // ----------------------------------------------------------------------------- |
| 11 // Usage documentation | 12 // Usage documentation |
| 12 // ----------------------------------------------------------------------------- | 13 // ----------------------------------------------------------------------------- |
| 13 // | 14 // |
| 14 // See base/callback.h for documentation. | 15 // See base/callback.h for documentation. |
| 15 // | 16 // |
| 16 // | 17 // |
| 17 // ----------------------------------------------------------------------------- | 18 // ----------------------------------------------------------------------------- |
| 18 // Implementation notes | 19 // Implementation notes |
| (...skipping 25 matching lines...) Expand all Loading... |
| 44 // 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 |
| 45 // 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. |
| 46 | 47 |
| 47 namespace base { | 48 namespace base { |
| 48 | 49 |
| 49 template <typename Functor, typename... Args> | 50 template <typename Functor, typename... Args> |
| 50 base::Callback< | 51 base::Callback< |
| 51 typename internal::BindState< | 52 typename internal::BindState< |
| 52 typename internal::FunctorTraits<Functor>::RunnableType, | 53 typename internal::FunctorTraits<Functor>::RunnableType, |
| 53 typename internal::FunctorTraits<Functor>::RunType, | 54 typename internal::FunctorTraits<Functor>::RunType, |
| 54 typename std::decay<Args>::type...>::UnboundRunType> | 55 typename internal::CallbackParamTraits<Args>::StorageType...> |
| 55 Bind(Functor functor, Args&&... args) { | 56 ::UnboundRunType> |
| 57 Bind(Functor functor, const Args&... args) { |
| 56 // Type aliases for how to store and run the functor. | 58 // Type aliases for how to store and run the functor. |
| 57 using RunnableType = typename internal::FunctorTraits<Functor>::RunnableType; | 59 using RunnableType = typename internal::FunctorTraits<Functor>::RunnableType; |
| 58 using RunType = typename internal::FunctorTraits<Functor>::RunType; | 60 using RunType = typename internal::FunctorTraits<Functor>::RunType; |
| 59 | 61 |
| 60 // Use RunnableType::RunType instead of RunType above because our | 62 // Use RunnableType::RunType instead of RunType above because our |
| 61 // checks below for bound references need to know what the actual | 63 // checks below for bound references need to know what the actual |
| 62 // functor is going to interpret the argument as. | 64 // functor is going to interpret the argument as. |
| 63 using BoundRunType = typename RunnableType::RunType; | 65 using BoundRunType = typename RunnableType::RunType; |
| 64 | 66 |
| 65 using BoundArgs = | 67 using BoundArgs = |
| (...skipping 14 matching lines...) Expand all Loading... |
| 80 // a scoped_refptr because BindState<> itself takes care of AddRef() for | 82 // a scoped_refptr because BindState<> itself takes care of AddRef() for |
| 81 // 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 |
| 82 // object. | 84 // object. |
| 83 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, | 85 static_assert(!internal::BindsArrayToFirstArg<is_method, Args...>::value, |
| 84 "first bound argument to method cannot be array"); | 86 "first bound argument to method cannot be array"); |
| 85 static_assert( | 87 static_assert( |
| 86 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, | 88 !internal::HasRefCountedParamAsRawPtr<is_method, Args...>::value, |
| 87 "a parameter is a refcounted type and needs scoped_refptr"); | 89 "a parameter is a refcounted type and needs scoped_refptr"); |
| 88 | 90 |
| 89 using BindState = internal::BindState< | 91 using BindState = internal::BindState< |
| 90 RunnableType, RunType, typename std::decay<Args>::type...>; | 92 RunnableType, RunType, |
| 93 typename internal::CallbackParamTraits<Args>::StorageType...>; |
| 91 | 94 |
| 92 return Callback<typename BindState::UnboundRunType>( | 95 return Callback<typename BindState::UnboundRunType>( |
| 93 new BindState(internal::MakeRunnable(functor), | 96 new BindState(internal::MakeRunnable(functor), args...)); |
| 94 std::forward<Args>(args)...)); | |
| 95 } | 97 } |
| 96 | 98 |
| 97 } // namespace base | 99 } // namespace base |
| 98 | 100 |
| 99 #endif // BASE_BIND_H_ | 101 #endif // BASE_BIND_H_ |
| OLD | NEW |