From bf49cc7ffc1d5206cb5b671cbaf2d9e895f10b5a Mon Sep 17 00:00:00 2001 From: Radoslaw Zarzynski Date: Tue, 17 Sep 2019 20:31:26 +0200 Subject: [PATCH] crimson: introduce stateful errors to errorator. Signed-off-by: Radoslaw Zarzynski --- src/crimson/common/errorator.h | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/crimson/common/errorator.h b/src/crimson/common/errorator.h index 0cc06ea1887..fc91b85fff3 100644 --- a/src/crimson/common/errorator.h +++ b/src/crimson/common/errorator.h @@ -99,6 +99,47 @@ std::exception_ptr unthrowable_wrapper::carrier_instance = \ std::make_exception_ptr< unthrowable_wrapper::throwable_carrier>({}); + +template +struct stateful_error_t : error_t> { + template + explicit stateful_error_t(Args&&... args) + : ep(std::make_exception_ptr(std::forward(args)...)) { + } + + template + static auto handle(Func&& func) { + static_assert(std::is_invocable_v); + return [ + func = std::forward(func) + ] (stateful_error_t&& e) mutable -> decltype(auto) { + try { + std::rethrow_exception(e.ep); + } catch (const ErrorT& obj) { + return std::invoke(std::forward(func), obj); + } + assert("exception type mismatch – impossible!" == nullptr); + }; + } + +private: + std::exception_ptr ep; + + explicit stateful_error_t(std::exception_ptr ep) : ep(std::move(ep)) {} + + static constexpr const std::type_info& exception_ptr_type_info() { + return typeid(ErrorT); + } + auto to_exception_ptr() const { + return ep; + } + static stateful_error_t from_exception_ptr(std::exception_ptr ep) { + return stateful_error_t(std::move(ep)); + } + + friend class error_t>; +}; + namespace _impl { template struct always_false : std::false_type {}; }; @@ -542,4 +583,7 @@ namespace ct_error { using object_corrupted = unthrowable_wrapper<_impl::ct_error, _impl::ct_error::object_corrupted>; } +using stateful_errc = stateful_error_t; +using stateful_errint = stateful_error_t; + } // namespace crimson -- 2.39.5