File indexing completed on 2026-05-03 08:13:42
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___STOP_TOKEN_STOP_CALLBACK_H
0011 #define _LIBCPP___CXX03___STOP_TOKEN_STOP_CALLBACK_H
0012
0013 #include <__cxx03/__concepts/constructible.h>
0014 #include <__cxx03/__concepts/destructible.h>
0015 #include <__cxx03/__concepts/invocable.h>
0016 #include <__cxx03/__config>
0017 #include <__cxx03/__stop_token/intrusive_shared_ptr.h>
0018 #include <__cxx03/__stop_token/stop_state.h>
0019 #include <__cxx03/__stop_token/stop_token.h>
0020 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
0021 #include <__cxx03/__utility/forward.h>
0022 #include <__cxx03/__utility/move.h>
0023 #include <__cxx03/__utility/private_constructor_tag.h>
0024
0025 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0026 # pragma GCC system_header
0027 #endif
0028
0029 _LIBCPP_PUSH_MACROS
0030 #include <__cxx03/__undef_macros>
0031
0032 _LIBCPP_BEGIN_NAMESPACE_STD
0033
0034 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS)
0035
0036 template <class _Callback>
0037 class _LIBCPP_AVAILABILITY_SYNC stop_callback : private __stop_callback_base {
0038 static_assert(invocable<_Callback>,
0039 "Mandates: stop_callback is instantiated with an argument for the template parameter Callback that "
0040 "satisfies invocable.");
0041 static_assert(destructible<_Callback>,
0042 "Mandates: stop_callback is instantiated with an argument for the template parameter Callback that "
0043 "satisfies destructible.");
0044
0045 public:
0046 using callback_type = _Callback;
0047
0048 template <class _Cb>
0049 requires constructible_from<_Callback, _Cb>
0050 _LIBCPP_HIDE_FROM_ABI explicit stop_callback(const stop_token& __st,
0051 _Cb&& __cb) noexcept(is_nothrow_constructible_v<_Callback, _Cb>)
0052 : stop_callback(__private_constructor_tag{}, __st.__state_, std::forward<_Cb>(__cb)) {}
0053
0054 template <class _Cb>
0055 requires constructible_from<_Callback, _Cb>
0056 _LIBCPP_HIDE_FROM_ABI explicit stop_callback(stop_token&& __st,
0057 _Cb&& __cb) noexcept(is_nothrow_constructible_v<_Callback, _Cb>)
0058 : stop_callback(__private_constructor_tag{}, std::move(__st.__state_), std::forward<_Cb>(__cb)) {}
0059
0060 _LIBCPP_HIDE_FROM_ABI ~stop_callback() {
0061 if (__state_) {
0062 __state_->__remove_callback(this);
0063 }
0064 }
0065
0066 stop_callback(const stop_callback&) = delete;
0067 stop_callback(stop_callback&&) = delete;
0068 stop_callback& operator=(const stop_callback&) = delete;
0069 stop_callback& operator=(stop_callback&&) = delete;
0070
0071 private:
0072 _LIBCPP_NO_UNIQUE_ADDRESS _Callback __callback_;
0073 __intrusive_shared_ptr<__stop_state> __state_;
0074
0075 friend __stop_callback_base;
0076
0077 template <class _StatePtr, class _Cb>
0078 _LIBCPP_HIDE_FROM_ABI explicit stop_callback(__private_constructor_tag, _StatePtr&& __state, _Cb&& __cb) noexcept(
0079 is_nothrow_constructible_v<_Callback, _Cb>)
0080 : __stop_callback_base([](__stop_callback_base* __cb_base) noexcept {
0081
0082 std::forward<_Callback>(static_cast<stop_callback*>(__cb_base)->__callback_)();
0083 }),
0084 __callback_(std::forward<_Cb>(__cb)),
0085 __state_() {
0086 if (__state && __state->__add_callback(this)) {
0087
0088 __state_ = std::forward<_StatePtr>(__state);
0089 }
0090 }
0091 };
0092
0093 template <class _Callback>
0094 _LIBCPP_AVAILABILITY_SYNC stop_callback(stop_token, _Callback) -> stop_callback<_Callback>;
0095
0096 #endif
0097
0098 _LIBCPP_END_NAMESPACE_STD
0099
0100 _LIBCPP_POP_MACROS
0101
0102 #endif