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