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

Side by Side Diff: media/base/bind_to_loop.h.pump

Issue 11092054: Teach BindToLoop to create callbacks that accept scoped parameters. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: xhwang CR 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 | « media/base/bind_to_loop.h ('k') | media/base/bind_to_loop_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 $$ This is a pump file for generating file templates. Pump is a python 1 $$ This is a pump file for generating file templates. Pump is a python
2 $$ script that is part of the Google Test suite of utilities. Description 2 $$ script that is part of the Google Test suite of utilities. Description
3 $$ can be found here: 3 $$ can be found here:
4 $$ 4 $$
5 $$ http://code.google.com/p/googletest/wiki/PumpManual 5 $$ http://code.google.com/p/googletest/wiki/PumpManual
6 $$ 6 $$
7 7
8 $$ See comment for MAX_ARITY in base/bind.h.pump. 8 $$ See comment for MAX_ARITY in base/bind.h.pump.
9 $var MAX_ARITY = 7 9 $var MAX_ARITY = 7
10 10
11 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 11 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
12 // Use of this source code is governed by a BSD-style license that can be 12 // Use of this source code is governed by a BSD-style license that can be
13 // found in the LICENSE file. 13 // found in the LICENSE file.
14 14
15 #ifndef MEDIA_BASE_BIND_TO_LOOP_H_ 15 #ifndef MEDIA_BASE_BIND_TO_LOOP_H_
16 #define MEDIA_BASE_BIND_TO_LOOP_H_ 16 #define MEDIA_BASE_BIND_TO_LOOP_H_
17 17
18 #include "base/bind.h" 18 #include "base/bind.h"
19 #include "base/callback_internal.h" // Avoid re-inventing CallbackForward.
20 #include "base/location.h" 19 #include "base/location.h"
21 #include "base/message_loop_proxy.h" 20 #include "base/message_loop_proxy.h"
22 21
23 // This is a helper utility for base::Bind()ing callbacks on to particular 22 // This is a helper utility for base::Bind()ing callbacks on to particular
24 // MessageLoops. A typical use is when |a| (of class |A|) wants to hand a 23 // MessageLoops. A typical use is when |a| (of class |A|) wants to hand a
25 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that 24 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that
26 // when |b| executes the callback, it does so on a particular MessageLoop. 25 // when |b| executes the callback, it does so on a particular MessageLoop.
27 // 26 //
28 // Typical usage: request to be called back on the current thread: 27 // Typical usage: request to be called back on the current thread:
29 // other->StartAsyncProcessAndCallMeBack( 28 // other->StartAsyncProcessAndCallMeBack(
30 // media::BindToLoop(MessageLoopProxy::current(), 29 // media::BindToLoop(MessageLoopProxy::current(),
31 // base::Bind(&MyClass::MyMethod, this))); 30 // base::Bind(&MyClass::MyMethod, this)));
32 // 31 //
33 // Note that like base::Bind(), BindToLoop() can't bind non-constant references, 32 // Note that like base::Bind(), BindToLoop() can't bind non-constant references,
34 // and that *unlike* base::Bind(), BindToLoop() makes copies of its arguments, 33 // and that *unlike* base::Bind(), BindToLoop() makes copies of its arguments,
35 // and thus can't be used with arrays. 34 // and thus can't be used with arrays.
36 35
37 namespace media { 36 namespace media {
38 37
38 // Mimic base::internal::CallbackForward, replacing p.Pass() with
39 // base::Passed(&p) to account for the extra layer of indirection.
40 namespace internal {
41 template <typename T>
42 T& TrampolineForward(T& t) { return t; }
43
44 template <typename T>
45 base::internal::PassedWrapper<scoped_ptr<T> > TrampolineForward(
46 scoped_ptr<T>& p) { return base::Passed(&p); }
47
48 template <typename T>
49 base::internal::PassedWrapper<scoped_array<T> > TrampolineForward(
50 scoped_array<T>& p) { return base::Passed(&p); }
51
52 template <typename T, typename R>
53 base::internal::PassedWrapper<scoped_ptr_malloc<T, R> > TrampolineForward(
54 scoped_ptr_malloc<T, R>& p) { base::Passed(&p); }
55
56 template <typename T>
57 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward(
58 ScopedVector<T>& p) { return base::Passed(&p); }
59
39 template <typename T> struct TrampolineHelper; 60 template <typename T> struct TrampolineHelper;
40 61
41 $range ARITY 0..MAX_ARITY 62 $range ARITY 0..MAX_ARITY
42 $for ARITY [[ 63 $for ARITY [[
43 $range ARG 1..ARITY 64 $range ARG 1..ARITY
44 65
45 template <$for ARG , [[typename A$(ARG)]]> 66 template <$for ARG , [[typename A$(ARG)]]>
46 struct TrampolineHelper<void($for ARG , [[A$(ARG)]])> { 67 struct TrampolineHelper<void($for ARG , [[A$(ARG)]])> {
47 static void Run( 68 static void Run(
48 const scoped_refptr<base::MessageLoopProxy>& loop, 69 const scoped_refptr<base::MessageLoopProxy>& loop,
49 const base::Callback<void($for ARG , [[A$(ARG)]])>& cb 70 const base::Callback<void($for ARG , [[A$(ARG)]])>& cb
50 $if ARITY != 0 [[, ]] 71 $if ARITY != 0 [[, ]]
51 $for ARG , [[A$(ARG) a$(ARG)]] 72 $for ARG , [[A$(ARG) a$(ARG)]]
52 ) { 73 ) {
53 loop->PostTask(FROM_HERE, base::Bind(cb 74 loop->PostTask(FROM_HERE, base::Bind(cb
54 $if ARITY != 0 [[, ]] 75 $if ARITY != 0 [[, ]]
55 $for ARG , [[base::internal::CallbackForward(a$(ARG))]])); 76 $for ARG , [[internal::TrampolineForward(a$(ARG))]]));
56 } 77 }
57 }; 78 };
58 79
59 80
60 ]] $$ for ARITY 81 ]] $$ for ARITY
61 82
83 } // namespace internal
84
62 template<typename T> 85 template<typename T>
63 static base::Callback<T> BindToLoop( 86 static base::Callback<T> BindToLoop(
64 const scoped_refptr<base::MessageLoopProxy>& loop, 87 const scoped_refptr<base::MessageLoopProxy>& loop,
65 const base::Callback<T>& cb) { 88 const base::Callback<T>& cb) {
66 return base::Bind(&TrampolineHelper<T>::Run, loop, cb); 89 return base::Bind(&internal::TrampolineHelper<T>::Run, loop, cb);
67 } 90 }
68 91
69 } // namespace media 92 } // namespace media
70 93
71 #endif // MEDIA_BASE_BIND_TO_LOOP_H_ 94 #endif // MEDIA_BASE_BIND_TO_LOOP_H_
OLDNEW
« no previous file with comments | « media/base/bind_to_loop.h ('k') | media/base/bind_to_loop_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698