invoke.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Implementation of INVOKE -*- C++ -*-
  2. // Copyright (C) 2016-2018 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. _GLIBCXX_END_NAMESPACE_VERSION
  81. } // namespace std
  82. #endif // C++11
  83. #endif // _GLIBCXX_INVOKE_H