Chromium Code Reviews| 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 <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 // R DoWorkAndReturn(); | 21 // R DoWorkAndReturn(); |
| 22 // void Callback(const R& result); | 22 // void Callback(const R& result); |
| 23 // | 23 // |
| 24 // and want to call them in a PostTaskAndReply kind of fashion where the | 24 // and want to call them in a PostTaskAndReply kind of fashion where the |
| 25 // result of DoWorkAndReturn is passed to the Callback, you can use | 25 // result of DoWorkAndReturn is passed to the Callback, you can use |
| 26 // PostTaskAndReplyWithResult as in this example: | 26 // PostTaskAndReplyWithResult as in this example: |
| 27 // | 27 // |
| 28 // PostTaskAndReplyWithResult( | 28 // PostTaskAndReplyWithResult( |
| 29 // target_thread_.task_runner(), | 29 // target_thread_.task_runner(), |
| 30 // FROM_HERE, | 30 // FROM_HERE, |
| 31 // Bind(&DoWorkAndReturn), | 31 // BindOnce(&DoWorkAndReturn), |
| 32 // Bind(&Callback)); | 32 // BindOnce(&Callback)); |
| 33 template <typename TaskReturnType, typename ReplyArgType> | |
| 34 bool PostTaskAndReplyWithResult(TaskRunner* task_runner, | |
| 35 const tracked_objects::Location& from_here, | |
| 36 OnceCallback<TaskReturnType()> task, | |
| 37 OnceCallback<void(ReplyArgType)> reply) { | |
| 38 DCHECK(task); | |
| 39 DCHECK(reply); | |
| 40 TaskReturnType* result = new TaskReturnType(); | |
| 41 return task_runner->PostTaskAndReply( | |
| 42 from_here, | |
| 43 BindOnce(&internal::ReturnAsParamAdapter<TaskReturnType>, std::move(task), | |
| 44 result), | |
| 45 BindOnce(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, | |
| 46 std::move(reply), Owned(result))); | |
| 47 } | |
| 48 | |
| 49 // Callback version of PostTaskAndReplyWithResult above. | |
| 50 // Though RepeatingCallback is convertible to OnceCallback, we need this since | |
| 51 // we can not use template deduction and object conversion at once on the | |
|
gab
2017/05/01 18:14:35
s/can not/cannot/
tzik
2017/05/02 15:28:12
Done.
| |
| 52 // overload resolution. | |
| 53 // TODO(crbug.com/714018): Update all callers of the Callback version to use | |
| 54 // OnceCallback. | |
| 33 template <typename TaskReturnType, typename ReplyArgType> | 55 template <typename TaskReturnType, typename ReplyArgType> |
| 34 bool PostTaskAndReplyWithResult(TaskRunner* task_runner, | 56 bool PostTaskAndReplyWithResult(TaskRunner* task_runner, |
| 35 const tracked_objects::Location& from_here, | 57 const tracked_objects::Location& from_here, |
| 36 Callback<TaskReturnType()> task, | 58 Callback<TaskReturnType()> task, |
| 37 Callback<void(ReplyArgType)> reply) { | 59 Callback<void(ReplyArgType)> reply) { |
| 38 DCHECK(task); | 60 return PostTaskAndReplyWithResult( |
| 39 DCHECK(reply); | 61 task_runner, from_here, |
| 40 TaskReturnType* result = new TaskReturnType(); | 62 static_cast<OnceCallback<TaskReturnType()>>(std::move(task)), |
| 41 return task_runner->PostTaskAndReply( | 63 static_cast<OnceCallback<void(ReplyArgType)>>(std::move(reply))); |
|
gab
2017/05/01 18:14:35
Use copy constructor directly instead of static_ca
tzik
2017/05/02 15:28:12
Ah, I forgot it. Done.
| |
| 42 from_here, | |
| 43 base::BindOnce(&internal::ReturnAsParamAdapter<TaskReturnType>, | |
| 44 std::move(task), result), | |
| 45 base::BindOnce(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, | |
| 46 std::move(reply), base::Owned(result))); | |
| 47 } | 64 } |
| 48 | 65 |
| 49 } // namespace base | 66 } // namespace base |
| 50 | 67 |
| 51 #endif // BASE_TASK_RUNNER_UTIL_H_ | 68 #endif // BASE_TASK_RUNNER_UTIL_H_ |
| OLD | NEW |