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

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

#include <error_handler.hpp>

Public Member Functions

 future_with_error_handler (std::future< T > future)
 
 future_with_error_handler (future_with_error_handler const &)=delete
 
auto operator= (future_with_error_handler const &) -> future_with_error_handler &=delete
 
 future_with_error_handler (future_with_error_handler &&)=default
 
auto operator= (future_with_error_handler &&) -> future_with_error_handler &=default
 
auto on_error (std::function< void(std::exception_ptr)> callback) -> future_with_error_handler &
 Attach an error callback.
 
template<typename Callback , std::enable_if_t<!std::is_same_v<::threadschedule::detail::remove_cvref_t< Callback >, std::function< void(std::exception_ptr)> >, int > = 0>
auto on_error (Callback &&callback) -> future_with_error_handler &
 
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::advanced::future_with_error_handler< T >

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

future_with_error_handler<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 285 of file error_handler.hpp.

Constructor & Destructor Documentation

◆ future_with_error_handler() [1/3]

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

Definition at line 288 of file error_handler.hpp.

◆ future_with_error_handler() [2/3]

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

◆ future_with_error_handler() [3/3]

template<typename T >
threadschedule::advanced::future_with_error_handler< T >::future_with_error_handler ( future_with_error_handler< T > &&  )
default

Member Function Documentation

◆ get()

template<typename T >
auto threadschedule::advanced::future_with_error_handler< 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. Exceptions from the callback are ignored so they cannot replace the original failure.

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

Definition at line 341 of file error_handler.hpp.

◆ on_error() [1/2]

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

Definition at line 320 of file error_handler.hpp.

◆ on_error() [2/2]

template<typename T >
auto threadschedule::advanced::future_with_error_handler< T >::on_error ( std::function< void(std::exception_ptr)>  callback) -> future_with_error_handler&
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 309 of file error_handler.hpp.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ valid()

template<typename T >
auto threadschedule::advanced::future_with_error_handler< T >::valid ( ) const -> bool
inline

Check whether the future refers to a shared state.

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

Definition at line 412 of file error_handler.hpp.

◆ wait()

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

Block until the result is ready.

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

Definition at line 373 of file error_handler.hpp.

◆ wait_for()

template<typename T >
template<typename Rep , typename Period >
auto threadschedule::advanced::future_with_error_handler< 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 387 of file error_handler.hpp.

◆ wait_until()

template<typename T >
template<typename Clock , typename duration >
auto threadschedule::advanced::future_with_error_handler< 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 401 of file error_handler.hpp.


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