1/* 2 * libjingle 3 * Copyright 2012 Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28// To generate bind.h from bind.h.pump, execute: 29// /home/build/google3/third_party/gtest/scripts/pump.py bind.h.pump 30 31// Bind() is an overloaded function that converts method calls into function 32// objects (aka functors). It captures any arguments to the method by value 33// when Bind is called, producing a stateful, nullary function object. Care 34// should be taken about the lifetime of objects captured by Bind(); the 35// returned functor knows nothing about the lifetime of the method's object or 36// any arguments passed by pointer, and calling the functor with a destroyed 37// object will surely do bad things. 38// 39// Example usage: 40// struct Foo { 41// int Test1() { return 42; } 42// int Test2() const { return 52; } 43// int Test3(int x) { return x*x; } 44// float Test4(int x, float y) { return x + y; } 45// }; 46// 47// int main() { 48// Foo foo; 49// cout << talk_base::Bind(&Foo::Test1, &foo)() << endl; 50// cout << talk_base::Bind(&Foo::Test2, &foo)() << endl; 51// cout << talk_base::Bind(&Foo::Test3, &foo, 3)() << endl; 52// cout << talk_base::Bind(&Foo::Test4, &foo, 7, 8.5f)() << endl; 53// } 54 55#ifndef TALK_BASE_BIND_H_ 56#define TALK_BASE_BIND_H_ 57 58#define NONAME 59 60namespace talk_base { 61namespace detail { 62// This is needed because the template parameters in Bind can't be resolved 63// if they're used both as parameters of the function pointer type and as 64// parameters to Bind itself: the function pointer parameters are exact 65// matches to the function prototype, but the parameters to bind have 66// references stripped. This trick allows the compiler to dictate the Bind 67// parameter types rather than deduce them. 68template <class T> struct identity { typedef T type; }; 69} // namespace detail 70 71$var n = 5 72$range i 0..n 73$for i [[ 74$range j 1..i 75 76template <class ObjectT, class MethodT, class R$for j [[, 77 class P$j]]> 78class MethodFunctor$i { 79 public: 80 MethodFunctor$i(MethodT method, ObjectT* object$for j [[, 81 P$j p$j]]) 82 : method_(method), object_(object)$for j [[, 83 p$(j)_(p$j)]] {} 84 R operator()() const { 85 return (object_->*method_)($for j , [[p$(j)_]]); } 86 private: 87 MethodT method_; 88 ObjectT* object_;$for j [[ 89 90 P$j p$(j)_;]] 91 92}; 93 94#define FP_T(x) R (ObjectT::*x)($for j , [[P$j]]) 95 96template <class ObjectT, class R$for j [[, 97 class P$j]]> 98MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]> 99Bind(FP_T(method), ObjectT* object$for j [[, 100 typename detail::identity<P$j>::type p$j]]) { 101 return MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>( 102 method, object$for j [[, p$j]]); 103} 104 105#undef FP_T 106#define FP_T(x) R (ObjectT::*x)($for j , [[P$j]]) const 107 108template <class ObjectT, class R$for j [[, 109 class P$j]]> 110MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]> 111Bind(FP_T(method), const ObjectT* object$for j [[, 112 typename detail::identity<P$j>::type p$j]]) { 113 return MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]>( 114 method, object$for j [[, p$j]]); 115} 116 117#undef FP_T 118 119]] 120 121} // namespace talk_base 122 123#undef NONAME 124 125#endif // TALK_BASE_BIND_H_ 126