libstdc++
bits/binders.h
1// Implementation of std::move_only_function, std::copyable_function
2// and std::function_ref -*- C++ -*-
3
4// Copyright The GNU Toolchain Authors.
5//
6// This file is part of the GNU ISO C++ Library. This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 3, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
20
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
25
26/** @file include/bits/binder.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{functional}
29 */
30
31#ifndef _GLIBCXX_BINDERS_H
32#define _GLIBCXX_BINDERS_H 1
33
34#ifdef _GLIBCXX_SYSHDR
35#pragma GCC system_header
36#endif
37
38#if __cplusplus >= 202002L
39
40#include <bits/invoke.h>
41#include <bits/move.h>
42
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 template<size_t, typename _Tp>
48 struct _Indexed_bound_arg
49 {
50 [[no_unique_address]] _Tp _M_val;
51 };
52
53 template<typename... _IndexedArgs>
54 struct _Bound_arg_storage : _IndexedArgs...
55 {
56 template<bool _Back, typename _Fd, typename _Self, typename... _CallArgs>
57 static constexpr
58 decltype(auto)
59 _S_apply(_Fd&& __fd, _Self&& __self, _CallArgs&&... __call_args)
60 {
61 if constexpr (_Back)
63 std::forward<_CallArgs>(__call_args)...,
64 __like_t<_Self, _IndexedArgs>(__self)._M_val...);
65 else
67 __like_t<_Self, _IndexedArgs>(__self)._M_val...,
68 std::forward<_CallArgs>(__call_args)...);
69 }
70 };
71
72 template<typename... _BoundArgs, typename... _Args>
73 constexpr auto
74 __make_bound_args(_Args&&... __args)
75 {
76 if constexpr (sizeof...(_BoundArgs) == 1)
77 // pack has one element, so return copy of arg
78 return (_BoundArgs(std::forward<_Args>(__args)), ...);
79 else
80 {
81 auto __impl = [&]<size_t... _Inds>(index_sequence<_Inds...>)
82 {
83 return _Bound_arg_storage<_Indexed_bound_arg<_Inds, _BoundArgs>...>
84 { {_BoundArgs(std::forward<_Args>(__args))}... };
85 };
86 return __impl(index_sequence_for<_BoundArgs...>());
87 }
88 }
89
90 template<bool _Back, typename _Fd, typename... _BoundArgs>
91 class _Binder
92 {
93 template<typename _Self, typename... _CallArgs>
94 using _Result_t = __conditional_t<
95 _Back,
96 invoke_result<__like_t<_Self, _Fd>,
97 _CallArgs..., __like_t<_Self, _BoundArgs>...>,
98 invoke_result<__like_t<_Self, _Fd>,
99 __like_t<_Self, _BoundArgs>..., _CallArgs...>>::type;
100
101 template<typename _Self, typename... _CallArgs>
102 static consteval bool
103 _S_noexcept_invocable()
104 {
105 if constexpr (_Back)
106 return is_nothrow_invocable_v< __like_t<_Self, _Fd>,
107 _CallArgs..., __like_t<_Self, _BoundArgs>...>;
108 else
109 return is_nothrow_invocable_v<__like_t<_Self, _Fd>,
110 __like_t<_Self, _BoundArgs>..., _CallArgs...>;
111 }
112
113 public:
114 static_assert(is_move_constructible_v<_Fd>);
115 static_assert((is_move_constructible_v<_BoundArgs> && ...));
116
117 // First parameter is to ensure this constructor is never used
118 // instead of the copy/move constructor.
119 template<typename _Fn, typename... _Args>
120 explicit constexpr
121 _Binder(int, _Fn&& __fn, _Args&&... __args)
122 noexcept(__and_<is_nothrow_constructible<_Fd, _Fn>,
123 is_nothrow_constructible<_BoundArgs, _Args>...>::value)
124 : _M_fd(std::forward<_Fn>(__fn)),
125 _M_bound_args(__make_bound_args<_BoundArgs...>(std::forward<_Args>(__args)...))
126 { static_assert(sizeof...(_Args) == sizeof...(_BoundArgs)); }
127
128#if __cpp_explicit_this_parameter
129 template<typename _Self, typename... _CallArgs>
130 constexpr _Result_t<_Self, _CallArgs...>
131 operator()(this _Self&& __self, _CallArgs&&... __call_args)
132 noexcept(_S_noexcept_invocable<_Self, _CallArgs...>())
133 {
134 return _S_call(__like_t<_Self, _Binder>(__self),
135 std::forward<_CallArgs>(__call_args)...);
136 }
137#else
138 template<typename... _CallArgs>
139 requires true
140 constexpr _Result_t<_Binder&, _CallArgs...>
141 operator()(_CallArgs&&... __call_args) &
142 noexcept(_S_noexcept_invocable<_Binder&, _CallArgs...>())
143 {
144 return _S_call(*this, std::forward<_CallArgs>(__call_args)...);
145 }
146
147 template<typename... _CallArgs>
148 requires true
149 constexpr _Result_t<const _Binder&, _CallArgs...>
150 operator()(_CallArgs&&... __call_args) const &
151 noexcept(_S_noexcept_invocable<const _Binder&, _CallArgs...>())
152 {
153 return _S_call(*this, std::forward<_CallArgs>(__call_args)...);
154 }
155
156 template<typename... _CallArgs>
157 requires true
158 constexpr _Result_t<_Binder&&, _CallArgs...>
159 operator()(_CallArgs&&... __call_args) &&
160 noexcept(_S_noexcept_invocable<_Binder&&, _CallArgs...>())
161 {
162 return _S_call(std::move(*this),
163 std::forward<_CallArgs>(__call_args)...);
164 }
165
166 template<typename... _CallArgs>
167 requires true
168 constexpr _Result_t<const _Binder&&, _CallArgs...>
169 operator()(_CallArgs&&... __call_args) const &&
170 noexcept(_S_noexcept_invocable<const _Binder&&, _CallArgs...>())
171 {
172 return _S_call(std::move(*this),
173 std::forward<_CallArgs>(__call_args)...);
174 }
175
176 template<typename... _CallArgs>
177 void operator()(_CallArgs&&...) & = delete;
178
179 template<typename... _CallArgs>
180 void operator()(_CallArgs&&...) const & = delete;
181
182 template<typename... _CallArgs>
183 void operator()(_CallArgs&&...) && = delete;
184
185 template<typename... _CallArgs>
186 void operator()(_CallArgs&&...) const && = delete;
187#endif
188
189 template<typename _Tp, typename... _CallArgs>
190 static constexpr
191 decltype(auto)
192 _S_call(_Tp&& __g, _CallArgs&&... __call_args)
193 {
194 if constexpr (sizeof...(_BoundArgs) > 1)
195 return _BoundArgsStorage::template _S_apply<_Back>(
196 std::forward<_Tp>(__g)._M_fd,
197 std::forward<_Tp>(__g)._M_bound_args,
198 std::forward<_CallArgs>(__call_args)...);
199 else if constexpr (sizeof...(_BoundArgs) == 0)
200 return std::__invoke(std::forward<_Tp>(__g)._M_fd,
201 std::forward<_CallArgs>(__call_args)...);
202 else if constexpr (_Back) // sizeof...(_BoundArgs) == 1
203 return std::__invoke(std::forward<_Tp>(__g)._M_fd,
204 std::forward<_CallArgs>(__call_args)...,
205 std::forward<_Tp>(__g)._M_bound_args);
206 else // !_Back && sizeof...(_BoundArgs) == 1
207 return std::__invoke(std::forward<_Tp>(__g)._M_fd,
208 std::forward<_Tp>(__g)._M_bound_args,
209 std::forward<_CallArgs>(__call_args)...);
210 }
211
212 private:
213 using _BoundArgsStorage
214 // _BoundArgs are required to be move-constructible, so this is valid.
215 = decltype(__make_bound_args<_BoundArgs...>(std::declval<_BoundArgs>()...));
216
217 [[no_unique_address]] _Fd _M_fd;
218 [[no_unique_address]] _BoundArgsStorage _M_bound_args;
219 };
220
221 template<typename _Fn, typename... _Args>
222 using _Bind_front_t = _Binder<false, decay_t<_Fn>, decay_t<_Args>...>;
223
224 // for zero bounds args behavior of bind_front and bind_back is the same,
225 // so reuse _Bind_front_t, i.e. _Binder<false, ...>
226 template<typename _Fn, typename... _Args>
227 using _Bind_back_t
228 = _Binder<(sizeof...(_Args) > 0), decay_t<_Fn>, decay_t<_Args>...>;
229
230_GLIBCXX_END_NAMESPACE_VERSION
231} // namespace std
232
233#endif // __cplusplus >= 202002L
234#endif // _GLIBCXX_BINDERS_H
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2892
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2670
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:92
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
ISO C++ entities toplevel namespace is std.
integer_sequence< size_t, _Idx... > index_sequence
Alias template index_sequence.
Definition utility.h:162
make_index_sequence< sizeof...(_Types)> index_sequence_for
Alias template index_sequence_for.
Definition utility.h:170