/* * Copyright 2012 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ // Bind() is an overloaded function that converts method calls into function // objects (aka functors). The method object is captured as a scoped_refptr<> if // possible, and as a raw pointer otherwise. Any arguments to the method are // captured by value. The return value of Bind is a stateful, nullary function // object. Care should be taken about the lifetime of objects captured by // Bind(); the returned functor knows nothing about the lifetime of a non // ref-counted method object or any arguments passed by pointer, and calling the // functor with a destroyed object will surely do bad things. // // To prevent the method object from being captured as a scoped_refptr<>, you // can use Unretained. But this should only be done when absolutely necessary, // and when the caller knows the extra reference isn't needed. // // Example usage: // struct Foo { // int Test1() { return 42; } // int Test2() const { return 52; } // int Test3(int x) { return x*x; } // float Test4(int x, float y) { return x + y; } // }; // // int main() { // Foo foo; // cout << rtc::Bind(&Foo::Test1, &foo)() << endl; // cout << rtc::Bind(&Foo::Test2, &foo)() << endl; // cout << rtc::Bind(&Foo::Test3, &foo, 3)() << endl; // cout << rtc::Bind(&Foo::Test4, &foo, 7, 8.5f)() << endl; // } // // Example usage of ref counted objects: // struct Bar { // int AddRef(); // int Release(); // // void Test() {} // void BindThis() { // // The functor passed to AsyncInvoke() will keep this object alive. // invoker.AsyncInvoke(RTC_FROM_HERE,rtc::Bind(&Bar::Test, this)); // } // }; // // int main() { // rtc::scoped_refptr bar = new rtc::RefCountedObject(); // auto functor = rtc::Bind(&Bar::Test, bar); // bar = nullptr; // // The functor stores an internal scoped_refptr, so this is safe. // functor(); // } // #ifndef RTC_BASE_BIND_H_ #define RTC_BASE_BIND_H_ #include #include #include "api/scoped_refptr.h" #define NONAME namespace rtc { namespace detail { // This is needed because the template parameters in Bind can't be resolved // if they're used both as parameters of the function pointer type and as // parameters to Bind itself: the function pointer parameters are exact // matches to the function prototype, but the parameters to bind have // references stripped. This trick allows the compiler to dictate the Bind // parameter types rather than deduce them. template struct identity { typedef T type; }; // IsRefCounted::value will be true for types that can be used in // rtc::scoped_refptr, i.e. types that implements nullary functions AddRef() // and Release(), regardless of their return types. AddRef() and Release() can // be defined in T or any superclass of T. template class IsRefCounted { // This is a complex implementation detail done with SFINAE. // Define types such that sizeof(Yes) != sizeof(No). struct Yes { char dummy[1]; }; struct No { char dummy[2]; }; // Define two overloaded template functions with return types of different // size. This way, we can use sizeof() on the return type to determine which // function the compiler would have chosen. One function will be preferred // over the other if it is possible to create it without compiler errors, // otherwise the compiler will simply remove it, and default to the less // preferred function. template static Yes test(R* r, decltype(r->AddRef(), r->Release(), 42)); template static No test(...); public: // Trick the compiler to tell if it's possible to call AddRef() and Release(). static const bool value = sizeof(test((T*)nullptr, 42)) == sizeof(Yes); }; // TernaryTypeOperator is a helper class to select a type based on a static bool // value. template struct TernaryTypeOperator {}; template struct TernaryTypeOperator { typedef IfTrueT type; }; template struct TernaryTypeOperator { typedef IfFalseT type; }; // PointerType::type will be scoped_refptr for ref counted types, and T* // otherwise. template struct PointerType { typedef typename TernaryTypeOperator::value, scoped_refptr, T*>::type type; }; template class UnretainedWrapper { public: explicit UnretainedWrapper(T* o) : ptr_(o) {} T* get() const { return ptr_; } private: T* ptr_; }; } // namespace detail template static inline detail::UnretainedWrapper Unretained(T* o) { return detail::UnretainedWrapper(o); } template class MethodFunctor { public: MethodFunctor(MethodT method, ObjectT* object, Args... args) : method_(method), object_(object), args_(args...) {} R operator()() const { return CallMethod(std::index_sequence_for()); } private: template R CallMethod(std::index_sequence) const { return (object_->*method_)(std::get(args_)...); } MethodT method_; typename detail::PointerType::type object_; typename std::tuple::type...> args_; }; template class UnretainedMethodFunctor { public: UnretainedMethodFunctor(MethodT method, detail::UnretainedWrapper object, Args... args) : method_(method), object_(object.get()), args_(args...) {} R operator()() const { return CallMethod(std::index_sequence_for()); } private: template R CallMethod(std::index_sequence) const { return (object_->*method_)(std::get(args_)...); } MethodT method_; ObjectT* object_; typename std::tuple::type...> args_; }; template class Functor { public: Functor(const FunctorT& functor, Args... args) : functor_(functor), args_(args...) {} R operator()() const { return CallFunction(std::index_sequence_for()); } private: template R CallFunction(std::index_sequence) const { return functor_(std::get(args_)...); } FunctorT functor_; typename std::tuple::type...> args_; }; #define FP_T(x) R (ObjectT::*x)(Args...) template MethodFunctor Bind( FP_T(method), ObjectT* object, typename detail::identity::type... args) { return MethodFunctor(method, object, args...); } template MethodFunctor Bind( FP_T(method), const scoped_refptr& object, typename detail::identity::type... args) { return MethodFunctor(method, object.get(), args...); } template UnretainedMethodFunctor Bind( FP_T(method), detail::UnretainedWrapper object, typename detail::identity::type... args) { return UnretainedMethodFunctor( method, object, args...); } #undef FP_T #define FP_T(x) R (ObjectT::*x)(Args...) const template MethodFunctor Bind( FP_T(method), const ObjectT* object, typename detail::identity::type... args) { return MethodFunctor(method, object, args...); } template UnretainedMethodFunctor Bind( FP_T(method), detail::UnretainedWrapper object, typename detail::identity::type... args) { return UnretainedMethodFunctor( method, object, args...); } #undef FP_T #define FP_T(x) R (*x)(Args...) template Functor Bind( FP_T(function), typename detail::identity::type... args) { return Functor(function, args...); } #undef FP_T } // namespace rtc #undef NONAME #endif // RTC_BASE_BIND_H_