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

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

Issue 1300523004: rtc::Bind: Capture method objects as scoped_refptr if they are ref counted (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Update bind.h.pump Created 5 years, 4 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
OLDNEW
1 /* 1 /*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 // To generate bind.h from bind.h.pump, execute: 11 // To generate bind.h from bind.h.pump, execute:
12 // /home/build/google3/third_party/gtest/scripts/pump.py bind.h.pump 12 // /home/build/google3/third_party/gtest/scripts/pump.py bind.h.pump
13 13
14 // Bind() is an overloaded function that converts method calls into function 14 // Bind() is an overloaded function that converts method calls into function
15 // objects (aka functors). It captures any arguments to the method by value 15 // objects (aka functors). The method object is captured as a scoped_refptr<> if
16 // when Bind is called, producing a stateful, nullary function object. Care 16 // possible, and as a raw pointer otherwise. Any arguments to the method are
17 // should be taken about the lifetime of objects captured by Bind(); the 17 // captured by value. The return value of Bind is a stateful, nullary function
18 // returned functor knows nothing about the lifetime of the method's object or 18 // object. Care should be taken about the lifetime of objects captured by
19 // any arguments passed by pointer, and calling the functor with a destroyed 19 // Bind(); the returned functor knows nothing about the lifetime of a non
20 // object will surely do bad things. 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.
21 // 22 //
22 // Example usage: 23 // Example usage:
23 // struct Foo { 24 // struct Foo {
24 // int Test1() { return 42; } 25 // int Test1() { return 42; }
25 // int Test2() const { return 52; } 26 // int Test2() const { return 52; }
26 // int Test3(int x) { return x*x; } 27 // int Test3(int x) { return x*x; }
27 // float Test4(int x, float y) { return x + y; } 28 // float Test4(int x, float y) { return x + y; }
28 // }; 29 // };
29 // 30 //
30 // int main() { 31 // int main() {
31 // Foo foo; 32 // Foo foo;
32 // cout << rtc::Bind(&Foo::Test1, &foo)() << endl; 33 // cout << rtc::Bind(&Foo::Test1, &foo)() << endl;
33 // cout << rtc::Bind(&Foo::Test2, &foo)() << endl; 34 // cout << rtc::Bind(&Foo::Test2, &foo)() << endl;
34 // cout << rtc::Bind(&Foo::Test3, &foo, 3)() << endl; 35 // cout << rtc::Bind(&Foo::Test3, &foo, 3)() << endl;
35 // cout << rtc::Bind(&Foo::Test4, &foo, 7, 8.5f)() << endl; 36 // cout << rtc::Bind(&Foo::Test4, &foo, 7, 8.5f)() << endl;
36 // } 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::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 //
37 59
38 #ifndef WEBRTC_BASE_BIND_H_ 60 #ifndef WEBRTC_BASE_BIND_H_
39 #define WEBRTC_BASE_BIND_H_ 61 #define WEBRTC_BASE_BIND_H_
40 62
63 #include "webrtc/base/scoped_ref_ptr.h"
64
41 #define NONAME 65 #define NONAME
42 66
43 namespace rtc { 67 namespace rtc {
44 namespace detail { 68 namespace detail {
45 // This is needed because the template parameters in Bind can't be resolved 69 // This is needed because the template parameters in Bind can't be resolved
46 // if they're used both as parameters of the function pointer type and as 70 // if they're used both as parameters of the function pointer type and as
47 // parameters to Bind itself: the function pointer parameters are exact 71 // parameters to Bind itself: the function pointer parameters are exact
48 // matches to the function prototype, but the parameters to bind have 72 // matches to the function prototype, but the parameters to bind have
49 // references stripped. This trick allows the compiler to dictate the Bind 73 // references stripped. This trick allows the compiler to dictate the Bind
50 // parameter types rather than deduce them. 74 // parameter types rather than deduce them.
51 template <class T> struct identity { typedef T type; }; 75 template <class T> struct identity { typedef T type; };
76
77 // IsRefCounted<T>::value will be true for types that can be used in
78 // rtc::scoped_refptr<T>, i.e. types that implements nullary functions AddRef()
79 // and Release(), regardless of their return types. AddRef() and Release() can
80 // be defined in T or any superclass of T.
81 template <typename T>
82 class IsRefCounted {
83 // Once you have figured out how the following code works, and realized what a
84 // terrible mistake that was, please increment the following counter as a
85 // warning to the next guy:
86 // total_hours_wasted_here = 5
87
88 // Define types such that sizeof(Yes) != sizeof(No).
89 struct Yes { char dummy[1]; };
90 struct No { char dummy[2]; };
91 // Define two overloaded template functions with return types of different
92 // size. This way, we can use sizeof() on the return type to determine which
93 // function the compiler would have chosen. One function will be preferred
94 // over the other if it is possible to create it without compiler errors,
95 // otherwise the compiler will simply remove it, and default to the less
96 // preferred function.
97 template <typename R>
98 static Yes test(R* r, decltype(r->AddRef(), r->Release(), 42));
99 template <typename C> static No test(...);
100
101 public:
102 // Trick the compiler to tell if it's possible to call AddRef() and Release().
103 static const bool value = sizeof(test<T>((T*)nullptr, 42)) == sizeof(Yes);
104 };
105
106 // TernaryTypeOperator is a helper class to select a type based on a static bool
107 // value.
108 template <bool condition, typename IfTrueT, typename IfFalseT>
109 struct TernaryTypeOperator {};
110
111 template <typename IfTrueT, typename IfFalseT>
112 struct TernaryTypeOperator<true, IfTrueT, IfFalseT> {
113 typedef IfTrueT type;
114 };
115
116 template <typename IfTrueT, typename IfFalseT>
117 struct TernaryTypeOperator<false, IfTrueT, IfFalseT> {
118 typedef IfFalseT type;
119 };
120
121 // PointerType<T>::type will be scoped_refptr<T> for ref counted types, and T*
122 // otherwise.
123 template <class T>
124 struct PointerType {
125 typedef typename TernaryTypeOperator<IsRefCounted<T>::value,
126 scoped_refptr<T>,
127 T*>::type type;
128 };
129
52 } // namespace detail 130 } // namespace detail
53 131
54 $var n = 6 132 $var n = 6
55 $range i 0..n 133 $range i 0..n
56 $for i [[ 134 $for i [[
57 $range j 1..i 135 $range j 1..i
58 136
59 template <class ObjectT, class MethodT, class R$for j [[, 137 template <class ObjectT, class MethodT, class R$for j [[,
60 class P$j]]> 138 class P$j]]>
61 class MethodFunctor$i { 139 class MethodFunctor$i {
62 public: 140 public:
63 MethodFunctor$i(MethodT method, ObjectT* object$for j [[, 141 MethodFunctor$i(MethodT method, ObjectT* object$for j [[,
64 P$j p$j]]) 142 P$j p$j]])
65 : method_(method), object_(object)$for j [[, 143 : method_(method), object_(object)$for j [[,
66 p$(j)_(p$j)]] {} 144 p$(j)_(p$j)]] {}
67 R operator()() const { 145 R operator()() const {
68 return (object_->*method_)($for j , [[p$(j)_]]); } 146 return (object_->*method_)($for j , [[p$(j)_]]); }
69 private: 147 private:
70 MethodT method_; 148 MethodT method_;
71 ObjectT* object_;$for j [[ 149 typename detail::PointerType<ObjectT>::type object_;$for j [[
72 150
73 P$j p$(j)_;]] 151 P$j p$(j)_;]]
74 152
75 }; 153 };
76 154
77 template <class FunctorT, class R$for j [[, 155 template <class FunctorT, class R$for j [[,
78 class P$j]]> 156 class P$j]]>
79 class Functor$i { 157 class Functor$i {
80 public: 158 public:
81 $if i == 0 [[explicit ]] 159 $if i == 0 [[explicit ]]
(...skipping 27 matching lines...) Expand all
109 template <class ObjectT, class R$for j [[, 187 template <class ObjectT, class R$for j [[,
110 class P$j]]> 188 class P$j]]>
111 MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]> 189 MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
112 Bind(FP_T(method), const ObjectT* object$for j [[, 190 Bind(FP_T(method), const ObjectT* object$for j [[,
113 typename detail::identity<P$j>::type p$j]]) { 191 typename detail::identity<P$j>::type p$j]]) {
114 return MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]>( 192 return MethodFunctor$i<const ObjectT, FP_T(NONAME), R$for j [[, P$j]]>(
115 method, object$for j [[, p$j]]); 193 method, object$for j [[, p$j]]);
116 } 194 }
117 195
118 #undef FP_T 196 #undef FP_T
197 #define FP_T(x) R (ObjectT::*x)($for j , [[P$j]])
198
199 template <class ObjectT, class R$for j [[,
200 class P$j]]>
201 MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
202 Bind(FP_T(method), const scoped_refptr<ObjectT>& object$for j [[,
203 typename detail::identity<P$j>::type p$j]]) {
204 return MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>(
205 method, object.get()$for j [[, p$j]]);
206 }
207
208 #undef FP_T
119 #define FP_T(x) R (*x)($for j , [[P$j]]) 209 #define FP_T(x) R (*x)($for j , [[P$j]])
120 210
121 template <class R$for j [[, 211 template <class R$for j [[,
122 class P$j]]> 212 class P$j]]>
123 Functor$i<FP_T(NONAME), R$for j [[, P$j]]> 213 Functor$i<FP_T(NONAME), R$for j [[, P$j]]>
124 Bind(FP_T(function)$for j [[, 214 Bind(FP_T(function)$for j [[,
125 typename detail::identity<P$j>::type p$j]]) { 215 typename detail::identity<P$j>::type p$j]]) {
126 return Functor$i<FP_T(NONAME), R$for j [[, P$j]]>( 216 return Functor$i<FP_T(NONAME), R$for j [[, P$j]]>(
127 function$for j [[, p$j]]); 217 function$for j [[, p$j]]);
128 } 218 }
129 219
130 #undef FP_T 220 #undef FP_T
131 221
132 ]] 222 ]]
133 223
134 } // namespace rtc 224 } // namespace rtc
135 225
136 #undef NONAME 226 #undef NONAME
137 227
138 #endif // WEBRTC_BASE_BIND_H_ 228 #endif // WEBRTC_BASE_BIND_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698