xref: /openbmc/sdbusplus/include/sdbusplus/async/stdexec/__detail/__stop_token.hpp (revision 10d0b4b7d1498cfd5c3d37edea271a54d1984e41)
1 /*
2  * Copyright (c) 2021-2022 Facebook, Inc. and its affiliates
3  * Copyright (c) 2021-2024 NVIDIA Corporation
4  *
5  * Licensed under the Apache License Version 2.0 with LLVM Exceptions
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  *   https://llvm.org/LICENSE.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #pragma once
18 
19 #include "__execution_fwd.hpp"
20 
21 #include "__concepts.hpp"
22 
23 namespace stdexec {
24   namespace __stok {
25     template <template <class> class>
26     struct __check_type_alias_exists;
27   } // namespace __stok
28 
29   template <class _Token, class _Callback>
30   using stop_callback_for_t = _Token::template callback_type<_Callback>;
31 
32   template <class _Token>
33   concept stoppable_token =
34     __nothrow_copy_constructible<_Token> && __nothrow_move_constructible<_Token>
35     && equality_comparable<_Token> && requires(const _Token& __token) {
36          { __token.stop_requested() } noexcept -> __boolean_testable_;
37          { __token.stop_possible() } noexcept -> __boolean_testable_;
38     // workaround ICE in appleclang 13.1
39 #if !defined(__clang__)
40          typename __stok::__check_type_alias_exists<_Token::template callback_type>;
41 #endif
42        };
43 
44   template <class _Token, typename _Callback, typename _Initializer = _Callback>
45   concept stoppable_token_for =
46     stoppable_token<_Token> && __callable<_Callback>
47     && requires { typename stop_callback_for_t<_Token, _Callback>; }
48     && constructible_from<_Callback, _Initializer>
49     && constructible_from<stop_callback_for_t<_Token, _Callback>, const _Token&, _Initializer>;
50 
51   template <class _Token>
52   concept unstoppable_token = stoppable_token<_Token> && requires {
53     { _Token::stop_possible() } -> __boolean_testable_;
54   } && (!_Token::stop_possible());
55 
56   // [stoptoken.never], class never_stop_token
57   struct never_stop_token {
58    private:
59     struct __callback_type {
__callback_typestdexec::never_stop_token::__callback_type60       explicit __callback_type(never_stop_token, __ignore) noexcept {
61       }
62     };
63    public:
64     template <class>
65     using callback_type = __callback_type;
66 
stop_requestedstdexec::never_stop_token67     static constexpr auto stop_requested() noexcept -> bool {
68       return false;
69     }
70 
stop_possiblestdexec::never_stop_token71     static constexpr auto stop_possible() noexcept -> bool {
72       return false;
73     }
74 
75     auto operator==(const never_stop_token&) const noexcept -> bool = default;
76   };
77 } // namespace stdexec
78