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" |
(...skipping 14 matching lines...) Expand all Loading... | |
25 | 25 |
26 // Helper class for TaskRunner::PostTaskAndReplyWithResult. | 26 // Helper class for TaskRunner::PostTaskAndReplyWithResult. |
27 template <typename ReturnType> | 27 template <typename ReturnType> |
28 Closure ReturnAsParam(const Callback<ReturnType(void)>& func, | 28 Closure ReturnAsParam(const Callback<ReturnType(void)>& func, |
29 ReturnType* result) { | 29 ReturnType* result) { |
30 DCHECK(result); | 30 DCHECK(result); |
31 return Bind(&ReturnAsParamAdapter<ReturnType>, func, result); | 31 return Bind(&ReturnAsParamAdapter<ReturnType>, func, result); |
32 } | 32 } |
33 | 33 |
34 // Helper class for TaskRunner::PostTaskAndReplyWithResult. | 34 // Helper class for TaskRunner::PostTaskAndReplyWithResult. |
35 template <typename ReturnType> | 35 template <typename ReturnType, typename ResultType> |
Jeffrey Yasskin
2012/11/05 22:13:22
Could you give these template arguments the same n
awong
2012/11/19 05:57:09
Done.
| |
36 void ReplyAdapter(const Callback<void(ReturnType)>& callback, | 36 void ReplyAdapter(const Callback<void(ReturnType)>& callback, |
37 ReturnType* result) { | 37 ResultType* result) { |
38 DCHECK(result); | 38 DCHECK(result); |
39 if(!callback.is_null()) | 39 if(!callback.is_null()) |
40 callback.Run(CallbackForward(*result)); | 40 callback.Run(CallbackForward(*result)); |
41 } | 41 } |
42 | 42 |
43 // Helper class for TaskRunner::PostTaskAndReplyWithResult. | 43 // Helper class for TaskRunner::PostTaskAndReplyWithResult. |
44 template <typename ReturnType, typename OwnedType> | 44 template <typename ReturnType, typename ResultType> |
Jeffrey Yasskin
2012/11/05 22:13:22
Ditto here.
awong
2012/11/19 05:57:09
Done.
awong
2012/11/19 05:57:09
Done.
| |
45 Closure ReplyHelper(const Callback<void(ReturnType)>& callback, | 45 Closure ReplyHelper(const Callback<void(ReturnType)>& callback, |
46 OwnedType result) { | 46 ResultType* result) { |
47 return Bind(&ReplyAdapter<ReturnType>, callback, result); | 47 return Bind(&ReplyAdapter<ReturnType, ResultType>, callback, Owned(result)); |
48 } | 48 } |
49 | 49 |
50 } // namespace internal | 50 } // namespace internal |
51 | 51 |
52 // When you have these methods | 52 // When you have these methods |
53 // | 53 // |
54 // R DoWorkAndReturn(); | 54 // R DoWorkAndReturn(); |
55 // void Callback(const R& result); | 55 // void Callback(const R& result); |
56 // | 56 // |
57 // and want to call them in a PostTaskAndReply kind of fashion where the | 57 // 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 | 58 // result of DoWorkAndReturn is passed to the Callback, you can use |
59 // PostTaskAndReplyWithResult as in this example: | 59 // PostTaskAndReplyWithResult as in this example: |
60 // | 60 // |
61 // PostTaskAndReplyWithResult( | 61 // PostTaskAndReplyWithResult( |
62 // target_thread_.message_loop_proxy(), | 62 // target_thread_.message_loop_proxy(), |
63 // FROM_HERE, | 63 // FROM_HERE, |
64 // Bind(&DoWorkAndReturn), | 64 // Bind(&DoWorkAndReturn), |
65 // Bind(&Callback)); | 65 // Bind(&Callback)); |
66 template <typename ReturnType> | 66 template <typename TaskReturnType, typename ReplyArgType> |
67 bool PostTaskAndReplyWithResult( | 67 bool PostTaskAndReplyWithResult( |
68 TaskRunner* task_runner, | 68 TaskRunner* task_runner, |
69 const tracked_objects::Location& from_here, | 69 const tracked_objects::Location& from_here, |
70 const Callback<ReturnType(void)>& task, | 70 const Callback<TaskReturnType(void)>& task, |
71 const Callback<void(ReturnType)>& reply) { | 71 const Callback<void(ReplyArgType)>& reply) { |
Jeffrey Yasskin
2012/11/05 22:13:22
Could you double-check that users get a sensible e
awong
2012/11/19 05:57:09
Define sensible :)
It looks good enough to me. Th
Jeffrey Yasskin
2012/11/19 10:33:46
LGTM
| |
72 ReturnType* result = new ReturnType; | 72 TaskReturnType* result = new TaskReturnType; |
73 return task_runner->PostTaskAndReply( | 73 return task_runner->PostTaskAndReply( |
74 from_here, | 74 from_here, |
75 internal::ReturnAsParam<ReturnType>(task, result), | 75 internal::ReturnAsParam<TaskReturnType>(task, result), |
76 internal::ReplyHelper(reply, Owned(result))); | 76 internal::ReplyHelper(reply, result)); |
77 } | 77 } |
78 | 78 |
79 } // namespace base | 79 } // namespace base |
80 | 80 |
81 #endif // BASE_TASK_RUNNER_UTIL_H_ | 81 #endif // BASE_TASK_RUNNER_UTIL_H_ |
OLD | NEW |