You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
9.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. // Bind() is an overloaded function that converts method calls into function
  11. // objects (aka functors). The method object is captured as a scoped_refptr<> if
  12. // possible, and as a raw pointer otherwise. Any arguments to the method are
  13. // captured by value. The return value of Bind is a stateful, nullary function
  14. // object. Care should be taken about the lifetime of objects captured by
  15. // Bind(); the returned functor knows nothing about the lifetime of a non
  16. // ref-counted method object or any arguments passed by pointer, and calling the
  17. // functor with a destroyed object will surely do bad things.
  18. //
  19. // To prevent the method object from being captured as a scoped_refptr<>, you
  20. // can use Unretained. But this should only be done when absolutely necessary,
  21. // and when the caller knows the extra reference isn't needed.
  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. #ifndef RTC_BASE_BIND_H_
  60. #define RTC_BASE_BIND_H_
  61. #include <tuple>
  62. #include <type_traits>
  63. #include "api/scoped_refptr.h"
  64. #include "rtc_base/template_util.h"
  65. #define NONAME
  66. namespace rtc {
  67. namespace detail {
  68. // This is needed because the template parameters in Bind can't be resolved
  69. // if they're used both as parameters of the function pointer type and as
  70. // parameters to Bind itself: the function pointer parameters are exact
  71. // matches to the function prototype, but the parameters to bind have
  72. // references stripped. This trick allows the compiler to dictate the Bind
  73. // parameter types rather than deduce them.
  74. template <class T>
  75. struct identity {
  76. 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. // Define types such that sizeof(Yes) != sizeof(No).
  86. struct Yes {
  87. char dummy[1];
  88. };
  89. struct No {
  90. char dummy[2];
  91. };
  92. // Define two overloaded template functions with return types of different
  93. // size. This way, we can use sizeof() on the return type to determine which
  94. // function the compiler would have chosen. One function will be preferred
  95. // over the other if it is possible to create it without compiler errors,
  96. // otherwise the compiler will simply remove it, and default to the less
  97. // preferred function.
  98. template <typename R>
  99. static Yes test(R* r, decltype(r->AddRef(), r->Release(), 42));
  100. template <typename C>
  101. static No test(...);
  102. public:
  103. // Trick the compiler to tell if it's possible to call AddRef() and Release().
  104. static const bool value = sizeof(test<T>((T*)nullptr, 42)) == sizeof(Yes);
  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. template <typename IfTrueT, typename IfFalseT>
  111. struct TernaryTypeOperator<true, IfTrueT, IfFalseT> {
  112. typedef IfTrueT type;
  113. };
  114. template <typename IfTrueT, typename IfFalseT>
  115. struct TernaryTypeOperator<false, IfTrueT, IfFalseT> {
  116. typedef IfFalseT type;
  117. };
  118. // PointerType<T>::type will be scoped_refptr<T> for ref counted types, and T*
  119. // otherwise.
  120. template <class T>
  121. struct PointerType {
  122. typedef typename TernaryTypeOperator<IsRefCounted<T>::value,
  123. scoped_refptr<T>,
  124. T*>::type type;
  125. };
  126. template <typename T>
  127. class UnretainedWrapper {
  128. public:
  129. explicit UnretainedWrapper(T* o) : ptr_(o) {}
  130. T* get() const { return ptr_; }
  131. private:
  132. T* ptr_;
  133. };
  134. } // namespace detail
  135. template <typename T>
  136. static inline detail::UnretainedWrapper<T> Unretained(T* o) {
  137. return detail::UnretainedWrapper<T>(o);
  138. }
  139. template <class ObjectT, class MethodT, class R, typename... Args>
  140. class MethodFunctor {
  141. public:
  142. MethodFunctor(MethodT method, ObjectT* object, Args... args)
  143. : method_(method), object_(object), args_(args...) {}
  144. R operator()() const {
  145. return CallMethod(typename sequence_generator<sizeof...(Args)>::type());
  146. }
  147. private:
  148. // Use sequence_generator (see template_util.h) to expand a MethodFunctor
  149. // with 2 arguments to (std::get<0>(args_), std::get<1>(args_)), for
  150. // instance.
  151. template <int... S>
  152. R CallMethod(sequence<S...>) const {
  153. return (object_->*method_)(std::get<S>(args_)...);
  154. }
  155. MethodT method_;
  156. typename detail::PointerType<ObjectT>::type object_;
  157. typename std::tuple<typename std::remove_reference<Args>::type...> args_;
  158. };
  159. template <class ObjectT, class MethodT, class R, typename... Args>
  160. class UnretainedMethodFunctor {
  161. public:
  162. UnretainedMethodFunctor(MethodT method,
  163. detail::UnretainedWrapper<ObjectT> object,
  164. Args... args)
  165. : method_(method), object_(object.get()), args_(args...) {}
  166. R operator()() const {
  167. return CallMethod(typename sequence_generator<sizeof...(Args)>::type());
  168. }
  169. private:
  170. // Use sequence_generator (see template_util.h) to expand an
  171. // UnretainedMethodFunctor with 2 arguments to (std::get<0>(args_),
  172. // std::get<1>(args_)), for instance.
  173. template <int... S>
  174. R CallMethod(sequence<S...>) const {
  175. return (object_->*method_)(std::get<S>(args_)...);
  176. }
  177. MethodT method_;
  178. ObjectT* object_;
  179. typename std::tuple<typename std::remove_reference<Args>::type...> args_;
  180. };
  181. template <class FunctorT, class R, typename... Args>
  182. class Functor {
  183. public:
  184. Functor(const FunctorT& functor, Args... args)
  185. : functor_(functor), args_(args...) {}
  186. R operator()() const {
  187. return CallFunction(typename sequence_generator<sizeof...(Args)>::type());
  188. }
  189. private:
  190. // Use sequence_generator (see template_util.h) to expand a Functor
  191. // with 2 arguments to (std::get<0>(args_), std::get<1>(args_)), for
  192. // instance.
  193. template <int... S>
  194. R CallFunction(sequence<S...>) const {
  195. return functor_(std::get<S>(args_)...);
  196. }
  197. FunctorT functor_;
  198. typename std::tuple<typename std::remove_reference<Args>::type...> args_;
  199. };
  200. #define FP_T(x) R (ObjectT::*x)(Args...)
  201. template <class ObjectT, class R, typename... Args>
  202. MethodFunctor<ObjectT, FP_T(NONAME), R, Args...> Bind(
  203. FP_T(method),
  204. ObjectT* object,
  205. typename detail::identity<Args>::type... args) {
  206. return MethodFunctor<ObjectT, FP_T(NONAME), R, Args...>(method, object,
  207. args...);
  208. }
  209. template <class ObjectT, class R, typename... Args>
  210. MethodFunctor<ObjectT, FP_T(NONAME), R, Args...> Bind(
  211. FP_T(method),
  212. const scoped_refptr<ObjectT>& object,
  213. typename detail::identity<Args>::type... args) {
  214. return MethodFunctor<ObjectT, FP_T(NONAME), R, Args...>(method, object.get(),
  215. args...);
  216. }
  217. template <class ObjectT, class R, typename... Args>
  218. UnretainedMethodFunctor<ObjectT, FP_T(NONAME), R, Args...> Bind(
  219. FP_T(method),
  220. detail::UnretainedWrapper<ObjectT> object,
  221. typename detail::identity<Args>::type... args) {
  222. return UnretainedMethodFunctor<ObjectT, FP_T(NONAME), R, Args...>(
  223. method, object, args...);
  224. }
  225. #undef FP_T
  226. #define FP_T(x) R (ObjectT::*x)(Args...) const
  227. template <class ObjectT, class R, typename... Args>
  228. MethodFunctor<const ObjectT, FP_T(NONAME), R, Args...> Bind(
  229. FP_T(method),
  230. const ObjectT* object,
  231. typename detail::identity<Args>::type... args) {
  232. return MethodFunctor<const ObjectT, FP_T(NONAME), R, Args...>(method, object,
  233. args...);
  234. }
  235. template <class ObjectT, class R, typename... Args>
  236. UnretainedMethodFunctor<const ObjectT, FP_T(NONAME), R, Args...> Bind(
  237. FP_T(method),
  238. detail::UnretainedWrapper<const ObjectT> object,
  239. typename detail::identity<Args>::type... args) {
  240. return UnretainedMethodFunctor<const ObjectT, FP_T(NONAME), R, Args...>(
  241. method, object, args...);
  242. }
  243. #undef FP_T
  244. #define FP_T(x) R (*x)(Args...)
  245. template <class R, typename... Args>
  246. Functor<FP_T(NONAME), R, Args...> Bind(
  247. FP_T(function),
  248. typename detail::identity<Args>::type... args) {
  249. return Functor<FP_T(NONAME), R, Args...>(function, args...);
  250. }
  251. #undef FP_T
  252. } // namespace rtc
  253. #undef NONAME
  254. #endif // RTC_BASE_BIND_H_