robo_caller.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2020 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. #ifndef RTC_BASE_ROBO_CALLER_H_
  11. #define RTC_BASE_ROBO_CALLER_H_
  12. #include <utility>
  13. #include <vector>
  14. #include "api/function_view.h"
  15. #include "rtc_base/system/assume.h"
  16. #include "rtc_base/system/inline.h"
  17. #include "rtc_base/untyped_function.h"
  18. namespace webrtc {
  19. namespace robo_caller_impl {
  20. class RoboCallerReceivers {
  21. public:
  22. RoboCallerReceivers();
  23. RoboCallerReceivers(const RoboCallerReceivers&) = delete;
  24. RoboCallerReceivers& operator=(const RoboCallerReceivers&) = delete;
  25. RoboCallerReceivers(RoboCallerReceivers&&) = delete;
  26. RoboCallerReceivers& operator=(RoboCallerReceivers&&) = delete;
  27. ~RoboCallerReceivers();
  28. template <typename UntypedFunctionArgsT>
  29. RTC_NO_INLINE void AddReceiver(UntypedFunctionArgsT args) {
  30. receivers_.push_back(UntypedFunction::Create(args));
  31. }
  32. void Foreach(rtc::FunctionView<void(UntypedFunction&)> fv);
  33. private:
  34. std::vector<UntypedFunction> receivers_;
  35. };
  36. extern template void RoboCallerReceivers::AddReceiver(
  37. UntypedFunction::TrivialUntypedFunctionArgs<1>);
  38. extern template void RoboCallerReceivers::AddReceiver(
  39. UntypedFunction::TrivialUntypedFunctionArgs<2>);
  40. extern template void RoboCallerReceivers::AddReceiver(
  41. UntypedFunction::TrivialUntypedFunctionArgs<3>);
  42. extern template void RoboCallerReceivers::AddReceiver(
  43. UntypedFunction::TrivialUntypedFunctionArgs<4>);
  44. extern template void RoboCallerReceivers::AddReceiver(
  45. UntypedFunction::NontrivialUntypedFunctionArgs);
  46. extern template void RoboCallerReceivers::AddReceiver(
  47. UntypedFunction::FunctionPointerUntypedFunctionArgs);
  48. } // namespace robo_caller_impl
  49. // A collection of receivers (callable objects) that can be called all at once.
  50. // Optimized for minimal binary size.
  51. //
  52. // Neither copyable nor movable. Could easily be made movable if necessary.
  53. //
  54. // TODO(kwiberg): Add support for removing receivers, if necessary. AddReceiver
  55. // would have to return some sort of ID that the caller could save and then pass
  56. // to RemoveReceiver. Alternatively, the callable objects could return one value
  57. // if they wish to stay in the CSC and another value if they wish to be removed.
  58. // It depends on what's convenient for the callers...
  59. template <typename... ArgT>
  60. class RoboCaller {
  61. public:
  62. RoboCaller() = default;
  63. RoboCaller(const RoboCaller&) = delete;
  64. RoboCaller& operator=(const RoboCaller&) = delete;
  65. RoboCaller(RoboCaller&&) = delete;
  66. RoboCaller& operator=(RoboCaller&&) = delete;
  67. // Adds a new receiver. The receiver (a callable object or a function pointer)
  68. // must be movable, but need not be copyable. Its call signature should be
  69. // `void(ArgT...)`.
  70. template <typename F>
  71. void AddReceiver(F&& f) {
  72. receivers_.AddReceiver(
  73. UntypedFunction::PrepareArgs<void(ArgT...)>(std::forward<F>(f)));
  74. }
  75. // Calls all receivers with the given arguments.
  76. template <typename... ArgU>
  77. void Send(ArgU&&... args) {
  78. receivers_.Foreach([&](UntypedFunction& f) {
  79. f.Call<void(ArgT...)>(std::forward<ArgU>(args)...);
  80. });
  81. }
  82. private:
  83. robo_caller_impl::RoboCallerReceivers receivers_;
  84. };
  85. } // namespace webrtc
  86. #endif // RTC_BASE_ROBO_CALLER_H_