From 23826669cf1fa8c6b60e4c6497aff9be36bf48ae Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 31 Dec 2023 07:37:59 -0800 Subject: [PATCH] Cleanup error handling --- include/fmt/core.h | 2 ++ include/fmt/format.h | 73 ++++++++++++++++++-------------------------- test/xchar-test.cc | 2 +- 3 files changed, 32 insertions(+), 45 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 1e0503db..6fa22d3a 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -644,6 +644,7 @@ enum { pointer_set = set(type::pointer_type) }; +// DEPRECATED! FMT_NORETURN FMT_API void throw_format_error(const char* message); struct error_handler { @@ -1762,6 +1763,7 @@ template class basic_format_context { } auto args() const -> const format_args& { return args_; } + // DEPRECATED! FMT_CONSTEXPR auto error_handler() -> detail::error_handler { return {}; } void on_error(const char* message) { error_handler().on_error(message); } diff --git a/include/fmt/format.h b/include/fmt/format.h index ded5d513..bc0d4762 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2430,9 +2430,8 @@ struct float_specs { bool showpoint : 1; }; -template -FMT_CONSTEXPR auto parse_float_type_spec(const format_specs& specs, - ErrorHandler&& eh = {}) +template +FMT_CONSTEXPR auto parse_float_type_spec(const format_specs& specs) -> float_specs { auto result = float_specs(); result.showpoint = specs.alt; @@ -2468,7 +2467,7 @@ FMT_CONSTEXPR auto parse_float_type_spec(const format_specs& specs, result.format = float_format::hex; break; default: - eh.on_error("invalid format specifier"); + throw_format_error("invalid format specifier"); break; } return result; @@ -3813,51 +3812,39 @@ template struct custom_formatter { template void operator()(T) const {} }; -template class width_checker { - public: - explicit FMT_CONSTEXPR width_checker(ErrorHandler& eh) : handler_(eh) {} - +struct width_checker { template ::value)> FMT_CONSTEXPR auto operator()(T value) -> unsigned long long { - if (is_negative(value)) handler_.on_error("negative width"); + if (is_negative(value)) throw_format_error("negative width"); return static_cast(value); } template ::value)> FMT_CONSTEXPR auto operator()(T) -> unsigned long long { - handler_.on_error("width is not integer"); + throw_format_error("width is not integer"); return 0; } - - private: - ErrorHandler& handler_; }; -template class precision_checker { - public: - explicit FMT_CONSTEXPR precision_checker(ErrorHandler& eh) : handler_(eh) {} - +struct precision_checker { template ::value)> FMT_CONSTEXPR auto operator()(T value) -> unsigned long long { - if (is_negative(value)) handler_.on_error("negative precision"); + if (is_negative(value)) throw_format_error("negative precision"); return static_cast(value); } template ::value)> FMT_CONSTEXPR auto operator()(T) -> unsigned long long { - handler_.on_error("precision is not integer"); + throw_format_error("precision is not integer"); return 0; } - - private: - ErrorHandler& handler_; }; -template