battre@chromium.org | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_TASK_RUNNER_UTIL_H_ |
| 6 | #define BASE_TASK_RUNNER_UTIL_H_ |
battre@chromium.org | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 7 | |
Mikel Astiz | c076a97 | 2018-07-25 13:49:25 | [diff] [blame] | 8 | #include <memory> |
tzik | 0352751 | 2017-02-08 12:29:47 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
battre@chromium.org | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 11 | #include "base/bind.h" |
| 12 | #include "base/bind_helpers.h" |
tzik | 0352751 | 2017-02-08 12:29:47 | [diff] [blame] | 13 | #include "base/callback.h" |
jdoerrie | 19cf521 | 2019-04-26 09:50:47 | [diff] [blame] | 14 | #include "base/callback_helpers.h" |
battre@chromium.org | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 15 | #include "base/logging.h" |
fdoray | e94a945 | 2016-11-30 16:59:21 | [diff] [blame] | 16 | #include "base/post_task_and_reply_with_result_internal.h" |
battre@chromium.org | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 17 | #include "base/task_runner.h" |
| 18 | |
| 19 | namespace base { |
| 20 | |
battre@chromium.org | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 21 | // When you have these methods |
| 22 | // |
| 23 | // R DoWorkAndReturn(); |
| 24 | // void Callback(const R& result); |
| 25 | // |
| 26 | // and want to call them in a PostTaskAndReply kind of fashion where the |
| 27 | // result of DoWorkAndReturn is passed to the Callback, you can use |
| 28 | // PostTaskAndReplyWithResult as in this example: |
| 29 | // |
| 30 | // PostTaskAndReplyWithResult( |
skyostil | b1f0299 | 2015-06-19 17:22:54 | [diff] [blame] | 31 | // target_thread_.task_runner(), |
battre@chromium.org | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 32 | // FROM_HERE, |
tzik | d5346b59 | 2017-05-02 16:48:18 | [diff] [blame] | 33 | // BindOnce(&DoWorkAndReturn), |
| 34 | // BindOnce(&Callback)); |
jdoerrie | 19cf521 | 2019-04-26 09:50:47 | [diff] [blame] | 35 | // |
| 36 | // Though RepeatingCallback is convertible to OnceCallback, we need a |
| 37 | // CallbackType template since we can not use template deduction and object |
| 38 | // conversion at once on the overload resolution. |
| 39 | // TODO(crbug.com/714018): Update all callers of the RepeatingCallback version |
| 40 | // to use OnceCallback and remove the CallbackType template. |
| 41 | template <template <typename> class CallbackType, |
| 42 | typename TaskReturnType, |
| 43 | typename ReplyArgType, |
| 44 | typename = EnableIfIsBaseCallback<CallbackType>> |
tzik | 0352751 | 2017-02-08 12:29:47 | [diff] [blame] | 45 | bool PostTaskAndReplyWithResult(TaskRunner* task_runner, |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 46 | const Location& from_here, |
jdoerrie | 19cf521 | 2019-04-26 09:50:47 | [diff] [blame] | 47 | CallbackType<TaskReturnType()> task, |
| 48 | CallbackType<void(ReplyArgType)> reply) { |
fdoray | 3be0e4b | 2016-11-30 12:03:20 | [diff] [blame] | 49 | DCHECK(task); |
| 50 | DCHECK(reply); |
Mikel Astiz | c076a97 | 2018-07-25 13:49:25 | [diff] [blame] | 51 | // std::unique_ptr used to avoid the need of a default constructor. |
| 52 | auto* result = new std::unique_ptr<TaskReturnType>(); |
battre@chromium.org | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 53 | return task_runner->PostTaskAndReply( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 54 | from_here, |
tzik | d5346b59 | 2017-05-02 16:48:18 | [diff] [blame] | 55 | BindOnce(&internal::ReturnAsParamAdapter<TaskReturnType>, std::move(task), |
| 56 | result), |
| 57 | BindOnce(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, |
| 58 | std::move(reply), Owned(result))); |
| 59 | } |
| 60 | |
battre@chromium.org | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 61 | } // namespace base |
| 62 | |
| 63 | #endif // BASE_TASK_RUNNER_UTIL_H_ |