Home | History | Annotate | Download | only in base
      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
      3 $$ can be found here:
      4 $$
      5 $$ http://code.google.com/p/googletest/wiki/PumpManual
      6 $$
      7 
      8 $$ See comment for MAX_ARITY in base/bind.h.pump.
      9 $var MAX_ARITY = 7
     10 
     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.
     14 
     15 #ifndef MEDIA_BASE_BIND_TO_LOOP_H_
     16 #define MEDIA_BASE_BIND_TO_LOOP_H_
     17 
     18 #include "base/bind.h"
     19 #include "base/location.h"
     20 #include "base/message_loop/message_loop_proxy.h"
     21 
     22 // This is a helper utility for base::Bind()ing callbacks on to particular
     23 // MessageLoops.  A typical use is when |a| (of class |A|) wants to hand a
     24 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that
     25 // when |b| executes the callback, it does so on a particular MessageLoop.
     26 //
     27 // Typical usage: request to be called back on the current thread:
     28 // other->StartAsyncProcessAndCallMeBack(
     29 //    media::BindToLoop(MessageLoopProxy::current(),
     30 //                      base::Bind(&MyClass::MyMethod, this)));
     31 //
     32 // Note that like base::Bind(), BindToLoop() can't bind non-constant references,
     33 // and that *unlike* base::Bind(), BindToLoop() makes copies of its arguments,
     34 // and thus can't be used with arrays.
     35 
     36 namespace media {
     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) { return base::Passed(&p); }
     55 
     56 template <typename T>
     57 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward(
     58     ScopedVector<T>& p) { return base::Passed(&p); }
     59 
     60 template <typename T> struct TrampolineHelper;
     61 
     62 $range ARITY 0..MAX_ARITY
     63 $for ARITY [[
     64 $range ARG 1..ARITY
     65 
     66 template <$for ARG , [[typename A$(ARG)]]>
     67 struct TrampolineHelper<void($for ARG , [[A$(ARG)]])> {
     68   static void Run(
     69       const scoped_refptr<base::MessageLoopProxy>& loop,
     70       const base::Callback<void($for ARG , [[A$(ARG)]])>& cb
     71 $if ARITY != 0 [[, ]]
     72 $for ARG , [[A$(ARG) a$(ARG)]]
     73 ) {
     74     loop->PostTask(FROM_HERE, base::Bind(cb
     75 $if ARITY != 0 [[, ]]
     76 $for ARG , [[internal::TrampolineForward(a$(ARG))]]));
     77   }
     78 };
     79 
     80 
     81 ]]  $$ for ARITY
     82 
     83 }  // namespace internal
     84 
     85 template<typename T>
     86 static base::Callback<T> BindToLoop(
     87     const scoped_refptr<base::MessageLoopProxy>& loop,
     88     const base::Callback<T>& cb) {
     89   return base::Bind(&internal::TrampolineHelper<T>::Run, loop, cb);
     90 }
     91 
     92 template<typename T>
     93 static base::Callback<T> BindToCurrentLoop(
     94     const base::Callback<T>& cb) {
     95   return BindToLoop(base::MessageLoopProxy::current(), cb);
     96 }
     97 
     98 }  // namespace media
     99 
    100 #endif  // MEDIA_BASE_BIND_TO_LOOP_H_
    101