| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_TASK_RUNNER_UTIL_H_ | 5 #ifndef BASE_TASK_RUNNER_UTIL_H_ |
| 6 #define BASE_TASK_RUNNER_UTIL_H_ | 6 #define BASE_TASK_RUNNER_UTIL_H_ |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/callback_internal.h" | |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
| 13 | 12 |
| 14 namespace base { | 13 namespace base { |
| 15 | 14 |
| 16 namespace internal { | 15 namespace internal { |
| 17 | 16 |
| 18 // Adapts a function that produces a result via a return value to | 17 // Adapts a function that produces a result via a return value to |
| 19 // one that returns via an output parameter. | 18 // one that returns via an output parameter. |
| 20 template <typename ReturnType> | 19 template <typename ReturnType> |
| 21 void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func, | 20 void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func, |
| 22 ReturnType* result) { | 21 ReturnType* result) { |
| 23 *result = func.Run(); | 22 *result = func.Run(); |
| 24 } | 23 } |
| 25 | 24 |
| 26 // Adapts a T* result to a callblack that expects a T. | 25 // Adapts a T* result to a callblack that expects a T. |
| 27 template <typename TaskReturnType, typename ReplyArgType> | 26 template <typename TaskReturnType, typename ReplyArgType> |
| 28 void ReplyAdapter(const Callback<void(ReplyArgType)>& callback, | 27 void ReplyAdapter(const Callback<void(ReplyArgType)>& callback, |
| 29 TaskReturnType* result) { | 28 TaskReturnType* result) { |
| 30 // TODO(ajwong): Remove this conditional and add a DCHECK to enforce that | 29 // TODO(ajwong): Remove this conditional and add a DCHECK to enforce that |
| 31 // |reply| must be non-null in PostTaskAndReplyWithResult() below after | 30 // |reply| must be non-null in PostTaskAndReplyWithResult() below after |
| 32 // current code that relies on this API softness has been removed. | 31 // current code that relies on this API softness has been removed. |
| 33 // http://crbug.com/162712 | 32 // http://crbug.com/162712 |
| 34 if (!callback.is_null()) | 33 if (!callback.is_null()) |
| 35 callback.Run(CallbackForward(*result)); | 34 callback.Run(std::move(*result)); |
| 36 } | 35 } |
| 37 | 36 |
| 38 } // namespace internal | 37 } // namespace internal |
| 39 | 38 |
| 40 // When you have these methods | 39 // When you have these methods |
| 41 // | 40 // |
| 42 // R DoWorkAndReturn(); | 41 // R DoWorkAndReturn(); |
| 43 // void Callback(const R& result); | 42 // void Callback(const R& result); |
| 44 // | 43 // |
| 45 // and want to call them in a PostTaskAndReply kind of fashion where the | 44 // and want to call them in a PostTaskAndReply kind of fashion where the |
| (...skipping 16 matching lines...) Expand all Loading... |
| 62 from_here, | 61 from_here, |
| 63 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, | 62 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, |
| 64 result), | 63 result), |
| 65 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, | 64 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, |
| 66 base::Owned(result))); | 65 base::Owned(result))); |
| 67 } | 66 } |
| 68 | 67 |
| 69 } // namespace base | 68 } // namespace base |
| 70 | 69 |
| 71 #endif // BASE_TASK_RUNNER_UTIL_H_ | 70 #endif // BASE_TASK_RUNNER_UTIL_H_ |
| OLD | NEW |