Skip to content

Commit 89f1e58

Browse files
committed
stdexec::stoppable_token & ::stop_token_for_t: Support std::stop_token
Both the stdexec::stoppable_token concept and the stdexec:: stop_token_for_t template type alias depend on the stop token type having a unary member template named callback_type. While P2300 added the aforementioned member template to std::stop_token that means that before C++26 the aforementioned utilities don't work therewith. Special cased: - stdexec::stoppable_token to allow std::stop_token without checking for the above-mentioned member template, and - stdexec::stop_token_t to resolve to std::stop_callback<Callback> when its first template argument is std::stop_token
1 parent 5ffee46 commit 89f1e58

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

include/stdexec/__detail/__stop_token.hpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,40 @@
2020

2121
#include "__concepts.hpp"
2222

23+
#include <stop_token>
24+
2325
namespace stdexec {
2426
namespace __stok {
2527
template <template <class> class>
2628
struct __check_type_alias_exists;
2729
} // namespace __stok
2830

2931
template <class _Token, class _Callback>
30-
using stop_callback_for_t = _Token::template callback_type<_Callback>;
32+
struct __stop_callback_for {
33+
using __t = _Token::template callback_type<_Callback>;
34+
};
35+
template <class _Callback>
36+
struct __stop_callback_for<std::stop_token, _Callback> {
37+
using __t = std::stop_callback<_Callback>;
38+
};
39+
40+
template <class _Token, class _Callback>
41+
using stop_callback_for_t = __stop_callback_for<_Token, _Callback>::__t;
3142

3243
template <class _Token>
3344
concept stoppable_token =
3445
__nothrow_copy_constructible<_Token> && __nothrow_move_constructible<_Token>
3546
&& equality_comparable<_Token> && requires(const _Token& __token) {
3647
{ __token.stop_requested() } noexcept -> __boolean_testable_;
3748
{ __token.stop_possible() } noexcept -> __boolean_testable_;
38-
// workaround ICE in appleclang 13.1
49+
}
50+
// workaround ICE in appleclang 13.1
3951
#if !defined(__clang__)
52+
&& (__same_as<_Token, std::stop_token> || requires {
4053
typename __stok::__check_type_alias_exists<_Token::template callback_type>;
54+
})
4155
#endif
42-
};
56+
;
4357

4458
template <class _Token, typename _Callback, typename _Initializer = _Callback>
4559
concept stoppable_token_for =

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(stdexec_test_sources
2929
stdexec/concepts/test_concepts_receiver.cpp
3030
stdexec/concepts/test_concept_operation_state.cpp
3131
stdexec/concepts/test_concepts_sender.cpp
32+
stdexec/concepts/test_concepts_stop_tokens.cpp
3233
stdexec/concepts/test_awaitables.cpp
3334
stdexec/algos/factories/test_just.cpp
3435
stdexec/algos/factories/test_transfer_just.cpp
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* Copyright (c) 2025 Robert Leahy. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
*
6+
* Licensed under the Apache License, Version 2.0 with LLVM Exceptions (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://llvm.org/LICENSE.txt
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <stdexec/execution.hpp>
20+
21+
#include <stop_token>
22+
#include <type_traits>
23+
24+
namespace {
25+
26+
struct on_stop_request {
27+
void operator()() && noexcept {
28+
}
29+
};
30+
31+
static_assert(::stdexec::stoppable_token<::stdexec::never_stop_token>);
32+
static_assert(::stdexec::unstoppable_token<::stdexec::never_stop_token>);
33+
static_assert(::stdexec::stoppable_token<::stdexec::inplace_stop_token>);
34+
static_assert(!::stdexec::unstoppable_token<::stdexec::inplace_stop_token>);
35+
static_assert(std::is_same_v<
36+
::stdexec::stop_callback_for_t<::stdexec::never_stop_token, on_stop_request>,
37+
::stdexec::never_stop_token::callback_type<on_stop_request>>);
38+
static_assert(::stdexec::stoppable_token<std::stop_token>);
39+
static_assert(std::is_same_v<
40+
::stdexec::stop_callback_for_t<std::stop_token, on_stop_request>,
41+
std::stop_callback<on_stop_request>>);
42+
43+
} // namespace

0 commit comments

Comments
 (0)