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.

164 satır
5.9KB

  1. // Implementation of INVOKE -*- C++ -*-
  2. // Copyright (C) 2016-2020 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. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. /**
  35. * @addtogroup utilities
  36. * @{
  37. */
  38. // Used by __invoke_impl instead of std::forward<_Tp> so that a
  39. // reference_wrapper is converted to an lvalue-reference.
  40. template<typename _Tp, typename _Up = typename __inv_unwrap<_Tp>::type>
  41. constexpr _Up&&
  42. __invfwd(typename remove_reference<_Tp>::type& __t) noexcept
  43. { return static_cast<_Up&&>(__t); }
  44. template<typename _Res, typename _Fn, typename... _Args>
  45. constexpr _Res
  46. __invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
  47. { return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }
  48. template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  49. constexpr _Res
  50. __invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
  51. _Args&&... __args)
  52. { return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
  53. template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  54. constexpr _Res
  55. __invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
  56. _Args&&... __args)
  57. {
  58. return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
  59. }
  60. template<typename _Res, typename _MemPtr, typename _Tp>
  61. constexpr _Res
  62. __invoke_impl(__invoke_memobj_ref, _MemPtr&& __f, _Tp&& __t)
  63. { return __invfwd<_Tp>(__t).*__f; }
  64. template<typename _Res, typename _MemPtr, typename _Tp>
  65. constexpr _Res
  66. __invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
  67. { return (*std::forward<_Tp>(__t)).*__f; }
  68. /// Invoke a callable object.
  69. template<typename _Callable, typename... _Args>
  70. constexpr typename __invoke_result<_Callable, _Args...>::type
  71. __invoke(_Callable&& __fn, _Args&&... __args)
  72. noexcept(__is_nothrow_invocable<_Callable, _Args...>::value)
  73. {
  74. using __result = __invoke_result<_Callable, _Args...>;
  75. using __type = typename __result::type;
  76. using __tag = typename __result::__invoke_type;
  77. return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  78. std::forward<_Args>(__args)...);
  79. }
  80. #if __cplusplus >= 201703L
  81. // INVOKE<R>: Invoke a callable object and convert the result to R.
  82. template<typename _Res, typename _Callable, typename... _Args>
  83. constexpr enable_if_t<is_invocable_r_v<_Res, _Callable, _Args...>, _Res>
  84. __invoke_r(_Callable&& __fn, _Args&&... __args)
  85. noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>)
  86. {
  87. using __result = __invoke_result<_Callable, _Args...>;
  88. using __type = typename __result::type;
  89. using __tag = typename __result::__invoke_type;
  90. if constexpr (is_void_v<_Res>)
  91. std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  92. std::forward<_Args>(__args)...);
  93. else
  94. return std::__invoke_impl<__type>(__tag{},
  95. std::forward<_Callable>(__fn),
  96. std::forward<_Args>(__args)...);
  97. }
  98. #else // C++11
  99. template<typename _Res, typename _Callable, typename... _Args>
  100. using __can_invoke_as_void = __enable_if_t<
  101. __and_<is_void<_Res>, __is_invocable<_Callable, _Args...>>::value,
  102. _Res
  103. >;
  104. template<typename _Res, typename _Callable, typename... _Args>
  105. using __can_invoke_as_nonvoid = __enable_if_t<
  106. __and_<__not_<is_void<_Res>>,
  107. is_convertible<typename __invoke_result<_Callable, _Args...>::type,
  108. _Res>
  109. >::value,
  110. _Res
  111. >;
  112. // INVOKE<R>: Invoke a callable object and convert the result to R.
  113. template<typename _Res, typename _Callable, typename... _Args>
  114. constexpr __can_invoke_as_nonvoid<_Res, _Callable, _Args...>
  115. __invoke_r(_Callable&& __fn, _Args&&... __args)
  116. {
  117. using __result = __invoke_result<_Callable, _Args...>;
  118. using __type = typename __result::type;
  119. using __tag = typename __result::__invoke_type;
  120. return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  121. std::forward<_Args>(__args)...);
  122. }
  123. // INVOKE<R> when R is cv void
  124. template<typename _Res, typename _Callable, typename... _Args>
  125. _GLIBCXX14_CONSTEXPR __can_invoke_as_void<_Res, _Callable, _Args...>
  126. __invoke_r(_Callable&& __fn, _Args&&... __args)
  127. {
  128. using __result = __invoke_result<_Callable, _Args...>;
  129. using __type = typename __result::type;
  130. using __tag = typename __result::__invoke_type;
  131. std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  132. std::forward<_Args>(__args)...);
  133. }
  134. #endif // C++11
  135. _GLIBCXX_END_NAMESPACE_VERSION
  136. } // namespace std
  137. #endif // C++11
  138. #endif // _GLIBCXX_INVOKE_H