1 // This file was GENERATED by command: 2 // pump.py bind_to_loop.h.pump 3 // DO NOT EDIT BY HAND!!! 4 5 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. 9 10 #ifndef MEDIA_BASE_BIND_TO_LOOP_H_ 11 #define MEDIA_BASE_BIND_TO_LOOP_H_ 12 13 #include "base/bind.h" 14 #include "base/location.h" 15 #include "base/message_loop/message_loop_proxy.h" 16 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. 21 // 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))); 26 // 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. 30 31 namespace media { 32 33 // Mimic base::internal::CallbackForward, replacing p.Pass() with 34 // base::Passed(&p) to account for the extra layer of indirection. 35 namespace internal { 36 template <typename T> 37 T& TrampolineForward(T& t) { return t; } 38 39 template <typename T> 40 base::internal::PassedWrapper<scoped_ptr<T> > TrampolineForward( 41 scoped_ptr<T>& p) { return base::Passed(&p); } 42 43 template <typename T> 44 base::internal::PassedWrapper<scoped_ptr<T[]> > TrampolineForward( 45 scoped_ptr<T[]>& p) { return base::Passed(&p); } 46 47 template <typename T, typename R> 48 base::internal::PassedWrapper<scoped_ptr_malloc<T, R> > TrampolineForward( 49 scoped_ptr_malloc<T, R>& p) { return base::Passed(&p); } 50 51 template <typename T> 52 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward( 53 ScopedVector<T>& p) { return base::Passed(&p); } 54 55 template <typename T> struct TrampolineHelper; 56 57 template <> 58 struct TrampolineHelper<void()> { 59 static void Run( 60 const scoped_refptr<base::MessageLoopProxy>& loop, 61 const base::Callback<void()>& cb) { 62 loop->PostTask(FROM_HERE, base::Bind(cb)); 63 } 64 }; 65 66 67 template <typename A1> 68 struct TrampolineHelper<void(A1)> { 69 static void Run( 70 const scoped_refptr<base::MessageLoopProxy>& loop, 71 const base::Callback<void(A1)>& cb, A1 a1) { 72 loop->PostTask(FROM_HERE, base::Bind(cb, internal::TrampolineForward(a1))); 73 } 74 }; 75 76 77 template <typename A1, typename A2> 78 struct TrampolineHelper<void(A1, A2)> { 79 static void Run( 80 const scoped_refptr<base::MessageLoopProxy>& loop, 81 const base::Callback<void(A1, A2)>& cb, A1 a1, A2 a2) { 82 loop->PostTask(FROM_HERE, base::Bind(cb, internal::TrampolineForward(a1), 83 internal::TrampolineForward(a2))); 84 } 85 }; 86 87 88 template <typename A1, typename A2, typename A3> 89 struct TrampolineHelper<void(A1, A2, A3)> { 90 static void Run( 91 const scoped_refptr<base::MessageLoopProxy>& loop, 92 const base::Callback<void(A1, A2, A3)>& cb, A1 a1, A2 a2, A3 a3) { 93 loop->PostTask(FROM_HERE, base::Bind(cb, internal::TrampolineForward(a1), 94 internal::TrampolineForward(a2), internal::TrampolineForward(a3))); 95 } 96 }; 97 98 99 template <typename A1, typename A2, typename A3, typename A4> 100 struct TrampolineHelper<void(A1, A2, A3, A4)> { 101 static void Run( 102 const scoped_refptr<base::MessageLoopProxy>& loop, 103 const base::Callback<void(A1, A2, A3, A4)>& cb, A1 a1, A2 a2, A3 a3, 104 A4 a4) { 105 loop->PostTask(FROM_HERE, base::Bind(cb, internal::TrampolineForward(a1), 106 internal::TrampolineForward(a2), internal::TrampolineForward(a3), 107 internal::TrampolineForward(a4))); 108 } 109 }; 110 111 112 template <typename A1, typename A2, typename A3, typename A4, typename A5> 113 struct TrampolineHelper<void(A1, A2, A3, A4, A5)> { 114 static void Run( 115 const scoped_refptr<base::MessageLoopProxy>& loop, 116 const base::Callback<void(A1, A2, A3, A4, A5)>& cb, A1 a1, A2 a2, A3 a3, 117 A4 a4, A5 a5) { 118 loop->PostTask(FROM_HERE, base::Bind(cb, internal::TrampolineForward(a1), 119 internal::TrampolineForward(a2), internal::TrampolineForward(a3), 120 internal::TrampolineForward(a4), internal::TrampolineForward(a5))); 121 } 122 }; 123 124 125 template <typename A1, typename A2, typename A3, typename A4, typename A5, 126 typename A6> 127 struct TrampolineHelper<void(A1, A2, A3, A4, A5, A6)> { 128 static void Run( 129 const scoped_refptr<base::MessageLoopProxy>& loop, 130 const base::Callback<void(A1, A2, A3, A4, A5, A6)>& cb, A1 a1, A2 a2, 131 A3 a3, A4 a4, A5 a5, A6 a6) { 132 loop->PostTask(FROM_HERE, base::Bind(cb, internal::TrampolineForward(a1), 133 internal::TrampolineForward(a2), internal::TrampolineForward(a3), 134 internal::TrampolineForward(a4), internal::TrampolineForward(a5), 135 internal::TrampolineForward(a6))); 136 } 137 }; 138 139 140 template <typename A1, typename A2, typename A3, typename A4, typename A5, 141 typename A6, typename A7> 142 struct TrampolineHelper<void(A1, A2, A3, A4, A5, A6, A7)> { 143 static void Run( 144 const scoped_refptr<base::MessageLoopProxy>& loop, 145 const base::Callback<void(A1, A2, A3, A4, A5, A6, A7)>& cb, A1 a1, A2 a2, 146 A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) { 147 loop->PostTask(FROM_HERE, base::Bind(cb, internal::TrampolineForward(a1), 148 internal::TrampolineForward(a2), internal::TrampolineForward(a3), 149 internal::TrampolineForward(a4), internal::TrampolineForward(a5), 150 internal::TrampolineForward(a6), internal::TrampolineForward(a7))); 151 } 152 }; 153 154 155 } // namespace internal 156 157 template<typename T> 158 static base::Callback<T> BindToLoop( 159 const scoped_refptr<base::MessageLoopProxy>& loop, 160 const base::Callback<T>& cb) { 161 return base::Bind(&internal::TrampolineHelper<T>::Run, loop, cb); 162 } 163 164 template<typename T> 165 static base::Callback<T> BindToCurrentLoop( 166 const base::Callback<T>& cb) { 167 return BindToLoop(base::MessageLoopProxy::current(), cb); 168 } 169 170 } // namespace media 171 172 #endif // MEDIA_BASE_BIND_TO_LOOP_H_ 173