9#include "../detail/callable/copyable_function.hpp"
10#include "../detail/callable/move_only_function.hpp"
11#include "../task_error.hpp"
25namespace error_handler_detail
70 template <
typename Callback,
71 std::enable_if_t<!std::is_same_v<::threadschedule::detail::remove_cvref_t<Callback>,
error_callback>,
int>
76 static_assert(std::is_invocable_r_v<void, Callback&, task_error const&>,
77 "Error callback must be invocable with task_error const&");
78 return emplace_callback(
91 std::lock_guard<std::mutex> lock(mutex_);
92 return callbacks_.erase(
id) > 0;
101 std::lock_guard<std::mutex> lock(mutex_);
102 return callbacks_.count(
id) > 0;
114 std::lock_guard<std::mutex> lock(mutex_);
130 std::vector<error_handler_detail::error_callback_storage> snapshot;
132 std::lock_guard<std::mutex> lock(mutex_);
134 snapshot.reserve(callbacks_.size());
135 for (
auto const& [
id, callback] : callbacks_)
136 snapshot.push_back(callback);
139 for (
auto& callback : snapshot)
162 std::lock_guard<std::mutex> lock(mutex_);
172 std::lock_guard<std::mutex> lock(mutex_);
180 std::lock_guard<std::mutex> lock(mutex_);
181 size_t const id = next_callback_id_++;
182 callbacks_.emplace(
id, std::move(callback));
186 mutable std::mutex mutex_;
187 std::map<size_t, error_handler_detail::error_callback_storage> callbacks_;
188 size_t next_callback_id_{ 0 };
189 size_t error_count_{ 0 };
214template <
typename Func>
219 : func_(
std::move(func)), handler_(
std::move(handler)), description_(
std::move(description))
239 std::shared_ptr<error_handler> handler_;
240 std::string description_;
255template <
typename Func>
259 using function_type = std::decay_t<Func>;
261 std::move(description));
289 : future_(
std::move(future)), error_callback_(nullptr), has_callback_(false)
312 has_callback_ =
true;
316 template <
typename Callback, std::enable_if_t<!std::is_same_v<::threadschedule::detail::remove_cvref_t<Callback>,
317 std::function<
void(std::exception_ptr)>>,
322 static_assert(std::is_invocable_r_v<void, Callback&, std::exception_ptr>,
323 "Error callback must be invocable with std::exception_ptr");
325 = ::threadschedule::detail::make_move_only_function<void(std::exception_ptr)>(std::forward<Callback>(callback));
326 has_callback_ =
true;
345 if constexpr (std::is_void_v<T>)
348 return future_.get();
352 auto const original = std::current_exception();
353 if (has_callback_ && error_callback_)
357 error_callback_(original);
363 std::rethrow_exception(original);
385 template <
typename Rep,
typename Period>
387 wait_for(std::chrono::duration<Rep, Period>
const& timeout_duration)
const
389 return future_.wait_for(timeout_duration);
399 template <
typename Clock,
typename duration>
401 wait_until(std::chrono::time_point<Clock, duration>
const& timeout_time)
const
403 return future_.wait_until(timeout_time);
414 return future_.valid();
418 std::future<T> future_;
420 bool has_callback_{
false };
Callable wrapper that catches exceptions and routes them to an error_handler.
error_handled_task(Func func, std::shared_ptr< error_handler > handler, std::string description="")
Central registry and dispatcher for task-error callbacks.
auto remove_callback(size_t id) -> bool
Remove a single callback by its ID.
void reset_error_count()
Reset the cumulative error count to zero.
auto error_count() const -> size_t
Return the total number of errors handled since the last reset.
void clear_callbacks()
Remove all registered error callbacks.
void handle_error(task_error const &error)
Dispatch an error to all registered callbacks.
auto add_callback(Callback &&callback) -> size_t
auto has_callback(size_t id) const -> bool
Check whether a callback with the given ID is registered.
auto add_callback(error_callback callback) -> size_t
Register an error callback.
A move-only future wrapper that supports an error callback.
void wait() const
Block until the result is ready.
auto operator=(future_with_error_handler const &) -> future_with_error_handler &=delete
auto operator=(future_with_error_handler &&) -> future_with_error_handler &=default
auto wait_for(std::chrono::duration< Rep, Period > const &timeout_duration) const
Block until the result is ready or the timeout elapses.
auto on_error(std::function< void(std::exception_ptr)> callback) -> future_with_error_handler &
Attach an error callback.
auto get() -> T
Retrieve the result, invoking the error callback on failure.
auto on_error(Callback &&callback) -> future_with_error_handler &
future_with_error_handler(future_with_error_handler const &)=delete
future_with_error_handler(future_with_error_handler &&)=default
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.
future_with_error_handler(std::future< T > future)
auto valid() const -> bool
Check whether the future refers to a shared state.
::threadschedule::detail::move_only_function< void(std::exception_ptr)> future_error_callback
::threadschedule::detail::copyable_function< void(task_error const &)> error_callback_storage
auto make_error_handled_task(Func &&func, std::shared_ptr< error_handler > handler, std::string description="")
Factory function that creates an error_handled_task with perfect forwarding.
auto make_copyable_function(Callable &&callable) -> copyable_function< Signature >
std::function< Signature > copyable_function
std::function< void(task_error const &)> error_callback
Callback signature for asynchronous task error reporting.
Captured information about a task failure.
static auto capture(std::string description={}) -> task_error
Capture failure context for the current exception state.