|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef _LIBCPP___TYPE_TRAITS_IS_APPLICABLE_H |
| 10 | +#define _LIBCPP___TYPE_TRAITS_IS_APPLICABLE_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__cstddef/size_t.h> |
| 14 | +#include <__fwd/get.h> |
| 15 | +#include <__tuple/tuple_like.h> |
| 16 | +#include <__tuple/tuple_size.h> |
| 17 | +#include <__type_traits/conjunction.h> |
| 18 | +#include <__type_traits/integral_constant.h> |
| 19 | +#include <__type_traits/invoke.h> |
| 20 | +#include <__type_traits/remove_reference.h> |
| 21 | +#include <__utility/declval.h> |
| 22 | +#include <__utility/integer_sequence.h> |
| 23 | + |
| 24 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 25 | +# pragma GCC system_header |
| 26 | +#endif |
| 27 | + |
| 28 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 29 | + |
| 30 | +#if _LIBCPP_STD_VER >= 26 |
| 31 | + |
| 32 | +template <class _Fn, class _Tuple> |
| 33 | +struct __apply_result_disabled_base {}; |
| 34 | + |
| 35 | +template <class _Fn, class _Tuple, class _Tp> |
| 36 | +struct __apply_result_enabled_base { |
| 37 | + using type _LIBCPP_NODEBUG = _Tp; |
| 38 | +}; |
| 39 | + |
| 40 | +template <bool _Applicable, bool _Nothrow, class _Tp> |
| 41 | +struct __applicability_traits { |
| 42 | + static constexpr bool __applicable = true; |
| 43 | + static constexpr bool __nothrow_applicable = _Nothrow; |
| 44 | + |
| 45 | + template <class _Fn, class _Tuple> |
| 46 | + using __base_type _LIBCPP_NODEBUG = __apply_result_enabled_base<_Fn, _Tuple, _Tp>; |
| 47 | +}; |
| 48 | + |
| 49 | +template <bool _Nothrow, class _Tp> |
| 50 | +struct __applicability_traits<false, _Nothrow, _Tp> { |
| 51 | + static_assert(!_Nothrow, "misspecified [_Applicable = false, _Nothrow = true]"); |
| 52 | + static constexpr bool __applicable = false; |
| 53 | + static constexpr bool __nothrow_applicable = false; |
| 54 | + |
| 55 | + template <class _Fn, class _Tuple> |
| 56 | + using __base_type _LIBCPP_NODEBUG = __apply_result_disabled_base<_Fn, _Tuple>; |
| 57 | +}; |
| 58 | + |
| 59 | +template <class _Fn, class _Tuple, size_t... _Is> |
| 60 | +concept __tuple_applicable_impl = requires(_Tuple&& __tuple) { |
| 61 | + [](auto&&...) {}(std::get<_Is>(static_cast<_Tuple &&>(__tuple))...); |
| 62 | +} && __is_invocable_v<_Fn, decltype(std::get<_Is>(std::declval<_Tuple>()))...>; |
| 63 | + |
| 64 | +template <class _Fn, class _Tuple, size_t... _Is> |
| 65 | +concept __tuple_nothrow_applicable_impl = requires(_Tuple&& __tuple) { |
| 66 | + { |
| 67 | + [](auto&&...) noexcept {}(std::get<_Is>(static_cast<_Tuple &&>(__tuple))...) |
| 68 | + } noexcept; |
| 69 | +} && __is_nothrow_invocable_v<_Fn, decltype(std::get<_Is>(std::declval<_Tuple>()))...>; |
| 70 | + |
| 71 | +template <class _Fn, class _Tuple> |
| 72 | +consteval auto __applicability_traits_of() { |
| 73 | + if constexpr (__tuple_like<_Tuple>) |
| 74 | + return []<size_t... _Is>(index_sequence<_Is...>) { |
| 75 | + if constexpr (__tuple_applicable_impl<_Fn, _Tuple, _Is...>) { |
| 76 | + return __applicability_traits< |
| 77 | + true, |
| 78 | + __tuple_nothrow_applicable_impl<_Fn, _Tuple, _Is...>, |
| 79 | + // FIXME: Use __invoke_result_y after merging https://github.com/llvm/llvm-project/pull/151028. |
| 80 | + typename __invoke_result<_Fn, decltype(std::get<_Is>(std::declval<_Tuple>()))...>::type>{}; |
| 81 | + } else |
| 82 | + return __applicability_traits<false, false, void>{}; |
| 83 | + }(make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>{}); |
| 84 | + else |
| 85 | + return __applicability_traits<false, false, void>{}; |
| 86 | +} |
| 87 | + |
| 88 | +template <class _Fn, class _Tuple> |
| 89 | +struct _LIBCPP_NO_SPECIALIZATIONS is_applicable |
| 90 | + : bool_constant<decltype(std::__applicability_traits_of<_Fn, _Tuple>())::__applicable> {}; |
| 91 | + |
| 92 | +template <class _Fn, class _Tuple> |
| 93 | +struct _LIBCPP_NO_SPECIALIZATIONS is_nothrow_applicable |
| 94 | + : bool_constant<decltype(std::__applicability_traits_of<_Fn, _Tuple>())::__nothrow_applicable> {}; |
| 95 | + |
| 96 | +template <class _Fn, class _Tuple> |
| 97 | +_LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_applicable_v = |
| 98 | + decltype(std::__applicability_traits_of<_Fn, _Tuple>())::__applicable; |
| 99 | + |
| 100 | +template <class _Fn, class _Tuple> |
| 101 | +_LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_nothrow_applicable_v = |
| 102 | + decltype(std::__applicability_traits_of<_Fn, _Tuple>())::__nothrow_applicable; |
| 103 | + |
| 104 | +template <class _Fn, class _Tuple> |
| 105 | +struct _LIBCPP_NO_SPECIALIZATIONS apply_result |
| 106 | + : decltype(std::__applicability_traits_of<_Fn, _Tuple>())::template __base_type<_Fn, _Tuple> {}; |
| 107 | + |
| 108 | +template <class _Fn, class _Tuple> |
| 109 | +using apply_result_t = apply_result<_Fn, _Tuple>::type; |
| 110 | + |
| 111 | +#endif // _LIBCPP_STD_VER >= 26 |
| 112 | + |
| 113 | +_LIBCPP_END_NAMESPACE_STD |
| 114 | + |
| 115 | +#endif // _LIBCPP___TYPE_TRAITS_IS_APPLICABLE_H |
0 commit comments