1 /*
2  * Copyright (c) 2021-2024 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 
18 #include "__basic_sender.hpp"
19 #include "__diagnostics.hpp"
20 #include "__domain.hpp"
21 #include "__execution_fwd.hpp"
22 #include "__meta.hpp"
23 #include "__sender_adaptor_closure.hpp"
24 #include "__senders.hpp"
25 #include "__senders_core.hpp"
26 #include "__transform_completion_signatures.hpp"
27 #include "__transform_sender.hpp"
28 
29 // include these after __execution_fwd.hpp
30 namespace stdexec
31 {
32 /////////////////////////////////////////////////////////////////////////////
33 // [execution.senders.adaptors.upon_stopped]
34 namespace __upon_stopped
35 {
36 inline constexpr __mstring __upon_stopped_context =
37     "In stdexec::upon_stopped(Sender, Function)..."_mstr;
38 using __on_not_callable = __callable_error<__upon_stopped_context>;
39 
40 template <class _Fun, class _CvrefSender, class... _Env>
41 using __completion_signatures_t = //
42     transform_completion_signatures<
43         __completion_signatures_of_t<_CvrefSender, _Env...>,
44         __with_error_invoke_t<__on_not_callable, set_stopped_t, _Fun,
45                               _CvrefSender, _Env...>,
46         __sigs::__default_set_value, __sigs::__default_set_error,
47         __set_value_invoke_t<_Fun>>;
48 
49 ////////////////////////////////////////////////////////////////////////////////////////////////
50 struct upon_stopped_t
51 {
52     template <sender _Sender, __movable_value _Fun>
53         requires __callable<_Fun>
operator ()stdexec::__upon_stopped::upon_stopped_t54     auto operator()(_Sender&& __sndr, _Fun __fun) const -> __well_formed_sender
55         auto
56     {
57         auto __domain = __get_early_domain(__sndr);
58         return stdexec::transform_sender(
59             __domain,
60             __make_sexpr<upon_stopped_t>(static_cast<_Fun&&>(__fun),
61                                          static_cast<_Sender&&>(__sndr)));
62     }
63 
64     template <__movable_value _Fun>
65         requires __callable<_Fun>
66     STDEXEC_ATTRIBUTE((always_inline))
operator ()stdexec::__upon_stopped::upon_stopped_t67     auto operator()(_Fun __fun) const -> __binder_back<upon_stopped_t, _Fun>
68     {
69         return {{static_cast<_Fun&&>(__fun)}, {}, {}};
70     }
71 
72     using _Sender = __1;
73     using _Fun = __0;
74     using __legacy_customizations_t =
75         __types<tag_invoke_t(upon_stopped_t,
76                              get_completion_scheduler_t<set_value_t>(
77                                  get_env_t(_Sender&)),
78                              _Sender, _Fun),
79                 tag_invoke_t(upon_stopped_t, _Sender, _Fun)>;
80 };
81 
82 struct __upon_stopped_impl : __sexpr_defaults
83 {
84     static constexpr auto get_completion_signatures = //
85         []<class _Sender, class... _Env>(_Sender&&, _Env&&...) noexcept
86         -> __completion_signatures_t<__decay_t<__data_of<_Sender>>,
87                                      __child_of<_Sender>, _Env...> {
88         static_assert(sender_expr_for<_Sender, upon_stopped_t>);
89         return {};
90     };
91 
92     static constexpr auto complete = //
93         []<class _Tag, class _State, class _Receiver, class... _Args>(
94             __ignore, _State& __state, _Receiver& __rcvr, _Tag,
95             _Args&&... __args) noexcept -> void {
96         if constexpr (__same_as<_Tag, set_stopped_t>)
97         {
98             stdexec::__set_value_invoke(static_cast<_Receiver&&>(__rcvr),
99                                         static_cast<_State&&>(__state),
100                                         static_cast<_Args&&>(__args)...);
101         }
102         else
103         {
104             _Tag()(static_cast<_Receiver&&>(__rcvr),
105                    static_cast<_Args&&>(__args)...);
106         }
107     };
108 };
109 } // namespace __upon_stopped
110 
111 using __upon_stopped::upon_stopped_t;
112 inline constexpr upon_stopped_t upon_stopped{};
113 
114 template <>
115 struct __sexpr_impl<upon_stopped_t> : __upon_stopped::__upon_stopped_impl
116 {};
117 
118 } // namespace stdexec
119