ThreadSchedule 2.2.0
Modern C++ thread management library
Loading...
Searching...
No Matches
threadschedule::FutureWithErrorHandler< T > Class Template Reference

A move-only future wrapper that supports an error callback. More...

#include <error_handler.hpp>

Public Member Functions

 FutureWithErrorHandler (std::future< T > future)
 FutureWithErrorHandler (FutureWithErrorHandler const &)=delete
auto operator= (FutureWithErrorHandler const &) -> FutureWithErrorHandler &=delete
 FutureWithErrorHandler (FutureWithErrorHandler &&)=default
auto operator= (FutureWithErrorHandler &&) -> FutureWithErrorHandler &=default
auto on_error (std::function< void(std::exception_ptr)> callback) -> FutureWithErrorHandler &
 Attach an error callback.
template<typename Callback, std::enable_if_t<!std::is_same_v< detail::remove_cvref_t< Callback >, std::function< void(std::exception_ptr)> >, int > = 0>
auto on_error (Callback &&callback) -> FutureWithErrorHandler &
auto get () -> T
 Retrieve the result, invoking the error callback on failure.
void wait () const
 Block until the result is ready.
template<typename Rep, typename Period>
auto wait_for (std::chrono::duration< Rep, Period > const &timeout_duration) const
 Block until the result is ready or the timeout elapses.
template<typename Clock, typename Duration>
auto wait_until (std::chrono::time_point< Clock, Duration > const &timeout_time) const
 Block until the result is ready or the given time point is reached.
auto valid () const -> bool
 Check whether the future refers to a shared state.

Detailed Description

template<typename T>
class threadschedule::FutureWithErrorHandler< T >

A move-only future wrapper that supports an error callback.

FutureWithErrorHandler<T> wraps a std::future<T> and adds an optional error callback that fires when get() encounters an exception.

Template Parameters
TThe value type of the underlying future.
Move semantics
Like std::future, this type is move-only (copy construction and copy assignment are deleted).
Error callback behaviour
  • Attach a callback with on_error(). At most one callback is supported; a subsequent call to on_error() replaces the previous callback.
  • The callback is invoked before the exception is re-thrown from get().
  • wait(), wait_for(), and wait_until() do not trigger the error callback.
  • valid() delegates directly to the underlying std::future::valid().

Definition at line 353 of file error_handler.hpp.

Constructor & Destructor Documentation

◆ FutureWithErrorHandler() [1/3]

template<typename T>
threadschedule::FutureWithErrorHandler< T >::FutureWithErrorHandler ( std::future< T > future)
inlineexplicit

◆ FutureWithErrorHandler() [2/3]

template<typename T>
threadschedule::FutureWithErrorHandler< T >::FutureWithErrorHandler ( FutureWithErrorHandler< T > const & )
delete

◆ FutureWithErrorHandler() [3/3]

template<typename T>
threadschedule::FutureWithErrorHandler< T >::FutureWithErrorHandler ( FutureWithErrorHandler< T > && )
default

Member Function Documentation

◆ get()

template<typename T>
auto threadschedule::FutureWithErrorHandler< T >::get ( ) -> T
inline

Retrieve the result, invoking the error callback on failure.

If the underlying future holds an exception, the error callback (if any) is called before the exception is re-thrown to the caller.

Returns
The stored value of type T (void when T is void).
Exceptions
Anyexception stored in the underlying std::future.

Definition at line 404 of file error_handler.hpp.

◆ on_error() [1/2]

template<typename T>
template<typename Callback, std::enable_if_t<!std::is_same_v< detail::remove_cvref_t< Callback >, std::function< void(std::exception_ptr)> >, int > = 0>
auto threadschedule::FutureWithErrorHandler< T >::on_error ( Callback && callback) -> FutureWithErrorHandler&
inline

◆ on_error() [2/2]

template<typename T>
auto threadschedule::FutureWithErrorHandler< T >::on_error ( std::function< void(std::exception_ptr)> callback) -> FutureWithErrorHandler&
inline

Attach an error callback.

The callback will be invoked with the current std::exception_ptr if get() encounters an exception. Only one callback is stored; calling on_error() again replaces the previous callback.

Parameters
callbackCallable invoked with the exception pointer on failure.
Returns
Reference to *this, allowing fluent chaining.

Definition at line 376 of file error_handler.hpp.

References FutureWithErrorHandler().

◆ operator=() [1/2]

template<typename T>
auto threadschedule::FutureWithErrorHandler< T >::operator= ( FutureWithErrorHandler< T > && ) -> FutureWithErrorHandler &=default
default

◆ operator=() [2/2]

template<typename T>
auto threadschedule::FutureWithErrorHandler< T >::operator= ( FutureWithErrorHandler< T > const & ) -> FutureWithErrorHandler &=delete
delete

◆ valid()

template<typename T>
auto threadschedule::FutureWithErrorHandler< T >::valid ( ) const -> bool
inlinenodiscard

Check whether the future refers to a shared state.

Delegates directly to std::future::valid().

Definition at line 464 of file error_handler.hpp.

◆ wait()

template<typename T>
void threadschedule::FutureWithErrorHandler< T >::wait ( ) const
inline

Block until the result is ready.

Does not trigger the error callback regardless of the stored state.

Definition at line 428 of file error_handler.hpp.

◆ wait_for()

template<typename T>
template<typename Rep, typename Period>
auto threadschedule::FutureWithErrorHandler< T >::wait_for ( std::chrono::duration< Rep, Period > const & timeout_duration) const
inline

Block until the result is ready or the timeout elapses.

Does not trigger the error callback.

Returns
The std::future_status indicating whether the result is ready.

Definition at line 441 of file error_handler.hpp.

◆ wait_until()

template<typename T>
template<typename Clock, typename Duration>
auto threadschedule::FutureWithErrorHandler< T >::wait_until ( std::chrono::time_point< Clock, Duration > const & timeout_time) const
inline

Block until the result is ready or the given time point is reached.

Does not trigger the error callback.

Returns
The std::future_status indicating whether the result is ready.

Definition at line 454 of file error_handler.hpp.


The documentation for this class was generated from the following file: