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.h" | |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/post_task_and_reply_with_result_internal.h" | 12 #include "base/post_task_and_reply_with_result_internal.h" |
12 #include "base/task_runner.h" | 13 #include "base/task_runner.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 | 16 |
16 // When you have these methods | 17 // When you have these methods |
17 // | 18 // |
18 // R DoWorkAndReturn(); | 19 // R DoWorkAndReturn(); |
19 // void Callback(const R& result); | 20 // void Callback(const R& result); |
20 // | 21 // |
21 // and want to call them in a PostTaskAndReply kind of fashion where the | 22 // and want to call them in a PostTaskAndReply kind of fashion where the |
22 // result of DoWorkAndReturn is passed to the Callback, you can use | 23 // result of DoWorkAndReturn is passed to the Callback, you can use |
23 // PostTaskAndReplyWithResult as in this example: | 24 // PostTaskAndReplyWithResult as in this example: |
24 // | 25 // |
25 // PostTaskAndReplyWithResult( | 26 // PostTaskAndReplyWithResult( |
26 // target_thread_.task_runner(), | 27 // target_thread_.task_runner(), |
27 // FROM_HERE, | 28 // FROM_HERE, |
28 // Bind(&DoWorkAndReturn), | 29 // Bind(&DoWorkAndReturn), |
29 // Bind(&Callback)); | 30 // Bind(&Callback)); |
30 template <typename TaskReturnType, typename ReplyArgType> | 31 template <typename TaskReturnType, typename ReplyArgType> |
31 bool PostTaskAndReplyWithResult( | 32 bool PostTaskAndReplyWithResult(TaskRunner* task_runner, |
32 TaskRunner* task_runner, | 33 const tracked_objects::Location& from_here, |
33 const tracked_objects::Location& from_here, | 34 Callback<TaskReturnType()> task, |
34 const Callback<TaskReturnType(void)>& task, | 35 Callback<void(ReplyArgType)> reply) { |
35 const Callback<void(ReplyArgType)>& reply) { | |
36 DCHECK(task); | 36 DCHECK(task); |
37 DCHECK(reply); | 37 DCHECK(reply); |
38 TaskReturnType* result = new TaskReturnType(); | 38 TaskReturnType* result = new TaskReturnType(); |
39 return task_runner->PostTaskAndReply( | 39 return task_runner->PostTaskAndReply( |
40 from_here, | 40 from_here, base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, |
41 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, | 41 std::move(task), result), |
42 result), | 42 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, |
43 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, | 43 std::move(reply), base::Owned(result))); |
gab
2017/02/07 15:46:08
#include <utility>
tzik
2017/02/08 01:39:32
Done.
| |
44 base::Owned(result))); | |
45 } | 44 } |
46 | 45 |
47 } // namespace base | 46 } // namespace base |
48 | 47 |
49 #endif // BASE_TASK_RUNNER_UTIL_H_ | 48 #endif // BASE_TASK_RUNNER_UTIL_H_ |
OLD | NEW |