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
5 $$ http://code.google.com/p/googletest/wiki/PumpManual
8 $$ See comment for MAX_ARITY in base/bind.h.pump.
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
13 // found in the LICENSE file.
15 #ifndef MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_
16 #define MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_
18 #include "base/bind.h"
19 #include "base/location.h"
20 #include "base/message_loop/message_loop_proxy.h"
21 #include "base/single_thread_task_runner.h"
23 // This is a helper utility for base::Bind()ing callbacks to the current
24 // MessageLoop. The 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
26 // when |b| executes the callback, it does so on |a|'s current MessageLoop.
28 // Typical usage: request to be called back on the current thread:
29 // other->StartAsyncProcessAndCallMeBack(
30 // media::BindToCurrentLoop(base::Bind(&MyClass::MyMethod, this)));
32 // Note that like base::Bind(), BindToCurrentLoop() can't bind non-constant
33 // references, and that *unlike* base::Bind(), BindToCurrentLoop() makes copies
34 // of its arguments, and thus can't be used with arrays.
38 // Mimic base::internal::CallbackForward, replacing p.Pass() with
39 // base::Passed(&p) to account for the extra layer of indirection.
42 T& TrampolineForward(T& t) { return t; }
44 template <typename T, typename R>
45 base::internal::PassedWrapper<scoped_ptr<T, R> > TrampolineForward(
46 scoped_ptr<T, R>& p) { return base::Passed(&p); }
49 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward(
50 ScopedVector<T>& p) { return base::Passed(&p); }
52 template <typename T> struct TrampolineHelper;
54 $range ARITY 0..MAX_ARITY
58 template <$for ARG , [[typename A$(ARG)]]>
59 struct TrampolineHelper<void($for ARG , [[A$(ARG)]])> {
61 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
62 const base::Callback<void($for ARG , [[A$(ARG)]])>& cb
64 $for ARG , [[A$(ARG) a$(ARG)]]
66 task_runner->PostTask(FROM_HERE, base::Bind(cb
68 $for ARG , [[internal::TrampolineForward(a$(ARG))]]));
75 } // namespace internal
78 static base::Callback<T> BindToCurrentLoop(
79 const base::Callback<T>& cb) {
80 return base::Bind(&internal::TrampolineHelper<T>::Run,
81 base::MessageLoopProxy::current(), cb);
86 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_