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

Side by Side Diff: base/task_runner_util.h

Issue 2842493004: Add OnceCallback support to PostTaskAndReplyWithResult (Closed)
Patch Set: . Created 3 years, 7 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
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> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 10 matching lines...) Expand all
21 // R DoWorkAndReturn(); 21 // R DoWorkAndReturn();
22 // void Callback(const R& result); 22 // void Callback(const R& result);
23 // 23 //
24 // 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
25 // result of DoWorkAndReturn is passed to the Callback, you can use 25 // result of DoWorkAndReturn is passed to the Callback, you can use
26 // PostTaskAndReplyWithResult as in this example: 26 // PostTaskAndReplyWithResult as in this example:
27 // 27 //
28 // PostTaskAndReplyWithResult( 28 // PostTaskAndReplyWithResult(
29 // target_thread_.task_runner(), 29 // target_thread_.task_runner(),
30 // FROM_HERE, 30 // FROM_HERE,
31 // Bind(&DoWorkAndReturn), 31 // BindOnce(&DoWorkAndReturn),
32 // Bind(&Callback)); 32 // BindOnce(&Callback));
33 template <typename TaskReturnType, typename ReplyArgType>
34 bool PostTaskAndReplyWithResult(TaskRunner* task_runner,
35 const tracked_objects::Location& from_here,
36 OnceCallback<TaskReturnType()> task,
37 OnceCallback<void(ReplyArgType)> reply) {
38 DCHECK(task);
39 DCHECK(reply);
40 TaskReturnType* result = new TaskReturnType();
41 return task_runner->PostTaskAndReply(
42 from_here,
43 BindOnce(&internal::ReturnAsParamAdapter<TaskReturnType>, std::move(task),
44 result),
45 BindOnce(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>,
46 std::move(reply), Owned(result)));
47 }
48
49 // Callback version of PostTaskAndReplyWithResult above.
50 // Though RepeatingCallback is convertible to OnceCallback, we need this since
51 // we can not use template deduction and object conversion at once on the
52 // overload resolution.
53 // TODO(tzik): Update all callers of the Callback version to use OnceCallback.
33 template <typename TaskReturnType, typename ReplyArgType> 54 template <typename TaskReturnType, typename ReplyArgType>
34 bool PostTaskAndReplyWithResult(TaskRunner* task_runner, 55 bool PostTaskAndReplyWithResult(TaskRunner* task_runner,
35 const tracked_objects::Location& from_here, 56 const tracked_objects::Location& from_here,
36 Callback<TaskReturnType()> task, 57 Callback<TaskReturnType()> task,
37 Callback<void(ReplyArgType)> reply) { 58 Callback<void(ReplyArgType)> reply) {
38 DCHECK(task); 59 return PostTaskAndReplyWithResult(
39 DCHECK(reply); 60 task_runner, from_here,
40 TaskReturnType* result = new TaskReturnType(); 61 static_cast<OnceCallback<TaskReturnType()>>(std::move(task)),
41 return task_runner->PostTaskAndReply( 62 static_cast<OnceCallback<void(ReplyArgType)>>(std::move(reply)));
42 from_here,
43 base::BindOnce(&internal::ReturnAsParamAdapter<TaskReturnType>,
44 std::move(task), result),
45 base::BindOnce(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>,
46 std::move(reply), base::Owned(result)));
47 } 63 }
48 64
49 } // namespace base 65 } // namespace base
50 66
51 #endif // BASE_TASK_RUNNER_UTIL_H_ 67 #endif // BASE_TASK_RUNNER_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | components/drive/chromeos/change_list_loader.cc » ('j') | content/public/browser/browser_thread.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698