invoke.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___FUNCTIONAL_INVOKE_H
  10. #define _LIBCPP___FUNCTIONAL_INVOKE_H
  11. #include <__config>
  12. #include <__functional/weak_result_type.h>
  13. #include <__utility/forward.h>
  14. #include <type_traits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. template <class _Ret, bool = is_void<_Ret>::value>
  20. struct __invoke_void_return_wrapper
  21. {
  22. #ifndef _LIBCPP_CXX03_LANG
  23. template <class ..._Args>
  24. static _Ret __call(_Args&&... __args) {
  25. return _VSTD::__invoke(_VSTD::forward<_Args>(__args)...);
  26. }
  27. #else
  28. template <class _Fn>
  29. static _Ret __call(_Fn __f) {
  30. return _VSTD::__invoke(__f);
  31. }
  32. template <class _Fn, class _A0>
  33. static _Ret __call(_Fn __f, _A0& __a0) {
  34. return _VSTD::__invoke(__f, __a0);
  35. }
  36. template <class _Fn, class _A0, class _A1>
  37. static _Ret __call(_Fn __f, _A0& __a0, _A1& __a1) {
  38. return _VSTD::__invoke(__f, __a0, __a1);
  39. }
  40. template <class _Fn, class _A0, class _A1, class _A2>
  41. static _Ret __call(_Fn __f, _A0& __a0, _A1& __a1, _A2& __a2){
  42. return _VSTD::__invoke(__f, __a0, __a1, __a2);
  43. }
  44. #endif
  45. };
  46. template <class _Ret>
  47. struct __invoke_void_return_wrapper<_Ret, true>
  48. {
  49. #ifndef _LIBCPP_CXX03_LANG
  50. template <class ..._Args>
  51. static void __call(_Args&&... __args) {
  52. _VSTD::__invoke(_VSTD::forward<_Args>(__args)...);
  53. }
  54. #else
  55. template <class _Fn>
  56. static void __call(_Fn __f) {
  57. _VSTD::__invoke(__f);
  58. }
  59. template <class _Fn, class _A0>
  60. static void __call(_Fn __f, _A0& __a0) {
  61. _VSTD::__invoke(__f, __a0);
  62. }
  63. template <class _Fn, class _A0, class _A1>
  64. static void __call(_Fn __f, _A0& __a0, _A1& __a1) {
  65. _VSTD::__invoke(__f, __a0, __a1);
  66. }
  67. template <class _Fn, class _A0, class _A1, class _A2>
  68. static void __call(_Fn __f, _A0& __a0, _A1& __a1, _A2& __a2) {
  69. _VSTD::__invoke(__f, __a0, __a1, __a2);
  70. }
  71. #endif
  72. };
  73. #if _LIBCPP_STD_VER > 14
  74. template <class _Fn, class ..._Args>
  75. _LIBCPP_CONSTEXPR_AFTER_CXX17 invoke_result_t<_Fn, _Args...>
  76. invoke(_Fn&& __f, _Args&&... __args)
  77. noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
  78. {
  79. return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
  80. }
  81. #endif // _LIBCPP_STD_VER > 14
  82. _LIBCPP_END_NAMESPACE_STD
  83. #endif // _LIBCPP___FUNCTIONAL_INVOKE_H