xref: /freebsd/contrib/llvm-project/libcxx/include/__functional/bind_back.h (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
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_BACK_H
11 #define _LIBCPP___FUNCTIONAL_BIND_BACK_H
12 
13 #include <__config>
14 #include <__functional/invoke.h>
15 #include <__functional/perfect_forward.h>
16 #include <__type_traits/decay.h>
17 #include <__utility/forward.h>
18 #include <__utility/integer_sequence.h>
19 #include <tuple>
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #  pragma GCC system_header
23 #endif
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 #if _LIBCPP_STD_VER >= 20
28 
29 template <size_t _NBound, class = make_index_sequence<_NBound>>
30 struct __bind_back_op;
31 
32 template <size_t _NBound, size_t... _Ip>
33 struct __bind_back_op<_NBound, index_sequence<_Ip...>> {
34   template <class _Fn, class _BoundArgs, class... _Args>
35   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&& __f, _BoundArgs&& __bound_args, _Args&&... __args) const
36       noexcept(noexcept(std::invoke(std::forward<_Fn>(__f),
37                                     std::forward<_Args>(__args)...,
38                                     std::get<_Ip>(std::forward<_BoundArgs>(__bound_args))...)))
39           -> decltype(std::invoke(std::forward<_Fn>(__f),
40                                   std::forward<_Args>(__args)...,
41                                   std::get<_Ip>(std::forward<_BoundArgs>(__bound_args))...)) {
42     return std::invoke(std::forward<_Fn>(__f),
43                        std::forward<_Args>(__args)...,
44                        std::get<_Ip>(std::forward<_BoundArgs>(__bound_args))...);
45   }
46 };
47 
48 template <class _Fn, class _BoundArgs>
49 struct __bind_back_t : __perfect_forward<__bind_back_op<tuple_size_v<_BoundArgs>>, _Fn, _BoundArgs> {
50   using __perfect_forward<__bind_back_op<tuple_size_v<_BoundArgs>>, _Fn, _BoundArgs>::__perfect_forward;
51 };
52 
53 template <class _Fn, class... _Args>
54   requires is_constructible_v<decay_t<_Fn>, _Fn> && is_move_constructible_v<decay_t<_Fn>> &&
55            (is_constructible_v<decay_t<_Args>, _Args> && ...) && (is_move_constructible_v<decay_t<_Args>> && ...)
56 _LIBCPP_HIDE_FROM_ABI constexpr auto __bind_back(_Fn&& __f, _Args&&... __args) noexcept(
57     noexcept(__bind_back_t<decay_t<_Fn>, tuple<decay_t<_Args>...>>(
58         std::forward<_Fn>(__f), std::forward_as_tuple(std::forward<_Args>(__args)...))))
59     -> decltype(__bind_back_t<decay_t<_Fn>, tuple<decay_t<_Args>...>>(
60         std::forward<_Fn>(__f), std::forward_as_tuple(std::forward<_Args>(__args)...))) {
61   return __bind_back_t<decay_t<_Fn>, tuple<decay_t<_Args>...>>(
62       std::forward<_Fn>(__f), std::forward_as_tuple(std::forward<_Args>(__args)...));
63 }
64 
65 #endif // _LIBCPP_STD_VER >= 20
66 
67 _LIBCPP_END_NAMESPACE_STD
68 
69 #endif // _LIBCPP___FUNCTIONAL_BIND_BACK_H
70