Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: webrtc/base/bind.h.pump

Issue 2719683002: Rewrite rtc::Bind using variadic templates. (Closed)
Patch Set: Mention where sequence_generator comes from in a comment. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/bind.h ('k') | webrtc/base/bind_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
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
15 // 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.
22 //
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 // }
38 //
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.
47 // invoker.AsyncInvoke(RTC_FROM_HERE,rtc::Bind(&Bar::Test, this));
48 // }
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 //
59
60 #ifndef WEBRTC_BASE_BIND_H_
61 #define WEBRTC_BASE_BIND_H_
62
63 #include "webrtc/base/scoped_ref_ptr.h"
64 #include "webrtc/base/template_util.h"
65
66 #define NONAME
67
68 namespace rtc {
69 namespace 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.
76 template <class T> struct identity { typedef T type; };
77
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.
82 template <typename T>
83 class 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
99 public:
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.
106 template <bool condition, typename IfTrueT, typename IfFalseT>
107 struct TernaryTypeOperator {};
108
109 template <typename IfTrueT, typename IfFalseT>
110 struct TernaryTypeOperator<true, IfTrueT, IfFalseT> {
111 typedef IfTrueT type;
112 };
113
114 template <typename IfTrueT, typename IfFalseT>
115 struct 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.
121 template <class T>
122 struct PointerType {
123 typedef typename TernaryTypeOperator<IsRefCounted<T>::value,
124 scoped_refptr<T>,
125 T*>::type type;
126 };
127
128 } // namespace detail
129
130 $var n = 9
131 $range i 0..n
132 $for i [[
133 $range j 1..i
134
135 template <class ObjectT, class MethodT, class R$for j [[,
136 class P$j]]>
137 class 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_;
147 typename detail::PointerType<ObjectT>::type object_;$for j [[
148
149 typename rtc::remove_reference<P$j>::type p$(j)_;]]
150
151 };
152
153 template <class FunctorT, class R$for j [[,
154 class P$j]]>
155 class Functor$i {
156 public:
157 $if i == 0 [[explicit ]]
158 Functor$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
166 typename rtc::remove_reference<P$j>::type p$(j)_;]]
167
168 };
169
170
171 #define FP_T(x) R (ObjectT::*x)($for j , [[P$j]])
172
173 template <class ObjectT, class R$for j [[,
174 class P$j]]>
175 MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
176 Bind(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
185 template <class ObjectT, class R$for j [[,
186 class P$j]]>
187 MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
188 Bind(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
195 #define FP_T(x) R (ObjectT::*x)($for j , [[P$j]])
196
197 template <class ObjectT, class R$for j [[,
198 class P$j]]>
199 MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
200 Bind(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
207 #define FP_T(x) R (*x)($for j , [[P$j]])
208
209 template <class R$for j [[,
210 class P$j]]>
211 Functor$i<FP_T(NONAME), R$for j [[, P$j]]>
212 Bind(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_
OLDNEW
« no previous file with comments | « webrtc/base/bind.h ('k') | webrtc/base/bind_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698