Path: blob/main/contrib/llvm-project/libcxx/include/__functional/bind.h
35260 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___FUNCTIONAL_BIND_H10#define _LIBCPP___FUNCTIONAL_BIND_H1112#include <__config>13#include <__functional/invoke.h>14#include <__functional/weak_result_type.h>15#include <__fwd/functional.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>2122#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)23# pragma GCC system_header24#endif2526_LIBCPP_BEGIN_NAMESPACE_STD2728template <class _Tp>29struct is_bind_expression30: _If< _IsSame<_Tp, __remove_cvref_t<_Tp> >::value, false_type, is_bind_expression<__remove_cvref_t<_Tp> > > {};3132#if _LIBCPP_STD_VER >= 1733template <class _Tp>34inline constexpr bool is_bind_expression_v = is_bind_expression<_Tp>::value;35#endif3637template <class _Tp>38struct is_placeholder39: _If< _IsSame<_Tp, __remove_cvref_t<_Tp> >::value,40integral_constant<int, 0>,41is_placeholder<__remove_cvref_t<_Tp> > > {};4243#if _LIBCPP_STD_VER >= 1744template <class _Tp>45inline constexpr int is_placeholder_v = is_placeholder<_Tp>::value;46#endif4748namespace placeholders {4950template <int _Np>51struct __ph {};5253// C++17 recommends that we implement placeholders as `inline constexpr`, but allows54// implementing them as `extern <implementation-defined>`. Libc++ implements them as55// `extern const` in all standard modes to avoid an ABI break in C++03: making them56// `inline constexpr` requires removing their definition in the shared library to57// avoid ODR violations, which is an ABI break.58//59// In practice, since placeholders are empty, `extern const` is almost impossible60// to distinguish from `inline constexpr` from a usage stand point.61_LIBCPP_EXPORTED_FROM_ABI extern const __ph<1> _1;62_LIBCPP_EXPORTED_FROM_ABI extern const __ph<2> _2;63_LIBCPP_EXPORTED_FROM_ABI extern const __ph<3> _3;64_LIBCPP_EXPORTED_FROM_ABI extern const __ph<4> _4;65_LIBCPP_EXPORTED_FROM_ABI extern const __ph<5> _5;66_LIBCPP_EXPORTED_FROM_ABI extern const __ph<6> _6;67_LIBCPP_EXPORTED_FROM_ABI extern const __ph<7> _7;68_LIBCPP_EXPORTED_FROM_ABI extern const __ph<8> _8;69_LIBCPP_EXPORTED_FROM_ABI extern const __ph<9> _9;70_LIBCPP_EXPORTED_FROM_ABI extern const __ph<10> _10;7172} // namespace placeholders7374template <int _Np>75struct is_placeholder<placeholders::__ph<_Np> > : public integral_constant<int, _Np> {};7677#ifndef _LIBCPP_CXX03_LANG7879template <class _Tp, class _Uj>80inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __mu(reference_wrapper<_Tp> __t, _Uj&) {81return __t.get();82}8384template <class _Ti, class... _Uj, size_t... _Indx>85inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __invoke_of<_Ti&, _Uj...>::type86__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>) {87return __ti(std::forward<_Uj>(std::get<_Indx>(__uj))...);88}8990template <class _Ti, class... _Uj, __enable_if_t<is_bind_expression<_Ti>::value, int> = 0>91inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __invoke_of<_Ti&, _Uj...>::type92__mu(_Ti& __ti, tuple<_Uj...>& __uj) {93typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;94return std::__mu_expand(__ti, __uj, __indices());95}9697template <bool _IsPh, class _Ti, class _Uj>98struct __mu_return2 {};99100template <class _Ti, class _Uj>101struct __mu_return2<true, _Ti, _Uj> {102typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;103};104105template <class _Ti, class _Uj, __enable_if_t<0 < is_placeholder<_Ti>::value, int> = 0>106inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20107typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type108__mu(_Ti&, _Uj& __uj) {109const size_t __indx = is_placeholder<_Ti>::value - 1;110return std::forward<typename tuple_element<__indx, _Uj>::type>(std::get<__indx>(__uj));111}112113template <class _Ti,114class _Uj,115__enable_if_t<!is_bind_expression<_Ti>::value && is_placeholder<_Ti>::value == 0 &&116!__is_reference_wrapper<_Ti>::value,117int> = 0>118inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Ti& __mu(_Ti& __ti, _Uj&) {119return __ti;120}121122template <class _Ti, bool _IsReferenceWrapper, bool _IsBindEx, bool _IsPh, class _TupleUj>123struct __mu_return_impl;124125template <bool _Invokable, class _Ti, class... _Uj>126struct __mu_return_invokable // false127{128typedef __nat type;129};130131template <class _Ti, class... _Uj>132struct __mu_return_invokable<true, _Ti, _Uj...> {133typedef typename __invoke_of<_Ti&, _Uj...>::type type;134};135136template <class _Ti, class... _Uj>137struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >138: public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...> {};139140template <class _Ti, class _TupleUj>141struct __mu_return_impl<_Ti, false, false, true, _TupleUj> {142typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _TupleUj>::type&& type;143};144145template <class _Ti, class _TupleUj>146struct __mu_return_impl<_Ti, true, false, false, _TupleUj> {147typedef typename _Ti::type& type;148};149150template <class _Ti, class _TupleUj>151struct __mu_return_impl<_Ti, false, false, false, _TupleUj> {152typedef _Ti& type;153};154155template <class _Ti, class _TupleUj>156struct __mu_return157: public __mu_return_impl<158_Ti,159__is_reference_wrapper<_Ti>::value,160is_bind_expression<_Ti>::value,1610 < is_placeholder<_Ti>::value && is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,162_TupleUj> {};163164template <class _Fp, class _BoundArgs, class _TupleUj>165struct __is_valid_bind_return {166static const bool value = false;167};168169template <class _Fp, class... _BoundArgs, class _TupleUj>170struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj> {171static const bool value = __invokable<_Fp, typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;172};173174template <class _Fp, class... _BoundArgs, class _TupleUj>175struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj> {176static const bool value = __invokable<_Fp, typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;177};178179template <class _Fp, class _BoundArgs, class _TupleUj, bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>180struct __bind_return;181182template <class _Fp, class... _BoundArgs, class _TupleUj>183struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true> {184typedef typename __invoke_of< _Fp&, typename __mu_return< _BoundArgs, _TupleUj >::type... >::type type;185};186187template <class _Fp, class... _BoundArgs, class _TupleUj>188struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true> {189typedef typename __invoke_of< _Fp&, typename __mu_return< const _BoundArgs, _TupleUj >::type... >::type type;190};191192template <class _Fp, class _BoundArgs, size_t... _Indx, class _Args>193inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bind_return<_Fp, _BoundArgs, _Args>::type194__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>, _Args&& __args) {195return std::__invoke(__f, std::__mu(std::get<_Indx>(__bound_args), __args)...);196}197198template <class _Fp, class... _BoundArgs>199class __bind : public __weak_result_type<__decay_t<_Fp> > {200protected:201using _Fd = __decay_t<_Fp>;202typedef tuple<__decay_t<_BoundArgs>...> _Td;203204private:205_Fd __f_;206_Td __bound_args_;207208typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;209210public:211template <212class _Gp,213class... _BA,214__enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind>::value,215int> = 0>216_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bind(_Gp&& __f, _BA&&... __bound_args)217: __f_(std::forward<_Gp>(__f)), __bound_args_(std::forward<_BA>(__bound_args)...) {}218219template <class... _Args>220_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type221operator()(_Args&&... __args) {222return std::__apply_functor(__f_, __bound_args_, __indices(), tuple<_Args&&...>(std::forward<_Args>(__args)...));223}224225template <class... _Args>226_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20227typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type228operator()(_Args&&... __args) const {229return std::__apply_functor(__f_, __bound_args_, __indices(), tuple<_Args&&...>(std::forward<_Args>(__args)...));230}231};232233template <class _Fp, class... _BoundArgs>234struct is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};235236template <class _Rp, class _Fp, class... _BoundArgs>237class __bind_r : public __bind<_Fp, _BoundArgs...> {238typedef __bind<_Fp, _BoundArgs...> base;239typedef typename base::_Fd _Fd;240typedef typename base::_Td _Td;241242public:243typedef _Rp result_type;244245template <246class _Gp,247class... _BA,248__enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind_r>::value,249int> = 0>250_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bind_r(_Gp&& __f, _BA&&... __bound_args)251: base(std::forward<_Gp>(__f), std::forward<_BA>(__bound_args)...) {}252253template <254class... _Args,255__enable_if_t<is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type, result_type>::value ||256is_void<_Rp>::value,257int> = 0>258_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 result_type operator()(_Args&&... __args) {259typedef __invoke_void_return_wrapper<_Rp> _Invoker;260return _Invoker::__call(static_cast<base&>(*this), std::forward<_Args>(__args)...);261}262263template <class... _Args,264__enable_if_t<is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,265result_type>::value ||266is_void<_Rp>::value,267int> = 0>268_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 result_type operator()(_Args&&... __args) const {269typedef __invoke_void_return_wrapper<_Rp> _Invoker;270return _Invoker::__call(static_cast<base const&>(*this), std::forward<_Args>(__args)...);271}272};273274template <class _Rp, class _Fp, class... _BoundArgs>275struct is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};276277template <class _Fp, class... _BoundArgs>278inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bind<_Fp, _BoundArgs...>279bind(_Fp&& __f, _BoundArgs&&... __bound_args) {280typedef __bind<_Fp, _BoundArgs...> type;281return type(std::forward<_Fp>(__f), std::forward<_BoundArgs>(__bound_args)...);282}283284template <class _Rp, class _Fp, class... _BoundArgs>285inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bind_r<_Rp, _Fp, _BoundArgs...>286bind(_Fp&& __f, _BoundArgs&&... __bound_args) {287typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;288return type(std::forward<_Fp>(__f), std::forward<_BoundArgs>(__bound_args)...);289}290291#endif // _LIBCPP_CXX03_LANG292293_LIBCPP_END_NAMESPACE_STD294295#endif // _LIBCPP___FUNCTIONAL_BIND_H296297298