xref: /openbmc/sdbusplus/include/sdbusplus/async/stdexec/__detail/__upon_error.hpp (revision 10d0b4b7d1498cfd5c3d37edea271a54d1984e41)
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 "__execution_fwd.hpp"
19 
20 #include "__basic_sender.hpp"
21 #include "__diagnostics.hpp"
22 #include "__domain.hpp"
23 #include "__meta.hpp"
24 #include "__senders_core.hpp"
25 #include "__sender_adaptor_closure.hpp"
26 #include "__transform_completion_signatures.hpp"
27 #include "__transform_sender.hpp"
28 #include "__senders.hpp" // IWYU pragma: keep for __well_formed_sender
29 
30 // include these after __execution_fwd.hpp
31 namespace stdexec {
32   /////////////////////////////////////////////////////////////////////////////
33   // [execution.senders.adaptors.upon_error]
34   namespace __upon_error {
35     inline constexpr __mstring __upon_error_context =
36       "In stdexec::upon_error(Sender, Function)..."_mstr;
37     using __on_not_callable = __callable_error<__upon_error_context>;
38 
39     template <class _Fun, class _CvrefSender, class... _Env>
40     using __completion_signatures_t = transform_completion_signatures<
41       __completion_signatures_of_t<_CvrefSender, _Env...>,
42       __with_error_invoke_t<__on_not_callable, set_error_t, _Fun, _CvrefSender, _Env...>,
43       __sigs::__default_set_value,
44       __mbind_front<__mtry_catch_q<__set_value_invoke_t, __on_not_callable>, _Fun>::template __f
45     >;
46 
47     ////////////////////////////////////////////////////////////////////////////////////////////////
48     struct upon_error_t {
49       template <sender _Sender, __movable_value _Fun>
operator ()stdexec::__upon_error::upon_error_t50       auto operator()(_Sender&& __sndr, _Fun __fun) const -> __well_formed_sender auto {
51         auto __domain = __get_early_domain(__sndr);
52         return stdexec::transform_sender(
53           __domain,
54           __make_sexpr<upon_error_t>(static_cast<_Fun&&>(__fun), static_cast<_Sender&&>(__sndr)));
55       }
56 
57       template <__movable_value _Fun>
STDEXEC_ATTRIBUTEstdexec::__upon_error::upon_error_t58       STDEXEC_ATTRIBUTE(always_inline)
59       auto operator()(_Fun __fun) const -> __binder_back<upon_error_t, _Fun> {
60         return {{static_cast<_Fun&&>(__fun)}, {}, {}};
61       }
62     };
63 
64     struct __upon_error_impl : __sexpr_defaults {
65       static constexpr auto get_completion_signatures =
66         []<class _Sender, class... _Env>(_Sender&&, _Env&&...) noexcept
67         -> __completion_signatures_t<__decay_t<__data_of<_Sender>>, __child_of<_Sender>, _Env...> {
68         static_assert(sender_expr_for<_Sender, upon_error_t>);
69         return {};
70       };
71 
72       static constexpr auto complete =
73         []<class _Tag, class _State, class _Receiver, class... _Args>(
74           __ignore,
75           _State& __state,
76           _Receiver& __rcvr,
77           _Tag,
78           _Args&&... __args) noexcept -> void {
79         if constexpr (__same_as<_Tag, set_error_t>) {
80           stdexec::__set_value_invoke(
81             static_cast<_Receiver&&>(__rcvr),
82             static_cast<_State&&>(__state),
83             static_cast<_Args&&>(__args)...);
84         } else {
85           _Tag()(static_cast<_Receiver&&>(__rcvr), static_cast<_Args&&>(__args)...);
86         }
87       };
88     };
89   } // namespace __upon_error
90 
91   using __upon_error::upon_error_t;
92   inline constexpr upon_error_t upon_error{};
93 
94   template <>
95   struct __sexpr_impl<upon_error_t> : __upon_error::__upon_error_impl { };
96 } // namespace stdexec
97