Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:30

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___FUNCTIONAL_BIND_H
0011 #define _LIBCPP___CXX03___FUNCTIONAL_BIND_H
0012 
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__functional/invoke.h>
0015 #include <__cxx03/__functional/weak_result_type.h>
0016 #include <__cxx03/__fwd/functional.h>
0017 #include <__cxx03/__type_traits/decay.h>
0018 #include <__cxx03/__type_traits/is_reference_wrapper.h>
0019 #include <__cxx03/__type_traits/is_void.h>
0020 #include <__cxx03/cstddef>
0021 #include <__cxx03/tuple>
0022 
0023 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0024 #  pragma GCC system_header
0025 #endif
0026 
0027 _LIBCPP_BEGIN_NAMESPACE_STD
0028 
0029 template <class _Tp>
0030 struct is_bind_expression
0031     : _If< _IsSame<_Tp, __remove_cvref_t<_Tp> >::value, false_type, is_bind_expression<__remove_cvref_t<_Tp> > > {};
0032 
0033 #if _LIBCPP_STD_VER >= 17
0034 template <class _Tp>
0035 inline constexpr bool is_bind_expression_v = is_bind_expression<_Tp>::value;
0036 #endif
0037 
0038 template <class _Tp>
0039 struct is_placeholder
0040     : _If< _IsSame<_Tp, __remove_cvref_t<_Tp> >::value,
0041            integral_constant<int, 0>,
0042            is_placeholder<__remove_cvref_t<_Tp> > > {};
0043 
0044 #if _LIBCPP_STD_VER >= 17
0045 template <class _Tp>
0046 inline constexpr int is_placeholder_v = is_placeholder<_Tp>::value;
0047 #endif
0048 
0049 namespace placeholders {
0050 
0051 template <int _Np>
0052 struct __ph {};
0053 
0054 // C++17 recommends that we implement placeholders as `inline constexpr`, but allows
0055 // implementing them as `extern <implementation-defined>`. Libc++ implements them as
0056 // `extern const` in all standard modes to avoid an ABI break in C++03: making them
0057 // `inline constexpr` requires removing their definition in the shared library to
0058 // avoid ODR violations, which is an ABI break.
0059 //
0060 // In practice, since placeholders are empty, `extern const` is almost impossible
0061 // to distinguish from `inline constexpr` from a usage stand point.
0062 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<1> _1;
0063 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<2> _2;
0064 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<3> _3;
0065 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<4> _4;
0066 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<5> _5;
0067 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<6> _6;
0068 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<7> _7;
0069 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<8> _8;
0070 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<9> _9;
0071 _LIBCPP_EXPORTED_FROM_ABI extern const __ph<10> _10;
0072 
0073 } // namespace placeholders
0074 
0075 template <int _Np>
0076 struct is_placeholder<placeholders::__ph<_Np> > : public integral_constant<int, _Np> {};
0077 
0078 #ifndef _LIBCPP_CXX03_LANG
0079 
0080 template <class _Tp, class _Uj>
0081 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __mu(reference_wrapper<_Tp> __t, _Uj&) {
0082   return __t.get();
0083 }
0084 
0085 template <class _Ti, class... _Uj, size_t... _Indx>
0086 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __invoke_of<_Ti&, _Uj...>::type
0087 __mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>) {
0088   return __ti(std::forward<_Uj>(std::get<_Indx>(__uj))...);
0089 }
0090 
0091 template <class _Ti, class... _Uj, __enable_if_t<is_bind_expression<_Ti>::value, int> = 0>
0092 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __invoke_of<_Ti&, _Uj...>::type
0093 __mu(_Ti& __ti, tuple<_Uj...>& __uj) {
0094   typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
0095   return std::__mu_expand(__ti, __uj, __indices());
0096 }
0097 
0098 template <bool _IsPh, class _Ti, class _Uj>
0099 struct __mu_return2 {};
0100 
0101 template <class _Ti, class _Uj>
0102 struct __mu_return2<true, _Ti, _Uj> {
0103   typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
0104 };
0105 
0106 template <class _Ti, class _Uj, __enable_if_t<0 < is_placeholder<_Ti>::value, int> = 0>
0107 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
0108 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
0109 __mu(_Ti&, _Uj& __uj) {
0110   const size_t __indx = is_placeholder<_Ti>::value - 1;
0111   return std::forward<typename tuple_element<__indx, _Uj>::type>(std::get<__indx>(__uj));
0112 }
0113 
0114 template <class _Ti,
0115           class _Uj,
0116           __enable_if_t<!is_bind_expression<_Ti>::value && is_placeholder<_Ti>::value == 0 &&
0117                             !__is_reference_wrapper<_Ti>::value,
0118                         int> = 0>
0119 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Ti& __mu(_Ti& __ti, _Uj&) {
0120   return __ti;
0121 }
0122 
0123 template <class _Ti, bool _IsReferenceWrapper, bool _IsBindEx, bool _IsPh, class _TupleUj>
0124 struct __mu_return_impl;
0125 
0126 template <bool _Invokable, class _Ti, class... _Uj>
0127 struct __mu_return_invokable // false
0128 {
0129   typedef __nat type;
0130 };
0131 
0132 template <class _Ti, class... _Uj>
0133 struct __mu_return_invokable<true, _Ti, _Uj...> {
0134   typedef typename __invoke_of<_Ti&, _Uj...>::type type;
0135 };
0136 
0137 template <class _Ti, class... _Uj>
0138 struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
0139     : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...> {};
0140 
0141 template <class _Ti, class _TupleUj>
0142 struct __mu_return_impl<_Ti, false, false, true, _TupleUj> {
0143   typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _TupleUj>::type&& type;
0144 };
0145 
0146 template <class _Ti, class _TupleUj>
0147 struct __mu_return_impl<_Ti, true, false, false, _TupleUj> {
0148   typedef typename _Ti::type& type;
0149 };
0150 
0151 template <class _Ti, class _TupleUj>
0152 struct __mu_return_impl<_Ti, false, false, false, _TupleUj> {
0153   typedef _Ti& type;
0154 };
0155 
0156 template <class _Ti, class _TupleUj>
0157 struct __mu_return
0158     : public __mu_return_impl<
0159           _Ti,
0160           __is_reference_wrapper<_Ti>::value,
0161           is_bind_expression<_Ti>::value,
0162           0 < is_placeholder<_Ti>::value && is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
0163           _TupleUj> {};
0164 
0165 template <class _Fp, class _BoundArgs, class _TupleUj>
0166 struct __is_valid_bind_return {
0167   static const bool value = false;
0168 };
0169 
0170 template <class _Fp, class... _BoundArgs, class _TupleUj>
0171 struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj> {
0172   static const bool value = __invokable<_Fp, typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
0173 };
0174 
0175 template <class _Fp, class... _BoundArgs, class _TupleUj>
0176 struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj> {
0177   static const bool value = __invokable<_Fp, typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
0178 };
0179 
0180 template <class _Fp, class _BoundArgs, class _TupleUj, bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
0181 struct __bind_return;
0182 
0183 template <class _Fp, class... _BoundArgs, class _TupleUj>
0184 struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true> {
0185   typedef typename __invoke_of< _Fp&, typename __mu_return< _BoundArgs, _TupleUj >::type... >::type type;
0186 };
0187 
0188 template <class _Fp, class... _BoundArgs, class _TupleUj>
0189 struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true> {
0190   typedef typename __invoke_of< _Fp&, typename __mu_return< const _BoundArgs, _TupleUj >::type... >::type type;
0191 };
0192 
0193 template <class _Fp, class _BoundArgs, size_t... _Indx, class _Args>
0194 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bind_return<_Fp, _BoundArgs, _Args>::type
0195 __apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>, _Args&& __args) {
0196   return std::__invoke(__f, std::__mu(std::get<_Indx>(__bound_args), __args)...);
0197 }
0198 
0199 template <class _Fp, class... _BoundArgs>
0200 class __bind : public __weak_result_type<__decay_t<_Fp> > {
0201 protected:
0202   using _Fd = __decay_t<_Fp>;
0203   typedef tuple<__decay_t<_BoundArgs>...> _Td;
0204 
0205 private:
0206   _Fd __f_;
0207   _Td __bound_args_;
0208 
0209   typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
0210 
0211 public:
0212   template <
0213       class _Gp,
0214       class... _BA,
0215       __enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind>::value,
0216                     int> = 0>
0217   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bind(_Gp&& __f, _BA&&... __bound_args)
0218       : __f_(std::forward<_Gp>(__f)), __bound_args_(std::forward<_BA>(__bound_args)...) {}
0219 
0220   template <class... _Args>
0221   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
0222   operator()(_Args&&... __args) {
0223     return std::__apply_functor(__f_, __bound_args_, __indices(), tuple<_Args&&...>(std::forward<_Args>(__args)...));
0224   }
0225 
0226   template <class... _Args>
0227   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
0228   typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
0229   operator()(_Args&&... __args) const {
0230     return std::__apply_functor(__f_, __bound_args_, __indices(), tuple<_Args&&...>(std::forward<_Args>(__args)...));
0231   }
0232 };
0233 
0234 template <class _Fp, class... _BoundArgs>
0235 struct is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
0236 
0237 template <class _Rp, class _Fp, class... _BoundArgs>
0238 class __bind_r : public __bind<_Fp, _BoundArgs...> {
0239   typedef __bind<_Fp, _BoundArgs...> base;
0240   typedef typename base::_Fd _Fd;
0241   typedef typename base::_Td _Td;
0242 
0243 public:
0244   typedef _Rp result_type;
0245 
0246   template <
0247       class _Gp,
0248       class... _BA,
0249       __enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind_r>::value,
0250                     int> = 0>
0251   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bind_r(_Gp&& __f, _BA&&... __bound_args)
0252       : base(std::forward<_Gp>(__f), std::forward<_BA>(__bound_args)...) {}
0253 
0254   template <
0255       class... _Args,
0256       __enable_if_t<is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type, result_type>::value ||
0257                         is_void<_Rp>::value,
0258                     int> = 0>
0259   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 result_type operator()(_Args&&... __args) {
0260     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
0261     return _Invoker::__call(static_cast<base&>(*this), std::forward<_Args>(__args)...);
0262   }
0263 
0264   template <class... _Args,
0265             __enable_if_t<is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
0266                                          result_type>::value ||
0267                               is_void<_Rp>::value,
0268                           int> = 0>
0269   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 result_type operator()(_Args&&... __args) const {
0270     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
0271     return _Invoker::__call(static_cast<base const&>(*this), std::forward<_Args>(__args)...);
0272   }
0273 };
0274 
0275 template <class _Rp, class _Fp, class... _BoundArgs>
0276 struct is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
0277 
0278 template <class _Fp, class... _BoundArgs>
0279 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bind<_Fp, _BoundArgs...>
0280 bind(_Fp&& __f, _BoundArgs&&... __bound_args) {
0281   typedef __bind<_Fp, _BoundArgs...> type;
0282   return type(std::forward<_Fp>(__f), std::forward<_BoundArgs>(__bound_args)...);
0283 }
0284 
0285 template <class _Rp, class _Fp, class... _BoundArgs>
0286 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bind_r<_Rp, _Fp, _BoundArgs...>
0287 bind(_Fp&& __f, _BoundArgs&&... __bound_args) {
0288   typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
0289   return type(std::forward<_Fp>(__f), std::forward<_BoundArgs>(__bound_args)...);
0290 }
0291 
0292 #endif // _LIBCPP_CXX03_LANG
0293 
0294 _LIBCPP_END_NAMESPACE_STD
0295 
0296 #endif // _LIBCPP___CXX03___FUNCTIONAL_BIND_H