blob: 2ebb8955159ce0d6c59522312084a3d2132da456 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000094template <class FunctorT, class R$for j [[,
95 class P$j]]>
96class Functor$i {
97 public:
98 $if i == 0 [[explicit ]]
99Functor$i(const FunctorT& functor$for j [[, P$j p$j]])
100 : functor_(functor)$for j [[,
101 p$(j)_(p$j)]] {}
102 R operator()() const {
103 return functor_($for j , [[p$(j)_]]); }
104 private:
105 FunctorT functor_;$for j [[
106
107 P$j p$(j)_;]]
108
109};
110
111
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112#define FP_T(x) R (ObjectT::*x)($for j , [[P$j]])
113
114template <class ObjectT, class R$for j [[,
115 class P$j]]>
116MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
117Bind(FP_T(method), ObjectT* object$for j [[,
118 typename detail::identity<P$j>::type p$j]]) {
119 return MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>(
120 method, object$for j [[, p$j]]);
121}
122
123#undef FP_T
124#define FP_T(x) R (ObjectT::*x)($for j , [[P$j]]) const
125
126template <class ObjectT, class R$for j [[,
127 class P$j]]>
128MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
129Bind(FP_T(method), const ObjectT* object$for j [[,
130 typename detail::identity<P$j>::type p$j]]) {
131 return MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]>(
132 method, object$for j [[, p$j]]);
133}
134
135#undef FP_T
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000136#define FP_T(x) R (*x)($for j , [[P$j]])
137
138template <class R$for j [[,
139 class P$j]]>
140Functor$i<FP_T(NONAME), R$for j [[, P$j]]>
141Bind(FP_T(function)$for j [[,
142 typename detail::identity<P$j>::type p$j]]) {
143 return Functor$i<FP_T(NONAME), R$for j [[, P$j]]>(
144 function$for j [[, p$j]]);
145}
146
147#undef FP_T
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148
149]]
150
151} // namespace talk_base
152
153#undef NONAME
154
155#endif // TALK_BASE_BIND_H_