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