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.

104 lines
3.5 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. // To generate callback.h from callback.h.pump, execute:
  11. // ../third_party/googletest/src/googletest/scripts/pump.py callback.h.pump
  12. // Callbacks are callable object containers. They can hold a function pointer
  13. // or a function object and behave like a value type. Internally, data is
  14. // reference-counted, making copies and pass-by-value inexpensive.
  15. //
  16. // Callbacks are typed using template arguments. The format is:
  17. // CallbackN<ReturnType, ParamType1, ..., ParamTypeN>
  18. // where N is the number of arguments supplied to the callable object.
  19. // Callbacks are invoked using operator(), just like a function or a function
  20. // object. Default-constructed callbacks are "empty," and executing an empty
  21. // callback does nothing. A callback can be made empty by assigning it from
  22. // a default-constructed callback.
  23. //
  24. // Callbacks are similar in purpose to std::function (which isn't available on
  25. // all platforms we support) and a lightweight alternative to sigslots. Since
  26. // they effectively hide the type of the object they call, they're useful in
  27. // breaking dependencies between objects that need to interact with one another.
  28. // Notably, they can hold the results of Bind(), std::bind*, etc, without needing
  29. // to know the resulting object type of those calls.
  30. //
  31. // Sigslots, on the other hand, provide a fuller feature set, such as multiple
  32. // subscriptions to a signal, optional thread-safety, and lifetime tracking of
  33. // slots. When these features are needed, choose sigslots.
  34. //
  35. // Example:
  36. // int sqr(int x) { return x * x; }
  37. // struct AddK {
  38. // int k;
  39. // int operator()(int x) const { return x + k; }
  40. // } add_k = {5};
  41. //
  42. // Callback1<int, int> my_callback;
  43. // cout << my_callback.empty() << endl; // true
  44. //
  45. // my_callback = Callback1<int, int>(&sqr);
  46. // cout << my_callback.empty() << endl; // false
  47. // cout << my_callback(3) << endl; // 9
  48. //
  49. // my_callback = Callback1<int, int>(add_k);
  50. // cout << my_callback(10) << endl; // 15
  51. //
  52. // my_callback = Callback1<int, int>();
  53. // cout << my_callback.empty() << endl; // true
  54. #ifndef RTC_BASE_CALLBACK_H_
  55. #define RTC_BASE_CALLBACK_H_
  56. #include "rtc_base/ref_count.h"
  57. #include "rtc_base/ref_counted_object.h"
  58. #include "api/scoped_refptr.h"
  59. namespace rtc {
  60. $var n = 5
  61. $range i 0..n
  62. $for i [[
  63. $range j 1..i
  64. template <class R$for j [[,
  65. class P$j]]>
  66. class Callback$i {
  67. public:
  68. // Default copy operations are appropriate for this class.
  69. Callback$i() {}
  70. template <class T> Callback$i(const T& functor)
  71. : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
  72. R operator()($for j , [[P$j p$j]]) {
  73. if (empty())
  74. return R();
  75. return helper_->Run($for j , [[p$j]]);
  76. }
  77. bool empty() const { return !helper_; }
  78. private:
  79. struct Helper : RefCountInterface {
  80. virtual ~Helper() {}
  81. virtual R Run($for j , [[P$j p$j]]) = 0;
  82. };
  83. template <class T> struct HelperImpl : Helper {
  84. explicit HelperImpl(const T& functor) : functor_(functor) {}
  85. virtual R Run($for j , [[P$j p$j]]) {
  86. return functor_($for j , [[p$j]]);
  87. }
  88. T functor_;
  89. };
  90. scoped_refptr<Helper> helper_;
  91. };
  92. ]]
  93. } // namespace rtc
  94. #endif // RTC_BASE_CALLBACK_H_