ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
threadschedule::advanced::error_handler Class Reference

Central registry and dispatcher for task-error callbacks. More...

#include <error_handler.hpp>

Public Member Functions

auto add_callback (error_callback callback) -> size_t
 Register an error callback.
 
template<typename Callback , std::enable_if_t<!std::is_same_v<::threadschedule::detail::remove_cvref_t< Callback >, error_callback >, int > = 0>
auto add_callback (Callback &&callback) -> size_t
 
auto remove_callback (size_t id) -> bool
 Remove a single callback by its ID.
 
auto has_callback (size_t id) const -> bool
 Check whether a callback with the given ID is registered.
 
void clear_callbacks ()
 Remove all registered error callbacks.
 
void handle_error (task_error const &error)
 Dispatch an error to all registered callbacks.
 
auto error_count () const -> size_t
 Return the total number of errors handled since the last reset.
 
void reset_error_count ()
 Reset the cumulative error count to zero.
 

Detailed Description

Central registry and dispatcher for task-error callbacks.

error_handler maintains an ordered list of error_callback functions and invokes them whenever a task reports a failure through handle_error().

Thread safety
All public methods are guarded by an internal std::mutex, so the handler can be shared across threads (typically via std::shared_ptr).
Callback execution
  • Callbacks are invoked in the order they were registered (FIFO).
  • The callback list is copied under the lock, then callbacks run without holding the handler mutex.
  • If a callback itself throws, the exception is silently swallowed so that remaining callbacks still execute.
Callback management
add_callback() returns a stable ID that can be passed to remove_callback() to unregister a single callback. clear_callbacks() removes all at once. The error count returned by error_count() is monotonically increasing and is only reset by an explicit call to reset_error_count().

Definition at line 55 of file error_handler.hpp.

Member Function Documentation

◆ add_callback() [1/2]

template<typename Callback , std::enable_if_t<!std::is_same_v<::threadschedule::detail::remove_cvref_t< Callback >, error_callback >, int > = 0>
auto threadschedule::advanced::error_handler::add_callback ( Callback &&  callback) -> size_t
inline

◆ add_callback() [2/2]

auto threadschedule::advanced::error_handler::add_callback ( error_callback  callback) -> size_t
inline

Register an error callback.

Parameters
callbackCallable to invoke when a task throws.
Returns
Stable ID for the callback, usable with remove_callback().

Definition at line 65 of file error_handler.hpp.

◆ clear_callbacks()

void threadschedule::advanced::error_handler::clear_callbacks ( )
inline

Remove all registered error callbacks.

After this call, handle_error() will still increment the error count but no callbacks will be invoked.

Definition at line 112 of file error_handler.hpp.

◆ error_count()

auto threadschedule::advanced::error_handler::error_count ( ) const -> size_t
inline

Return the total number of errors handled since the last reset.

The count is monotonically increasing and is only set back to zero by an explicit call to reset_error_count().

Returns
Cumulative number of handle_error() invocations.

Definition at line 160 of file error_handler.hpp.

◆ handle_error()

void threadschedule::advanced::error_handler::handle_error ( task_error const &  error)
inline

Dispatch an error to all registered callbacks.

Increments the internal error counter and then invokes every registered callback in insertion order. If any callback throws, the exception is caught and silently discarded so that subsequent callbacks still run.

Parameters
errorDiagnostic information about the failed task.

Definition at line 128 of file error_handler.hpp.

◆ has_callback()

auto threadschedule::advanced::error_handler::has_callback ( size_t  id) const -> bool
inline

Check whether a callback with the given ID is registered.

Definition at line 99 of file error_handler.hpp.

◆ remove_callback()

auto threadschedule::advanced::error_handler::remove_callback ( size_t  id) -> bool
inline

Remove a single callback by its ID.

Parameters
idThe ID returned by add_callback().
Returns
true if the callback was found and removed, false otherwise.

Definition at line 89 of file error_handler.hpp.

◆ reset_error_count()

void threadschedule::advanced::error_handler::reset_error_count ( )
inline

Reset the cumulative error count to zero.

Definition at line 170 of file error_handler.hpp.


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