|
| 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 | +// Bind() is an overloaded function that converts method calls into function |
| 12 | +// objects (aka functors). The method object is captured as a scoped_refptr<> if |
| 13 | +// possible, and as a raw pointer otherwise. Any arguments to the method are |
| 14 | +// captured by value. The return value of Bind is a stateful, nullary function |
| 15 | +// object. Care should be taken about the lifetime of objects captured by |
| 16 | +// Bind(); the returned functor knows nothing about the lifetime of a non |
| 17 | +// ref-counted method object or any arguments passed by pointer, and calling the |
| 18 | +// functor with a destroyed object will surely do bad things. |
| 19 | +// |
| 20 | +// To prevent the method object from being captured as a scoped_refptr<>, you |
| 21 | +// can use Unretained. But this should only be done when absolutely necessary, |
| 22 | +// and when the caller knows the extra reference isn't needed. |
| 23 | +// |
| 24 | +// Example usage: |
| 25 | +// struct Foo { |
| 26 | +// int Test1() { return 42; } |
| 27 | +// int Test2() const { return 52; } |
| 28 | +// int Test3(int x) { return x*x; } |
| 29 | +// float Test4(int x, float y) { return x + y; } |
| 30 | +// }; |
| 31 | +// |
| 32 | +// int main() { |
| 33 | +// Foo foo; |
| 34 | +// cout << rtc::Bind(&Foo::Test1, &foo)() << endl; |
| 35 | +// cout << rtc::Bind(&Foo::Test2, &foo)() << endl; |
| 36 | +// cout << rtc::Bind(&Foo::Test3, &foo, 3)() << endl; |
| 37 | +// cout << rtc::Bind(&Foo::Test4, &foo, 7, 8.5f)() << endl; |
| 38 | +// } |
| 39 | +// |
| 40 | +// Example usage of ref counted objects: |
| 41 | +// struct Bar { |
| 42 | +// int AddRef(); |
| 43 | +// int Release(); |
| 44 | +// |
| 45 | +// void Test() {} |
| 46 | +// void BindThis() { |
| 47 | +// // The functor passed to AsyncInvoke() will keep this object alive. |
| 48 | +// invoker.AsyncInvoke(RTC_FROM_HERE,rtc::Bind(&Bar::Test, this)); |
| 49 | +// } |
| 50 | +// }; |
| 51 | +// |
| 52 | +// int main() { |
| 53 | +// rtc::scoped_refptr<Bar> bar = new rtc::RefCountedObject<Bar>(); |
| 54 | +// auto functor = rtc::Bind(&Bar::Test, bar); |
| 55 | +// bar = nullptr; |
| 56 | +// // The functor stores an internal scoped_refptr<Bar>, so this is safe. |
| 57 | +// functor(); |
| 58 | +// } |
| 59 | +// |
| 60 | + |
| 61 | +#ifndef RTC_BASE_BIND_H_ |
| 62 | +#define RTC_BASE_BIND_H_ |
| 63 | + |
| 64 | +#include <tuple> |
| 65 | +#include <type_traits> |
| 66 | + |
| 67 | +#include "api/scoped_refptr.h" |
| 68 | +#include "rtc_base/template_util.h" |
| 69 | + |
| 70 | +#define NONAME |
| 71 | + |
| 72 | +namespace rtc { |
| 73 | +namespace detail { |
| 74 | +// This is needed because the template parameters in Bind can't be resolved |
| 75 | +// if they're used both as parameters of the function pointer type and as |
| 76 | +// parameters to Bind itself: the function pointer parameters are exact |
| 77 | +// matches to the function prototype, but the parameters to bind have |
| 78 | +// references stripped. This trick allows the compiler to dictate the Bind |
| 79 | +// parameter types rather than deduce them. |
| 80 | +template <class T> |
| 81 | +struct identity { |
| 82 | + typedef T type; |
| 83 | +}; |
| 84 | + |
| 85 | +//* IsRefCounted<T>::value will be true for types that can be used in |
| 86 | +// rtc::scoped_refptr<T>, i.e. types that implements nullary functions AddRef() |
| 87 | +// and Release(), regardless of their return types. AddRef() and Release() can |
| 88 | +// be defined in T or any superclass of T. |
| 89 | +template <typename T> |
| 90 | +class IsRefCounted { |
| 91 | + // This is a complex implementation detail done with SFINAE. |
| 92 | + |
| 93 | + // Define types such that sizeof(Yes) != sizeof(No). |
| 94 | + struct Yes { |
| 95 | + char dummy[1]; |
| 96 | + }; |
| 97 | + struct No { |
| 98 | + char dummy[2]; |
| 99 | + }; |
| 100 | + // Define two overloaded template functions with return types of different |
| 101 | + // size. This way, we can use sizeof() on the return type to determine which |
| 102 | + // function the compiler would have chosen. One function will be preferred |
| 103 | + // over the other if it is possible to create it without compiler errors, |
| 104 | + // otherwise the compiler will simply remove it, and default to the less |
| 105 | + // preferred function. |
| 106 | + template <typename R> |
| 107 | + static Yes test(R* r, decltype(r->AddRef(), r->Release(), 42)); |
| 108 | + template <typename C> |
| 109 | + static No test(...); |
| 110 | + |
| 111 | + public: |
| 112 | + // Trick the compiler to tell if it's possible to call AddRef() and Release(). |
| 113 | + static const bool value = sizeof(test<T>((T*)nullptr, 42)) == sizeof(Yes); |
| 114 | +}; |
| 115 | + |
| 116 | +// TernaryTypeOperator is a helper class to select a type based on a static bool |
| 117 | +// value. |
| 118 | +template <bool condition, typename IfTrueT, typename IfFalseT> |
| 119 | +struct TernaryTypeOperator {}; |
| 120 | + |
| 121 | +template <typename IfTrueT, typename IfFalseT> |
| 122 | +struct TernaryTypeOperator<true, IfTrueT, IfFalseT> { |
| 123 | + typedef IfTrueT type; |
| 124 | +}; |
| 125 | + |
| 126 | +template <typename IfTrueT, typename IfFalseT> |
| 127 | +struct TernaryTypeOperator<false, IfTrueT, IfFalseT> { |
| 128 | + typedef IfFalseT type; |
| 129 | +}; |
| 130 | + |
| 131 | +// PointerType<T>::type will be scoped_refptr<T> for ref counted types, and T* |
| 132 | +// otherwise. |
| 133 | +template <class T> |
| 134 | +struct PointerType { |
| 135 | + typedef typename TernaryTypeOperator<IsRefCounted<T>::value, |
| 136 | + scoped_refptr<T>, |
| 137 | + T*>::type type; |
| 138 | +}; |
| 139 | + |
| 140 | +template <typename T> |
| 141 | +class UnretainedWrapper { |
| 142 | + public: |
| 143 | + explicit UnretainedWrapper(T* o) : ptr_(o) {} |
| 144 | + T* get() const { return ptr_; } |
| 145 | + |
| 146 | + private: |
| 147 | + T* ptr_; |
| 148 | +}; |
| 149 | + |
| 150 | +} // namespace detail |
| 151 | + |
| 152 | +template <typename T> |
| 153 | +static inline detail::UnretainedWrapper<T> Unretained(T* o) { |
| 154 | + return detail::UnretainedWrapper<T>(o); |
| 155 | +} |
| 156 | + |
| 157 | +template <class ObjectT, class MethodT, class R, typename... Args> |
| 158 | +class MethodFunctor { |
| 159 | + public: |
| 160 | + MethodFunctor(MethodT method, ObjectT* object, Args... args) |
| 161 | + : method_(method), object_(object), args_(args...) {} |
| 162 | + R operator()() const { |
| 163 | + return CallMethod(typename sequence_generator<sizeof...(Args)>::type()); |
| 164 | + } |
| 165 | + |
| 166 | + private: |
| 167 | + // Use sequence_generator (see template_util.h) to expand a MethodFunctor |
| 168 | + // with 2 arguments to (std::get<0>(args_), std::get<1>(args_)), for |
| 169 | + // instance. |
| 170 | + template <int... S> |
| 171 | + R CallMethod(sequence<S...>) const { |
| 172 | + return (object_->*method_)(std::get<S>(args_)...); |
| 173 | + } |
| 174 | + |
| 175 | + MethodT method_; |
| 176 | + typename detail::PointerType<ObjectT>::type object_; |
| 177 | + typename std::tuple<typename std::remove_reference<Args>::type...> args_; |
| 178 | +}; |
| 179 | + |
| 180 | +template <class ObjectT, class MethodT, class R, typename... Args> |
| 181 | +class UnretainedMethodFunctor { |
| 182 | + public: |
| 183 | + UnretainedMethodFunctor(MethodT method, |
| 184 | + detail::UnretainedWrapper<ObjectT> object, |
| 185 | + Args... args) |
| 186 | + : method_(method), object_(object.get()), args_(args...) {} |
| 187 | + R operator()() const { |
| 188 | + return CallMethod(typename sequence_generator<sizeof...(Args)>::type()); |
| 189 | + } |
| 190 | + |
| 191 | + private: |
| 192 | + // Use sequence_generator (see template_util.h) to expand an |
| 193 | + // UnretainedMethodFunctor with 2 arguments to (std::get<0>(args_), |
| 194 | + // std::get<1>(args_)), for instance. |
| 195 | + template <int... S> |
| 196 | + R CallMethod(sequence<S...>) const { |
| 197 | + return (object_->*method_)(std::get<S>(args_)...); |
| 198 | + } |
| 199 | + |
| 200 | + MethodT method_; |
| 201 | + ObjectT* object_; |
| 202 | + typename std::tuple<typename std::remove_reference<Args>::type...> args_; |
| 203 | +}; |
| 204 | + |
| 205 | +template <class FunctorT, class R, typename... Args> |
| 206 | +class Functor { |
| 207 | + public: |
| 208 | + Functor(const FunctorT& functor, Args... args) |
| 209 | + : functor_(functor), args_(args...) {} |
| 210 | + R operator()() const { |
| 211 | + return CallFunction(typename sequence_generator<sizeof...(Args)>::type()); |
| 212 | + } |
| 213 | + |
| 214 | + private: |
| 215 | + // Use sequence_generator (see template_util.h) to expand a Functor |
| 216 | + // with 2 arguments to (std::get<0>(args_), std::get<1>(args_)), for |
| 217 | + // instance. |
| 218 | + template <int... S> |
| 219 | + R CallFunction(sequence<S...>) const { |
| 220 | + return functor_(std::get<S>(args_)...); |
| 221 | + } |
| 222 | + |
| 223 | + FunctorT functor_; |
| 224 | + typename std::tuple<typename std::remove_reference<Args>::type...> args_; |
| 225 | +}; |
| 226 | + |
| 227 | +#define FP_T(x) R (ObjectT::*x)(Args...) |
| 228 | + |
| 229 | +template <class ObjectT, class R, typename... Args> |
| 230 | +MethodFunctor<ObjectT, FP_T(NONAME), R, Args...> Bind( |
| 231 | + FP_T(method), |
| 232 | + ObjectT* object, |
| 233 | + typename detail::identity<Args>::type... args) { |
| 234 | + return MethodFunctor<ObjectT, FP_T(NONAME), R, Args...>(method, object, |
| 235 | + args...); |
| 236 | +} |
| 237 | + |
| 238 | +template <class ObjectT, class R, typename... Args> |
| 239 | +MethodFunctor<ObjectT, FP_T(NONAME), R, Args...> Bind( |
| 240 | + FP_T(method), |
| 241 | + const scoped_refptr<ObjectT>& object, |
| 242 | + typename detail::identity<Args>::type... args) { |
| 243 | + return MethodFunctor<ObjectT, FP_T(NONAME), R, Args...>(method, object.get(), |
| 244 | + args...); |
| 245 | +} |
| 246 | + |
| 247 | +template <class ObjectT, class R, typename... Args> |
| 248 | +UnretainedMethodFunctor<ObjectT, FP_T(NONAME), R, Args...> Bind( |
| 249 | + FP_T(method), |
| 250 | + detail::UnretainedWrapper<ObjectT> object, |
| 251 | + typename detail::identity<Args>::type... args) { |
| 252 | + return UnretainedMethodFunctor<ObjectT, FP_T(NONAME), R, Args...>( |
| 253 | + method, object, args...); |
| 254 | +} |
| 255 | + |
| 256 | +#undef FP_T |
| 257 | +#define FP_T(x) R (ObjectT::*x)(Args...) const |
| 258 | + |
| 259 | +template <class ObjectT, class R, typename... Args> |
| 260 | +MethodFunctor<const ObjectT, FP_T(NONAME), R, Args...> Bind( |
| 261 | + FP_T(method), |
| 262 | + const ObjectT* object, |
| 263 | + typename detail::identity<Args>::type... args) { |
| 264 | + return MethodFunctor<const ObjectT, FP_T(NONAME), R, Args...>(method, object, |
| 265 | + args...); |
| 266 | +} |
| 267 | +template <class ObjectT, class R, typename... Args> |
| 268 | +UnretainedMethodFunctor<const ObjectT, FP_T(NONAME), R, Args...> Bind( |
| 269 | + FP_T(method), |
| 270 | + detail::UnretainedWrapper<const ObjectT> object, |
| 271 | + typename detail::identity<Args>::type... args) { |
| 272 | + return UnretainedMethodFunctor<const ObjectT, FP_T(NONAME), R, Args...>( |
| 273 | + method, object, args...); |
| 274 | +} |
| 275 | + |
| 276 | +#undef FP_T |
| 277 | +#define FP_T(x) R (*x)(Args...) |
| 278 | + |
| 279 | +template <class R, typename... Args> |
| 280 | +Functor<FP_T(NONAME), R, Args...> Bind( |
| 281 | + FP_T(function), |
| 282 | + typename detail::identity<Args>::type... args) { |
| 283 | + return Functor<FP_T(NONAME), R, Args...>(function, args...); |
| 284 | +} |
| 285 | + |
| 286 | +#undef FP_T |
| 287 | + |
| 288 | +} // namespace rtc |
| 289 | + |
| 290 | +#undef NONAME |
| 291 | + |
| 292 | +#endif // RTC_BASE_BIND_H_ |
0 commit comments