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

Side by Side Diff: base/task_runner_helpers.h

Issue 9416060: Move PostTaskAndReplyWithStatus into task_runner_helpers.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 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 | Annotate | Revision Log
« no previous file with comments | « base/file_util_proxy.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef BASE_TASK_RUNNER_HELPERS_H_
6 #define BASE_TASK_RUNNER_HELPERS_H_
7 #pragma once
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/logging.h"
12
13 namespace base {
14
15 // Helper templates to call methods and reply with the returned value.
16 //
17 // Typically when you have these methods:
18 // R DoWorkAndReturn();
19 // void Callback(R& result);
darin (slow to review) 2012/02/29 00:27:48 why does Callback take a non-const reference? sho
battre 2012/03/07 20:01:48 Done.
20 //
21 // You can pass the result of DoWorkAndReturn to the Callback by:
22 //
23 // R* result = new R;
24 // message_loop_proxy->PostTaskAndReply(
25 // from_here,
26 // ReturnAsParam<R>(Bind(&DoWorkAndReturn), result),
27 // RelayHelper(Bind(&Callback), Owned(result)));
satorux1 2012/02/28 23:58:13 drive-by: Where is RelayHelper defined? 'git grep'
battre 2012/03/07 20:01:48 This was a copy&pasted typo (->ReplyHelper). Done
28 //
29 // Or just use PostTaskAndReplyWithStatus helper template (see the code below).
30 //
31 // PostTaskAndReplyWithStatus(
32 // message_loop_proxy, from_here,
33 // Bind(&DoWorkAndReturn),
34 // Bind(&Callback),
35 // new R);
36 template <typename R1, typename R2>
willchan no longer on Chromium 2012/03/01 02:17:24 I don't think I like ReturnValueTranslator. Do the
battre 2012/03/07 20:01:48 Done.
37 struct ReturnValueTranslator {
38 static R2 Value(const R1& value);
39 };
40
41 // Template that may be specialized to convert the return value of the
42 // DoWorkAndReturn function before being passed to the Callback function.
43 template <typename R>
44 struct ReturnValueTranslator<R, R> {
45 static R Value(const R& value) { return value; }
46 };
47
48 template <typename R1, typename R2>
49 void ReturnAsParamAdapter(const Callback<R1(void)>& func, R2* result) {
50 if (!func.is_null())
willchan no longer on Chromium 2012/03/01 02:17:24 I don't think we should allow func to be NULL. Why
battre 2012/03/07 20:01:48 Some unit tests rely on this (see the failed tests
51 *result = ReturnValueTranslator<R1, R2>::Value(func.Run());
willchan no longer on Chromium 2012/03/01 02:17:24 I think this should just be *result = fun.Run(); a
battre 2012/03/07 20:01:48 Done.
52 }
53
54 template <typename R1, typename R2>
55 Closure ReturnAsParam(const Callback<R1(void)>& func, R2* result) {
56 DCHECK(result);
57 return Bind(&ReturnAsParamAdapter<R1, R2>, func, result);
58 }
59
60 template <typename R, typename A1>
61 void ReturnAsParamAdapter1(const Callback<R(A1)>& func, A1 a1, R* result) {
willchan no longer on Chromium 2012/03/01 02:17:24 Do we need pump to generate multiple variants of t
battre 2012/03/07 20:01:48 No, we can achieve the same effect by using Bind.
62 if (!func.is_null())
63 *result = func.Run(a1);
64 }
65
66 template <typename R, typename A1>
67 Closure ReturnAsParam(const Callback<R(A1)>& func, A1 a1, R* result) {
68 DCHECK(result);
69 return Bind(&ReturnAsParamAdapter1<R, A1>, func, a1, result);
70 }
71
72 template <typename R>
73 void ReplyAdapter(const Callback<void(R)>& callback, R* result) {
74 DCHECK(result);
75 if (!callback.is_null())
willchan no longer on Chromium 2012/03/01 02:17:24 I think we should DCHECK in PostTaskAndReplyWithSt
battre 2012/03/07 20:01:48 See above.
76 callback.Run(*result);
77 }
78
79 template <typename R, typename OWNED>
willchan no longer on Chromium 2012/03/01 02:17:24 I think we should only have one parameter, let's n
battre 2012/03/07 20:01:48 Done.
80 Closure ReplyHelper(const Callback<void(R)>& callback, OWNED result) {
81 return Bind(&ReplyAdapter<R>, callback, result);
82 }
83
84 template <typename R1, typename R2>
85 bool PostTaskAndReplyWithStatus(
darin (slow to review) 2012/02/29 00:27:48 why is this function named *WithStatus?
willchan no longer on Chromium 2012/03/01 02:17:24 I think this should be the only function in base.
battre 2012/03/07 20:01:48 Done.
battre 2012/03/07 20:01:48 I have renamed it to PostTaskAndReplyWithResult. I
86 const scoped_refptr<TaskRunner>& task_runner,
87 const tracked_objects::Location& from_here,
88 const Callback<R1(void)>& do_work_and_return,
89 const Callback<void(R2)>& callback,
90 R2* result) {
91 return task_runner->PostTaskAndReply(
92 from_here,
93 ReturnAsParam<R1>(do_work_and_return, result),
94 ReplyHelper(callback, Owned(result)));
95 }
96
97 } // namespace base
98
99 #endif // BASE_TASK_RUNNER_HELPERS_H_
OLDNEW
« no previous file with comments | « base/file_util_proxy.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698