From 4986b4c0efd0fad653463e867d068c12f42e9e0a Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 1 Sep 2024 21:59:39 -0700 Subject: [PATCH] Simplify arg_mapper --- include/fmt/base.h | 40 +++++++++++++++++++--------------------- include/fmt/format.h | 8 ++++---- include/fmt/ranges.h | 2 +- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 6c722ef8..9a440365 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -893,9 +893,7 @@ struct unformattable_pointer : unformattable {}; // Maps formatting arguments to reduce the set of types we need to work with. // Returns unformattable* on errors to be SFINAE-friendly. -template struct arg_mapper { - using char_type = typename Context::char_type; - +template struct arg_mapper { FMT_MAP_API auto map(signed char x) -> int { return x; } FMT_MAP_API auto map(unsigned char x) -> unsigned { return x; } FMT_MAP_API auto map(short x) -> int { return x; } @@ -911,8 +909,8 @@ template struct arg_mapper { FMT_MAP_API auto map(bool x) -> bool { return x; } template ::value || - std::is_same::value)> - FMT_MAP_API auto map(T x) -> char_type { + std::is_same::value)> + FMT_MAP_API auto map(T x) -> Char { return x; } template ::value || @@ -921,7 +919,7 @@ template struct arg_mapper { #endif std::is_same::value || std::is_same::value) && - !std::is_same::value, + !std::is_same::value, int> = 0> FMT_MAP_API auto map(T) -> unformattable_char { return {}; @@ -940,16 +938,16 @@ template struct arg_mapper { return {}; } - FMT_MAP_API auto map(char_type* x) -> const char_type* { return x; } - FMT_MAP_API auto map(const char_type* x) -> const char_type* { return x; } + FMT_MAP_API auto map(Char* x) -> const Char* { return x; } + FMT_MAP_API auto map(const Char* x) -> const Char* { return x; } template , - FMT_ENABLE_IF(std::is_same::value && + FMT_ENABLE_IF(std::is_same::value && !std::is_pointer::value)> FMT_MAP_API auto map(const T& x) -> basic_string_view { return to_string_view(x); } template , - FMT_ENABLE_IF(!std::is_same::value && + FMT_ENABLE_IF(!std::is_same::value && !std::is_pointer::value)> FMT_MAP_API auto map(const T&) -> unformattable_char { return {}; @@ -973,7 +971,7 @@ template struct arg_mapper { std::is_pointer::value || std::is_member_pointer::value || std::is_function::type>::value || (std::is_array::value && - !std::is_convertible::value))> + !std::is_convertible::value))> FMT_MAP_API auto map(const T&) -> unformattable_pointer { return {}; } @@ -993,8 +991,8 @@ template struct arg_mapper { template > struct formattable - : bool_constant() || - (std::is_constructible>::value && + : bool_constant() || + (std::is_constructible>::value && !std::is_const::value)> {}; template ::value)> @@ -1027,13 +1025,13 @@ template struct arg_mapper { }; template -using mapped_t = decltype(detail::arg_mapper>::map( - std::declval())); +using mapped_t = decltype(detail::arg_mapper::map(std::declval())); -// A type constant after applying arg_mapper. +// A type constant after applying arg_mapper. template using mapped_type_constant = - type_constant::map(std::declval())), + type_constant::map( + std::declval())), typename Context::char_type>; template FMT_CONSTEXPR auto make_arg(T& val) -> value { - using arg_type = remove_cvref_t::map(val))>; + using char_type = typename Context::char_type; + using arg_type = remove_cvref_t::map(val))>; // Use enum instead of constexpr because the latter may generate code. enum { @@ -2426,8 +2425,7 @@ FMT_CONSTEXPR auto make_arg(T& val) -> value { enum { formattable = !std::is_same::value }; #if defined(__cpp_if_constexpr) - if constexpr (!formattable) - type_is_unformattable_for _; + if constexpr (!formattable) type_is_unformattable_for _; if constexpr (!Context::builtin_types && !std::is_same::value) return {unwrap_named_arg(val), custom_tag()}; #endif @@ -2435,7 +2433,7 @@ FMT_CONSTEXPR auto make_arg(T& val) -> value { formattable, "Cannot format an argument. To make type T formattable provide a " "formatter specialization: https://fmt.dev/latest/api.html#udt"); - return {arg_mapper::map(val)}; + return {arg_mapper::map(val)}; } template diff --git a/include/fmt/format.h b/include/fmt/format.h index 154e5871..884d2c6d 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1085,7 +1085,7 @@ class loc_value { template ::value)> loc_value(T value) { value_.type_ = detail::mapped_type_constant::value; - value_.value_ = detail::arg_mapper::map(value); + value_.value_ = detail::arg_mapper::map(value); } template ::value)> @@ -3652,10 +3652,10 @@ template enable_if_t< std::is_class::value && !has_to_string_view::value && !is_floating_point::value && !std::is_same::value && - !std::is_same::map(value))>>::value, + !std::is_same< + T, remove_cvref_t::map(value))>>::value, OutputIt> { - return write(out, arg_mapper::map(value)); + return write(out, arg_mapper::map(value)); } template using has_formatter = std::is_constructible>; template struct range_mapper { - using mapper = arg_mapper; + using mapper = arg_mapper; using char_type = typename Context::char_type; template