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

function_ref.h 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2019 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_
  15. #define ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_
  16. #include <cassert>
  17. #include <functional>
  18. #include <type_traits>
  19. #include "absl/base/internal/invoke.h"
  20. #include "absl/meta/type_traits.h"
  21. namespace absl
  22. {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace functional_internal
  25. {
  26. // Like a void* that can handle function pointers as well. The standard does not
  27. // allow function pointers to round-trip through void*, but void(*)() is fine.
  28. //
  29. // Note: It's important that this class remains trivial and is the same size as
  30. // a pointer, since this allows the compiler to perform tail-call optimizations
  31. // when the underlying function is a callable object with a matching signature.
  32. union VoidPtr
  33. {
  34. const void* obj;
  35. void (*fun)();
  36. };
  37. // Chooses the best type for passing T as an argument.
  38. // Attempt to be close to SystemV AMD64 ABI. Objects with trivial copy ctor are
  39. // passed by value.
  40. template<typename T>
  41. constexpr bool PassByValue()
  42. {
  43. return !std::is_lvalue_reference<T>::value &&
  44. absl::is_trivially_copy_constructible<T>::value &&
  45. absl::is_trivially_copy_assignable<
  46. typename std::remove_cv<T>::type>::value &&
  47. std::is_trivially_destructible<T>::value &&
  48. sizeof(T) <= 2 * sizeof(void*);
  49. }
  50. template<typename T>
  51. struct ForwardT : std::conditional<PassByValue<T>(), T, T&&>
  52. {
  53. };
  54. // An Invoker takes a pointer to the type-erased invokable object, followed by
  55. // the arguments that the invokable object expects.
  56. //
  57. // Note: The order of arguments here is an optimization, since member functions
  58. // have an implicit "this" pointer as their first argument, putting VoidPtr
  59. // first allows the compiler to perform tail-call optimization in many cases.
  60. template<typename R, typename... Args>
  61. using Invoker = R (*)(VoidPtr, typename ForwardT<Args>::type...);
  62. //
  63. // InvokeObject and InvokeFunction provide static "Invoke" functions that can be
  64. // used as Invokers for objects or functions respectively.
  65. //
  66. // static_cast<R> handles the case the return type is void.
  67. template<typename Obj, typename R, typename... Args>
  68. R InvokeObject(VoidPtr ptr, typename ForwardT<Args>::type... args)
  69. {
  70. auto o = static_cast<const Obj*>(ptr.obj);
  71. return static_cast<R>(
  72. absl::base_internal::invoke(*o, std::forward<Args>(args)...)
  73. );
  74. }
  75. template<typename Fun, typename R, typename... Args>
  76. R InvokeFunction(VoidPtr ptr, typename ForwardT<Args>::type... args)
  77. {
  78. auto f = reinterpret_cast<Fun>(ptr.fun);
  79. return static_cast<R>(
  80. absl::base_internal::invoke(f, std::forward<Args>(args)...)
  81. );
  82. }
  83. template<typename Sig>
  84. void AssertNonNull(const std::function<Sig>& f)
  85. {
  86. assert(f != nullptr);
  87. (void)f;
  88. }
  89. template<typename F>
  90. void AssertNonNull(const F&)
  91. {
  92. }
  93. template<typename F, typename C>
  94. void AssertNonNull(F C::*f)
  95. {
  96. assert(f != nullptr);
  97. (void)f;
  98. }
  99. template<bool C>
  100. using EnableIf = typename ::std::enable_if<C, int>::type;
  101. } // namespace functional_internal
  102. ABSL_NAMESPACE_END
  103. } // namespace absl
  104. #endif // ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_