bind.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #ifndef _LIBCPP___FUNCTIONAL_BIND_H
  10. #define _LIBCPP___FUNCTIONAL_BIND_H
  11. #include <__config>
  12. #include <__functional/invoke.h>
  13. #include <__functional/weak_result_type.h>
  14. #include <__type_traits/decay.h>
  15. #include <__type_traits/is_reference_wrapper.h>
  16. #include <__type_traits/is_void.h>
  17. #include <cstddef>
  18. #include <tuple>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. template<class _Tp>
  24. struct is_bind_expression : _If<
  25. _IsSame<_Tp, __remove_cvref_t<_Tp> >::value,
  26. false_type,
  27. is_bind_expression<__remove_cvref_t<_Tp> >
  28. > {};
  29. #if _LIBCPP_STD_VER >= 17
  30. template <class _Tp>
  31. inline constexpr bool is_bind_expression_v = is_bind_expression<_Tp>::value;
  32. #endif
  33. template<class _Tp>
  34. struct is_placeholder : _If<
  35. _IsSame<_Tp, __remove_cvref_t<_Tp> >::value,
  36. integral_constant<int, 0>,
  37. is_placeholder<__remove_cvref_t<_Tp> >
  38. > {};
  39. #if _LIBCPP_STD_VER >= 17
  40. template <class _Tp>
  41. inline constexpr int is_placeholder_v = is_placeholder<_Tp>::value;
  42. #endif
  43. namespace placeholders
  44. {
  45. template <int _Np> struct __ph {};
  46. // C++17 recommends that we implement placeholders as `inline constexpr`, but allows
  47. // implementing them as `extern <implementation-defined>`. Libc++ implements them as
  48. // `extern const` in all standard modes to avoid an ABI break in C++03: making them
  49. // `inline constexpr` requires removing their definition in the shared library to
  50. // avoid ODR violations, which is an ABI break.
  51. //
  52. // In practice, since placeholders are empty, `extern const` is almost impossible
  53. // to distinguish from `inline constexpr` from a usage stand point.
  54. _LIBCPP_EXPORTED_FROM_ABI extern const __ph<1> _1;
  55. _LIBCPP_EXPORTED_FROM_ABI extern const __ph<2> _2;
  56. _LIBCPP_EXPORTED_FROM_ABI extern const __ph<3> _3;
  57. _LIBCPP_EXPORTED_FROM_ABI extern const __ph<4> _4;
  58. _LIBCPP_EXPORTED_FROM_ABI extern const __ph<5> _5;
  59. _LIBCPP_EXPORTED_FROM_ABI extern const __ph<6> _6;
  60. _LIBCPP_EXPORTED_FROM_ABI extern const __ph<7> _7;
  61. _LIBCPP_EXPORTED_FROM_ABI extern const __ph<8> _8;
  62. _LIBCPP_EXPORTED_FROM_ABI extern const __ph<9> _9;
  63. _LIBCPP_EXPORTED_FROM_ABI extern const __ph<10> _10;
  64. } // namespace placeholders
  65. template<int _Np>
  66. struct is_placeholder<placeholders::__ph<_Np> >
  67. : public integral_constant<int, _Np> {};
  68. #ifndef _LIBCPP_CXX03_LANG
  69. template <class _Tp, class _Uj>
  70. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  71. _Tp&
  72. __mu(reference_wrapper<_Tp> __t, _Uj&)
  73. {
  74. return __t.get();
  75. }
  76. template <class _Ti, class ..._Uj, size_t ..._Indx>
  77. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  78. typename __invoke_of<_Ti&, _Uj...>::type
  79. __mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
  80. {
  81. return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
  82. }
  83. template <class _Ti, class ..._Uj, __enable_if_t<is_bind_expression<_Ti>::value, int> = 0>
  84. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  85. typename __invoke_of<_Ti&, _Uj...>::type
  86. __mu(_Ti& __ti, tuple<_Uj...>& __uj)
  87. {
  88. typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
  89. return _VSTD::__mu_expand(__ti, __uj, __indices());
  90. }
  91. template <bool IsPh, class _Ti, class _Uj>
  92. struct __mu_return2 {};
  93. template <class _Ti, class _Uj>
  94. struct __mu_return2<true, _Ti, _Uj>
  95. {
  96. typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
  97. };
  98. template <class _Ti, class _Uj, __enable_if_t<0 < is_placeholder<_Ti>::value, int> = 0>
  99. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  100. typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
  101. __mu(_Ti&, _Uj& __uj)
  102. {
  103. const size_t __indx = is_placeholder<_Ti>::value - 1;
  104. return _VSTD::forward<typename tuple_element<__indx, _Uj>::type>(_VSTD::get<__indx>(__uj));
  105. }
  106. template <class _Ti, class _Uj, __enable_if_t<!is_bind_expression<_Ti>::value &&
  107. is_placeholder<_Ti>::value == 0 &&
  108. !__is_reference_wrapper<_Ti>::value, int> = 0>
  109. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  110. _Ti&
  111. __mu(_Ti& __ti, _Uj&)
  112. {
  113. return __ti;
  114. }
  115. template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
  116. class _TupleUj>
  117. struct __mu_return_impl;
  118. template <bool _Invokable, class _Ti, class ..._Uj>
  119. struct __mu_return_invokable // false
  120. {
  121. typedef __nat type;
  122. };
  123. template <class _Ti, class ..._Uj>
  124. struct __mu_return_invokable<true, _Ti, _Uj...>
  125. {
  126. typedef typename __invoke_of<_Ti&, _Uj...>::type type;
  127. };
  128. template <class _Ti, class ..._Uj>
  129. struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
  130. : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
  131. {
  132. };
  133. template <class _Ti, class _TupleUj>
  134. struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
  135. {
  136. typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
  137. _TupleUj>::type&& type;
  138. };
  139. template <class _Ti, class _TupleUj>
  140. struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
  141. {
  142. typedef typename _Ti::type& type;
  143. };
  144. template <class _Ti, class _TupleUj>
  145. struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
  146. {
  147. typedef _Ti& type;
  148. };
  149. template <class _Ti, class _TupleUj>
  150. struct __mu_return
  151. : public __mu_return_impl<_Ti,
  152. __is_reference_wrapper<_Ti>::value,
  153. is_bind_expression<_Ti>::value,
  154. 0 < is_placeholder<_Ti>::value &&
  155. is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
  156. _TupleUj>
  157. {
  158. };
  159. template <class _Fp, class _BoundArgs, class _TupleUj>
  160. struct __is_valid_bind_return
  161. {
  162. static const bool value = false;
  163. };
  164. template <class _Fp, class ..._BoundArgs, class _TupleUj>
  165. struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
  166. {
  167. static const bool value = __invokable<_Fp,
  168. typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
  169. };
  170. template <class _Fp, class ..._BoundArgs, class _TupleUj>
  171. struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
  172. {
  173. static const bool value = __invokable<_Fp,
  174. typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
  175. };
  176. template <class _Fp, class _BoundArgs, class _TupleUj,
  177. bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
  178. struct __bind_return;
  179. template <class _Fp, class ..._BoundArgs, class _TupleUj>
  180. struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
  181. {
  182. typedef typename __invoke_of
  183. <
  184. _Fp&,
  185. typename __mu_return
  186. <
  187. _BoundArgs,
  188. _TupleUj
  189. >::type...
  190. >::type type;
  191. };
  192. template <class _Fp, class ..._BoundArgs, class _TupleUj>
  193. struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
  194. {
  195. typedef typename __invoke_of
  196. <
  197. _Fp&,
  198. typename __mu_return
  199. <
  200. const _BoundArgs,
  201. _TupleUj
  202. >::type...
  203. >::type type;
  204. };
  205. template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
  206. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  207. typename __bind_return<_Fp, _BoundArgs, _Args>::type
  208. __apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
  209. _Args&& __args)
  210. {
  211. return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
  212. }
  213. template<class _Fp, class ..._BoundArgs>
  214. class __bind : public __weak_result_type<__decay_t<_Fp> >
  215. {
  216. protected:
  217. using _Fd = __decay_t<_Fp>;
  218. typedef tuple<__decay_t<_BoundArgs>...> _Td;
  219. private:
  220. _Fd __f_;
  221. _Td __bound_args_;
  222. typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
  223. public:
  224. template <class _Gp, class ..._BA,
  225. __enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind>::value, int> = 0>
  226. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  227. explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
  228. : __f_(_VSTD::forward<_Gp>(__f)),
  229. __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
  230. template <class ..._Args>
  231. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  232. typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
  233. operator()(_Args&& ...__args)
  234. {
  235. return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
  236. tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
  237. }
  238. template <class ..._Args>
  239. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  240. typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
  241. operator()(_Args&& ...__args) const
  242. {
  243. return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
  244. tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
  245. }
  246. };
  247. #if defined(__CUDACC__) && defined(_MSC_VER)
  248. # define Y_CUDAFE_MSVC_BUG
  249. #endif
  250. template<class _Fp, class ..._BoundArgs>
  251. struct is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
  252. template<class _Rp, class _Fp, class ..._BoundArgs>
  253. class __bind_r
  254. : public __bind<_Fp, _BoundArgs...>
  255. {
  256. typedef __bind<_Fp, _BoundArgs...> base;
  257. typedef typename base::_Fd _Fd;
  258. #if !defined(Y_CUDAFE_MSVC_BUG)
  259. typedef typename base::_Td _Td;
  260. #else
  261. typedef typename tuple<typename decay<_BoundArgs>::type...> _Td;
  262. #endif
  263. public:
  264. typedef _Rp result_type;
  265. template <class _Gp, class ..._BA,
  266. __enable_if_t<is_constructible<_Fd, _Gp>::value && !is_same<__libcpp_remove_reference_t<_Gp>, __bind_r>::value, int> = 0>
  267. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  268. explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
  269. : base(_VSTD::forward<_Gp>(__f),
  270. _VSTD::forward<_BA>(__bound_args)...) {}
  271. template <class ..._Args, __enable_if_t<is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
  272. result_type>::value || is_void<_Rp>::value, int> = 0>
  273. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  274. result_type
  275. operator()(_Args&& ...__args)
  276. {
  277. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  278. return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
  279. }
  280. template <class ..._Args, __enable_if_t<is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
  281. result_type>::value || is_void<_Rp>::value, int> = 0>
  282. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  283. result_type
  284. operator()(_Args&& ...__args) const
  285. {
  286. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  287. return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
  288. }
  289. };
  290. template<class _Rp, class _Fp, class ..._BoundArgs>
  291. struct is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
  292. template<class _Fp, class ..._BoundArgs>
  293. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  294. __bind<_Fp, _BoundArgs...>
  295. bind(_Fp&& __f, _BoundArgs&&... __bound_args)
  296. {
  297. typedef __bind<_Fp, _BoundArgs...> type;
  298. return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
  299. }
  300. template<class _Rp, class _Fp, class ..._BoundArgs>
  301. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  302. __bind_r<_Rp, _Fp, _BoundArgs...>
  303. bind(_Fp&& __f, _BoundArgs&&... __bound_args)
  304. {
  305. typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
  306. return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
  307. }
  308. #endif // _LIBCPP_CXX03_LANG
  309. _LIBCPP_END_NAMESPACE_STD
  310. #endif // _LIBCPP___FUNCTIONAL_BIND_H