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, __enable_if_t<is_bind_expression<_Ti>::value, int> = 0> 102 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 103 typename __invoke_of<_Ti&, _Uj...>::type 104 __mu(_Ti& __ti, tuple<_Uj...>& __uj) 105 { 106 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices; 107 return _VSTD::__mu_expand(__ti, __uj, __indices()); 108 } 109 110 template <bool IsPh, class _Ti, class _Uj> 111 struct __mu_return2 {}; 112 113 template <class _Ti, class _Uj> 114 struct __mu_return2<true, _Ti, _Uj> 115 { 116 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type; 117 }; 118 119 template <class _Ti, class _Uj, __enable_if_t<0 < is_placeholder<_Ti>::value, int> = 0> 120 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 121 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type 122 __mu(_Ti&, _Uj& __uj) 123 { 124 const size_t __indx = is_placeholder<_Ti>::value - 1; 125 return _VSTD::forward<typename tuple_element<__indx, _Uj>::type>(_VSTD::get<__indx>(__uj)); 126 } 127 128 template <class _Ti, class _Uj, __enable_if_t<!is_bind_expression<_Ti>::value && 129 is_placeholder<_Ti>::value == 0 && 130 !__is_reference_wrapper<_Ti>::value, int> = 0> 131 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 132 _Ti& 133 __mu(_Ti& __ti, _Uj&) 134 { 135 return __ti; 136 } 137 138 template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh, 139 class _TupleUj> 140 struct __mu_return_impl; 141 142 template <bool _Invokable, class _Ti, class ..._Uj> 143 struct __mu_return_invokable // false 144 { 145 typedef __nat type; 146 }; 147 148 template <class _Ti, class ..._Uj> 149 struct __mu_return_invokable<true, _Ti, _Uj...> 150 { 151 typedef typename __invoke_of<_Ti&, _Uj...>::type type; 152 }; 153 154 template <class _Ti, class ..._Uj> 155 struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> > 156 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...> 157 { 158 }; 159 160 template <class _Ti, class _TupleUj> 161 struct __mu_return_impl<_Ti, false, false, true, _TupleUj> 162 { 163 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, 164 _TupleUj>::type&& type; 165 }; 166 167 template <class _Ti, class _TupleUj> 168 struct __mu_return_impl<_Ti, true, false, false, _TupleUj> 169 { 170 typedef typename _Ti::type& type; 171 }; 172 173 template <class _Ti, class _TupleUj> 174 struct __mu_return_impl<_Ti, false, false, false, _TupleUj> 175 { 176 typedef _Ti& type; 177 }; 178 179 template <class _Ti, class _TupleUj> 180 struct __mu_return 181 : public __mu_return_impl<_Ti, 182 __is_reference_wrapper<_Ti>::value, 183 is_bind_expression<_Ti>::value, 184 0 < is_placeholder<_Ti>::value && 185 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value, 186 _TupleUj> 187 { 188 }; 189 190 template <class _Fp, class _BoundArgs, class _TupleUj> 191 struct __is_valid_bind_return 192 { 193 static const bool value = false; 194 }; 195 196 template <class _Fp, class ..._BoundArgs, class _TupleUj> 197 struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj> 198 { 199 static const bool value = __invokable<_Fp, 200 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value; 201 }; 202 203 template <class _Fp, class ..._BoundArgs, class _TupleUj> 204 struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj> 205 { 206 static const bool value = __invokable<_Fp, 207 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value; 208 }; 209 210 template <class _Fp, class _BoundArgs, class _TupleUj, 211 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value> 212 struct __bind_return; 213 214 template <class _Fp, class ..._BoundArgs, class _TupleUj> 215 struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true> 216 { 217 typedef typename __invoke_of 218 < 219 _Fp&, 220 typename __mu_return 221 < 222 _BoundArgs, 223 _TupleUj 224 >::type... 225 >::type type; 226 }; 227 228 template <class _Fp, class ..._BoundArgs, class _TupleUj> 229 struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true> 230 { 231 typedef typename __invoke_of 232 < 233 _Fp&, 234 typename __mu_return 235 < 236 const _BoundArgs, 237 _TupleUj 238 >::type... 239 >::type type; 240 }; 241 242 template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args> 243 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 244 typename __bind_return<_Fp, _BoundArgs, _Args>::type 245 __apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>, 246 _Args&& __args) 247 { 248 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...); 249 } 250 251 template<class _Fp, class ..._BoundArgs> 252 class __bind : public __weak_result_type<__decay_t<_Fp> > 253 { 254 protected: 255 using _Fd = __decay_t<_Fp>; 256 typedef tuple<__decay_t<_BoundArgs>...> _Td; 257 private: 258 _Fd __f_; 259 _Td __bound_args_; 260 261 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices; 262 public: 263 template <class _Gp, class ..._BA, 264 __enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind>::value, int> = 0> 265 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 266 explicit __bind(_Gp&& __f, _BA&& ...__bound_args) 267 : __f_(_VSTD::forward<_Gp>(__f)), 268 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {} 269 270 template <class ..._Args> 271 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 272 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type 273 operator()(_Args&& ...__args) 274 { 275 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(), 276 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...)); 277 } 278 279 template <class ..._Args> 280 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 281 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type 282 operator()(_Args&& ...__args) const 283 { 284 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(), 285 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...)); 286 } 287 }; 288 289 template<class _Fp, class ..._BoundArgs> 290 struct is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {}; 291 292 template<class _Rp, class _Fp, class ..._BoundArgs> 293 class __bind_r 294 : public __bind<_Fp, _BoundArgs...> 295 { 296 typedef __bind<_Fp, _BoundArgs...> base; 297 typedef typename base::_Fd _Fd; 298 typedef typename base::_Td _Td; 299 public: 300 typedef _Rp result_type; 301 302 303 template <class _Gp, class ..._BA, 304 __enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind_r>::value, int> = 0> 305 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 306 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args) 307 : base(_VSTD::forward<_Gp>(__f), 308 _VSTD::forward<_BA>(__bound_args)...) {} 309 310 template <class ..._Args, __enable_if_t<is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type, 311 result_type>::value || is_void<_Rp>::value, int> = 0> 312 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 313 result_type 314 operator()(_Args&& ...__args) 315 { 316 typedef __invoke_void_return_wrapper<_Rp> _Invoker; 317 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...); 318 } 319 320 template <class ..._Args, __enable_if_t<is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type, 321 result_type>::value || is_void<_Rp>::value, int> = 0> 322 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 323 result_type 324 operator()(_Args&& ...__args) const 325 { 326 typedef __invoke_void_return_wrapper<_Rp> _Invoker; 327 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...); 328 } 329 }; 330 331 template<class _Rp, class _Fp, class ..._BoundArgs> 332 struct is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {}; 333 334 template<class _Fp, class ..._BoundArgs> 335 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 336 __bind<_Fp, _BoundArgs...> 337 bind(_Fp&& __f, _BoundArgs&&... __bound_args) 338 { 339 typedef __bind<_Fp, _BoundArgs...> type; 340 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...); 341 } 342 343 template<class _Rp, class _Fp, class ..._BoundArgs> 344 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 345 __bind_r<_Rp, _Fp, _BoundArgs...> 346 bind(_Fp&& __f, _BoundArgs&&... __bound_args) 347 { 348 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type; 349 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...); 350 } 351 352 #endif // _LIBCPP_CXX03_LANG 353 354 _LIBCPP_END_NAMESPACE_STD 355 356 #endif // _LIBCPP___FUNCTIONAL_BIND_H 357