From d142579e977bdcae86daf2f4d22c052e497d6651 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 2 Jun 2021 16:13:39 -0700 Subject: [PATCH] Cleanup the format API --- include/fmt/format.h | 37 ++++++++++++++++++------------------- include/fmt/xchar.h | 2 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 65d2c251..8d422fbd 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2237,7 +2237,7 @@ FMT_API std::system_error vsystem_error(int error_code, string_view format_str, /** \rst Constructs :class:`std::system_error` with a message formatted with - ``fmt::format(message, args...)``. + ``fmt::format(fmt, args...)``. *error_code* is a system error code as given by ``errno``. **Example**:: @@ -2251,10 +2251,10 @@ FMT_API std::system_error vsystem_error(int error_code, string_view format_str, throw fmt::system_error(errno, "cannot open file '{}'", filename); \endrst */ -template -std::system_error system_error(int error_code, string_view message, - const Args&... args) { - return vsystem_error(error_code, message, fmt::make_format_args(args...)); +template +auto system_error(int error_code, format_string fmt, T&&... args) + -> std::system_error { + return vsystem_error(error_code, fmt, fmt::make_format_args(args...)); } /** @@ -2413,12 +2413,22 @@ struct formatter : formatter, Char> { // }; template class dynamic_formatter { private: + detail::dynamic_format_specs specs_; + const Char* format_str_; + struct null_handler : detail::error_handler { void on_align(align_t) {} void on_sign(sign_t) {} void on_hash() {} }; + template void handle_specs(Context& ctx) { + detail::handle_dynamic_spec(specs_.width, + specs_.width_ref, ctx); + detail::handle_dynamic_spec( + specs_.precision, specs_.precision_ref, ctx); + } + public: template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { @@ -2439,17 +2449,6 @@ template class dynamic_formatter { if (specs_.precision >= 0) checker.end_precision(); return detail::write(ctx.out(), val, specs_, ctx.locale()); } - - private: - template void handle_specs(Context& ctx) { - detail::handle_dynamic_spec(specs_.width, - specs_.width_ref, ctx); - detail::handle_dynamic_spec( - specs_.precision, specs_.precision_ref, ctx); - } - - detail::dynamic_format_specs specs_; - const Char* format_str_; }; /** @@ -2461,14 +2460,14 @@ template class dynamic_formatter { auto s = fmt::format("{}", fmt::ptr(p)); \endrst */ -template const void* ptr(T p) { +template auto ptr(T p) -> const void* { static_assert(std::is_pointer::value, ""); return detail::bit_cast(p); } -template const void* ptr(const std::unique_ptr& p) { +template auto ptr(const std::unique_ptr& p) -> const void* { return p.get(); } -template const void* ptr(const std::shared_ptr& p) { +template auto ptr(const std::shared_ptr& p) -> const void* { return p.get(); } diff --git a/include/fmt/xchar.h b/include/fmt/xchar.h index 7f07b6ea..4fae7999 100644 --- a/include/fmt/xchar.h +++ b/include/fmt/xchar.h @@ -183,7 +183,7 @@ inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) { detail::vformat_to(buffer, fmt, args); buffer.push_back(L'\0'); if (std::fputws(buffer.data(), f) == -1) - FMT_THROW(system_error(errno, "cannot write to file")); + FMT_THROW(system_error(errno, FMT_STRING("cannot write to file"))); } inline void vprint(wstring_view fmt, wformat_args args) {