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/logging.h" | 10 #include "base/logging.h" |
11 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 namespace internal { | 15 namespace internal { |
16 | 16 |
17 // 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 |
18 // one that returns via an output parameter. | 18 // one that returns via an output parameter. |
19 template <typename ReturnType> | 19 template <typename ReturnType> |
20 void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func, | 20 void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func, |
21 ReturnType* result) { | 21 ReturnType* result) { |
22 *result = func.Run(); | 22 *result = func.Run(); |
23 } | 23 } |
24 | 24 |
25 // Adapts a T* result to a callblack that expects a T. | 25 // Adapts a T* result to a callblack that expects a T. |
26 template <typename TaskReturnType, typename ReplyArgType> | 26 template <typename TaskReturnType, typename ReplyArgType> |
27 void ReplyAdapter(const Callback<void(ReplyArgType)>& callback, | 27 void ReplyAdapter(const Callback<void(ReplyArgType)>& callback, |
28 TaskReturnType* result) { | 28 TaskReturnType* result) { |
29 // TODO(ajwong): Remove this conditional and add a DCHECK to enforce that | 29 callback.Run(std::move(*result)); |
30 // |reply| must be non-null in PostTaskAndReplyWithResult() below after | |
31 // current code that relies on this API softness has been removed. | |
32 // http://crbug.com/162712 | |
33 if (!callback.is_null()) | |
34 callback.Run(std::move(*result)); | |
35 } | 30 } |
36 | 31 |
37 } // namespace internal | 32 } // namespace internal |
38 | 33 |
39 // When you have these methods | 34 // When you have these methods |
40 // | 35 // |
41 // R DoWorkAndReturn(); | 36 // R DoWorkAndReturn(); |
42 // void Callback(const R& result); | 37 // void Callback(const R& result); |
43 // | 38 // |
44 // and want to call them in a PostTaskAndReply kind of fashion where the | 39 // and want to call them in a PostTaskAndReply kind of fashion where the |
45 // result of DoWorkAndReturn is passed to the Callback, you can use | 40 // result of DoWorkAndReturn is passed to the Callback, you can use |
46 // PostTaskAndReplyWithResult as in this example: | 41 // PostTaskAndReplyWithResult as in this example: |
47 // | 42 // |
48 // PostTaskAndReplyWithResult( | 43 // PostTaskAndReplyWithResult( |
49 // target_thread_.task_runner(), | 44 // target_thread_.task_runner(), |
50 // FROM_HERE, | 45 // FROM_HERE, |
51 // Bind(&DoWorkAndReturn), | 46 // Bind(&DoWorkAndReturn), |
52 // Bind(&Callback)); | 47 // Bind(&Callback)); |
53 template <typename TaskReturnType, typename ReplyArgType> | 48 template <typename TaskReturnType, typename ReplyArgType> |
54 bool PostTaskAndReplyWithResult( | 49 bool PostTaskAndReplyWithResult( |
55 TaskRunner* task_runner, | 50 TaskRunner* task_runner, |
56 const tracked_objects::Location& from_here, | 51 const tracked_objects::Location& from_here, |
57 const Callback<TaskReturnType(void)>& task, | 52 const Callback<TaskReturnType(void)>& task, |
58 const Callback<void(ReplyArgType)>& reply) { | 53 const Callback<void(ReplyArgType)>& reply) { |
54 DCHECK(task); | |
55 DCHECK(reply); | |
dcheng
2016/11/29 21:13:11
TIL that Callback can convert to bool.
| |
59 TaskReturnType* result = new TaskReturnType(); | 56 TaskReturnType* result = new TaskReturnType(); |
60 return task_runner->PostTaskAndReply( | 57 return task_runner->PostTaskAndReply( |
61 from_here, | 58 from_here, |
62 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, | 59 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, |
63 result), | 60 result), |
64 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, | 61 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, |
65 base::Owned(result))); | 62 base::Owned(result))); |
66 } | 63 } |
67 | 64 |
68 } // namespace base | 65 } // namespace base |
69 | 66 |
70 #endif // BASE_TASK_RUNNER_UTIL_H_ | 67 #endif // BASE_TASK_RUNNER_UTIL_H_ |
OLD | NEW |