1 // This file was GENERATED by command:
2 // pump.py bind_to_loop.h.pump
3 // DO NOT EDIT BY HAND!!!
6 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file.
10 #ifndef MEDIA_BASE_BIND_TO_LOOP_H_
11 #define MEDIA_BASE_BIND_TO_LOOP_H_
13 #include "base/bind.h"
14 #include "base/location.h"
15 #include "base/message_loop/message_loop_proxy.h"
17 // This is a helper utility for base::Bind()ing callbacks on to particular
18 // MessageLoops. A typical use is when |a| (of class |A|) wants to hand a
19 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that
20 // when |b| executes the callback, it does so on a particular MessageLoop.
22 // Typical usage: request to be called back on the current thread:
23 // other->StartAsyncProcessAndCallMeBack(
24 // media::BindToLoop(MessageLoopProxy::current(),
25 // base::Bind(&MyClass::MyMethod, this)));
27 // Note that like base::Bind(), BindToLoop() can't bind non-constant references,
28 // and that *unlike* base::Bind(), BindToLoop() makes copies of its arguments,
29 // and thus can't be used with arrays.
33 // Mimic base::internal::CallbackForward, replacing p.Pass() with
34 // base::Passed(&p) to account for the extra layer of indirection.
37 T
& TrampolineForward(T
& t
) { return t
; }
40 base::internal::PassedWrapper
<scoped_ptr
<T
> > TrampolineForward(
41 scoped_ptr
<T
>& p
) { return base::Passed(&p
); }
43 template <typename T
, typename R
>
44 base::internal::PassedWrapper
<scoped_ptr_malloc
<T
, R
> > TrampolineForward(
45 scoped_ptr_malloc
<T
, R
>& p
) { return base::Passed(&p
); }
48 base::internal::PassedWrapper
<ScopedVector
<T
> > TrampolineForward(
49 ScopedVector
<T
>& p
) { return base::Passed(&p
); }
51 template <typename T
> struct TrampolineHelper
;
54 struct TrampolineHelper
<void()> {
56 const scoped_refptr
<base::MessageLoopProxy
>& loop
,
57 const base::Callback
<void()>& cb
) {
58 loop
->PostTask(FROM_HERE
, base::Bind(cb
));
63 template <typename A1
>
64 struct TrampolineHelper
<void(A1
)> {
66 const scoped_refptr
<base::MessageLoopProxy
>& loop
,
67 const base::Callback
<void(A1
)>& cb
, A1 a1
) {
68 loop
->PostTask(FROM_HERE
, base::Bind(cb
, internal::TrampolineForward(a1
)));
73 template <typename A1
, typename A2
>
74 struct TrampolineHelper
<void(A1
, A2
)> {
76 const scoped_refptr
<base::MessageLoopProxy
>& loop
,
77 const base::Callback
<void(A1
, A2
)>& cb
, A1 a1
, A2 a2
) {
78 loop
->PostTask(FROM_HERE
, base::Bind(cb
, internal::TrampolineForward(a1
),
79 internal::TrampolineForward(a2
)));
84 template <typename A1
, typename A2
, typename A3
>
85 struct TrampolineHelper
<void(A1
, A2
, A3
)> {
87 const scoped_refptr
<base::MessageLoopProxy
>& loop
,
88 const base::Callback
<void(A1
, A2
, A3
)>& cb
, A1 a1
, A2 a2
, A3 a3
) {
89 loop
->PostTask(FROM_HERE
, base::Bind(cb
, internal::TrampolineForward(a1
),
90 internal::TrampolineForward(a2
), internal::TrampolineForward(a3
)));
95 template <typename A1
, typename A2
, typename A3
, typename A4
>
96 struct TrampolineHelper
<void(A1
, A2
, A3
, A4
)> {
98 const scoped_refptr
<base::MessageLoopProxy
>& loop
,
99 const base::Callback
<void(A1
, A2
, A3
, A4
)>& cb
, A1 a1
, A2 a2
, A3 a3
,
101 loop
->PostTask(FROM_HERE
, base::Bind(cb
, internal::TrampolineForward(a1
),
102 internal::TrampolineForward(a2
), internal::TrampolineForward(a3
),
103 internal::TrampolineForward(a4
)));
108 template <typename A1
, typename A2
, typename A3
, typename A4
, typename A5
>
109 struct TrampolineHelper
<void(A1
, A2
, A3
, A4
, A5
)> {
111 const scoped_refptr
<base::MessageLoopProxy
>& loop
,
112 const base::Callback
<void(A1
, A2
, A3
, A4
, A5
)>& cb
, A1 a1
, A2 a2
, A3 a3
,
114 loop
->PostTask(FROM_HERE
, base::Bind(cb
, internal::TrampolineForward(a1
),
115 internal::TrampolineForward(a2
), internal::TrampolineForward(a3
),
116 internal::TrampolineForward(a4
), internal::TrampolineForward(a5
)));
121 template <typename A1
, typename A2
, typename A3
, typename A4
, typename A5
,
123 struct TrampolineHelper
<void(A1
, A2
, A3
, A4
, A5
, A6
)> {
125 const scoped_refptr
<base::MessageLoopProxy
>& loop
,
126 const base::Callback
<void(A1
, A2
, A3
, A4
, A5
, A6
)>& cb
, A1 a1
, A2 a2
,
127 A3 a3
, A4 a4
, A5 a5
, A6 a6
) {
128 loop
->PostTask(FROM_HERE
, base::Bind(cb
, internal::TrampolineForward(a1
),
129 internal::TrampolineForward(a2
), internal::TrampolineForward(a3
),
130 internal::TrampolineForward(a4
), internal::TrampolineForward(a5
),
131 internal::TrampolineForward(a6
)));
136 template <typename A1
, typename A2
, typename A3
, typename A4
, typename A5
,
137 typename A6
, typename A7
>
138 struct TrampolineHelper
<void(A1
, A2
, A3
, A4
, A5
, A6
, A7
)> {
140 const scoped_refptr
<base::MessageLoopProxy
>& loop
,
141 const base::Callback
<void(A1
, A2
, A3
, A4
, A5
, A6
, A7
)>& cb
, A1 a1
, A2 a2
,
142 A3 a3
, A4 a4
, A5 a5
, A6 a6
, A7 a7
) {
143 loop
->PostTask(FROM_HERE
, base::Bind(cb
, internal::TrampolineForward(a1
),
144 internal::TrampolineForward(a2
), internal::TrampolineForward(a3
),
145 internal::TrampolineForward(a4
), internal::TrampolineForward(a5
),
146 internal::TrampolineForward(a6
), internal::TrampolineForward(a7
)));
151 } // namespace internal
154 static base::Callback
<T
> BindToLoop(
155 const scoped_refptr
<base::MessageLoopProxy
>& loop
,
156 const base::Callback
<T
>& cb
) {
157 return base::Bind(&internal::TrampolineHelper
<T
>::Run
, loop
, cb
);
161 static base::Callback
<T
> BindToCurrentLoop(
162 const base::Callback
<T
>& cb
) {
163 return BindToLoop(base::MessageLoopProxy::current(), cb
);
168 #endif // MEDIA_BASE_BIND_TO_LOOP_H_