ThreadSchedule 1.0.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>

Inheritance diagram for threadschedule::FutureWithErrorHandler< T >:
[legend]

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

Constructor & Destructor Documentation

◆ FutureWithErrorHandler()

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

Definition at line 297 of file error_handler.hpp.

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.
Exceptions
Anyexception stored in the underlying std::future.

Definition at line 333 of file error_handler.hpp.

◆ on_error()

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

◆ 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 390 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 354 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 367 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 380 of file error_handler.hpp.


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