bind.h 11 KB

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