blob: 2a1a55c0e5491f1e4a5ec088a28083e14a580cc2 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// To generate bind.h from bind.h.pump, execute:
12// /home/build/google3/third_party/gtest/scripts/pump.py bind.h.pump
13
14// Bind() is an overloaded function that converts method calls into function
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020015// objects (aka functors). The method object is captured as a scoped_refptr<> if
16// possible, and as a raw pointer otherwise. Any arguments to the method are
17// captured by value. The return value of Bind is a stateful, nullary function
18// object. Care should be taken about the lifetime of objects captured by
19// Bind(); the returned functor knows nothing about the lifetime of a non
20// ref-counted method object or any arguments passed by pointer, and calling the
21// functor with a destroyed object will surely do bad things.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022//
23// Example usage:
24// struct Foo {
25// int Test1() { return 42; }
26// int Test2() const { return 52; }
27// int Test3(int x) { return x*x; }
28// float Test4(int x, float y) { return x + y; }
29// };
30//
31// int main() {
32// Foo foo;
33// cout << rtc::Bind(&Foo::Test1, &foo)() << endl;
34// cout << rtc::Bind(&Foo::Test2, &foo)() << endl;
35// cout << rtc::Bind(&Foo::Test3, &foo, 3)() << endl;
36// cout << rtc::Bind(&Foo::Test4, &foo, 7, 8.5f)() << endl;
37// }
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020038//
39// Example usage of ref counted objects:
40// struct Bar {
41// int AddRef();
42// int Release();
43//
44// void Test() {}
45// void BindThis() {
46// // The functor passed to AsyncInvoke() will keep this object alive.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070047// invoker.AsyncInvoke(RTC_FROM_HERE,rtc::Bind(&Bar::Test, this));
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020048// }
49// };
50//
51// int main() {
52// rtc::scoped_refptr<Bar> bar = new rtc::RefCountedObject<Bar>();
53// auto functor = rtc::Bind(&Bar::Test, bar);
54// bar = nullptr;
55// // The functor stores an internal scoped_refptr<Bar>, so this is safe.
56// functor();
57// }
58//
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
60#ifndef WEBRTC_BASE_BIND_H_
61#define WEBRTC_BASE_BIND_H_
62
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020063#include "webrtc/base/scoped_ref_ptr.h"
noahric5d9b92b2015-10-24 11:14:46 -070064#include "webrtc/base/template_util.h"
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020065
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066#define NONAME
67
68namespace rtc {
69namespace detail {
70// This is needed because the template parameters in Bind can't be resolved
71// if they're used both as parameters of the function pointer type and as
72// parameters to Bind itself: the function pointer parameters are exact
73// matches to the function prototype, but the parameters to bind have
74// references stripped. This trick allows the compiler to dictate the Bind
75// parameter types rather than deduce them.
76template <class T> struct identity { typedef T type; };
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020077
78// IsRefCounted<T>::value will be true for types that can be used in
79// rtc::scoped_refptr<T>, i.e. types that implements nullary functions AddRef()
80// and Release(), regardless of their return types. AddRef() and Release() can
81// be defined in T or any superclass of T.
82template <typename T>
83class IsRefCounted {
84 // This is a complex implementation detail done with SFINAE.
85
86 // Define types such that sizeof(Yes) != sizeof(No).
87 struct Yes { char dummy[1]; };
88 struct No { char dummy[2]; };
89 // Define two overloaded template functions with return types of different
90 // size. This way, we can use sizeof() on the return type to determine which
91 // function the compiler would have chosen. One function will be preferred
92 // over the other if it is possible to create it without compiler errors,
93 // otherwise the compiler will simply remove it, and default to the less
94 // preferred function.
95 template <typename R>
96 static Yes test(R* r, decltype(r->AddRef(), r->Release(), 42));
97 template <typename C> static No test(...);
98
99public:
100 // Trick the compiler to tell if it's possible to call AddRef() and Release().
101 static const bool value = sizeof(test<T>((T*)nullptr, 42)) == sizeof(Yes);
102};
103
104// TernaryTypeOperator is a helper class to select a type based on a static bool
105// value.
106template <bool condition, typename IfTrueT, typename IfFalseT>
107struct TernaryTypeOperator {};
108
109template <typename IfTrueT, typename IfFalseT>
110struct TernaryTypeOperator<true, IfTrueT, IfFalseT> {
111 typedef IfTrueT type;
112};
113
114template <typename IfTrueT, typename IfFalseT>
115struct TernaryTypeOperator<false, IfTrueT, IfFalseT> {
116 typedef IfFalseT type;
117};
118
119// PointerType<T>::type will be scoped_refptr<T> for ref counted types, and T*
120// otherwise.
121template <class T>
122struct PointerType {
123 typedef typename TernaryTypeOperator<IsRefCounted<T>::value,
124 scoped_refptr<T>,
125 T*>::type type;
126};
127
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128} // namespace detail
129
noahric5d9b92b2015-10-24 11:14:46 -0700130$var n = 9
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131$range i 0..n
132$for i [[
133$range j 1..i
134
135template <class ObjectT, class MethodT, class R$for j [[,
136 class P$j]]>
137class MethodFunctor$i {
138 public:
139 MethodFunctor$i(MethodT method, ObjectT* object$for j [[,
140 P$j p$j]])
141 : method_(method), object_(object)$for j [[,
142 p$(j)_(p$j)]] {}
143 R operator()() const {
144 return (object_->*method_)($for j , [[p$(j)_]]); }
145 private:
146 MethodT method_;
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200147 typename detail::PointerType<ObjectT>::type object_;$for j [[
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148
noahric5d9b92b2015-10-24 11:14:46 -0700149 typename rtc::remove_reference<P$j>::type p$(j)_;]]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150
151};
152
153template <class FunctorT, class R$for j [[,
154 class P$j]]>
155class Functor$i {
156 public:
157 $if i == 0 [[explicit ]]
158Functor$i(const FunctorT& functor$for j [[, P$j p$j]])
159 : functor_(functor)$for j [[,
160 p$(j)_(p$j)]] {}
161 R operator()() const {
162 return functor_($for j , [[p$(j)_]]); }
163 private:
164 FunctorT functor_;$for j [[
165
noahric5d9b92b2015-10-24 11:14:46 -0700166 typename rtc::remove_reference<P$j>::type p$(j)_;]]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167
168};
169
170
171#define FP_T(x) R (ObjectT::*x)($for j , [[P$j]])
172
173template <class ObjectT, class R$for j [[,
174 class P$j]]>
175MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
176Bind(FP_T(method), ObjectT* object$for j [[,
177 typename detail::identity<P$j>::type p$j]]) {
178 return MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>(
179 method, object$for j [[, p$j]]);
180}
181
182#undef FP_T
183#define FP_T(x) R (ObjectT::*x)($for j , [[P$j]]) const
184
185template <class ObjectT, class R$for j [[,
186 class P$j]]>
187MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
188Bind(FP_T(method), const ObjectT* object$for j [[,
189 typename detail::identity<P$j>::type p$j]]) {
190 return MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]>(
191 method, object$for j [[, p$j]]);
192}
193
194#undef FP_T
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200195#define FP_T(x) R (ObjectT::*x)($for j , [[P$j]])
196
197template <class ObjectT, class R$for j [[,
198 class P$j]]>
199MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
200Bind(FP_T(method), const scoped_refptr<ObjectT>& object$for j [[,
201 typename detail::identity<P$j>::type p$j]]) {
202 return MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>(
203 method, object.get()$for j [[, p$j]]);
204}
205
206#undef FP_T
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000207#define FP_T(x) R (*x)($for j , [[P$j]])
208
209template <class R$for j [[,
210 class P$j]]>
211Functor$i<FP_T(NONAME), R$for j [[, P$j]]>
212Bind(FP_T(function)$for j [[,
213 typename detail::identity<P$j>::type p$j]]) {
214 return Functor$i<FP_T(NONAME), R$for j [[, P$j]]>(
215 function$for j [[, p$j]]);
216}
217
218#undef FP_T
219
220]]
221
222} // namespace rtc
223
224#undef NONAME
225
226#endif // WEBRTC_BASE_BIND_H_