Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: base/task_runner_util.h

Issue 2122543002: Replace Closure in TaskRunner::PostTask with OneShotCallback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@07_oneshot
Patch Set: fix Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/task_runner.cc ('k') | base/task_scheduler/post_task.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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(OnceCallback<ReturnType()> func,
21 ReturnType* result) { 21 ReturnType* result) {
22 *result = func.Run(); 22 *result = std::move(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(OnceCallback<void(ReplyArgType)> callback,
28 TaskReturnType* result) { 28 TaskReturnType* result) {
29 // TODO(ajwong): Remove this conditional and add a DCHECK to enforce that 29 // TODO(ajwong): Remove this conditional and add a DCHECK to enforce that
30 // |reply| must be non-null in PostTaskAndReplyWithResult() below after 30 // |reply| must be non-null in PostTaskAndReplyWithResult() below after
31 // current code that relies on this API softness has been removed. 31 // current code that relies on this API softness has been removed.
32 // http://crbug.com/162712 32 // http://crbug.com/162712
33 if (!callback.is_null()) 33 if (!callback.is_null())
34 callback.Run(std::move(*result)); 34 std::move(callback).Run(std::move(*result));
35 } 35 }
36 36
37 } // namespace internal 37 } // namespace internal
38 38
39 // When you have these methods 39 // When you have these methods
40 // 40 //
41 // R DoWorkAndReturn(); 41 // R DoWorkAndReturn();
42 // void Callback(const R& result); 42 // void Callback(const R& result);
43 // 43 //
44 // and want to call them in a PostTaskAndReply kind of fashion where the 44 // 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 45 // result of DoWorkAndReturn is passed to the Callback, you can use
46 // PostTaskAndReplyWithResult as in this example: 46 // PostTaskAndReplyWithResult as in this example:
47 // 47 //
48 // PostTaskAndReplyWithResult( 48 // PostTaskAndReplyWithResult(
49 // target_thread_.task_runner(), 49 // target_thread_.task_runner(),
50 // FROM_HERE, 50 // FROM_HERE,
51 // Bind(&DoWorkAndReturn), 51 // Bind(&DoWorkAndReturn),
52 // Bind(&Callback)); 52 // Bind(&Callback));
53 template <typename TaskReturnType, typename ReplyArgType> 53 template <typename TaskReturnType, typename ReplyArgType>
54 bool PostTaskAndReplyWithResult(TaskRunner* task_runner,
55 const tracked_objects::Location& from_here,
56 OnceCallback<TaskReturnType()> task,
57 OnceCallback<void(ReplyArgType)> reply) {
58 TaskReturnType* result = new TaskReturnType();
59 return task_runner->PostTaskAndReply(
60 from_here, BindOnce(&internal::ReturnAsParamAdapter<TaskReturnType>,
61 std::move(task), result),
62 BindOnce(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>,
63 std::move(reply), base::Owned(result)));
64 }
65
66 template <typename TaskReturnType,
67 typename ReplyArgType,
68 internal::CopyMode task_copy_mode,
69 internal::RepeatMode task_repeat_mode,
70 internal::CopyMode reply_copy_mode,
71 internal::RepeatMode reply_repeat_mode>
54 bool PostTaskAndReplyWithResult( 72 bool PostTaskAndReplyWithResult(
55 TaskRunner* task_runner, 73 TaskRunner* task_runner,
56 const tracked_objects::Location& from_here, 74 const tracked_objects::Location& from_here,
57 const Callback<TaskReturnType(void)>& task, 75 Callback<TaskReturnType(), task_copy_mode, task_repeat_mode> task,
58 const Callback<void(ReplyArgType)>& reply) { 76 Callback<void(ReplyArgType), reply_copy_mode, reply_repeat_mode> reply) {
59 TaskReturnType* result = new TaskReturnType(); 77 return PostTaskAndReplyWithResult(
60 return task_runner->PostTaskAndReply( 78 task_runner, from_here,
61 from_here, 79 OnceCallback<TaskReturnType()>(std::move(task)),
62 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, 80 OnceCallback<void(ReplyArgType)>(std::move(reply)));
63 result),
64 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply,
65 base::Owned(result)));
66 } 81 }
67 82
68 } // namespace base 83 } // namespace base
69 84
70 #endif // BASE_TASK_RUNNER_UTIL_H_ 85 #endif // BASE_TASK_RUNNER_UTIL_H_
OLDNEW
« no previous file with comments | « base/task_runner.cc ('k') | base/task_scheduler/post_task.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698