callback.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // This file was GENERATED by command:
  2. // pump.py callback.h.pump
  3. // DO NOT EDIT BY HAND!!!
  4. /*
  5. * Copyright 2012 The WebRTC Project Authors. All rights reserved.
  6. *
  7. * Use of this source code is governed by a BSD-style license
  8. * that can be found in the LICENSE file in the root of the source
  9. * tree. An additional intellectual property rights grant can be found
  10. * in the file PATENTS. All contributing project authors may
  11. * be found in the AUTHORS file in the root of the source tree.
  12. */
  13. // To generate callback.h from callback.h.pump, execute:
  14. // ../third_party/googletest/src/googletest/scripts/pump.py callback.h.pump
  15. // Callbacks are callable object containers. They can hold a function pointer
  16. // or a function object and behave like a value type. Internally, data is
  17. // reference-counted, making copies and pass-by-value inexpensive.
  18. //
  19. // Callbacks are typed using template arguments. The format is:
  20. // CallbackN<ReturnType, ParamType1, ..., ParamTypeN>
  21. // where N is the number of arguments supplied to the callable object.
  22. // Callbacks are invoked using operator(), just like a function or a function
  23. // object. Default-constructed callbacks are "empty," and executing an empty
  24. // callback does nothing. A callback can be made empty by assigning it from
  25. // a default-constructed callback.
  26. //
  27. // Callbacks are similar in purpose to std::function (which isn't available on
  28. // all platforms we support) and a lightweight alternative to sigslots. Since
  29. // they effectively hide the type of the object they call, they're useful in
  30. // breaking dependencies between objects that need to interact with one another.
  31. // Notably, they can hold the results of Bind(), std::bind*, etc, without
  32. // needing
  33. // to know the resulting object type of those calls.
  34. //
  35. // Sigslots, on the other hand, provide a fuller feature set, such as multiple
  36. // subscriptions to a signal, optional thread-safety, and lifetime tracking of
  37. // slots. When these features are needed, choose sigslots.
  38. //
  39. // Example:
  40. // int sqr(int x) { return x * x; }
  41. // struct AddK {
  42. // int k;
  43. // int operator()(int x) const { return x + k; }
  44. // } add_k = {5};
  45. //
  46. // Callback1<int, int> my_callback;
  47. // cout << my_callback.empty() << endl; // true
  48. //
  49. // my_callback = Callback1<int, int>(&sqr);
  50. // cout << my_callback.empty() << endl; // false
  51. // cout << my_callback(3) << endl; // 9
  52. //
  53. // my_callback = Callback1<int, int>(add_k);
  54. // cout << my_callback(10) << endl; // 15
  55. //
  56. // my_callback = Callback1<int, int>();
  57. // cout << my_callback.empty() << endl; // true
  58. #ifndef RTC_BASE_CALLBACK_H_
  59. #define RTC_BASE_CALLBACK_H_
  60. #include "api/scoped_refptr.h"
  61. #include "rtc_base/ref_count.h"
  62. #include "rtc_base/ref_counted_object.h"
  63. namespace rtc {
  64. template <class R>
  65. class Callback0 {
  66. public:
  67. // Default copy operations are appropriate for this class.
  68. Callback0() {}
  69. template <class T>
  70. Callback0(const T& functor)
  71. : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
  72. R operator()() {
  73. if (empty())
  74. return R();
  75. return helper_->Run();
  76. }
  77. bool empty() const { return !helper_; }
  78. private:
  79. struct Helper : RefCountInterface {
  80. virtual ~Helper() {}
  81. virtual R Run() = 0;
  82. };
  83. template <class T>
  84. struct HelperImpl : Helper {
  85. explicit HelperImpl(const T& functor) : functor_(functor) {}
  86. virtual R Run() { return functor_(); }
  87. T functor_;
  88. };
  89. scoped_refptr<Helper> helper_;
  90. };
  91. template <class R, class P1>
  92. class Callback1 {
  93. public:
  94. // Default copy operations are appropriate for this class.
  95. Callback1() {}
  96. template <class T>
  97. Callback1(const T& functor)
  98. : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
  99. R operator()(P1 p1) {
  100. if (empty())
  101. return R();
  102. return helper_->Run(p1);
  103. }
  104. bool empty() const { return !helper_; }
  105. private:
  106. struct Helper : RefCountInterface {
  107. virtual ~Helper() {}
  108. virtual R Run(P1 p1) = 0;
  109. };
  110. template <class T>
  111. struct HelperImpl : Helper {
  112. explicit HelperImpl(const T& functor) : functor_(functor) {}
  113. virtual R Run(P1 p1) { return functor_(p1); }
  114. T functor_;
  115. };
  116. scoped_refptr<Helper> helper_;
  117. };
  118. template <class R, class P1, class P2>
  119. class Callback2 {
  120. public:
  121. // Default copy operations are appropriate for this class.
  122. Callback2() {}
  123. template <class T>
  124. Callback2(const T& functor)
  125. : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
  126. R operator()(P1 p1, P2 p2) {
  127. if (empty())
  128. return R();
  129. return helper_->Run(p1, p2);
  130. }
  131. bool empty() const { return !helper_; }
  132. private:
  133. struct Helper : RefCountInterface {
  134. virtual ~Helper() {}
  135. virtual R Run(P1 p1, P2 p2) = 0;
  136. };
  137. template <class T>
  138. struct HelperImpl : Helper {
  139. explicit HelperImpl(const T& functor) : functor_(functor) {}
  140. virtual R Run(P1 p1, P2 p2) { return functor_(p1, p2); }
  141. T functor_;
  142. };
  143. scoped_refptr<Helper> helper_;
  144. };
  145. template <class R, class P1, class P2, class P3>
  146. class Callback3 {
  147. public:
  148. // Default copy operations are appropriate for this class.
  149. Callback3() {}
  150. template <class T>
  151. Callback3(const T& functor)
  152. : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
  153. R operator()(P1 p1, P2 p2, P3 p3) {
  154. if (empty())
  155. return R();
  156. return helper_->Run(p1, p2, p3);
  157. }
  158. bool empty() const { return !helper_; }
  159. private:
  160. struct Helper : RefCountInterface {
  161. virtual ~Helper() {}
  162. virtual R Run(P1 p1, P2 p2, P3 p3) = 0;
  163. };
  164. template <class T>
  165. struct HelperImpl : Helper {
  166. explicit HelperImpl(const T& functor) : functor_(functor) {}
  167. virtual R Run(P1 p1, P2 p2, P3 p3) { return functor_(p1, p2, p3); }
  168. T functor_;
  169. };
  170. scoped_refptr<Helper> helper_;
  171. };
  172. template <class R, class P1, class P2, class P3, class P4>
  173. class Callback4 {
  174. public:
  175. // Default copy operations are appropriate for this class.
  176. Callback4() {}
  177. template <class T>
  178. Callback4(const T& functor)
  179. : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
  180. R operator()(P1 p1, P2 p2, P3 p3, P4 p4) {
  181. if (empty())
  182. return R();
  183. return helper_->Run(p1, p2, p3, p4);
  184. }
  185. bool empty() const { return !helper_; }
  186. private:
  187. struct Helper : RefCountInterface {
  188. virtual ~Helper() {}
  189. virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) = 0;
  190. };
  191. template <class T>
  192. struct HelperImpl : Helper {
  193. explicit HelperImpl(const T& functor) : functor_(functor) {}
  194. virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) {
  195. return functor_(p1, p2, p3, p4);
  196. }
  197. T functor_;
  198. };
  199. scoped_refptr<Helper> helper_;
  200. };
  201. template <class R, class P1, class P2, class P3, class P4, class P5>
  202. class Callback5 {
  203. public:
  204. // Default copy operations are appropriate for this class.
  205. Callback5() {}
  206. template <class T>
  207. Callback5(const T& functor)
  208. : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
  209. R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
  210. if (empty())
  211. return R();
  212. return helper_->Run(p1, p2, p3, p4, p5);
  213. }
  214. bool empty() const { return !helper_; }
  215. private:
  216. struct Helper : RefCountInterface {
  217. virtual ~Helper() {}
  218. virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) = 0;
  219. };
  220. template <class T>
  221. struct HelperImpl : Helper {
  222. explicit HelperImpl(const T& functor) : functor_(functor) {}
  223. virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
  224. return functor_(p1, p2, p3, p4, p5);
  225. }
  226. T functor_;
  227. };
  228. scoped_refptr<Helper> helper_;
  229. };
  230. } // namespace rtc
  231. #endif // RTC_BASE_CALLBACK_H_