3 * Copyright 2012 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 // To generate bind.h from bind.h.pump, execute:
29 // /home/build/google3/third_party/gtest/scripts/pump.py bind.h.pump
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.
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; }
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;
55 #ifndef TALK_BASE_BIND_H_
56 #define TALK_BASE_BIND_H_
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.
68 template <class T> struct identity { typedef T type; };
76 template <class ObjectT, class MethodT, class R$for j [[,
78 class MethodFunctor$i {
80 MethodFunctor$i(MethodT method, ObjectT* object$for j [[,
82 : method_(method), object_(object)$for j [[,
84 R operator()() const {
85 return (object_->*method_)($for j , [[p$(j)_]]); }
88 ObjectT* object_;$for j [[
94 #define FP_T(x) R (ObjectT::*x)($for j , [[P$j]])
96 template <class ObjectT, class R$for j [[,
98 MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
99 Bind(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]]);
106 #define FP_T(x) R (ObjectT::*x)($for j , [[P$j]]) const
108 template <class ObjectT, class R$for j [[,
110 MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
111 Bind(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]]);
121 } // namespace talk_base
125 #endif // TALK_BASE_BIND_H_