xref: /freebsd/contrib/llvm-project/libcxx/include/__functional/bind.h (revision 0ad011ececb978e22a9bff2acf76633b094f1ff6)
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 
10 #ifndef _LIBCPP___FUNCTIONAL_BIND_H
11 #define _LIBCPP___FUNCTIONAL_BIND_H
12 
13 #include <__config>
14 #include <__functional/invoke.h>
15 #include <__functional/weak_result_type.h>
16 #include <__type_traits/decay.h>
17 #include <__type_traits/is_reference_wrapper.h>
18 #include <__type_traits/is_void.h>
19 #include <cstddef>
20 #include <tuple>
21 
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #  pragma GCC system_header
24 #endif
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 template<class _Tp>
29 struct is_bind_expression : _If<
30     _IsSame<_Tp, __remove_cvref_t<_Tp> >::value,
31     false_type,
32     is_bind_expression<__remove_cvref_t<_Tp> >
33 > {};
34 
35 #if _LIBCPP_STD_VER >= 17
36 template <class _Tp>
37 inline constexpr bool is_bind_expression_v = is_bind_expression<_Tp>::value;
38 #endif
39 
40 template<class _Tp>
41 struct is_placeholder : _If<
42     _IsSame<_Tp, __remove_cvref_t<_Tp> >::value,
43     integral_constant<int, 0>,
44     is_placeholder<__remove_cvref_t<_Tp> >
45 > {};
46 
47 #if _LIBCPP_STD_VER >= 17
48 template <class _Tp>
49 inline constexpr int is_placeholder_v = is_placeholder<_Tp>::value;
50 #endif
51 
52 namespace placeholders
53 {
54 
55 template <int _Np> struct __ph {};
56 
57 // C++17 recommends that we implement placeholders as `inline constexpr`, but allows
58 // implementing them as `extern <implementation-defined>`. Libc++ implements them as
59 // `extern const` in all standard modes to avoid an ABI break in C++03: making them
60 // `inline constexpr` requires removing their definition in the shared library to
61 // avoid ODR violations, which is an ABI break.
62 //
63 // In practice, since placeholders are empty, `extern const` is almost impossible
64 // to distinguish from `inline constexpr` from a usage stand point.
65 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<1>   _1;
66 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<2>   _2;
67 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<3>   _3;
68 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<4>   _4;
69 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<5>   _5;
70 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<6>   _6;
71 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<7>   _7;
72 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<8>   _8;
73 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<9>   _9;
74 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<10> _10;
75 
76 } // namespace placeholders
77 
78 template<int _Np>
79 struct is_placeholder<placeholders::__ph<_Np> >
80     : public integral_constant<int, _Np> {};
81 
82 
83 #ifndef _LIBCPP_CXX03_LANG
84 
85 template <class _Tp, class _Uj>
86 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
87 _Tp&
88 __mu(reference_wrapper<_Tp> __t, _Uj&)
89 {
90     return __t.get();
91 }
92 
93 template <class _Ti, class ..._Uj, size_t ..._Indx>
94 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
95 typename __invoke_of<_Ti&, _Uj...>::type
96 __mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
97 {
98     return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
99 }
100 
101 template <class _Ti, class ..._Uj>
102 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
103 typename __enable_if_t
104 <
105     is_bind_expression<_Ti>::value,
106     __invoke_of<_Ti&, _Uj...>
107 >::type
108 __mu(_Ti& __ti, tuple<_Uj...>& __uj)
109 {
110     typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
111     return _VSTD::__mu_expand(__ti, __uj, __indices());
112 }
113 
114 template <bool IsPh, class _Ti, class _Uj>
115 struct __mu_return2 {};
116 
117 template <class _Ti, class _Uj>
118 struct __mu_return2<true, _Ti, _Uj>
119 {
120     typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
121 };
122 
123 template <class _Ti, class _Uj>
124 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
125 typename enable_if
126 <
127     0 < is_placeholder<_Ti>::value,
128     typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
129 >::type
130 __mu(_Ti&, _Uj& __uj)
131 {
132     const size_t __indx = is_placeholder<_Ti>::value - 1;
133     return _VSTD::forward<typename tuple_element<__indx, _Uj>::type>(_VSTD::get<__indx>(__uj));
134 }
135 
136 template <class _Ti, class _Uj>
137 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
138 typename enable_if
139 <
140     !is_bind_expression<_Ti>::value &&
141     is_placeholder<_Ti>::value == 0 &&
142     !__is_reference_wrapper<_Ti>::value,
143     _Ti&
144 >::type
145 __mu(_Ti& __ti, _Uj&)
146 {
147     return __ti;
148 }
149 
150 template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
151           class _TupleUj>
152 struct __mu_return_impl;
153 
154 template <bool _Invokable, class _Ti, class ..._Uj>
155 struct __mu_return_invokable  // false
156 {
157     typedef __nat type;
158 };
159 
160 template <class _Ti, class ..._Uj>
161 struct __mu_return_invokable<true, _Ti, _Uj...>
162 {
163     typedef typename __invoke_of<_Ti&, _Uj...>::type type;
164 };
165 
166 template <class _Ti, class ..._Uj>
167 struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
168     : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
169 {
170 };
171 
172 template <class _Ti, class _TupleUj>
173 struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
174 {
175     typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
176                                    _TupleUj>::type&& type;
177 };
178 
179 template <class _Ti, class _TupleUj>
180 struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
181 {
182     typedef typename _Ti::type& type;
183 };
184 
185 template <class _Ti, class _TupleUj>
186 struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
187 {
188     typedef _Ti& type;
189 };
190 
191 template <class _Ti, class _TupleUj>
192 struct __mu_return
193     : public __mu_return_impl<_Ti,
194                               __is_reference_wrapper<_Ti>::value,
195                               is_bind_expression<_Ti>::value,
196                               0 < is_placeholder<_Ti>::value &&
197                               is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
198                               _TupleUj>
199 {
200 };
201 
202 template <class _Fp, class _BoundArgs, class _TupleUj>
203 struct __is_valid_bind_return
204 {
205     static const bool value = false;
206 };
207 
208 template <class _Fp, class ..._BoundArgs, class _TupleUj>
209 struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
210 {
211     static const bool value = __invokable<_Fp,
212                     typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
213 };
214 
215 template <class _Fp, class ..._BoundArgs, class _TupleUj>
216 struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
217 {
218     static const bool value = __invokable<_Fp,
219                     typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
220 };
221 
222 template <class _Fp, class _BoundArgs, class _TupleUj,
223           bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
224 struct __bind_return;
225 
226 template <class _Fp, class ..._BoundArgs, class _TupleUj>
227 struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
228 {
229     typedef typename __invoke_of
230     <
231         _Fp&,
232         typename __mu_return
233         <
234             _BoundArgs,
235             _TupleUj
236         >::type...
237     >::type type;
238 };
239 
240 template <class _Fp, class ..._BoundArgs, class _TupleUj>
241 struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
242 {
243     typedef typename __invoke_of
244     <
245         _Fp&,
246         typename __mu_return
247         <
248             const _BoundArgs,
249             _TupleUj
250         >::type...
251     >::type type;
252 };
253 
254 template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
255 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
256 typename __bind_return<_Fp, _BoundArgs, _Args>::type
257 __apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
258                 _Args&& __args)
259 {
260     return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
261 }
262 
263 template<class _Fp, class ..._BoundArgs>
264 class __bind : public __weak_result_type<__decay_t<_Fp> >
265 {
266 protected:
267     using _Fd = __decay_t<_Fp>;
268     typedef tuple<__decay_t<_BoundArgs>...> _Td;
269 private:
270     _Fd __f_;
271     _Td __bound_args_;
272 
273     typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
274 public:
275     template <class _Gp, class ..._BA,
276               class = typename enable_if
277                                <
278                                   is_constructible<_Fd, _Gp>::value &&
279                                   !is_same<__libcpp_remove_reference_t<_Gp>,
280                                            __bind>::value
281                                >::type>
282       _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
283       explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
284         : __f_(_VSTD::forward<_Gp>(__f)),
285           __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
286 
287     template <class ..._Args>
288         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
289         typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
290         operator()(_Args&& ...__args)
291         {
292             return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
293                                   tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
294         }
295 
296     template <class ..._Args>
297         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
298         typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
299         operator()(_Args&& ...__args) const
300         {
301             return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
302                                    tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
303         }
304 };
305 
306 template<class _Fp, class ..._BoundArgs>
307 struct is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
308 
309 template<class _Rp, class _Fp, class ..._BoundArgs>
310 class __bind_r
311     : public __bind<_Fp, _BoundArgs...>
312 {
313     typedef __bind<_Fp, _BoundArgs...> base;
314     typedef typename base::_Fd _Fd;
315     typedef typename base::_Td _Td;
316 public:
317     typedef _Rp result_type;
318 
319 
320     template <class _Gp, class ..._BA,
321               class = typename enable_if
322                                <
323                                   is_constructible<_Fd, _Gp>::value &&
324                                   !is_same<__libcpp_remove_reference_t<_Gp>,
325                                            __bind_r>::value
326                                >::type>
327       _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
328       explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
329         : base(_VSTD::forward<_Gp>(__f),
330                _VSTD::forward<_BA>(__bound_args)...) {}
331 
332     template <class ..._Args>
333         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
334         typename enable_if
335         <
336             is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
337                            result_type>::value || is_void<_Rp>::value,
338             result_type
339         >::type
340         operator()(_Args&& ...__args)
341         {
342             typedef __invoke_void_return_wrapper<_Rp> _Invoker;
343             return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
344         }
345 
346     template <class ..._Args>
347         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
348         typename enable_if
349         <
350             is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
351                            result_type>::value || is_void<_Rp>::value,
352             result_type
353         >::type
354         operator()(_Args&& ...__args) const
355         {
356             typedef __invoke_void_return_wrapper<_Rp> _Invoker;
357             return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
358         }
359 };
360 
361 template<class _Rp, class _Fp, class ..._BoundArgs>
362 struct is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
363 
364 template<class _Fp, class ..._BoundArgs>
365 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
366 __bind<_Fp, _BoundArgs...>
367 bind(_Fp&& __f, _BoundArgs&&... __bound_args)
368 {
369     typedef __bind<_Fp, _BoundArgs...> type;
370     return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
371 }
372 
373 template<class _Rp, class _Fp, class ..._BoundArgs>
374 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
375 __bind_r<_Rp, _Fp, _BoundArgs...>
376 bind(_Fp&& __f, _BoundArgs&&... __bound_args)
377 {
378     typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
379     return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
380 }
381 
382 #endif // _LIBCPP_CXX03_LANG
383 
384 _LIBCPP_END_NAMESPACE_STD
385 
386 #endif // _LIBCPP___FUNCTIONAL_BIND_H
387