invoke.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Implementation of INVOKE -*- C++ -*-
  2. // Copyright (C) 2016-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/bits/invoke.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{functional}
  23. */
  24. #ifndef _GLIBCXX_INVOKE_H
  25. #define _GLIBCXX_INVOKE_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <type_traits>
  31. #include <bits/move.h> // forward
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @addtogroup utilities
  37. * @{
  38. */
  39. // Used by __invoke_impl instead of std::forward<_Tp> so that a
  40. // reference_wrapper is converted to an lvalue-reference.
  41. template<typename _Tp, typename _Up = typename __inv_unwrap<_Tp>::type>
  42. constexpr _Up&&
  43. __invfwd(typename remove_reference<_Tp>::type& __t) noexcept
  44. { return static_cast<_Up&&>(__t); }
  45. template<typename _Res, typename _Fn, typename... _Args>
  46. constexpr _Res
  47. __invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
  48. { return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }
  49. template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  50. constexpr _Res
  51. __invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
  52. _Args&&... __args)
  53. { return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
  54. template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  55. constexpr _Res
  56. __invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
  57. _Args&&... __args)
  58. {
  59. return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
  60. }
  61. template<typename _Res, typename _MemPtr, typename _Tp>
  62. constexpr _Res
  63. __invoke_impl(__invoke_memobj_ref, _MemPtr&& __f, _Tp&& __t)
  64. { return __invfwd<_Tp>(__t).*__f; }
  65. template<typename _Res, typename _MemPtr, typename _Tp>
  66. constexpr _Res
  67. __invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
  68. { return (*std::forward<_Tp>(__t)).*__f; }
  69. /// Invoke a callable object.
  70. template<typename _Callable, typename... _Args>
  71. constexpr typename __invoke_result<_Callable, _Args...>::type
  72. __invoke(_Callable&& __fn, _Args&&... __args)
  73. noexcept(__is_nothrow_invocable<_Callable, _Args...>::value)
  74. {
  75. using __result = __invoke_result<_Callable, _Args...>;
  76. using __type = typename __result::type;
  77. using __tag = typename __result::__invoke_type;
  78. return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  79. std::forward<_Args>(__args)...);
  80. }
  81. #if __cplusplus >= 201703L
  82. // INVOKE<R>: Invoke a callable object and convert the result to R.
  83. template<typename _Res, typename _Callable, typename... _Args>
  84. constexpr enable_if_t<is_invocable_r_v<_Res, _Callable, _Args...>, _Res>
  85. __invoke_r(_Callable&& __fn, _Args&&... __args)
  86. noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>)
  87. {
  88. using __result = __invoke_result<_Callable, _Args...>;
  89. using __type = typename __result::type;
  90. using __tag = typename __result::__invoke_type;
  91. if constexpr (is_void_v<_Res>)
  92. std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  93. std::forward<_Args>(__args)...);
  94. else
  95. return std::__invoke_impl<__type>(__tag{},
  96. std::forward<_Callable>(__fn),
  97. std::forward<_Args>(__args)...);
  98. }
  99. #else // C++11
  100. template<typename _Res, typename _Callable, typename... _Args>
  101. using __can_invoke_as_void = __enable_if_t<
  102. __and_<is_void<_Res>, __is_invocable<_Callable, _Args...>>::value,
  103. _Res
  104. >;
  105. template<typename _Res, typename _Callable, typename... _Args>
  106. using __can_invoke_as_nonvoid = __enable_if_t<
  107. __and_<__not_<is_void<_Res>>,
  108. is_convertible<typename __invoke_result<_Callable, _Args...>::type,
  109. _Res>
  110. >::value,
  111. _Res
  112. >;
  113. // INVOKE<R>: Invoke a callable object and convert the result to R.
  114. template<typename _Res, typename _Callable, typename... _Args>
  115. constexpr __can_invoke_as_nonvoid<_Res, _Callable, _Args...>
  116. __invoke_r(_Callable&& __fn, _Args&&... __args)
  117. {
  118. using __result = __invoke_result<_Callable, _Args...>;
  119. using __type = typename __result::type;
  120. using __tag = typename __result::__invoke_type;
  121. return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  122. std::forward<_Args>(__args)...);
  123. }
  124. // INVOKE<R> when R is cv void
  125. template<typename _Res, typename _Callable, typename... _Args>
  126. _GLIBCXX14_CONSTEXPR __can_invoke_as_void<_Res, _Callable, _Args...>
  127. __invoke_r(_Callable&& __fn, _Args&&... __args)
  128. {
  129. using __result = __invoke_result<_Callable, _Args...>;
  130. using __type = typename __result::type;
  131. using __tag = typename __result::__invoke_type;
  132. std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  133. std::forward<_Args>(__args)...);
  134. }
  135. #endif // C++11
  136. _GLIBCXX_END_NAMESPACE_VERSION
  137. } // namespace std
  138. #endif // C++11
  139. #endif // _GLIBCXX_INVOKE_H