From 2af403ce6414ee8402a8a141a69c35854c74a234 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 14 Sep 2024 20:59:54 -0700 Subject: [PATCH] Simplify type mapping --- include/fmt/base.h | 53 ++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index ac0b3cb4..63e2294c 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -1164,28 +1164,17 @@ template struct arg_mapper { return {}; } - FMT_MAP_API auto map(void* x) -> const void* { return x; } - FMT_MAP_API auto map(const void* x) -> const void* { return x; } - FMT_MAP_API auto map(volatile void* x) -> const void* { - return const_cast(x); - } - FMT_MAP_API auto map(const volatile void* x) -> const void* { - return const_cast(x); - } - FMT_MAP_API auto map(std::nullptr_t x) -> const void* { return x; } + static auto map(void*) -> const void*; + static auto map(const void*) -> const void*; + static auto map(volatile void*) -> const void*; + static auto map(const volatile void*) -> const void*; + static auto map(std::nullptr_t) -> const void*; - template < - typename T, typename Element = typename std::remove_extent::type, - FMT_ENABLE_IF( - std::is_pointer::value || std::is_member_pointer::value || - std::is_function::type>::value || - (std::is_array::value && !is_char::value))> - FMT_MAP_API auto map(const T&) -> unformattable_pointer { - return {}; - } + template ::value || + std::is_member_pointer::value)> + static auto map(const T&) -> unformattable_pointer; - template ::value)> + template ::value)> FMT_MAP_API auto map(const T (&x)[N]) -> const T (&)[N] { return x; } @@ -2140,6 +2129,10 @@ template class value { }; constexpr FMT_INLINE value() : no_value() {} + constexpr FMT_INLINE value(signed char x) : int_value(x) {} + constexpr FMT_INLINE value(unsigned char x) : uint_value(x) {} + constexpr FMT_INLINE value(signed short x) : int_value(x) {} + constexpr FMT_INLINE value(unsigned short x) : uint_value(x) {} constexpr FMT_INLINE value(int x) : int_value(x) {} constexpr FMT_INLINE value(unsigned x FMT_BUILTIN) : uint_value(x) {} FMT_CONSTEXPR FMT_INLINE value(long x FMT_BUILTIN) : value(long_type(x)) {} @@ -2172,6 +2165,10 @@ template class value { constexpr FMT_INLINE value(double x FMT_BUILTIN) : double_value(x) {} FMT_INLINE value(long double x FMT_BUILTIN) : long_double_value(x) {} + FMT_CONSTEXPR FMT_INLINE value(char_type* x FMT_BUILTIN) { + string.data = x; + if (is_constant_evaluated()) string.size = 0; + } FMT_CONSTEXPR FMT_INLINE value(const char_type* x FMT_BUILTIN) { string.data = x; if (is_constant_evaluated()) string.size = 0; @@ -2180,7 +2177,20 @@ template class value { string.data = x.data(); string.size = x.size(); } + FMT_INLINE value(void* x FMT_BUILTIN) : pointer(x) {} FMT_INLINE value(const void* x FMT_BUILTIN) : pointer(x) {} + FMT_INLINE value(volatile void* x FMT_BUILTIN) + : pointer(const_cast(x)) {} + FMT_INLINE value(const volatile void* x FMT_BUILTIN) + : pointer(const_cast(x)) {} + FMT_INLINE value(std::nullptr_t) : pointer(nullptr) {} + + template ::value || + std::is_member_pointer::value)> + value(const T&) { + static_assert(sizeof(T) == 0, + "formatting of non-void pointers is disallowed"); + } template ::value)> value(const T& x) : value(format_as(x)) {} @@ -2190,7 +2200,8 @@ template class value { template > using mappable = - bool_constant::value && !is_named_arg::value && + bool_constant::value && + !std::is_pointer::value && !is_named_arg::value && !use_format_as::value>; // We can't use mapped_t because of a bug in MSVC 2017.