|
| 1 | +// include/beman/execution26/detail/into_variant.hpp -*-C++-*- |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#ifndef INCLUDED_BEMAN_EXECUTION26_DETAIL_INTO_VARIANT |
| 5 | +#define INCLUDED_BEMAN_EXECUTION26_DETAIL_INTO_VARIANT |
| 6 | + |
| 7 | +#include <beman/execution26/detail/child_type.hpp> |
| 8 | +#include <beman/execution26/detail/completion_signatures_for.hpp> |
| 9 | +#include <beman/execution26/detail/default_impls.hpp> |
| 10 | +#include <beman/execution26/detail/decayed_tuple.hpp> |
| 11 | +#include <beman/execution26/detail/env_of_t.hpp> |
| 12 | +#include <beman/execution26/detail/error_types_of_t.hpp> |
| 13 | +#include <beman/execution26/detail/get_domain_early.hpp> |
| 14 | +#include <beman/execution26/detail/impls_for.hpp> |
| 15 | +#include <beman/execution26/detail/make_sender.hpp> |
| 16 | +#include <beman/execution26/detail/meta_combine.hpp> |
| 17 | +#include <beman/execution26/detail/sender.hpp> |
| 18 | +#include <beman/execution26/detail/sender_in.hpp> |
| 19 | +#include <beman/execution26/detail/sends_stopped.hpp> |
| 20 | +#include <beman/execution26/detail/set_error.hpp> |
| 21 | +#include <beman/execution26/detail/set_value.hpp> |
| 22 | +#include <beman/execution26/detail/transform_sender.hpp> |
| 23 | +#include <beman/execution26/detail/value_types_of_t.hpp> |
| 24 | + |
| 25 | +#include <concepts> |
| 26 | +#include <exception> |
| 27 | +#include <type_traits> |
| 28 | +#include <utility> |
| 29 | + |
| 30 | +// ---------------------------------------------------------------------------- |
| 31 | + |
| 32 | +namespace beman::execution26::detail |
| 33 | +{ |
| 34 | + struct into_variant_t |
| 35 | + { |
| 36 | + template <::beman::execution26::sender Sender> |
| 37 | + auto operator()(Sender&& sender) const |
| 38 | + { |
| 39 | + auto domain{::beman::execution26::detail::get_domain_early(sender)}; |
| 40 | + (void)domain; |
| 41 | + return |
| 42 | + ::beman::execution26::detail::make_sender(*this, {}, ::std::forward<Sender>(sender)) |
| 43 | + ; |
| 44 | + //return ::beman::execution26::transform_sender( |
| 45 | + // ::std::move(domain), |
| 46 | + // ::beman::execution26::detail::make_sender(*this, {}, ::std::forward<Sender>(sender)) |
| 47 | + //); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + template<> |
| 52 | + struct impls_for<::beman::execution26::detail::into_variant_t> |
| 53 | + : ::beman::execution26::detail::default_impls |
| 54 | + { |
| 55 | + static constexpr auto get_state |
| 56 | + = []<typename Sender, typename Receiver>(Sender&&, Receiver&&) noexcept |
| 57 | + -> ::std::type_identity< |
| 58 | + ::beman::execution26::value_types_of_t< |
| 59 | + ::beman::execution26::detail::child_type<Sender>, |
| 60 | + ::beman::execution26::env_of_t<Receiver> |
| 61 | + > |
| 62 | + > |
| 63 | + { |
| 64 | + return {}; |
| 65 | + }; |
| 66 | + static constexpr auto complete |
| 67 | + = []<typename State, typename Tag, typename... Args> |
| 68 | + (auto, State, auto& receiver, Tag, Args&&... args) noexcept |
| 69 | + -> void |
| 70 | + { |
| 71 | + if constexpr (::std::same_as<Tag, ::beman::execution26::set_value_t>) |
| 72 | + { |
| 73 | + using variant_type = typename State::type; |
| 74 | + using tuple_type = ::beman::execution26::detail::decayed_tuple<Args...>; |
| 75 | + try |
| 76 | + { |
| 77 | + if constexpr (sizeof...(Args) == 0u) |
| 78 | + ::beman::execution26::set_value( |
| 79 | + ::std::move(receiver) |
| 80 | + ); |
| 81 | + else |
| 82 | + ::beman::execution26::set_value( |
| 83 | + ::std::move(receiver), |
| 84 | + variant_type(tuple_type{::std::forward<Args>(args)...}) |
| 85 | + ); |
| 86 | + } |
| 87 | + catch(...) |
| 88 | + { |
| 89 | + ::beman::execution26::set_error( |
| 90 | + ::std::move(receiver), |
| 91 | + ::std::current_exception() |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + Tag()(::std::move(receiver), ::std::forward<Args>(args)...); |
| 99 | + } |
| 100 | + }; |
| 101 | + }; |
| 102 | + |
| 103 | + template <typename Sender, typename State, typename Env> |
| 104 | + struct completion_signatures_for_impl< |
| 105 | + ::beman::execution26::detail::basic_sender< |
| 106 | + ::beman::execution26::detail::into_variant_t, |
| 107 | + State, |
| 108 | + Sender |
| 109 | + >, |
| 110 | + Env |
| 111 | + > |
| 112 | + { |
| 113 | + using variant_type = ::beman::execution26::value_types_of_t<Sender, Env>; |
| 114 | + using value_types = ::std::conditional_t< |
| 115 | + ::std::same_as<variant_type, ::beman::execution26::detail::empty_variant>, |
| 116 | + ::beman::execution26::completion_signatures<>, |
| 117 | + ::beman::execution26::completion_signatures< |
| 118 | + ::beman::execution26::set_value_t(variant_type) |
| 119 | + > |
| 120 | + >; |
| 121 | + template <typename... E> |
| 122 | + using make_error_types |
| 123 | + = ::beman::execution26::completion_signatures<::beman::execution26::set_error_t(E)...>; |
| 124 | + |
| 125 | + using error_types |
| 126 | + = ::beman::execution26::error_types_of_t< |
| 127 | + Sender, |
| 128 | + Env, |
| 129 | + make_error_types |
| 130 | + >; |
| 131 | + using stopped_types |
| 132 | + = ::std::conditional_t< |
| 133 | + ::beman::execution26::sends_stopped<Sender, Env>, |
| 134 | + ::beman::execution26::completion_signatures<::beman::execution26::set_stopped_t()>, |
| 135 | + ::beman::execution26::completion_signatures<> |
| 136 | + >; |
| 137 | + using type = ::beman::execution26::detail::meta::combine< |
| 138 | + value_types, |
| 139 | + ::beman::execution26::detail::meta::combine<error_types, stopped_types> |
| 140 | + >; |
| 141 | + }; |
| 142 | +} |
| 143 | + |
| 144 | +namespace beman::execution26 |
| 145 | +{ |
| 146 | + using into_variant_t = ::beman::execution26::detail::into_variant_t; |
| 147 | + inline constexpr into_variant_t into_variant{}; |
| 148 | +} |
| 149 | + |
| 150 | +// ---------------------------------------------------------------------------- |
| 151 | + |
| 152 | +#endif |
0 commit comments