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

Side by Side Diff: base/task_runner_util.h

Issue 2678303002: Pass Callback by value on PostTaskAndReply family (Closed)
Patch Set: rebase Created 3 years, 10 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 <utility>
9
8 #include "base/bind.h" 10 #include "base/bind.h"
9 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/callback.h"
10 #include "base/logging.h" 13 #include "base/logging.h"
11 #include "base/post_task_and_reply_with_result_internal.h" 14 #include "base/post_task_and_reply_with_result_internal.h"
12 #include "base/task_runner.h" 15 #include "base/task_runner.h"
13 16
14 namespace base { 17 namespace base {
15 18
16 // When you have these methods 19 // When you have these methods
17 // 20 //
18 // R DoWorkAndReturn(); 21 // R DoWorkAndReturn();
19 // void Callback(const R& result); 22 // void Callback(const R& result);
20 // 23 //
21 // and want to call them in a PostTaskAndReply kind of fashion where the 24 // and want to call them in a PostTaskAndReply kind of fashion where the
22 // result of DoWorkAndReturn is passed to the Callback, you can use 25 // result of DoWorkAndReturn is passed to the Callback, you can use
23 // PostTaskAndReplyWithResult as in this example: 26 // PostTaskAndReplyWithResult as in this example:
24 // 27 //
25 // PostTaskAndReplyWithResult( 28 // PostTaskAndReplyWithResult(
26 // target_thread_.task_runner(), 29 // target_thread_.task_runner(),
27 // FROM_HERE, 30 // FROM_HERE,
28 // Bind(&DoWorkAndReturn), 31 // Bind(&DoWorkAndReturn),
29 // Bind(&Callback)); 32 // Bind(&Callback));
30 template <typename TaskReturnType, typename ReplyArgType> 33 template <typename TaskReturnType, typename ReplyArgType>
31 bool PostTaskAndReplyWithResult( 34 bool PostTaskAndReplyWithResult(TaskRunner* task_runner,
32 TaskRunner* task_runner, 35 const tracked_objects::Location& from_here,
33 const tracked_objects::Location& from_here, 36 Callback<TaskReturnType()> task,
34 const Callback<TaskReturnType(void)>& task, 37 Callback<void(ReplyArgType)> reply) {
35 const Callback<void(ReplyArgType)>& reply) {
36 DCHECK(task); 38 DCHECK(task);
37 DCHECK(reply); 39 DCHECK(reply);
38 TaskReturnType* result = new TaskReturnType(); 40 TaskReturnType* result = new TaskReturnType();
39 return task_runner->PostTaskAndReply( 41 return task_runner->PostTaskAndReply(
40 from_here, 42 from_here, base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>,
41 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, 43 std::move(task), result),
42 result), 44 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>,
43 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, 45 std::move(reply), base::Owned(result)));
44 base::Owned(result)));
45 } 46 }
46 47
47 } // namespace base 48 } // namespace base
48 49
49 #endif // BASE_TASK_RUNNER_UTIL_H_ 50 #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