299 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___FUNCTIONAL_BIND_H11#define _LIBCPP___FUNCTIONAL_BIND_H12 13#include <__config>14#include <__functional/weak_result_type.h>15#include <__fwd/functional.h>16#include <__type_traits/decay.h>17#include <__type_traits/invoke.h>18#include <__type_traits/is_reference_wrapper.h>19#include <__type_traits/is_void.h>20#include <tuple>21 22#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)23# pragma GCC system_header24#endif25 26_LIBCPP_BEGIN_NAMESPACE_STD27 28template <class _Tp>29struct is_bind_expression30 : _If< _IsSame<_Tp, __remove_cvref_t<_Tp> >::value, false_type, is_bind_expression<__remove_cvref_t<_Tp> > > {};31 32#if _LIBCPP_STD_VER >= 1733template <class _Tp>34inline constexpr bool is_bind_expression_v = is_bind_expression<_Tp>::value;35#endif36 37template <class _Tp>38struct is_placeholder39 : _If< _IsSame<_Tp, __remove_cvref_t<_Tp> >::value,40 integral_constant<int, 0>,41 is_placeholder<__remove_cvref_t<_Tp> > > {};42 43#if _LIBCPP_STD_VER >= 1744template <class _Tp>45inline constexpr int is_placeholder_v = is_placeholder<_Tp>::value;46#endif47 48namespace placeholders {49 50template <int _Np>51struct __ph {};52 53// 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;71 72} // namespace placeholders73 74template <int _Np>75struct is_placeholder<placeholders::__ph<_Np> > : public integral_constant<int, _Np> {};76 77#ifndef _LIBCPP_CXX03_LANG78 79template <class _Tp, class _Uj>80inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __mu(reference_wrapper<_Tp> __t, _Uj&) {81 return __t.get();82}83 84template <class _Ti, class... _Uj, size_t... _Indx>85inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __invoke_result_t<_Ti&, _Uj...>86__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __index_sequence<_Indx...>) {87 return __ti(std::forward<_Uj>(std::get<_Indx>(__uj))...);88}89 90template <class _Ti, class... _Uj, __enable_if_t<is_bind_expression<_Ti>::value, int> = 0>91inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __invoke_result_t<_Ti&, _Uj...>92__mu(_Ti& __ti, tuple<_Uj...>& __uj) {93 return std::__mu_expand(__ti, __uj, __make_index_sequence<sizeof...(_Uj)>());94}95 96template <bool _IsPh, class _Ti, class _Uj>97struct __mu_return2 {};98 99template <class _Ti, class _Uj>100struct __mu_return2<true, _Ti, _Uj> {101 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;102};103 104template <class _Ti, class _Uj, __enable_if_t<0 < is_placeholder<_Ti>::value, int> = 0>105inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20106typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type107__mu(_Ti&, _Uj& __uj) {108 const size_t __indx = is_placeholder<_Ti>::value - 1;109 return std::forward<typename tuple_element<__indx, _Uj>::type>(std::get<__indx>(__uj));110}111 112template <class _Ti,113 class _Uj,114 __enable_if_t<!is_bind_expression<_Ti>::value && is_placeholder<_Ti>::value == 0 &&115 !__is_reference_wrapper<_Ti>::value,116 int> = 0>117inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Ti& __mu(_Ti& __ti, _Uj&) {118 return __ti;119}120 121template <class _Ti, bool _IsReferenceWrapper, bool _IsBindEx, bool _IsPh, class _TupleUj>122struct __mu_return_impl;123 124template <bool _Invokable, class _Ti, class... _Uj>125struct __mu_return_invokable // false126{127 typedef __nat type;128};129 130template <class _Ti, class... _Uj>131struct __mu_return_invokable<true, _Ti, _Uj...> {132 using type _LIBCPP_NODEBUG = __invoke_result_t<_Ti&, _Uj...>;133};134 135template <class _Ti, class... _Uj>136struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >137 : public __mu_return_invokable<__is_invocable_v<_Ti&, _Uj...>, _Ti, _Uj...> {};138 139template <class _Ti, class _TupleUj>140struct __mu_return_impl<_Ti, false, false, true, _TupleUj> {141 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _TupleUj>::type&& type;142};143 144template <class _Ti, class _TupleUj>145struct __mu_return_impl<_Ti, true, false, false, _TupleUj> {146 typedef typename _Ti::type& type;147};148 149template <class _Ti, class _TupleUj>150struct __mu_return_impl<_Ti, false, false, false, _TupleUj> {151 typedef _Ti& type;152};153 154template <class _Ti, class _TupleUj>155struct __mu_return156 : public __mu_return_impl<157 _Ti,158 __is_reference_wrapper<_Ti>::value,159 is_bind_expression<_Ti>::value,160 0 < is_placeholder<_Ti>::value && is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,161 _TupleUj> {};162 163template <class _Fp, class _BoundArgs, class _TupleUj>164struct __is_valid_bind_return {165 static const bool value = false;166};167 168template <class _Fp, class... _BoundArgs, class _TupleUj>169struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj> {170 static const bool value = __is_invocable_v<_Fp, typename __mu_return<_BoundArgs, _TupleUj>::type...>;171};172 173template <class _Fp, class... _BoundArgs, class _TupleUj>174struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj> {175 static const bool value = __is_invocable_v<_Fp, typename __mu_return<const _BoundArgs, _TupleUj>::type...>;176};177 178template <class _Fp, class _BoundArgs, class _TupleUj, bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>179struct __bind_return;180 181template <class _Fp, class... _BoundArgs, class _TupleUj>182struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true> {183 using type _LIBCPP_NODEBUG = __invoke_result_t<_Fp&, typename __mu_return<_BoundArgs, _TupleUj>::type...>;184};185 186template <class _Fp, class... _BoundArgs, class _TupleUj>187struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true> {188 using type _LIBCPP_NODEBUG = __invoke_result_t<_Fp&, typename __mu_return<const _BoundArgs, _TupleUj>::type...>;189};190 191template <class _Fp, class _BoundArgs, size_t... _Indx, class _Args>192inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bind_return<_Fp, _BoundArgs, _Args>::type193__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __index_sequence<_Indx...>, _Args&& __args) {194 return std::__invoke(__f, std::__mu(std::get<_Indx>(__bound_args), __args)...);195}196 197template <class _Fp, class... _BoundArgs>198class __bind : public __weak_result_type<__decay_t<_Fp> > {199protected:200 using _Fd _LIBCPP_NODEBUG = __decay_t<_Fp>;201 typedef tuple<__decay_t<_BoundArgs>...> _Td;202 203private:204 _Fd __f_;205 _Td __bound_args_;206 207public:208 template <209 class _Gp,210 class... _BA,211 __enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind>::value,212 int> = 0>213 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bind(_Gp&& __f, _BA&&... __bound_args)214 : __f_(std::forward<_Gp>(__f)), __bound_args_(std::forward<_BA>(__bound_args)...) {}215 216 template <class... _Args>217 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type218 operator()(_Args&&... __args) {219 return std::__apply_functor(220 __f_,221 __bound_args_,222 __make_index_sequence<sizeof...(_BoundArgs)>(),223 tuple<_Args&&...>(std::forward<_Args>(__args)...));224 }225 226 template <class... _Args>227 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20228 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type229 operator()(_Args&&... __args) const {230 return std::__apply_functor(231 __f_,232 __bound_args_,233 __make_index_sequence<sizeof...(_BoundArgs)>(),234 tuple<_Args&&...>(std::forward<_Args>(__args)...));235 }236};237 238template <class _Fp, class... _BoundArgs>239struct is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};240 241template <class _Rp, class _Fp, class... _BoundArgs>242class __bind_r : public __bind<_Fp, _BoundArgs...> {243 typedef __bind<_Fp, _BoundArgs...> base;244 typedef typename base::_Fd _Fd;245 typedef typename base::_Td _Td;246 247public:248 typedef _Rp result_type;249 250 template <251 class _Gp,252 class... _BA,253 __enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind_r>::value,254 int> = 0>255 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bind_r(_Gp&& __f, _BA&&... __bound_args)256 : base(std::forward<_Gp>(__f), std::forward<_BA>(__bound_args)...) {}257 258 template <259 class... _Args,260 __enable_if_t<is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type, result_type>::value ||261 is_void<_Rp>::value,262 int> = 0>263 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 result_type operator()(_Args&&... __args) {264 return std::__invoke_r<_Rp>(static_cast<base&>(*this), std::forward<_Args>(__args)...);265 }266 267 template <class... _Args,268 __enable_if_t<is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,269 result_type>::value ||270 is_void<_Rp>::value,271 int> = 0>272 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 result_type operator()(_Args&&... __args) const {273 return std::__invoke_r<_Rp>(static_cast<base const&>(*this), std::forward<_Args>(__args)...);274 }275};276 277template <class _Rp, class _Fp, class... _BoundArgs>278struct is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};279 280template <class _Fp, class... _BoundArgs>281[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bind<_Fp, _BoundArgs...>282bind(_Fp&& __f, _BoundArgs&&... __bound_args) {283 typedef __bind<_Fp, _BoundArgs...> type;284 return type(std::forward<_Fp>(__f), std::forward<_BoundArgs>(__bound_args)...);285}286 287template <class _Rp, class _Fp, class... _BoundArgs>288[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bind_r<_Rp, _Fp, _BoundArgs...>289bind(_Fp&& __f, _BoundArgs&&... __bound_args) {290 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;291 return type(std::forward<_Fp>(__f), std::forward<_BoundArgs>(__bound_args)...);292}293 294#endif // _LIBCPP_CXX03_LANG295 296_LIBCPP_END_NAMESPACE_STD297 298#endif // _LIBCPP___FUNCTIONAL_BIND_H299