91 [[nodiscard]] auto
get() -> common::Result<R> {
93 return make_error_result<R>(error_code::operation_canceled);
96 return common::Result<R>::ok(future_.get());
97 } catch (
const std::exception& e) {
114 [[nodiscard]]
auto get_for(std::chrono::milliseconds timeout)
115 -> common::Result<std::optional<R>>
121 auto status =
future_.wait_for(timeout);
122 if (status == std::future_status::ready) {
124 return common::Result<std::optional<R>>::ok(
future_.get());
125 }
catch (
const std::exception& e) {
133 return common::Result<std::optional<R>>::ok(std::nullopt);
142 return future_.wait_for(std::chrono::seconds(0)) ==
143 std::future_status::ready;
171 [[nodiscard]]
auto valid() const ->
bool {
190 template<
typename Rep,
typename Period>
191 [[nodiscard]]
auto wait_for(
const std::chrono::duration<Rep, Period>& timeout)
const
192 -> std::future_status
194 return future_.wait_for(timeout);
231 [[nodiscard]] auto
get() -> common::VoidResult {
233 return make_error_result(error_code::operation_canceled);
238 } catch (
const std::exception& e) {
245 [[nodiscard]]
auto get_for(std::chrono::milliseconds timeout) -> common::Result<bool> {
250 auto status =
future_.wait_for(timeout);
251 if (status == std::future_status::ready) {
254 return common::Result<bool>::ok(
true);
255 }
catch (
const std::exception& e) {
261 return common::Result<bool>::ok(
false);
265 return future_.wait_for(std::chrono::seconds(0)) ==
266 std::future_status::ready;
277 [[nodiscard]]
auto valid() const ->
bool {
285 template<
typename Rep,
typename Period>
286 [[nodiscard]]
auto wait_for(
const std::chrono::duration<Rep, Period>& timeout)
const
287 -> std::future_status
289 return future_.wait_for(timeout);
auto get_token() const -> cancellation_token
auto valid() const -> bool
cancellation_token token_
auto is_cancelled() const -> bool
~cancellable_future()=default
cancellable_future & operator=(const cancellable_future &)=delete
auto is_ready() const -> bool
auto get_for(std::chrono::milliseconds timeout) -> common::Result< bool >
cancellable_future(std::future< void > future, cancellation_token token)
cancellable_future(const cancellable_future &)=delete
cancellable_future(cancellable_future &&) noexcept=default
auto wait_for(const std::chrono::duration< Rep, Period > &timeout) const -> std::future_status
std::future< void > future_
A future wrapper that supports cancellation.
auto wait_for(const std::chrono::duration< Rep, Period > &timeout) const -> std::future_status
Wait for the result with timeout.
~cancellable_future()=default
auto get() -> common::Result< R >
Wait for and retrieve the result.
auto is_cancelled() const -> bool
Check if the operation was cancelled.
void wait() const
Wait for the result to become ready.
cancellable_future(cancellable_future &&) noexcept=default
cancellable_future(const cancellable_future &)=delete
auto valid() const -> bool
Check if the future is valid.
void cancel()
Request cancellation of the operation.
cancellation_token token_
cancellable_future & operator=(const cancellable_future &)=delete
cancellable_future(std::future< R > future, cancellation_token token)
Construct a cancellable_future.
auto is_ready() const -> bool
Check if the result is ready.
auto get_token() const -> cancellation_token
Get the cancellation token.
auto get_for(std::chrono::milliseconds timeout) -> common::Result< std::optional< R > >
Wait for the result with timeout.
Provides a mechanism for cooperative cancellation of operations.
void cancel()
Cancels the operation.
bool is_cancelled() const
Checks if the token has been canceled.
Implementation of a cancellation token for cooperative cancellation.
Error codes and utilities for the thread system.
Core threading foundation of the thread system library.
common::VoidResult make_error_result(error_code code, const std::string &message="")
Create a common::VoidResult error from a thread::error_code.