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/post_task_and_reply_with_result_internal.h" |
11 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 | 15 |
15 namespace internal { | |
16 | |
17 // Adapts a function that produces a result via a return value to | |
18 // one that returns via an output parameter. | |
19 template <typename ReturnType> | |
20 void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func, | |
21 ReturnType* result) { | |
22 *result = func.Run(); | |
23 } | |
24 | |
25 // Adapts a T* result to a callblack that expects a T. | |
26 template <typename TaskReturnType, typename ReplyArgType> | |
27 void ReplyAdapter(const Callback<void(ReplyArgType)>& callback, | |
28 TaskReturnType* result) { | |
29 callback.Run(std::move(*result)); | |
30 } | |
31 | |
32 } // namespace internal | |
33 | |
34 // When you have these methods | 16 // When you have these methods |
35 // | 17 // |
36 // R DoWorkAndReturn(); | 18 // R DoWorkAndReturn(); |
37 // void Callback(const R& result); | 19 // void Callback(const R& result); |
38 // | 20 // |
39 // and want to call them in a PostTaskAndReply kind of fashion where the | 21 // and want to call them in a PostTaskAndReply kind of fashion where the |
40 // result of DoWorkAndReturn is passed to the Callback, you can use | 22 // result of DoWorkAndReturn is passed to the Callback, you can use |
41 // PostTaskAndReplyWithResult as in this example: | 23 // PostTaskAndReplyWithResult as in this example: |
42 // | 24 // |
43 // PostTaskAndReplyWithResult( | 25 // PostTaskAndReplyWithResult( |
(...skipping 14 matching lines...) Expand all Loading... |
58 from_here, | 40 from_here, |
59 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, | 41 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, |
60 result), | 42 result), |
61 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, | 43 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, |
62 base::Owned(result))); | 44 base::Owned(result))); |
63 } | 45 } |
64 | 46 |
65 } // namespace base | 47 } // namespace base |
66 | 48 |
67 #endif // BASE_TASK_RUNNER_UTIL_H_ | 49 #endif // BASE_TASK_RUNNER_UTIL_H_ |
OLD | NEW |