Patrick Williams | 4c2d73d | 2023-12-01 16:58:58 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 NVIDIA Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License Version 2.0 with LLVM Exceptions |
| 5 | * (the "License"); you may not use this file except in compliance with |
| 6 | * the License. You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://llvm.org/LICENSE.txt |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
Patrick Williams | 8723a54 | 2024-02-12 11:18:32 -0600 | [diff] [blame^] | 18 | #include "__concepts.hpp" |
Patrick Williams | 4c2d73d | 2023-12-01 16:58:58 -0600 | [diff] [blame] | 19 | #include "__config.hpp" |
| 20 | #include "__meta.hpp" |
Patrick Williams | 8723a54 | 2024-02-12 11:18:32 -0600 | [diff] [blame^] | 21 | #include "__type_traits.hpp" |
| 22 | |
| 23 | #include <cstddef> |
Patrick Williams | 4c2d73d | 2023-12-01 16:58:58 -0600 | [diff] [blame] | 24 | |
| 25 | namespace stdexec |
| 26 | { |
| 27 | namespace __tup |
| 28 | { |
| 29 | template <class _Ty, std::size_t _Idx> |
| 30 | struct __box |
| 31 | { |
| 32 | STDEXEC_IMMOVABLE_NO_UNIQUE_ADDRESS _Ty __value; |
| 33 | }; |
| 34 | |
| 35 | template <class _Idx, class... _Ts> |
| 36 | struct __tuple; |
| 37 | |
| 38 | template <std::size_t... _Idx, class... _Ts> |
| 39 | struct __tuple<__indices<_Idx...>, _Ts...> : __box<_Ts, _Idx>... |
| 40 | {}; |
| 41 | |
| 42 | template <class... _Ts> |
| 43 | STDEXEC_ATTRIBUTE((host, device)) |
| 44 | __tuple(_Ts...) -> __tuple<__indices_for<_Ts...>, _Ts...>; |
| 45 | |
| 46 | #if STDEXEC_GCC() |
| 47 | template <class... _Ts> |
| 48 | struct __mk_tuple |
| 49 | { |
| 50 | using __t = __tuple<__indices_for<_Ts...>, _Ts...>; |
| 51 | }; |
| 52 | template <class... _Ts> |
| 53 | using __tuple_for = __t<__mk_tuple<_Ts...>>; |
| 54 | #else |
| 55 | template <class... _Ts> |
| 56 | using __tuple_for = __tuple<__indices_for<_Ts...>, _Ts...>; |
| 57 | #endif |
| 58 | |
| 59 | template <std::size_t _Idx, class _Ty> |
| 60 | STDEXEC_ATTRIBUTE((always_inline)) |
| 61 | constexpr _Ty&& __get(__box<_Ty, _Idx>&& __self) noexcept |
| 62 | { |
| 63 | return (_Ty&&)__self.__value; |
| 64 | } |
| 65 | |
| 66 | template <std::size_t _Idx, class _Ty> |
| 67 | STDEXEC_ATTRIBUTE((always_inline)) |
| 68 | constexpr _Ty& __get(__box<_Ty, _Idx>& __self) noexcept |
| 69 | { |
| 70 | return __self.__value; |
| 71 | } |
| 72 | |
| 73 | template <std::size_t _Idx, class _Ty> |
| 74 | STDEXEC_ATTRIBUTE((always_inline)) |
| 75 | constexpr const _Ty& __get(const __box<_Ty, _Idx>& __self) noexcept |
| 76 | { |
| 77 | return __self.__value; |
| 78 | } |
| 79 | |
| 80 | template <std::size_t... _Idx, class... _Ts> |
| 81 | void __tuple_like_(const __tuple<__indices<_Idx...>, _Ts...>&); |
| 82 | |
| 83 | template <class _Tup> |
| 84 | concept __tuple_like = requires(_Tup& __tup) { __tup::__tuple_like_(__tup); }; |
| 85 | |
| 86 | struct __apply_ |
| 87 | { |
| 88 | template <class _Fun, class _Tuple, std::size_t... _Idx, class... _Ts> |
| 89 | requires __callable<_Fun, __copy_cvref_t<_Tuple, _Ts>...> |
| 90 | constexpr auto operator()( |
| 91 | _Fun&& __fun, _Tuple&& __tup, |
| 92 | const __tuple< |
| 93 | __indices<_Idx...>, |
| 94 | _Ts...>*) noexcept(__nothrow_callable<_Fun, __copy_cvref_t<_Tuple, |
| 95 | _Ts>...>) |
| 96 | -> __call_result_t<_Fun, __copy_cvref_t<_Tuple, _Ts>...> |
| 97 | { |
| 98 | return ((_Fun&&)__fun)( |
| 99 | static_cast<__copy_cvref_t<_Tuple, __box<_Ts, _Idx>>&&>(__tup) |
| 100 | .__value...); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | template <class _Fun, __tuple_like _Tuple> |
| 105 | STDEXEC_ATTRIBUTE((always_inline)) |
| 106 | constexpr auto __apply(_Fun&& __fun, _Tuple&& __tup) noexcept( |
| 107 | noexcept(__apply_()((_Fun&&)__fun, (_Tuple&&)__tup, &__tup))) |
| 108 | -> decltype(__apply_()((_Fun&&)__fun, (_Tuple&&)__tup, &__tup)) |
| 109 | { |
| 110 | return __apply_()((_Fun&&)__fun, (_Tuple&&)__tup, &__tup); |
| 111 | } |
| 112 | } // namespace __tup |
| 113 | |
| 114 | using __tup::__tuple; |
| 115 | |
| 116 | // So we can use __tuple as a typelist and ignore the first template parameter |
| 117 | template <class _Fn, class _Idx, class... _Ts> |
| 118 | requires __minvocable<_Fn, _Ts...> |
| 119 | struct __uncurry_<_Fn, __tuple<_Idx, _Ts...>> |
| 120 | { |
| 121 | using __t = __minvoke<_Fn, _Ts...>; |
| 122 | }; |
| 123 | } // namespace stdexec |