Home | History | Annotate | Download | only in base
      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, typename R>
     44 base::internal::PassedWrapper<scoped_ptr_malloc<T, R> > TrampolineForward(
     45     scoped_ptr_malloc<T, R>& p) { return base::Passed(&p); }
     46 
     47 template <typename T>
     48 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward(
     49     ScopedVector<T>& p) { return base::Passed(&p); }
     50 
     51 template <typename T> struct TrampolineHelper;
     52 
     53 template <>
     54 struct TrampolineHelper<void()> {
     55   static void Run(
     56       const scoped_refptr<base::MessageLoopProxy>& loop,
     57       const base::Callback<void()>& cb) {
     58     loop->PostTask(FROM_HERE, base::Bind(cb));
     59   }
     60 };
     61 
     62 
     63 template <typename A1>
     64 struct TrampolineHelper<void(A1)> {
     65   static void Run(
     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)));
     69   }
     70 };
     71 
     72 
     73 template <typename A1, typename A2>
     74 struct TrampolineHelper<void(A1, A2)> {
     75   static void Run(
     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)));
     80   }
     81 };
     82 
     83 
     84 template <typename A1, typename A2, typename A3>
     85 struct TrampolineHelper<void(A1, A2, A3)> {
     86   static void Run(
     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)));
     91   }
     92 };
     93 
     94 
     95 template <typename A1, typename A2, typename A3, typename A4>
     96 struct TrampolineHelper<void(A1, A2, A3, A4)> {
     97   static void Run(
     98       const scoped_refptr<base::MessageLoopProxy>& loop,
     99       const base::Callback<void(A1, A2, A3, A4)>& cb, A1 a1, A2 a2, A3 a3,
    100           A4 a4) {
    101     loop->PostTask(FROM_HERE, base::Bind(cb, internal::TrampolineForward(a1),
    102         internal::TrampolineForward(a2), internal::TrampolineForward(a3),
    103         internal::TrampolineForward(a4)));
    104   }
    105 };
    106 
    107 
    108 template <typename A1, typename A2, typename A3, typename A4, typename A5>
    109 struct TrampolineHelper<void(A1, A2, A3, A4, A5)> {
    110   static void Run(
    111       const scoped_refptr<base::MessageLoopProxy>& loop,
    112       const base::Callback<void(A1, A2, A3, A4, A5)>& cb, A1 a1, A2 a2, A3 a3,
    113           A4 a4, A5 a5) {
    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)));
    117   }
    118 };
    119 
    120 
    121 template <typename A1, typename A2, typename A3, typename A4, typename A5,
    122     typename A6>
    123 struct TrampolineHelper<void(A1, A2, A3, A4, A5, A6)> {
    124   static void Run(
    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)));
    132   }
    133 };
    134 
    135 
    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)> {
    139   static void Run(
    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)));
    147   }
    148 };
    149 
    150 
    151 }  // namespace internal
    152 
    153 template<typename T>
    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);
    158 }
    159 
    160 template<typename T>
    161 static base::Callback<T> BindToCurrentLoop(
    162     const base::Callback<T>& cb) {
    163   return BindToLoop(base::MessageLoopProxy::current(), cb);
    164 }
    165 
    166 }  // namespace media
    167 
    168 #endif  // MEDIA_BASE_BIND_TO_LOOP_H_
    169