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

Side by Side Diff: base/task_runner_util.h

Issue 11369061: Disassociate argument type from return type in PostTaskAndReplyWithResult template. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Don't rely on PTARWR allowing a null reply. Created 8 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « base/file_util_proxy.cc ('k') | base/task_runner_util_unittest.cc » ('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/callback_internal.h" 10 #include "base/callback_internal.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/task_runner.h" 12 #include "base/task_runner.h"
13 13
14 namespace base { 14 namespace base {
15 15
16 namespace internal { 16 namespace internal {
17 17
18 // Helper class for TaskRunner::PostTaskAndReplyWithResult. 18 // Adapts a function that produces a result via a return value to
19 // one that returns via an output parameter.
19 template <typename ReturnType> 20 template <typename ReturnType>
20 void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func, 21 void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func,
21 ReturnType* result) { 22 ReturnType* result) {
22 if (!func.is_null()) 23 *result = func.Run();
23 *result = func.Run();
24 } 24 }
25 25
26 // Helper class for TaskRunner::PostTaskAndReplyWithResult. 26 // Adapts a T* result to a calblack that expects a T.
Jeffrey Yasskin 2012/11/19 10:33:46 sp: calblack
awong 2012/11/26 21:57:27 Done.
27 template <typename ReturnType> 27 template <typename TaskReturnType, typename ReplyArgType>
28 Closure ReturnAsParam(const Callback<ReturnType(void)>& func, 28 void ReplyAdapter(const Callback<void(ReplyArgType)>& callback,
29 ReturnType* result) { 29 TaskReturnType* result) {
30 DCHECK(result); 30 callback.Run(CallbackForward(*result));
31 return Bind(&ReturnAsParamAdapter<ReturnType>, func, result);
32 }
33
34 // Helper class for TaskRunner::PostTaskAndReplyWithResult.
35 template <typename ReturnType>
36 void ReplyAdapter(const Callback<void(ReturnType)>& callback,
37 ReturnType* result) {
38 DCHECK(result);
39 if(!callback.is_null())
40 callback.Run(CallbackForward(*result));
41 }
42
43 // Helper class for TaskRunner::PostTaskAndReplyWithResult.
44 template <typename ReturnType, typename OwnedType>
45 Closure ReplyHelper(const Callback<void(ReturnType)>& callback,
46 OwnedType result) {
47 return Bind(&ReplyAdapter<ReturnType>, callback, result);
48 } 31 }
49 32
50 } // namespace internal 33 } // namespace internal
51 34
52 // When you have these methods 35 // When you have these methods
53 // 36 //
54 // R DoWorkAndReturn(); 37 // R DoWorkAndReturn();
55 // void Callback(const R& result); 38 // void Callback(const R& result);
56 // 39 //
57 // and want to call them in a PostTaskAndReply kind of fashion where the 40 // 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 41 // result of DoWorkAndReturn is passed to the Callback, you can use
59 // PostTaskAndReplyWithResult as in this example: 42 // PostTaskAndReplyWithResult as in this example:
60 // 43 //
61 // PostTaskAndReplyWithResult( 44 // PostTaskAndReplyWithResult(
62 // target_thread_.message_loop_proxy(), 45 // target_thread_.message_loop_proxy(),
63 // FROM_HERE, 46 // FROM_HERE,
64 // Bind(&DoWorkAndReturn), 47 // Bind(&DoWorkAndReturn),
65 // Bind(&Callback)); 48 // Bind(&Callback));
66 template <typename ReturnType> 49 template <typename TaskReturnType, typename ReplyArgType>
67 bool PostTaskAndReplyWithResult( 50 bool PostTaskAndReplyWithResult(
68 TaskRunner* task_runner, 51 TaskRunner* task_runner,
69 const tracked_objects::Location& from_here, 52 const tracked_objects::Location& from_here,
70 const Callback<ReturnType(void)>& task, 53 const Callback<TaskReturnType(void)>& task,
71 const Callback<void(ReturnType)>& reply) { 54 const Callback<void(ReplyArgType)>& reply) {
72 ReturnType* result = new ReturnType; 55 DCHECK(!reply.is_null());
56 TaskReturnType* result = new TaskReturnType();
73 return task_runner->PostTaskAndReply( 57 return task_runner->PostTaskAndReply(
74 from_here, 58 from_here,
75 internal::ReturnAsParam<ReturnType>(task, result), 59 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task,
76 internal::ReplyHelper(reply, Owned(result))); 60 result),
61 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply,
62 base::Owned(result)));
77 } 63 }
78 64
79 } // namespace base 65 } // namespace base
80 66
81 #endif // BASE_TASK_RUNNER_UTIL_H_ 67 #endif // BASE_TASK_RUNNER_UTIL_H_
OLDNEW
« no previous file with comments | « base/file_util_proxy.cc ('k') | base/task_runner_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698