function_view.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2016 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 API_FUNCTION_VIEW_H_
  11. #define API_FUNCTION_VIEW_H_
  12. #include <type_traits>
  13. #include <utility>
  14. #include "rtc_base/checks.h"
  15. // Just like std::function, FunctionView will wrap any callable and hide its
  16. // actual type, exposing only its signature. But unlike std::function,
  17. // FunctionView doesn't own its callable---it just points to it. Thus, it's a
  18. // good choice mainly as a function argument when the callable argument will
  19. // not be called again once the function has returned.
  20. //
  21. // Its constructors are implicit, so that callers won't have to convert lambdas
  22. // and other callables to FunctionView<Blah(Blah, Blah)> explicitly. This is
  23. // safe because FunctionView is only a reference to the real callable.
  24. //
  25. // Example use:
  26. //
  27. // void SomeFunction(rtc::FunctionView<int(int)> index_transform);
  28. // ...
  29. // SomeFunction([](int i) { return 2 * i + 1; });
  30. //
  31. // Note: FunctionView is tiny (essentially just two pointers) and trivially
  32. // copyable, so it's probably cheaper to pass it by value than by const
  33. // reference.
  34. namespace rtc {
  35. template <typename T>
  36. class FunctionView; // Undefined.
  37. template <typename RetT, typename... ArgT>
  38. class FunctionView<RetT(ArgT...)> final {
  39. public:
  40. // Constructor for lambdas and other callables; it accepts every type of
  41. // argument except those noted in its enable_if call.
  42. template <
  43. typename F,
  44. typename std::enable_if<
  45. // Not for function pointers; we have another constructor for that
  46. // below.
  47. !std::is_function<typename std::remove_pointer<
  48. typename std::remove_reference<F>::type>::type>::value &&
  49. // Not for nullptr; we have another constructor for that below.
  50. !std::is_same<std::nullptr_t,
  51. typename std::remove_cv<F>::type>::value &&
  52. // Not for FunctionView objects; we have another constructor for that
  53. // (the implicitly declared copy constructor).
  54. !std::is_same<FunctionView,
  55. typename std::remove_cv<typename std::remove_reference<
  56. F>::type>::type>::value>::type* = nullptr>
  57. FunctionView(F&& f)
  58. : call_(CallVoidPtr<typename std::remove_reference<F>::type>) {
  59. f_.void_ptr = &f;
  60. }
  61. // Constructor that accepts function pointers. If the argument is null, the
  62. // result is an empty FunctionView.
  63. template <
  64. typename F,
  65. typename std::enable_if<std::is_function<typename std::remove_pointer<
  66. typename std::remove_reference<F>::type>::type>::value>::type* =
  67. nullptr>
  68. FunctionView(F&& f)
  69. : call_(f ? CallFunPtr<typename std::remove_pointer<F>::type> : nullptr) {
  70. f_.fun_ptr = reinterpret_cast<void (*)()>(f);
  71. }
  72. // Constructor that accepts nullptr. It creates an empty FunctionView.
  73. template <typename F,
  74. typename std::enable_if<std::is_same<
  75. std::nullptr_t,
  76. typename std::remove_cv<F>::type>::value>::type* = nullptr>
  77. FunctionView(F&& f) : call_(nullptr) {}
  78. // Default constructor. Creates an empty FunctionView.
  79. FunctionView() : call_(nullptr) {}
  80. RetT operator()(ArgT... args) const {
  81. RTC_DCHECK(call_);
  82. return call_(f_, std::forward<ArgT>(args)...);
  83. }
  84. // Returns true if we have a function, false if we don't (i.e., we're null).
  85. explicit operator bool() const { return !!call_; }
  86. private:
  87. union VoidUnion {
  88. void* void_ptr;
  89. void (*fun_ptr)();
  90. };
  91. template <typename F>
  92. static RetT CallVoidPtr(VoidUnion vu, ArgT... args) {
  93. return (*static_cast<F*>(vu.void_ptr))(std::forward<ArgT>(args)...);
  94. }
  95. template <typename F>
  96. static RetT CallFunPtr(VoidUnion vu, ArgT... args) {
  97. return (reinterpret_cast<typename std::add_pointer<F>::type>(vu.fun_ptr))(
  98. std::forward<ArgT>(args)...);
  99. }
  100. // A pointer to the callable thing, with type information erased. It's a
  101. // union because we have to use separate types depending on if the callable
  102. // thing is a function pointer or something else.
  103. VoidUnion f_;
  104. // Pointer to a dispatch function that knows the type of the callable thing
  105. // that's stored in f_, and how to call it. A FunctionView object is empty
  106. // (null) iff call_ is null.
  107. RetT (*call_)(VoidUnion, ArgT...);
  108. };
  109. } // namespace rtc
  110. #endif // API_FUNCTION_VIEW_H_