Skip to content

Commit

Permalink
Cleanup error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Dec 31, 2023
1 parent 8e6b254 commit 2db638f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
2 changes: 2 additions & 0 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1762,6 +1763,7 @@ template <typename OutputIt, typename Char> 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); }

Expand Down
42 changes: 15 additions & 27 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3813,51 +3813,41 @@ template <typename Char> struct custom_formatter {
template <typename T> void operator()(T) const {}
};

template <typename ErrorHandler> class width_checker {
public:
explicit FMT_CONSTEXPR width_checker(ErrorHandler& eh) : handler_(eh) {}

struct width_checker {
template <typename T, FMT_ENABLE_IF(is_integer<T>::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<unsigned long long>(value);
}

template <typename T, FMT_ENABLE_IF(!is_integer<T>::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 <typename ErrorHandler> class precision_checker {
public:
explicit FMT_CONSTEXPR precision_checker(ErrorHandler& eh) : handler_(eh) {}
struct precision_checker {
constexpr precision_checker() {}

template <typename T, FMT_ENABLE_IF(is_integer<T>::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<unsigned long long>(value);
}

template <typename T, FMT_ENABLE_IF(!is_integer<T>::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 <template <typename> class Handler, typename FormatArg,
typename ErrorHandler>
FMT_CONSTEXPR auto get_dynamic_spec(FormatArg arg, ErrorHandler eh) -> int {
unsigned long long value = visit_format_arg(Handler<ErrorHandler>(eh), arg);
if (value > to_unsigned(max_value<int>())) eh.on_error("number is too big");
template <typename Handler, typename FormatArg>
FMT_CONSTEXPR auto get_dynamic_spec(FormatArg arg) -> int {
unsigned long long value = visit_format_arg(Handler(), arg);
if (value > to_unsigned(max_value<int>()))
throw_format_error("number is too big");
return static_cast<int>(value);
}

Expand All @@ -3868,20 +3858,18 @@ FMT_CONSTEXPR auto get_arg(Context& ctx, ID id) -> decltype(ctx.arg(id)) {
return arg;
}

template <template <typename> class Handler, typename Context>
template <typename Handler, typename Context>
FMT_CONSTEXPR void handle_dynamic_spec(int& value,
arg_ref<typename Context::char_type> ref,
Context& ctx) {
switch (ref.kind) {
case arg_id_kind::none:
break;
case arg_id_kind::index:
value = detail::get_dynamic_spec<Handler>(get_arg(ctx, ref.val.index),
ctx.error_handler());
value = detail::get_dynamic_spec<Handler>(get_arg(ctx, ref.val.index));
break;
case arg_id_kind::name:
value = detail::get_dynamic_spec<Handler>(get_arg(ctx, ref.val.name),
ctx.error_handler());
value = detail::get_dynamic_spec<Handler>(get_arg(ctx, ref.val.name));
break;
}
}
Expand Down

0 comments on commit 2db638f

Please sign in to comment.