henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 11 | // Bind() is an overloaded function that converts method calls into function |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 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. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | // |
| 20 | // Example usage: |
| 21 | // struct Foo { |
| 22 | // int Test1() { return 42; } |
| 23 | // int Test2() const { return 52; } |
| 24 | // int Test3(int x) { return x*x; } |
| 25 | // float Test4(int x, float y) { return x + y; } |
| 26 | // }; |
| 27 | // |
| 28 | // int main() { |
| 29 | // Foo foo; |
| 30 | // cout << rtc::Bind(&Foo::Test1, &foo)() << endl; |
| 31 | // cout << rtc::Bind(&Foo::Test2, &foo)() << endl; |
| 32 | // cout << rtc::Bind(&Foo::Test3, &foo, 3)() << endl; |
| 33 | // cout << rtc::Bind(&Foo::Test4, &foo, 7, 8.5f)() << endl; |
| 34 | // } |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 35 | // |
| 36 | // Example usage of ref counted objects: |
| 37 | // struct Bar { |
| 38 | // int AddRef(); |
| 39 | // int Release(); |
| 40 | // |
| 41 | // void Test() {} |
| 42 | // void BindThis() { |
| 43 | // // The functor passed to AsyncInvoke() will keep this object alive. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 44 | // invoker.AsyncInvoke(RTC_FROM_HERE,rtc::Bind(&Bar::Test, this)); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 45 | // } |
| 46 | // }; |
| 47 | // |
| 48 | // int main() { |
| 49 | // rtc::scoped_refptr<Bar> bar = new rtc::RefCountedObject<Bar>(); |
| 50 | // auto functor = rtc::Bind(&Bar::Test, bar); |
| 51 | // bar = nullptr; |
| 52 | // // The functor stores an internal scoped_refptr<Bar>, so this is safe. |
| 53 | // functor(); |
| 54 | // } |
| 55 | // |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | |
| 57 | #ifndef WEBRTC_BASE_BIND_H_ |
| 58 | #define WEBRTC_BASE_BIND_H_ |
| 59 | |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 60 | #include <tuple> |
| 61 | #include <type_traits> |
| 62 | |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 63 | #include "webrtc/base/scoped_ref_ptr.h" |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 64 | #include "webrtc/base/template_util.h" |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 65 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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; }; |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 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 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | } // namespace detail |
| 129 | |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 130 | template <class ObjectT, class MethodT, class R, typename... Args> |
| 131 | class MethodFunctor { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 132 | public: |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 133 | MethodFunctor(MethodT method, ObjectT* object, Args... args) |
| 134 | : method_(method), object_(object), args_(args...) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | R operator()() const { |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 136 | return CallMethod(typename sequence_generator<sizeof...(Args)>::type()); |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | private: |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 140 | // Use sequence_generator (see template_util.h) to expand a MethodFunctor |
| 141 | // with 2 arguments to (std::get<0>(args_), std::get<1>(args_)), for |
| 142 | // instance. |
| 143 | template <int... S> |
| 144 | R CallMethod(sequence<S...>) const { |
| 145 | return (object_->*method_)(std::get<S>(args_)...); |
| 146 | } |
| 147 | |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 148 | MethodT method_; |
| 149 | typename detail::PointerType<ObjectT>::type object_; |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 150 | typename std::tuple<typename std::remove_reference<Args>::type...> args_; |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 153 | template <class FunctorT, class R, typename... Args> |
| 154 | class Functor { |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 155 | public: |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 156 | Functor(const FunctorT& functor, Args... args) |
| 157 | : functor_(functor), args_(args...) {} |
| 158 | R operator()() const { |
| 159 | return CallFunction(typename sequence_generator<sizeof...(Args)>::type()); |
| 160 | } |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 161 | |
| 162 | private: |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 163 | // Use sequence_generator (see template_util.h) to expand a Functor |
| 164 | // with 2 arguments to (std::get<0>(args_), std::get<1>(args_)), for |
| 165 | // instance. |
| 166 | template <int... S> |
| 167 | R CallFunction(sequence<S...>) const { |
| 168 | return functor_(std::get<S>(args_)...); |
| 169 | } |
| 170 | |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 171 | FunctorT functor_; |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 172 | typename std::tuple<typename std::remove_reference<Args>::type...> args_; |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 175 | #define FP_T(x) R (ObjectT::*x)(Args...) |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 176 | |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 177 | template <class ObjectT, class R, typename... Args> |
| 178 | MethodFunctor<ObjectT, FP_T(NONAME), R, Args...> Bind( |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 179 | FP_T(method), |
| 180 | ObjectT* object, |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 181 | typename detail::identity<Args>::type... args) { |
| 182 | return MethodFunctor<ObjectT, FP_T(NONAME), R, Args...>(method, object, |
| 183 | args...); |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | #undef FP_T |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 187 | #define FP_T(x) R (ObjectT::*x)(Args...) const |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 188 | |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 189 | template <class ObjectT, class R, typename... Args> |
| 190 | MethodFunctor<const ObjectT, FP_T(NONAME), R, Args...> Bind( |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 191 | FP_T(method), |
| 192 | const ObjectT* object, |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 193 | typename detail::identity<Args>::type... args) { |
| 194 | return MethodFunctor<const ObjectT, FP_T(NONAME), R, Args...>(method, object, |
| 195 | args...); |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | #undef FP_T |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 199 | #define FP_T(x) R (ObjectT::*x)(Args...) |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 200 | |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 201 | template <class ObjectT, class R, typename... Args> |
| 202 | MethodFunctor<ObjectT, FP_T(NONAME), R, Args...> Bind( |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 203 | FP_T(method), |
| 204 | const scoped_refptr<ObjectT>& object, |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 205 | typename detail::identity<Args>::type... args) { |
| 206 | return MethodFunctor<ObjectT, FP_T(NONAME), R, Args...>(method, object.get(), |
| 207 | args...); |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | #undef FP_T |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 211 | #define FP_T(x) R (*x)(Args...) |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 212 | |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 213 | template <class R, typename... Args> |
| 214 | Functor<FP_T(NONAME), R, Args...> Bind( |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 215 | FP_T(function), |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 216 | typename detail::identity<Args>::type... args) { |
| 217 | return Functor<FP_T(NONAME), R, Args...>(function, args...); |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | #undef FP_T |
| 221 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 222 | } // namespace rtc |
| 223 | |
| 224 | #undef NONAME |
| 225 | |
| 226 | #endif // WEBRTC_BASE_BIND_H_ |